forked from tomnz/scroll-phat-hd-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
273 lines (239 loc) · 7.38 KB
/
driver.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package scrollphat
import (
"fmt"
"time"
"periph.io/x/periph/conn"
"periph.io/x/periph/conn/i2c"
"periph.io/x/periph/devices"
)
// NewDriver returns a new Scroll pHAT HD hardware driver. This implements the Device
// interface, and can be used by Display. Connects to the device on the given I2C bus
// at its standard address.
func NewDriver(bus i2c.Bus, opts ...DriverOption) (*Driver, error) {
return NewDriverWithConn(&i2c.Dev{Bus: bus, Addr: addr}, opts...)
}
// NewDriverWithConn returns a new Scroll pHAT HD hardware driver, using the given periph.io
// conn.Conn object. Typically NewDriver should be used instead, but this may be useful for
// testing using mocks, or a custom I2C connection with a different address than the default.
func NewDriverWithConn(periphConn conn.Conn, opts ...DriverOption) (*Driver, error) {
options := defaultDriverOptions
for _, opt := range opts {
opt(&options)
}
width, height := devWidth, devHeight
if options.rotation == Rotation90 || options.rotation == Rotation270 {
width, height = devHeight, devWidth
}
d := &Driver{
options: options,
i2c: periphConn,
brightness: 255,
width: width,
height: height,
}
if err := d.setup(); err != nil {
return nil, err
}
return d, nil
}
// Driver handles low level communication with a Scroll pHAT HD hardware device. It can
// be used directly if you do not have need for some of the higher-level features of the
// Display object.
type Driver struct {
options driverOptions
// Device handle for I2C bus
i2c conn.Conn
// Hardware frame currently in use
frame byte
buffer [][]byte
brightness byte
width, height int
}
// Width returns the width of the device in pixels.
func (s *Driver) Width() int {
return s.width
}
// Height returns the height of the device in pixels.
func (s *Driver) Height() int {
return s.height
}
// SetPixel sets the pixel at the given coordinate to the given value.
func (s *Driver) SetPixel(x, y int, val byte) error {
if x < 0 || x > s.width-1 {
return fmt.Errorf("received invalid x coordinate %d", x)
}
if y < 0 || y > s.height-1 {
return fmt.Errorf("received invalid y coordinate %d", y)
}
s.buffer[y][x] = val
return nil
}
// SetPixels copies all of the given pixels at once to the internal buffer.
// Dimensions of the incoming buffer are checked to ensure they match the width and height of
// the device.
// Note that the array should be indexed in row, col order.
func (s *Driver) SetPixels(pixels [][]byte) error {
if len(pixels) != s.height {
return fmt.Errorf("received invalid buffer of height %d", len(pixels))
}
for y, row := range pixels {
if len(row) != s.width {
return fmt.Errorf("received invalid buffer with row %d of width %d", y, len(row))
}
copy(s.buffer[y], row)
}
return nil
}
// SetBuffer allows setting all of the pixels at once by swapping out the internal buffer.
// This does NOT copy any of the data. This is exposed for performance reasons, but caution should
// be exercised! If the buffer is later updated externally, the contents of the internal buffer
// will also change!
// The dimensions of the incoming buffer are also not checked.
// When the final values are written to the device via Show, the internal buffer is copied, so
// this may increase safety some.
// Note that the array should be indexed in row, col order.
func (s *Driver) SetBuffer(buffer [][]byte) {
s.buffer = buffer
}
// SetBrightness sets the brightness of the device. This is applied to all pixels on Show.
// 0 is off, 255 is maximum brightness.
func (s *Driver) SetBrightness(brightness byte) {
s.brightness = brightness
}
// Clear turns off all pixels on the device.
func (s *Driver) Clear() error {
for _, row := range s.buffer {
for x := range row {
row[x] = 0
}
}
return s.Show()
}
// Show renders the contents of the internal buffer to the device. Brightness is applied.
func (s *Driver) Show() error {
// Maximum addressed LED is 134
output := make([]byte, 135)
for y, row := range s.buffer {
for x, val := range row {
output[s.pixelAddr(x, y)] = s.options.gamma[s.scaleVal(val)]
}
}
nextFrame := (s.frame + 1) % 2
if err := s.bank(nextFrame); err != nil {
return err
}
// Write the pixel data
// NOTE: Because we use the I2C bus directly (instead of via the SMBus subset) we can
// send all of the data at once, rather than chunking. This makes the data write atomic
// over the bus, eliminating the chance for someone else to interfere mid way.
if err := s.write(offsetColor, output...); err != nil {
return err
}
// Switch the active frame to the new frame
if err := s.writeRegister(regFrame, nextFrame); err != nil {
return err
}
s.frame = nextFrame
return nil
}
// scaleVal applies brightness to the given value.
func (s *Driver) scaleVal(val byte) byte {
// return byte(uint16(val) * uint16(s.brightness) / 255)
return s.brightness
}
// pixelAddr maps an x, y coordinate to the physical LED index that should be updated, after rotating
// the coordinates.
func (s *Driver) pixelAddr(x, y int) int {
switch s.options.rotation {
case Rotation0:
case Rotation90:
x, y = devWidth-1-y, x
case Rotation180:
x, y = devWidth-1-x, devHeight-1-y
case Rotation270:
x, y = y, devHeight-1-x
default:
panic("unknown rotation")
}
y = devHeight - y - 1
if x > 8 {
x -= 8
y = -y - 2
} else {
x = 8 - x
}
return x*16 + y
}
// setup performs initial setup of the I2C hardware device by sending initialization messages.
func (s *Driver) setup() error {
if err := s.reset(); err != nil {
return err
}
if err := s.writeRegister(regFrame, 0); err != nil {
return err
}
if err := s.writeRegister(regMode, modePicture); err != nil {
return err
}
if err := s.writeRegister(regAudioSync, 0); err != nil {
return err
}
if err := s.writeRegister(regBrightness, 255); err != nil {
return err
}
// Need to "turn on" all of the LEDs with an enable bit in the frames
// that we are going to use
enableRows := make([]byte, devWidth)
for i := range enableRows {
enableRows[i] = 255
}
if err := s.bank(1); err != nil {
return err
}
if err := s.write(offsetEnable, enableRows...); err != nil {
return err
}
if err := s.bank(0); err != nil {
return err
}
if err := s.write(offsetEnable, enableRows...); err != nil {
return err
}
s.buffer = make([][]byte, s.height)
for y := range s.buffer {
s.buffer[y] = make([]byte, s.width)
}
return s.Clear()
}
// reset reboots the hardware device.
func (s *Driver) reset() error {
if err := s.writeRegister(regShutdown, 0); err != nil {
return err
}
time.Sleep(time.Millisecond * 10)
return s.writeRegister(regShutdown, 1)
}
// writeRegister writes the corresponding value into the given register in the configuration bank
// on the device.
func (s *Driver) writeRegister(register, value byte) error {
if err := s.bank(configBank); err != nil {
return err
}
return s.write(register, value)
}
// bank switches the active bank on the device. The device uses multiple banks to multiplex the
// amount of data that it needs to access.
func (s *Driver) bank(bank byte) error {
return s.write(bankAddr, bank)
}
func (s *Driver) write(cmd byte, value ...byte) error {
msg := []byte{cmd}
msg = append(msg, value...)
return s.i2c.Tx(msg, nil)
}
// Halt implements devices.Device.
func (s *Driver) Halt() error {
return s.writeRegister(regShutdown, 0)
}
// Ensure the device actually implements the periph.io interface.
var _ devices.Device = &Driver{}