Compare commits
41 Commits
animate-se
...
856471ec7d
| Author | SHA1 | Date | |
|---|---|---|---|
| 856471ec7d | |||
| 843a009343 | |||
| 8e92059e54 | |||
| 9aacbf7399 | |||
| 3636854124 | |||
| fc0dfae1a5 | |||
|
|
c64a6bfe42 | ||
|
|
34de0be0a1 | ||
| d574df9938 | |||
| eab6b0d26f | |||
| 0f4addd5db | |||
| 2ac3a92b7a | |||
| 6ac630cd57 | |||
| fd94207114 | |||
| 7a02239545 | |||
| 18b2875ddf | |||
| bf1c448a84 | |||
| 2a5e5142ce | |||
| af060a1600 | |||
| aa3e51f56f | |||
| 7c086ff848 | |||
| 688e1d98f0 | |||
| 6eec19644e | |||
| 81e7607d53 | |||
| 1093a2c1b2 | |||
| 476ed823bc | |||
| 89ad771eab | |||
| 970f840140 | |||
| 41e317452f | |||
| c4ffac2251 | |||
| 3365c8f075 | |||
| 78a145dae8 | |||
| 1a0ea1833b | |||
| 94cfad1190 | |||
| 4621c93b86 | |||
| 7c3c385119 | |||
| 50e5422009 | |||
| 60287d91d6 | |||
| 87470bddcb | |||
| db3445e124 | |||
| b001c0f628 |
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
# [Choice] Go version (use -bullseye variants on local arm64/Apple Silicon): 1, 1.18, 1.17, 1-bullseye, 1.18-bullseye, 1.17-bullseye, 1-buster, 1.18-buster, 1.17-buster
|
# [Choice] Go version (use -bullseye variants on local arm64/Apple Silicon): 1, 1.18, 1.17, 1-bullseye, 1.18-bullseye, 1.17-bullseye, 1-buster, 1.18-buster, 1.17-buster
|
||||||
ARG VARIANT="1.18-bullseye"
|
ARG VARIANT="1.18-bullseye"
|
||||||
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}
|
FROM mcr.microsoft.com/vscode/devcontainers/go:${VARIANT}
|
||||||
|
|
||||||
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
|
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
|
||||||
ARG NODE_VERSION="none"
|
ARG NODE_VERSION="none"
|
||||||
|
|||||||
@@ -28,7 +28,8 @@
|
|||||||
|
|
||||||
// Add the IDs of extensions you want installed when the container is created.
|
// Add the IDs of extensions you want installed when the container is created.
|
||||||
"extensions": [
|
"extensions": [
|
||||||
"golang.Go"
|
"golang.Go",
|
||||||
|
"Continue.continue"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -44,6 +45,7 @@
|
|||||||
"features": {
|
"features": {
|
||||||
"docker-in-docker": "latest",
|
"docker-in-docker": "latest",
|
||||||
"kubectl-helm-minikube": "latest",
|
"kubectl-helm-minikube": "latest",
|
||||||
"git": "latest"
|
"git": "latest",
|
||||||
|
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ jobs:
|
|||||||
seccomp=unconfined
|
seccomp=unconfined
|
||||||
apparmor=unconfined
|
apparmor=unconfined
|
||||||
systempaths=unconfined
|
systempaths=unconfined
|
||||||
privileged=false
|
privileged=false
|
||||||
- run: docker buildx ls
|
- run: docker buildx ls
|
||||||
- name: push rgbboard
|
- name: push rgbboard
|
||||||
uses: docker/build-push-action@master
|
uses: docker/build-push-action@master
|
||||||
|
|||||||
@@ -34,4 +34,4 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
push: true
|
push: true
|
||||||
tags: registry.local/rgbboard:${{ steps.short-sha.outputs.short-sha }}
|
tags: registry.local/rgbboard:${{ steps.short-sha.outputs.short-sha }}
|
||||||
platforms: linux/arm64,linux/amd64
|
platforms: linux/arm64
|
||||||
31
animateDoorbell.go
Normal file
31
animateDoorbell.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
b64 "encoding/base64"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Doorbell struct {
|
||||||
|
image image.Image
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Animation) animateDoorbell() {
|
||||||
|
if a.countDown > 0 {
|
||||||
|
a.ctx.SetColor(color.Black)
|
||||||
|
a.ctx.Clear()
|
||||||
|
a.ctx.DrawImageAnchored(a.doorbell.image, 0, 0, 0, 0)
|
||||||
|
a.countDown -= 50
|
||||||
|
} else {
|
||||||
|
a.doorbell = Doorbell{}
|
||||||
|
a.countDown = 5000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (a *Animation) loadImage(imgType string, img string) {
|
||||||
|
baseImage, _ := b64.StdEncoding.DecodeString(img)
|
||||||
|
bigImage, _, _ := image.Decode(bytes.NewReader(baseImage))
|
||||||
|
a.doorbell.image = imaging.Resize(bigImage, 64, 64, imaging.Lanczos)
|
||||||
|
}
|
||||||
72
animationMario.go
Normal file
72
animationMario.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Mario struct {
|
||||||
|
position image.Point
|
||||||
|
dir image.Point
|
||||||
|
images map[string]image.Image
|
||||||
|
updown string
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadMario(file string) image.Image {
|
||||||
|
|
||||||
|
reader, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
rawMario, _, err := image.Decode(reader)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
mario := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
|
||||||
|
return mario
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialMap() map[string]image.Image {
|
||||||
|
imageMap := make(map[string]image.Image)
|
||||||
|
imageMap["marioUp"] = loadMario("marioUp.png")
|
||||||
|
imageMap["marioDown"] = loadMario("marioDown.png")
|
||||||
|
return imageMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// initializes the struct for the an play animation function, this could all be dumped into function that's wrapping go routine if I wanted
|
||||||
|
|
||||||
|
// this assumes mario context is up
|
||||||
|
func (a *Animation) animateMario() {
|
||||||
|
defer a.updateMarioPosition()
|
||||||
|
a.ctx.SetColor(color.Black)
|
||||||
|
a.ctx.Clear()
|
||||||
|
if a.mario.dir.X == 1 {
|
||||||
|
a.ctx.DrawImageAnchored(a.mario.images[a.mario.updown], a.mario.position.X, a.mario.position.Y, 0.5, 0.5)
|
||||||
|
} else {
|
||||||
|
a.ctx.DrawImageAnchored(imaging.FlipH(a.mario.images[a.mario.updown]), a.mario.position.X, a.mario.position.Y, 0.5, 0.5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// what mario does every frame
|
||||||
|
func (a *Animation) updateMarioPosition() {
|
||||||
|
a.mario.position.X += 1 * a.mario.dir.X
|
||||||
|
a.mario.position.Y += 1 * a.mario.dir.Y
|
||||||
|
|
||||||
|
if a.mario.position.Y+a.height > a.ctx.Height() {
|
||||||
|
a.mario.dir.Y = -1
|
||||||
|
a.mario.updown = "marioUp"
|
||||||
|
} else if a.mario.position.Y-a.height < 0 {
|
||||||
|
a.mario.updown = "marioDown"
|
||||||
|
a.mario.dir.Y = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.mario.position.X+a.width > a.ctx.Width() {
|
||||||
|
a.mario.dir.X = -1
|
||||||
|
} else if a.mario.position.X-a.width < 0 {
|
||||||
|
a.mario.dir.X = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
71
board.go
Normal file
71
board.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
4
go.mod
4
go.mod
@@ -1,6 +1,8 @@
|
|||||||
module rgb
|
module rgb
|
||||||
|
|
||||||
go 1.18
|
go 1.21.2
|
||||||
|
|
||||||
|
toolchain go1.21.6
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v1.0.0
|
gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v1.0.0
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -17,8 +17,11 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
|
|||||||
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||||
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||||
|
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d h1:grE48C8cjIY0aiHVmFyYgYxxSARQWBABLXKZfQPrBhY=
|
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d h1:grE48C8cjIY0aiHVmFyYgYxxSARQWBABLXKZfQPrBhY=
|
||||||
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
|
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
|
||||||
@@ -39,3 +42,4 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
|||||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
|||||||
181
main.go
181
main.go
@@ -1,48 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
b64 "encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image"
|
|
||||||
"image/color"
|
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"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"
|
|
||||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
"github.com/fogleman/gg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// contents of struct mostly don't matter for toolkit.
|
// flags from cmd line
|
||||||
type incomingImage struct {
|
|
||||||
Image string `json:"image"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Animation struct {
|
|
||||||
ctx *gg.Context
|
|
||||||
position image.Point
|
|
||||||
dir image.Point
|
|
||||||
height int
|
|
||||||
width int
|
|
||||||
stroke int
|
|
||||||
image []image.Image
|
|
||||||
images map[string]image.Image
|
|
||||||
updown string
|
|
||||||
mqmsg chan mqtt.Message
|
|
||||||
msg string
|
|
||||||
|
|
||||||
countDown int
|
|
||||||
}
|
|
||||||
|
|
||||||
//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,160 +27,18 @@ 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
|
// runs before main, parses flags
|
||||||
func listener(mqMessages chan mqtt.Message) {
|
|
||||||
opts := setupMQTT()
|
|
||||||
client := MQTT.NewClient(opts)
|
|
||||||
topic := "home/rgbboard"
|
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
||||||
log.Println(fmt.Sprintf("failed to connect to mq: %s", token.Error().Error()))
|
|
||||||
panic(token.Error())
|
|
||||||
}
|
|
||||||
log.Println("connected")
|
|
||||||
client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
|
|
||||||
log.Println("Receiving ", string(msg.Payload()), " on topic: ", msg.Topic())
|
|
||||||
mqMessages <- msg
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
//animator is a wrapping function for go routine that can receive an mq channel
|
|
||||||
func animator(tk *rgbmatrix.ToolKit, mqMessages chan mqtt.Message) {
|
|
||||||
//Playanimation comes from the toolkit, all it takes is an animation struct
|
|
||||||
tk.PlayAnimation(NewAnimation(image.Point{128, 64}, mqMessages))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//connection lost management
|
|
||||||
func onConnectionLostHandler(c MQTT.Client, reason error) {
|
|
||||||
log.Fatalf(reason.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
//setup connection to mqtt, topic to listen to, qos
|
|
||||||
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("MQTTPASSWORD"))
|
|
||||||
opts.SetClientID("rgbboard")
|
|
||||||
opts.SetAutoReconnect(true)
|
|
||||||
opts.SetConnectionLostHandler(onConnectionLostHandler)
|
|
||||||
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
|
||||||
//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
|
|
||||||
func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation {
|
|
||||||
imageMap := make(map[string]image.Image)
|
|
||||||
reader, err := os.Open("marioUp.png")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
rawMario, _, err := image.Decode(reader)
|
|
||||||
|
|
||||||
//marioUp := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
|
|
||||||
marioUp := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
|
|
||||||
imageMap["marioUp"] = marioUp
|
|
||||||
reader, err = os.Open("marioDown.png")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
rawMario, _, err = image.Decode(reader)
|
|
||||||
//marioDown := imaging.FlipH(imaging.Resize(rawMario, 16, 16, imaging.Lanczos))
|
|
||||||
marioDown := imaging.Resize(rawMario, 16, 16, imaging.Lanczos)
|
|
||||||
imageMap["marioDown"] = marioDown
|
|
||||||
imageSlice := []image.Image{marioUp, marioDown}
|
|
||||||
return &Animation{
|
|
||||||
ctx: gg.NewContext(sz.X, sz.Y),
|
|
||||||
dir: image.Point{1, 1},
|
|
||||||
height: 8,
|
|
||||||
width: 8,
|
|
||||||
stroke: 8,
|
|
||||||
image: imageSlice,
|
|
||||||
images: imageMap,
|
|
||||||
updown: "marioUp",
|
|
||||||
mqmsg: mqMessages,
|
|
||||||
countDown: 5000,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func appendImage(img string, a *Animation) {
|
|
||||||
baseImage, _ := b64.StdEncoding.DecodeString(img)
|
|
||||||
bigImage, _, _ := image.Decode(bytes.NewReader(baseImage))
|
|
||||||
a.images["doorbell"] = imaging.Resize(bigImage, 64, 64, imaging.Lanczos)
|
|
||||||
}
|
|
||||||
|
|
||||||
// what happens each frame, at an interval of 50 milliseconds
|
|
||||||
func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
|
|
||||||
incoming := incomingImage{}
|
|
||||||
a.animateMario()
|
|
||||||
if a.images["doorbell"] != nil {
|
|
||||||
if a.countDown > 0 {
|
|
||||||
a.ctx.DrawImageAnchored(a.image[2], 0, 0, 0, 0)
|
|
||||||
a.countDown -= 50
|
|
||||||
} else {
|
|
||||||
//a.image = a.image[:len(a.image)-1]
|
|
||||||
delete(a.images, "doorbell")
|
|
||||||
a.countDown = 5000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
a.ctx.SetColor(color.White)
|
|
||||||
select {
|
|
||||||
case msg := <-a.mqmsg:
|
|
||||||
json.Unmarshal([]byte(string(msg.Payload())), &incoming)
|
|
||||||
if incoming.Image == "" {
|
|
||||||
a.msg = string(msg.Payload())
|
|
||||||
a.ctx.DrawString(a.msg, 5, 9)
|
|
||||||
} else {
|
|
||||||
go appendImage(incoming.Image, a)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
return a.ctx.Image(), time.After(time.Millisecond * 50), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Animation) animateMario() {
|
|
||||||
defer a.updateMarioPosition()
|
|
||||||
a.ctx.SetColor(color.Black)
|
|
||||||
a.ctx.Clear()
|
|
||||||
if a.dir.X == 1 {
|
|
||||||
a.ctx.DrawImageAnchored(a.images[a.updown], a.position.X, a.position.Y, 0.5, 0.5)
|
|
||||||
} else {
|
|
||||||
a.ctx.DrawImageAnchored(imaging.FlipH(a.images[a.updown]), a.position.X, a.position.Y, 0.5, 0.5)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//what mario does every frame
|
|
||||||
func (a *Animation) updateMarioPosition() {
|
|
||||||
a.position.X += 1 * a.dir.X
|
|
||||||
a.position.Y += 1 * a.dir.Y
|
|
||||||
|
|
||||||
if a.position.Y+a.height > a.ctx.Height() {
|
|
||||||
a.dir.Y = -1
|
|
||||||
a.updown = "marioUp"
|
|
||||||
} else if a.position.Y-a.height < 0 {
|
|
||||||
a.updown = "marioDown"
|
|
||||||
a.dir.Y = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.position.X+a.width > a.ctx.Width() {
|
|
||||||
a.dir.X = -1
|
|
||||||
} else if a.position.X-a.width < 0 {
|
|
||||||
a.dir.X = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := &rgbmatrix.DefaultConfig
|
config := &rgbmatrix.DefaultConfig
|
||||||
config.Rows = *rows
|
config.Rows = *rows
|
||||||
@@ -233,7 +62,7 @@ func main() {
|
|||||||
tk := rgbmatrix.NewToolKit(m)
|
tk := rgbmatrix.NewToolKit(m)
|
||||||
defer tk.Close()
|
defer tk.Close()
|
||||||
log.Println("making animator")
|
log.Println("making animator")
|
||||||
go animator(tk, mqMessages)
|
go orchestrator(tk, mqMessages)
|
||||||
log.Println("I guess I'm at the end")
|
log.Println("I guess I'm at the end")
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|||||||
50
mqttcoms.go
Normal file
50
mqttcoms.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
)
|
||||||
|
|
||||||
|
// contents of struct mostly don't matter for toolkit.
|
||||||
|
type incomingMessage struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// listens on topic for messages
|
||||||
|
func listener(mqMessages chan mqtt.Message) {
|
||||||
|
opts := setupMQTT()
|
||||||
|
client := MQTT.NewClient(opts)
|
||||||
|
topic := "home/rgbboard"
|
||||||
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
log.Println(fmt.Sprintf("failed to connect to mq: %s", token.Error().Error()))
|
||||||
|
panic(token.Error())
|
||||||
|
}
|
||||||
|
log.Println("connected")
|
||||||
|
client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
|
||||||
|
log.Println("Receiving ", string(msg.Payload()), " on topic: ", msg.Topic())
|
||||||
|
mqMessages <- msg
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// connection lost management
|
||||||
|
func onConnectionLostHandler(c MQTT.Client, reason error) {
|
||||||
|
log.Fatalf(reason.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// setup connection to mqtt, topic to listen to, qos
|
||||||
|
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("MQTTPASSWORD"))
|
||||||
|
opts.SetClientID("rgbboard")
|
||||||
|
opts.SetAutoReconnect(true)
|
||||||
|
opts.SetConnectionLostHandler(onConnectionLostHandler)
|
||||||
|
|
||||||
|
return opts
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user