You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rgb-led-board/board.go

75 lines
1.6 KiB
Go

9 months ago
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
9 months ago
mario Mario
9 months ago
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
a.animateMario()
if a.doorbell != (Doorbell{}) {
if a.countDown > 0 {
a.ctx.DrawImageAnchored(a.doorbell.image, 0, 0, 0, 0)
a.countDown -= 50
} else {
//a.image = a.image[:len(a.image)-1]
a.doorbell = Doorbell{}
a.countDown = 5000
}
}
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
}