package main import ( "encoding/json" "fmt" "image" "image/color" "time" rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix" mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/fogleman/gg" ) type Animation struct { ctx *gg.Context height int width int stroke int mqmsg chan mqtt.Message msg string countDown int mario Mario doorbell Doorbell } func orchestrator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) { //Playanimation comes from the toolkit, all it takes is an animation struct tk.PlayAnimation(animate(image.Point{127, 64}, mqMessages)) } func animate(sz image.Point, mqMessages chan mqtt.Message) *Animation { return &Animation{ ctx: gg.NewContext(sz.X, sz.Y), height: 8, width: 8, stroke: 8, mqmsg: mqMessages, countDown: 5000, mario: Mario{ images: initialMap(), updown: "marioUp", dir: image.Point{1, 1}, }, } } func (a *Animation) Next() (image.Image, <-chan time.Time, error) { var incoming incomingMessage switch { case a.doorbell != (Doorbell{}): if a.countDown > 0 { a.ctx.DrawImageAnchored(a.doorbell.image, 0, 0, 0, 0) a.countDown -= 50 } else { a.doorbell = Doorbell{} a.countDown = 5000 } default: a.ctx.Pop() a.animateMario() } a.ctx.SetColor(color.White) select { case msg := <-a.mqmsg: json.Unmarshal([]byte(string(msg.Payload())), &incoming) fmt.Println(incoming.Type) if incoming.Type == "doorbell" { go loadImage(incoming.Type, incoming.Image, a) } else { a.ctx.DrawString(a.msg, 5, 9) } default: } return a.ctx.Image(), time.After(time.Millisecond * 50), nil }