aboutsummaryrefslogtreecommitdiff
path: root/unicorn/RealUnicorn.go
diff options
context:
space:
mode:
authorMartin Ashby <martin@martin-laptop.lan>2018-05-21 09:54:00 +0100
committerMartin Ashby <martin@martin-laptop.lan>2018-05-21 09:54:00 +0100
commit32e997346bb5766aefa6ef5f4caa189af128b498 (patch)
treeeb88888c4a6c6e8d58e31054df94199f66b69344 /unicorn/RealUnicorn.go
parent445de8359766b4fe04750335dbadec21312bc4c6 (diff)
downloadunicornpaint-32e997346bb5766aefa6ef5f4caa189af128b498.tar.gz
unicornpaint-32e997346bb5766aefa6ef5f4caa189af128b498.tar.bz2
unicornpaint-32e997346bb5766aefa6ef5f4caa189af128b498.tar.xz
unicornpaint-32e997346bb5766aefa6ef5f4caa189af128b498.zip
Moved unicorn lib to it's own package
Diffstat (limited to 'unicorn/RealUnicorn.go')
-rw-r--r--unicorn/RealUnicorn.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/unicorn/RealUnicorn.go b/unicorn/RealUnicorn.go
new file mode 100644
index 0000000..4e5c376
--- /dev/null
+++ b/unicorn/RealUnicorn.go
@@ -0,0 +1,66 @@
+// +build linux,arm linux,arm64
+
+package unicorn
+
+import (
+ //"golang.org/x/exp/io/spi"
+ "log"
+
+ "github.com/ecc1/spi"
+)
+
+type RealUnicorn struct {
+ BaseUnicorn
+ device *spi.Device
+}
+
+// NewUnicorn ...
+// Constructs a new real unicorn from fairy dust and sprinkles
+func NewUnicorn() (*RealUnicorn, error) {
+ dev, err := spi.Open("/dev/spidev0.0", 9000000, 0)
+ if err != nil {
+ return nil, err
+ }
+
+ return &RealUnicorn{
+ BaseUnicorn{
+ pixels: makePixels(16, 16),
+ },
+ dev,
+ }, nil
+}
+
+func (u *RealUnicorn) Show() {
+ // Width * height * colours + leading bit
+ width := int(u.GetWidth())
+ height := int(u.GetHeight())
+ sz := (width * height * 3) + 1
+ write := make([]byte, sz)
+
+ // Add the leading bit
+ write[0] = 0x72
+ // Add all the pixel values
+ ix := 1
+ for x := 0; x < width; x++ {
+ for y := 0; y < height; y++ {
+ for j := 0; j < 3; j++ {
+ write[ix] = byte(u.pixels[x][y][j])
+ ix++
+ }
+ }
+ }
+ // Write to the device
+ //err := u.device.Tx(write, nil)
+ err := u.device.Transfer(write)
+ if err != nil {
+ log.Printf("Error writing to SPI device %v", err)
+ }
+}
+
+func (u *RealUnicorn) Off() {
+ u.Close()
+}
+
+func (u *RealUnicorn) Close() error {
+ return u.device.Close()
+}