|
|
|
@ -2,6 +2,7 @@ package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"image"
|
|
|
|
|
"image/color"
|
|
|
|
|
_ "image/jpeg"
|
|
|
|
@ -11,9 +12,23 @@ import (
|
|
|
|
|
|
|
|
|
|
rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix"
|
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
|
"github.com/fogleman/gg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Animation struct {
|
|
|
|
|
ctx *gg.Context
|
|
|
|
|
position image.Point
|
|
|
|
|
dir image.Point
|
|
|
|
|
height int
|
|
|
|
|
width int
|
|
|
|
|
stroke int
|
|
|
|
|
image []image.Image
|
|
|
|
|
updown int
|
|
|
|
|
msg chan mqtt.Message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
rows = flag.Int("led-rows", 32, "number of rows supported")
|
|
|
|
|
cols = flag.Int("led-cols", 32, "number of columns supported")
|
|
|
|
@ -37,7 +52,7 @@ func main() {
|
|
|
|
|
config.ShowRefreshRate = *show_refresh
|
|
|
|
|
config.InverseColors = *inverse_colors
|
|
|
|
|
config.DisableHardwarePulsing = *disable_hardware_pulsing
|
|
|
|
|
|
|
|
|
|
setupMQTT()
|
|
|
|
|
/*bounds := mario.Bounds()
|
|
|
|
|
var histogram [16][4]int
|
|
|
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
|
|
|
@ -53,13 +68,39 @@ func main() {
|
|
|
|
|
}*/
|
|
|
|
|
m, err := rgbmatrix.NewRGBLedMatrix(config)
|
|
|
|
|
fatal(err)
|
|
|
|
|
|
|
|
|
|
mqMessages := make(chan mqtt.Message)
|
|
|
|
|
go listener(mqMessages)
|
|
|
|
|
tk := rgbmatrix.NewToolKit(m)
|
|
|
|
|
defer tk.Close()
|
|
|
|
|
go animator(tk, mqMessages)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func listener(mqMessages chan mqtt.Message) {
|
|
|
|
|
opts := setupMQTT()
|
|
|
|
|
client := MQTT.NewClient(opts)
|
|
|
|
|
topic := "home/rgbboard"
|
|
|
|
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
|
|
|
panic(token.Error())
|
|
|
|
|
}
|
|
|
|
|
client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
|
|
|
|
|
log.Println("Receiving ", string(msg.Payload()), " on topic: ", msg.Topic())
|
|
|
|
|
mqMessages <- msg
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func animator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) {
|
|
|
|
|
|
|
|
|
|
tk.PlayAnimation(NewAnimation(image.Point{64, 32}, mqMessages))
|
|
|
|
|
|
|
|
|
|
tk.PlayAnimation(NewAnimation(image.Point{64, 32}))
|
|
|
|
|
}
|
|
|
|
|
func setupMQTT() *mqtt.ClientOptions {
|
|
|
|
|
opts := MQTT.NewClientOptions()
|
|
|
|
|
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", os.Getenv("MQTTBROKER"), os.Getenv("MQTTPORT")))
|
|
|
|
|
opts.SetUsername(os.Getenv("MQTTUSER"))
|
|
|
|
|
opts.SetPassword(os.Getenv("MQTTPASS"))
|
|
|
|
|
|
|
|
|
|
return opts
|
|
|
|
|
}
|
|
|
|
|
func init() {
|
|
|
|
|
flag.Parse()
|
|
|
|
|
}
|
|
|
|
@ -70,18 +111,7 @@ func fatal(err error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Animation struct {
|
|
|
|
|
ctx *gg.Context
|
|
|
|
|
position image.Point
|
|
|
|
|
dir image.Point
|
|
|
|
|
height int
|
|
|
|
|
width int
|
|
|
|
|
stroke int
|
|
|
|
|
image []image.Image
|
|
|
|
|
updown int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewAnimation(sz image.Point) *Animation {
|
|
|
|
|
func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation {
|
|
|
|
|
reader, err := os.Open("marioUp.png")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
@ -107,6 +137,7 @@ func NewAnimation(sz image.Point) *Animation {
|
|
|
|
|
stroke: 8,
|
|
|
|
|
image: images,
|
|
|
|
|
updown: 0,
|
|
|
|
|
msg: mqMessages,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -114,16 +145,17 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
|
|
|
|
|
defer a.updatePosition()
|
|
|
|
|
a.ctx.SetColor(color.Black)
|
|
|
|
|
a.ctx.Clear()
|
|
|
|
|
//a.ctx.SetColor(color.RGBA{0, 255, 0, 255})
|
|
|
|
|
//a.ctx.DrawCircle(float64(8), float64(8), float64(a.stroke))
|
|
|
|
|
//a.ctx.Fill()
|
|
|
|
|
if a.dir.X == 1 {
|
|
|
|
|
a.ctx.DrawImageAnchored(a.image[a.updown], a.position.X, a.position.Y, 0.5, 0.5)
|
|
|
|
|
} else {
|
|
|
|
|
a.ctx.DrawImageAnchored(imaging.FlipH(a.image[a.updown]), a.position.X, a.position.Y, 0.5, 0.5)
|
|
|
|
|
}
|
|
|
|
|
a.ctx.SetColor(color.White)
|
|
|
|
|
a.ctx.DrawString("mario", 5, 9)
|
|
|
|
|
select {
|
|
|
|
|
case msg := <-a.msg:
|
|
|
|
|
a.ctx.DrawString(string(msg.Payload()), 5, 9)
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|