Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active November 11, 2020 22:34
Show Gist options
  • Save peterhellberg/e9f4116fff294dbb4e440f5c3d7820c7 to your computer and use it in GitHub Desktop.
Save peterhellberg/e9f4116fff294dbb4e440f5c3d7820c7 to your computer and use it in GitHub Desktop.
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
}
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)
}
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))
}
}
}
}
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,
})
}
}
}
}
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)
}
}
}
@peterhellberg
Copy link
Author

peterhellberg commented Dec 31, 2017

Matrix Random Plasma
screen shot 2017-12-31 at 06 58 15 screen shot 2017-12-31 at 06 58 47 screen shot 2017-12-31 at 06 59 22

@peterhellberg
Copy link
Author

Installation of gnatsd

go get github.com/nats-io/gnatsd or brew install gnatsd

@peterhellberg
Copy link
Author

peterhellberg commented Dec 31, 2017

Sense HAT

nd-sense.go draws a *natsdraw.Image on the Sense HAT LEDs using bobblehat

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