|
|
@ -14,6 +14,7 @@ import (
|
|
|
|
"os/signal"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix"
|
|
|
|
rgbmatrix "gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix"
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
"github.com/disintegration/imaging"
|
|
|
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
|
@ -42,7 +43,7 @@ type Animation struct {
|
|
|
|
countDown int
|
|
|
|
countDown int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//flags from cmd line
|
|
|
|
// flags from cmd line
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
rows = flag.Int("led-rows", 64, "number of rows supported")
|
|
|
|
rows = flag.Int("led-rows", 64, "number of rows supported")
|
|
|
|
cols = flag.Int("led-cols", 64, "number of columns supported")
|
|
|
|
cols = flag.Int("led-cols", 64, "number of columns supported")
|
|
|
@ -56,7 +57,7 @@ var (
|
|
|
|
led_slowdown_gpio = flag.Int("led-slowdown-gpio", 1, "GPIO pin slowdown")
|
|
|
|
led_slowdown_gpio = flag.Int("led-slowdown-gpio", 1, "GPIO pin slowdown")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
//listens on topic for messages
|
|
|
|
// listens on topic for messages
|
|
|
|
func listener(mqMessages chan mqtt.Message) {
|
|
|
|
func listener(mqMessages chan mqtt.Message) {
|
|
|
|
opts := setupMQTT()
|
|
|
|
opts := setupMQTT()
|
|
|
|
client := MQTT.NewClient(opts)
|
|
|
|
client := MQTT.NewClient(opts)
|
|
|
@ -72,19 +73,19 @@ func listener(mqMessages chan mqtt.Message) {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//animator is a wrapping function for go routine that can receive an mq channel
|
|
|
|
// animator is a wrapping function for go routine that can receive an mq channel
|
|
|
|
func animator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) {
|
|
|
|
func animator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) {
|
|
|
|
//Playanimation comes from the toolkit, all it takes is an animation struct
|
|
|
|
//Playanimation comes from the toolkit, all it takes is an animation struct
|
|
|
|
tk.PlayAnimation(NewAnimation(image.Point{128, 64}, mqMessages))
|
|
|
|
tk.PlayAnimation(NewAnimation(image.Point{128, 64}, mqMessages))
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//connection lost management
|
|
|
|
// connection lost management
|
|
|
|
func onConnectionLostHandler(c MQTT.Client, reason error) {
|
|
|
|
func onConnectionLostHandler(c MQTT.Client, reason error) {
|
|
|
|
log.Fatalf(reason.Error())
|
|
|
|
log.Fatalf(reason.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//setup connection to mqtt, topic to listen to, qos
|
|
|
|
// setup connection to mqtt, topic to listen to, qos
|
|
|
|
func setupMQTT() *mqtt.ClientOptions {
|
|
|
|
func setupMQTT() *mqtt.ClientOptions {
|
|
|
|
opts := MQTT.NewClientOptions()
|
|
|
|
opts := MQTT.NewClientOptions()
|
|
|
|
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", os.Getenv("MQTTBROKER"), os.Getenv("MQTTPORT")))
|
|
|
|
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", os.Getenv("MQTTBROKER"), os.Getenv("MQTTPORT")))
|
|
|
@ -97,19 +98,19 @@ func setupMQTT() *mqtt.ClientOptions {
|
|
|
|
return opts
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//runs before main, parses flags
|
|
|
|
// runs before main, parses flags
|
|
|
|
func init() {
|
|
|
|
func init() {
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//small function for handling fatal errors
|
|
|
|
// small function for handling fatal errors
|
|
|
|
func fatal(err error) {
|
|
|
|
func fatal(err error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//initializes the struct for the an play animation function, this could all be dumped into function that's wrapping go routine if I wanted
|
|
|
|
// initializes the struct for the an play animation function, this could all be dumped into function that's wrapping go routine if I wanted
|
|
|
|
func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation {
|
|
|
|
func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation {
|
|
|
|
imageMap := make(map[string]image.Image)
|
|
|
|
imageMap := make(map[string]image.Image)
|
|
|
|
reader, err := os.Open("marioUp.png")
|
|
|
|
reader, err := os.Open("marioUp.png")
|
|
|
@ -156,7 +157,7 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
|
|
|
|
a.animateMario()
|
|
|
|
a.animateMario()
|
|
|
|
if a.images["doorbell"] != nil {
|
|
|
|
if a.images["doorbell"] != nil {
|
|
|
|
if a.countDown > 0 {
|
|
|
|
if a.countDown > 0 {
|
|
|
|
a.ctx.DrawImageAnchored(a.image[2], 0, 0, 0, 0)
|
|
|
|
a.ctx.DrawImageAnchored(a.images["doorbell"], 0, 0, 0, 0)
|
|
|
|
a.countDown -= 50
|
|
|
|
a.countDown -= 50
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
//a.image = a.image[:len(a.image)-1]
|
|
|
|
//a.image = a.image[:len(a.image)-1]
|
|
|
@ -190,7 +191,7 @@ func (a *Animation) animateMario() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//what mario does every frame
|
|
|
|
// what mario does every frame
|
|
|
|
func (a *Animation) updateMarioPosition() {
|
|
|
|
func (a *Animation) updateMarioPosition() {
|
|
|
|
a.position.X += 1 * a.dir.X
|
|
|
|
a.position.X += 1 * a.dir.X
|
|
|
|
a.position.Y += 1 * a.dir.Y
|
|
|
|
a.position.Y += 1 * a.dir.Y
|
|
|
|