From 32e997346bb5766aefa6ef5f4caa189af128b498 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Mon, 21 May 2018 09:54:00 +0100 Subject: Moved unicorn lib to it's own package --- unicorn/RealUnicorn.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 unicorn/RealUnicorn.go (limited to 'unicorn/RealUnicorn.go') 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() +} -- cgit v1.2.3-ZIG