aboutsummaryrefslogtreecommitdiff
path: root/unicorn/RealUnicorn2.go
diff options
context:
space:
mode:
authorMartin Ashby <martin@martin-laptop.lan>2018-05-28 16:31:18 +0100
committerMartin Ashby <martin@martin-laptop.lan>2018-05-28 16:31:18 +0100
commit28f0ac19eb3b42601b0511131b5e39c77b80da9c (patch)
treea0765d2742926bb418ce623175876a68cb000c8b /unicorn/RealUnicorn2.go
parent5766b0c285052d48ab7756c9c3eef6ae41ecd36e (diff)
parentf20896ef3b147ac07769eb36c67ba436c6d2ed22 (diff)
downloadunicornpaint-28f0ac19eb3b42601b0511131b5e39c77b80da9c.tar.gz
unicornpaint-28f0ac19eb3b42601b0511131b5e39c77b80da9c.tar.bz2
unicornpaint-28f0ac19eb3b42601b0511131b5e39c77b80da9c.tar.xz
unicornpaint-28f0ac19eb3b42601b0511131b5e39c77b80da9c.zip
Merge branch 'master' of github.com:MFAshby/unicornpaint
Diffstat (limited to 'unicorn/RealUnicorn2.go')
-rw-r--r--unicorn/RealUnicorn2.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/unicorn/RealUnicorn2.go b/unicorn/RealUnicorn2.go
new file mode 100644
index 0000000..991e79f
--- /dev/null
+++ b/unicorn/RealUnicorn2.go
@@ -0,0 +1,69 @@
+// +build linux,arm linux,arm64
+
+package unicorn
+
+import (
+ "image"
+ "log"
+ "os"
+ "os/signal"
+ "syscall"
+
+ "github.com/ecc1/spi"
+)
+
+type RealUnicorn2 struct {
+ BaseUnicorn2
+ device *spi.Device
+}
+
+func (u *RealUnicorn2) renderImage(im image.Image) {
+ b := im.Bounds()
+ width, height := b.Dx(), b.Dy()
+ sz := (width * height * 3) + 1
+ write := make([]byte, sz)
+
+ // Write leading bit
+ write[0] = 0x72
+
+ // Write color values
+ ix := 1
+ for x := 0; x < width; x++ {
+ for y := 0; y < height; y++ {
+ col := im.At(x, y)
+ r, g, b, _ := col.RGBA()
+ write[ix] = byte(r)
+ ix++
+ write[ix] = byte(g)
+ ix++
+ write[ix] = byte(b)
+ ix++
+ }
+ }
+ // Write to the device
+ err := u.device.Transfer(write)
+ if err != nil {
+ log.Printf("Error writing to SPI device %v", err)
+ }
+}
+
+// NewUnicorn2 ...
+// Constructs a new and improved unicorn from stuff and things
+func NewUnicorn2() (*RealUnicorn2, error) {
+ dev, err := spi.Open("/dev/spidev0.0", 9000000, 0)
+ if err != nil {
+ return nil, err
+ }
+ return &RealUnicorn2{
+ BaseUnicorn2{},
+ dev,
+ }, nil
+}
+
+// MainLoop ...
+// Just blocks until sigterm
+func (u *RealUnicorn2) MainLoop() {
+ c := make(chan os.Signal, 2)
+ signal.Notify(c, os.Interrupt, syscall.SIGTERM)
+ <-c
+}