trying to do image work

pull/3/head
nathan 1 year ago
parent 2e6d88de2a
commit 336a5c2bf6

@ -1,6 +1,9 @@
package main package main
import ( import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"image" "image"
@ -21,6 +24,10 @@ import (
// contents of struct mostly don't matter for toolkit. // contents of struct mostly don't matter for toolkit.
type incomingImage struct {
Image string `json:"image"`
}
type Animation struct { type Animation struct {
ctx *gg.Context ctx *gg.Context
position image.Point 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 // what happens each frame, at an interval of 50 milliseconds
func (a *Animation) Next() (image.Image, <-chan time.Time, error) { func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
incoming := incomingImage{}
defer a.updatePosition() defer a.updatePosition()
a.ctx.SetColor(color.Black) a.ctx.SetColor(color.Black)
a.ctx.Clear() a.ctx.Clear()
@ -144,10 +152,18 @@ func (a *Animation) Next() (image.Image, <-chan time.Time, error) {
a.ctx.SetColor(color.White) a.ctx.SetColor(color.White)
select { select {
case msg := <-a.mqmsg: case msg := <-a.mqmsg:
json.Unmarshal([]byte(string(msg.Payload())), &incoming)
if incoming.Image == "" {
a.msg = string(msg.Payload()) 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: default:
} }
a.ctx.DrawString(a.msg, 5, 9)
return a.ctx.Image(), time.After(time.Millisecond * 50), nil return a.ctx.Image(), time.After(time.Millisecond * 50), nil
} }
@ -194,9 +210,9 @@ func main() {
log.Println("making animator") log.Println("making animator")
go animator(tk, mqMessages) go animator(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)
done := make(chan bool, -1) done := make(chan bool, 1)
go func() { go func() {
sig := <-sigs sig := <-sigs
fmt.Println() fmt.Println()

Loading…
Cancel
Save