Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created March 29, 2020 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/35f7ff69e572def677820fadbde4b3cd to your computer and use it in GitHub Desktop.
Save peterhellberg/35f7ff69e572def677820fadbde4b3cd to your computer and use it in GitHub Desktop.
Drawing to the RPi (SenseHat) framebuffer over the network
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)
}
}
package main
import "net/http"
func main() {
u := "http://192.168.1.3/clear"
http.Post(u, "", nil)
}
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)
}
}
@peterhellberg
Copy link
Author

framebuffer

framebuffer-bouncing

@peterhellberg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment