Last active
November 11, 2020 22:34
-
-
Save peterhellberg/e9f4116fff294dbb4e440f5c3d7820c7 to your computer and use it in GitHub Desktop.
SenseHAT + NATS Draw https://github.com/peterhellberg/natsdraw
This file contains hidden or 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 ( | |
"image" | |
"image/color" | |
"math" | |
"math/rand" | |
"time" | |
"github.com/peterhellberg/natsdraw" | |
) | |
const w, h = 8, 8 | |
func main() { | |
m, err := natsdraw.New("sense.natsdraw", image.Rect(0, 0, w, h)) | |
if err != nil { | |
panic(err) | |
} | |
rand.Seed(123456) | |
particles := []*particle{} | |
fall := func(fx, fy float64) { | |
speed := 1 + (8.0 * rand.Float64()) | |
particles = append(particles, newParticle(fx, fy, speed, color.RGBA{0, uint8(32 + rand.Intn(224)), 0, 255})) | |
} | |
for i := 0; i < 32; i++ { | |
fall(float64(rand.Intn(w)), float64(rand.Intn(h))) | |
} | |
c := time.Tick(16 * time.Millisecond) | |
for range c { | |
for _, p := range particles { | |
p.update(0.06) | |
m.Set(int(p.position.X), int(p.position.Y), p.color) | |
} | |
} | |
} | |
type particle struct { | |
position vec | |
velocity vec | |
color color.RGBA | |
} | |
func newParticle(x, y, speed float64, c color.RGBA) *particle { | |
return &particle{ | |
position: vec{x, y}, | |
velocity: vec{ | |
X: speed * math.Cos(1.570), | |
Y: -speed * math.Sin(1.570), | |
}, | |
color: c, | |
} | |
} | |
func (p *particle) update(dt float64) { | |
p.position.X += p.velocity.X * dt | |
p.position.Y += p.velocity.Y * dt | |
if p.position.Y < 0 { | |
p.position.Y = 8 + float64(rand.Intn(h)) | |
p.position.X = float64(rand.Intn(w)) | |
} | |
} | |
type vec struct { | |
X float64 | |
Y float64 | |
} |
This file contains hidden or 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 ( | |
"image" | |
"image/color" | |
"github.com/faiface/pixel" | |
"github.com/faiface/pixel/imdraw" | |
"github.com/faiface/pixel/pixelgl" | |
"github.com/peterhellberg/natsdraw" | |
) | |
func run() { | |
m, err := natsdraw.New("sense.natsdraw", image.Rect(0, 0, 8, 8)) | |
if err != nil { | |
panic(err) | |
} | |
s := 32 | |
c, r := m.Bounds().Max.X, m.Bounds().Max.Y | |
w, h := c*s, r*s | |
win, err := pixelgl.NewWindow(pixelgl.WindowConfig{ | |
Bounds: pixel.R(0, 0, float64(w), float64(h)), | |
VSync: true, | |
Undecorated: true, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
win.Clear(color.RGBA{0, 0, 0, 255}) | |
for !win.Closed() { | |
imd := imdraw.New(nil) | |
for x := 0; x < c; x++ { | |
for y := 0; y < r; y++ { | |
imd.Color = m.At(x, y) | |
imd.Push(pixel.V(float64(x*s+s/2), float64(y*s+s/2))) | |
imd.Circle(float64(s)/2.1, 0) | |
} | |
} | |
imd.Draw(win) | |
win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ)) | |
win.Update() | |
} | |
} | |
func main() { | |
pixelgl.Run(run) | |
} |
This file contains hidden or 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 ( | |
"image" | |
"time" | |
"github.com/peterhellberg/natsdraw" | |
"github.com/peterhellberg/plasma" | |
"github.com/peterhellberg/plasma/palette" | |
) | |
func main() { | |
w, h, i := 8, 8, 0 | |
m, err := natsdraw.New("sense.natsdraw", image.Rect(0, 0, w, h)) | |
if err != nil { | |
panic(err) | |
} | |
c := time.Tick(64 * time.Millisecond) | |
for range c { | |
i++ | |
pi := plasma.New(w*16, h*16, 2.5).Image(w, h, i, palette.DefaultGradient) | |
w, h := m.Bounds().Max.X, m.Bounds().Max.Y | |
for x := 0; x < w; x++ { | |
for y := 0; y < h; y++ { | |
m.Set(x, y, pi.At(x, y)) | |
} | |
} | |
} | |
} |
This file contains hidden or 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 ( | |
"image" | |
"image/color" | |
"math/rand" | |
"time" | |
"github.com/peterhellberg/natsdraw" | |
) | |
const seed = 123456 | |
func main() { | |
rand.Seed(seed) | |
m, err := natsdraw.New("sense.natsdraw", image.Rect(0, 0, 8, 8)) | |
if err != nil { | |
panic(err) | |
} | |
c := time.Tick(128 * time.Millisecond) | |
for range c { | |
for x := 0; x < 8; x++ { | |
for y := 0; y < 8; y++ { | |
m.Set(x, y, color.NRGBA{ | |
uint8(rand.Intn(256)), | |
uint8(rand.Intn(256)), | |
uint8(rand.Intn(256)), | |
255, | |
}) | |
} | |
} | |
} | |
} |
This file contains hidden or 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 ( | |
"fmt" | |
"image" | |
"os" | |
"os/signal" | |
"github.com/nathany/bobblehat/sense/screen" | |
"github.com/peterhellberg/natsdraw" | |
) | |
func main() { | |
m, err := natsdraw.New("sense.natsdraw", image.Rect(0, 0, 8, 8), natsdraw.Connect("nats://192.168.1.12:4222")) | |
if err != nil { | |
panic(err) | |
} | |
screen.Clear() | |
signals := make(chan os.Signal, 1) | |
signal.Notify(signals, os.Interrupt, os.Kill) | |
for { | |
select { | |
case <-signals: | |
fmt.Println("") | |
screen.Clear() | |
return | |
default: | |
screen.DrawImage(m) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sense HAT
nd-sense.go
draws a*natsdraw.Image
on the Sense HAT LEDs using bobblehat