aboutsummaryrefslogtreecommitdiff
path: root/unicorn/RealUnicorn.go
blob: 4e5c376be86b74edd2e8fbab77db996d6acee5b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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()
}