Created
March 29, 2020 12:35
-
-
Save peterhellberg/35f7ff69e572def677820fadbde4b3cd to your computer and use it in GitHub Desktop.
Drawing to the RPi (SenseHat) framebuffer over the network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"image/color" | |
"image/draw" | |
"net/http" | |
"time" | |
"github.com/peterhellberg/gfx" | |
) | |
func main() { | |
gfx.RandSeed(3456) | |
d := 16 * time.Millisecond | |
u := "http://192.168.1.3/framebuffer.png" | |
p := gfx.PalettePICO8 | |
ticker := time.NewTicker(d) | |
balls := []*Ball{ | |
newBall(p.Random()), | |
newBall(p.Random()), | |
newBall(p.Random()), | |
newBall(p.Random()), | |
} | |
for range ticker.C { | |
m := gfx.NewImage(8, 8) | |
for _, ball := range balls { | |
ball.Update() | |
ball.Draw(m) | |
} | |
var b bytes.Buffer | |
w := bufio.NewWriter(&b) | |
gfx.EncodePNG(w, m) | |
w.Flush() | |
http.Post(u, "image/png", &b) | |
} | |
} | |
type Ball struct { | |
pos gfx.Vec | |
dir gfx.Vec | |
col color.Color | |
} | |
func newBall(col color.Color) *Ball { | |
pos := gfx.IV(gfx.RandIntn(8), gfx.RandIntn(8)) | |
dir := gfx.V(gfx.RandFloat64()/4, gfx.RandFloat64()/4) | |
return &Ball{pos: pos, dir: dir, col: col} | |
} | |
func (b *Ball) Update() { | |
b.pos.X += b.dir.X | |
b.pos.Y += b.dir.Y | |
if b.pos.Y < 0 || b.pos.Y > 7 { | |
b.dir.Y *= -1 | |
} | |
if b.pos.X < 0 || b.pos.X > 7 { | |
b.dir.X *= -1 | |
} | |
} | |
func (b *Ball) Draw(dst draw.Image) { | |
t := gfx.PV(b.pos.Pt()) | |
d := t.To(b.pos).Len() | |
if d < 0.9 { | |
gfx.SetVec(dst, b.pos, b.col) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "net/http" | |
func main() { | |
u := "http://192.168.1.3/clear" | |
http.Post(u, "", nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"net/http" | |
"time" | |
"github.com/peterhellberg/gfx" | |
) | |
func main() { | |
gfx.RandSeed(1234) | |
d := 16 * time.Millisecond | |
u := "http://192.168.1.3/framebuffer.png" | |
m := gfx.NewImage(8, 8) | |
p := gfx.PaletteAmmo8 | |
ticker := time.NewTicker(d) | |
for range ticker.C { | |
x := gfx.RandIntn(8) | |
y := gfx.RandIntn(8) | |
c := p.Random() | |
m.Set(x, y, c) | |
var b bytes.Buffer | |
w := bufio.NewWriter(&b) | |
gfx.EncodePNG(w, m) | |
w.Flush() | |
http.Post(u, "image/png", &b) | |
} | |
} |
Author
peterhellberg
commented
Apr 17, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment