Compare commits
3 Commits
main
...
animate-se
| Author | SHA1 | Date | |
|---|---|---|---|
| 41604a2810 | |||
| 3725f6752e | |||
| 69e5ea9b96 |
@@ -1,16 +0,0 @@
|
||||
name: Wagner Home Assistant
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
models:
|
||||
- name: Autodetect
|
||||
provider: ollama
|
||||
model: AUTODETECT
|
||||
apiBase: http://192.168.0.53
|
||||
context:
|
||||
- provider: code
|
||||
- provider: docs
|
||||
- provider: diff
|
||||
- provider: terminal
|
||||
- provider: problems
|
||||
- provider: folder
|
||||
- provider: codebase
|
||||
@@ -1,14 +0,0 @@
|
||||
name: memory
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
mcpServers:
|
||||
- name: git
|
||||
version: 0.0.1
|
||||
command: docker
|
||||
args:
|
||||
- run
|
||||
- -i
|
||||
- -v
|
||||
- /workspaces/rgb-led-board:/workspace
|
||||
- --rm
|
||||
- mcp/git
|
||||
@@ -1,12 +0,0 @@
|
||||
name: memory
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
mcpServers:
|
||||
- name: memory
|
||||
version: 0.0.1
|
||||
command: docker
|
||||
args:
|
||||
- run
|
||||
- -i
|
||||
- --rm
|
||||
- mcp/memory
|
||||
@@ -1,16 +0,0 @@
|
||||
name: sqlite
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
mcpServers:
|
||||
- name: sqlite
|
||||
version: 0.0.1
|
||||
command: docker
|
||||
args:
|
||||
- run
|
||||
- -i
|
||||
- -v
|
||||
- /workspaces/rgb-led-board:/sqlite
|
||||
- --rm
|
||||
- mcp/sqlite
|
||||
- --db-path
|
||||
- /sqlite/test.db
|
||||
@@ -1,12 +0,0 @@
|
||||
name: time
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
mcpServers:
|
||||
- name: time
|
||||
version: 0.0.1
|
||||
command: docker
|
||||
args:
|
||||
- run
|
||||
- -i
|
||||
- --rm
|
||||
- mcp/time
|
||||
@@ -1,12 +0,0 @@
|
||||
name: wikipedia
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
mcpServers:
|
||||
- name: wikipedia
|
||||
version: 0.0.1
|
||||
command: docker
|
||||
args:
|
||||
- run
|
||||
- -i
|
||||
- --rm
|
||||
- mcp/wikipedia-mcp
|
||||
@@ -2,12 +2,11 @@
|
||||
|
||||
# [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"
|
||||
FROM mcr.microsoft.com/vscode/devcontainers/go:${VARIANT}
|
||||
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/yarn-keyring.gpg
|
||||
RUN echo "deb [signed-by=/usr/share/keyrings/yarn-keyring.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list > /dev/null
|
||||
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}
|
||||
|
||||
# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
|
||||
ARG NODE_VERSION="none"
|
||||
#RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
|
||||
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
|
||||
|
||||
# [Optional] Uncomment this section to install additional OS packages.
|
||||
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
||||
|
||||
@@ -37,12 +37,13 @@
|
||||
// "forwardPorts": [],
|
||||
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
//"postCreateCommand": "ln -s /workspaces/rgb-led-board/.continue/config/config.yaml /home/vscode/.continue/config.yaml",
|
||||
// "postCreateCommand": "go version",
|
||||
|
||||
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
|
||||
"remoteUser": "vscode",
|
||||
"features": {
|
||||
// "docker-in-docker": "latest",
|
||||
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
|
||||
"docker-in-docker": "latest",
|
||||
"kubectl-helm-minikube": "latest",
|
||||
"git": "latest"
|
||||
}
|
||||
}
|
||||
|
||||
37
.gitea/workflows/Build.yaml
Normal file
37
.gitea/workflows/Build.yaml
Normal file
@@ -0,0 +1,37 @@
|
||||
name: build only rgb-board
|
||||
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "animate-separate"
|
||||
paths-ignore:
|
||||
- ".gitea/workflows/**"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: registry.local/catthehacker-home:act-latest
|
||||
volumes:
|
||||
- /var/run/user/1000/docker.sock:/var/run/docker.sock
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: short-sha
|
||||
id: short-sha
|
||||
run: echo "short-sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
with:
|
||||
driver-opts: |
|
||||
image=registry.local/buildkit-wagnerca:stable-3-rootless
|
||||
seccomp=unconfined
|
||||
apparmor=unconfined
|
||||
systempaths=unconfined
|
||||
privileged=false
|
||||
- run: docker buildx ls
|
||||
- name: push rgbboard
|
||||
uses: docker/build-push-action@master
|
||||
with:
|
||||
push: false
|
||||
tags: registry.local/rgbboard:${{ steps.short-sha.outputs.short-sha }}
|
||||
platforms: linux/arm64,linux/amd64
|
||||
@@ -6,7 +6,7 @@ on:
|
||||
- main
|
||||
paths-ignore:
|
||||
- ".gitea/workflows/**"
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -29,35 +29,9 @@ jobs:
|
||||
systempaths=unconfined
|
||||
privileged=false
|
||||
- run: docker buildx ls
|
||||
- name: build push rgbboard
|
||||
- name: push rgbboard
|
||||
uses: docker/build-push-action@master
|
||||
#id: build_push_rgbboard
|
||||
with:
|
||||
push: true
|
||||
tags: registry.local/rgbboard:${{ steps.short-sha.outputs.short-sha }}
|
||||
platforms: linux/arm64
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
repository: wagshome/rgbboard
|
||||
token: ${{ secrets.runner_deploy }}
|
||||
path: rgbboard
|
||||
- name: Set up Git
|
||||
run: |
|
||||
git config --global user.name "Gitea Actions"
|
||||
git config --global user.email "actions@nope.com"
|
||||
- name: Install yq
|
||||
uses: https://github.com/chrisdickinson/setup-yq@v1.0.0
|
||||
with:
|
||||
yq-version: v4.45.4
|
||||
- name: Update values.yaml
|
||||
run: |
|
||||
yq -i '.image.tag = env(SHORT_SHA)' rgbboard/rgbboard/values.yaml
|
||||
env:
|
||||
SHORT_SHA: ${{ steps.short-sha.outputs.short-sha }}
|
||||
- name: Commit and Push changes
|
||||
run: |
|
||||
cd rgbboard
|
||||
git add .
|
||||
git commit -m "Update image to ${{ steps.short_sha.outputs.short_sha }}"
|
||||
git push
|
||||
platforms: linux/arm64,linux/amd64
|
||||
@@ -11,3 +11,4 @@ FROM scratch
|
||||
COPY --from=builder /usr/src/app/rgb/rgb /usr/src/app/work/
|
||||
COPY --from=builder /usr/src/app/rgb/mari* /usr/src/app/work/
|
||||
WORKDIR /usr/src/app/work
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"math"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
type Mario struct {
|
||||
position image.Point
|
||||
dir image.Point
|
||||
images map[string]image.Image
|
||||
updown string
|
||||
a float64
|
||||
b float64
|
||||
angle float64
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Animation) updateMarioPosition() {
|
||||
centerX := a.ctx.Width() / 2
|
||||
centerY := a.ctx.Height() / 2
|
||||
|
||||
// Determine sprite size (use current updown image if available)
|
||||
var sprite image.Image
|
||||
if img, ok := a.mario.images[a.mario.updown]; ok && img != nil {
|
||||
sprite = img
|
||||
} else {
|
||||
for _, im := range a.mario.images {
|
||||
sprite = im
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// default half sizes if sprite missing
|
||||
halfW, halfH := 8, 8
|
||||
if sprite != nil {
|
||||
halfW = sprite.Bounds().Dx() / 2
|
||||
halfH = sprite.Bounds().Dy() / 2
|
||||
}
|
||||
|
||||
// allowable center range so the sprite stays fully on the panel
|
||||
minCenterX := halfW
|
||||
maxCenterX := a.ctx.Width() - 1 - halfW
|
||||
minCenterY := halfH
|
||||
maxCenterY := a.ctx.Height() - 1 - halfH
|
||||
|
||||
// If the sprite is larger than the panel in a dimension, collapse
|
||||
// the allowed center range to the panel center so we don't get
|
||||
// immediate collisions every frame which makes Mario flash.
|
||||
if maxCenterX < minCenterX {
|
||||
minCenterX = centerX
|
||||
maxCenterX = centerX
|
||||
}
|
||||
if maxCenterY < minCenterY {
|
||||
minCenterY = centerY
|
||||
maxCenterY = centerY
|
||||
}
|
||||
|
||||
// advance angle for this frame
|
||||
delta := 0.05
|
||||
t := a.mario.angle
|
||||
tNext := t + delta
|
||||
if tNext >= 2*math.Pi {
|
||||
tNext -= 2 * math.Pi
|
||||
}
|
||||
|
||||
// compute next candidate center from the next parametric position
|
||||
marioX := int(math.Round(a.mario.a*math.Cos(tNext))) + centerX
|
||||
marioY := int(math.Round(a.mario.b*math.Sin(tNext))) + centerY
|
||||
|
||||
// clamp into allowed center ranges so sprite stays fully on panel
|
||||
if marioX < minCenterX {
|
||||
marioX = minCenterX
|
||||
}
|
||||
if marioX > maxCenterX {
|
||||
marioX = maxCenterX
|
||||
}
|
||||
if marioY < minCenterY {
|
||||
marioY = minCenterY
|
||||
}
|
||||
if marioY > maxCenterY {
|
||||
marioY = maxCenterY
|
||||
}
|
||||
|
||||
// commit position and advance angle
|
||||
a.mario.position.X = marioX
|
||||
a.mario.position.Y = marioY
|
||||
a.mario.angle = tNext
|
||||
|
||||
// Direction logic (based on the motion between t and tNext)
|
||||
// approximate direction by comparing positions (preferable to trig sign when clamped)
|
||||
prevX := int(math.Round(a.mario.a*math.Cos(t))) + centerX
|
||||
if prevX == marioX {
|
||||
// if we didn't move horizontally, keep previous direction
|
||||
// (this prevents flicker when clamped)
|
||||
} else if marioX < prevX {
|
||||
a.mario.dir.X = -1
|
||||
} else {
|
||||
a.mario.dir.X = 1
|
||||
}
|
||||
|
||||
// up/down based on vertical movement
|
||||
prevY := int(math.Round(a.mario.b*math.Sin(t))) + centerY
|
||||
if prevY == marioY {
|
||||
// keep previous up/down state
|
||||
} else if marioY < prevY {
|
||||
a.mario.updown = "marioUp"
|
||||
} else {
|
||||
a.mario.updown = "marioDown"
|
||||
}
|
||||
}
|
||||
73
board.go
73
board.go
@@ -1,73 +0,0 @@
|
||||
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},
|
||||
a: 62,
|
||||
b: 31,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
14
go.mod
14
go.mod
@@ -1,13 +1,11 @@
|
||||
module rgb
|
||||
|
||||
go 1.21.2
|
||||
|
||||
toolchain go1.21.6
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v1.0.0
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.0
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.2
|
||||
github.com/fogleman/gg v1.3.0
|
||||
)
|
||||
|
||||
@@ -15,14 +13,14 @@ require (
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20221208032759-85de2813cf6b // indirect
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/jezek/xgb v1.1.0 // indirect
|
||||
golang.org/x/exp/shiny v0.0.0-20231006140011-7918f672742d // indirect
|
||||
golang.org/x/image v0.13.0 // indirect
|
||||
golang.org/x/mobile v0.0.0-20231006135142-2b44d11868fe // indirect
|
||||
golang.org/x/net v0.27.0 // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/sys v0.22.0 // indirect
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
|
||||
golang.org/x/sync v0.4.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
)
|
||||
|
||||
//replace gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v0.0.0-20220709235716-bf87ebdb3e55 => /usr/src/app
|
||||
|
||||
29
go.sum
29
go.sum
@@ -4,24 +4,22 @@ gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v1.0.0 h1:My2/df
|
||||
gitea.wagshome.duckdns.org/publicWagsHome/go-rpi-rgb-led-matrix v1.0.0/go.mod h1:irNP534nYpVyP1wwMz2O903jJ2Ve6se7LAGi6A8JSnE=
|
||||
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk=
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=
|
||||
github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA=
|
||||
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
||||
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk=
|
||||
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/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
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/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||
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/go.mod h1:UH99kUObWAZkDnWqppdQe5ZhPYESUw8I0zVV1uWBR+0=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
@@ -29,12 +27,15 @@ golang.org/x/image v0.13.0 h1:3cge/F/QTkNLauhf2QoE9zp+7sr+ZcL4HnoZmdwg9sg=
|
||||
golang.org/x/image v0.13.0/go.mod h1:6mmbMOeV28HuMTgA6OSRkdXKYw/t5W9Uwn2Yv1r3Yxk=
|
||||
golang.org/x/mobile v0.0.0-20231006135142-2b44d11868fe h1:lrXv4yHeD9FA8PSJATWowP1QvexpyAPWmPia+Kbzql8=
|
||||
golang.org/x/mobile v0.0.0-20231006135142-2b44d11868fe/go.mod h1:BrnXpEObnFxpaT75Jo9hsCazwOWcp7nVIa8NNuH5cuA=
|
||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
|
||||
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
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=
|
||||
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,19 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
b64 "encoding/base64"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
_ "image/jpeg"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"time"
|
||||
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"
|
||||
"github.com/fogleman/gg"
|
||||
)
|
||||
|
||||
// flags from cmd line
|
||||
// contents of struct mostly don't matter for toolkit.
|
||||
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 (
|
||||
rows = flag.Int("led-rows", 64, "number of rows supported")
|
||||
cols = flag.Int("led-cols", 64, "number of columns supported")
|
||||
@@ -27,18 +56,160 @@ var (
|
||||
led_slowdown_gpio = flag.Int("led-slowdown-gpio", 1, "GPIO pin slowdown")
|
||||
)
|
||||
|
||||
// runs before main, parses flags
|
||||
//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
|
||||
})
|
||||
}
|
||||
|
||||
//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() {
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
// small function for handling fatal errors
|
||||
//small function for handling fatal errors
|
||||
func fatal(err error) {
|
||||
if err != nil {
|
||||
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() {
|
||||
config := &rgbmatrix.DefaultConfig
|
||||
config.Rows = *rows
|
||||
@@ -62,7 +233,7 @@ func main() {
|
||||
tk := rgbmatrix.NewToolKit(m)
|
||||
defer tk.Close()
|
||||
log.Println("making animator")
|
||||
go orchestrator(tk, mqMessages)
|
||||
go animator(tk, mqMessages)
|
||||
log.Println("I guess I'm at the end")
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
50
mqttcoms.go
50
mqttcoms.go
@@ -1,50 +0,0 @@
|
||||
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