From 336a5c2bf63c0359774b2493c62cf1f4467dde83 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 14 Jun 2023 23:47:18 +0000 Subject: [PATCH] trying to do image work --- main.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 776096a..c09aed6 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,9 @@ package main import ( + "bytes" + b64 "encoding/base64" + "encoding/json" "flag" "fmt" "image" @@ -21,6 +24,10 @@ import ( // 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 @@ -133,6 +140,7 @@ func NewAnimation(sz image.Point, mqMessages chan mqtt.Message) *Animation { // what happens each frame, at an interval of 50 milliseconds func (a *Animation) Next() (image.Image, <-chan time.Time, error) { + incoming := incomingImage{} defer a.updatePosition() a.ctx.SetColor(color.Black) a.ctx.Clear() @@ -144,10 +152,18 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) { a.ctx.SetColor(color.White) select { case msg := <-a.mqmsg: - a.msg = string(msg.Payload()) + json.Unmarshal([]byte(string(msg.Payload())), &incoming) + if incoming.Image == "" { + a.msg = string(msg.Payload()) + a.ctx.DrawString(a.msg, 5, 9) + } else { + baseImage, _ := b64.StdEncoding.DecodeString(incoming.Image) + bigImage, _, _ := image.Decode(bytes.NewReader(baseImage)) + littleImage := imaging.Resize(bigImage, 32, 32, imaging.Lanczos) + a.ctx.DrawImageAnchored(littleImage, 0, 0, 0, 0) + } default: } - a.ctx.DrawString(a.msg, 5, 9) return a.ctx.Image(), time.After(time.Millisecond * 50), nil } @@ -194,9 +210,9 @@ func main() { log.Println("making animator") go animator(tk, mqMessages) 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) - done := make(chan bool, -1) + done := make(chan bool, 1) go func() { sig := <-sigs fmt.Println()