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.
		
		
		
		
		
			
		
			
				
	
	
		
			72 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
			
		
		
	
	
			72 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	"fmt"
 | 
						|
	"image"
 | 
						|
	"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) useMsg() {
 | 
						|
	var incoming incomingMessage
 | 
						|
	select {
 | 
						|
	case msg := <-a.mqmsg:
 | 
						|
		json.Unmarshal([]byte(string(msg.Payload())), &incoming)
 | 
						|
		fmt.Println(incoming.Type)
 | 
						|
		if incoming.Type == "doorbell" {
 | 
						|
			go a.loadImage(incoming.Type, incoming.Image)
 | 
						|
		} else {
 | 
						|
			a.ctx.DrawString(a.msg, 5, 9)
 | 
						|
		}
 | 
						|
	default:
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
 | 
						|
	switch {
 | 
						|
	case a.doorbell != (Doorbell{}):
 | 
						|
		a.animateDoorbell()
 | 
						|
	default:
 | 
						|
		a.animateMario()
 | 
						|
	}
 | 
						|
	a.useMsg()
 | 
						|
	return a.ctx.Image(), time.After(time.Millisecond * 50), nil
 | 
						|
}
 |