forked from aykevl/board
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board-pybadge.go
206 lines (171 loc) · 4.27 KB
/
board-pybadge.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
//go:build pybadge
package board
import (
"machine"
"math/bits"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/lis3dh"
"tinygo.org/x/drivers/pixel"
"tinygo.org/x/drivers/shifter"
"tinygo.org/x/drivers/st7735"
"tinygo.org/x/drivers/ws2812"
)
const (
Name = "pybadge"
)
var (
Power = mainBattery{}
Sensors = &allSensors{}
Display = mainDisplay{}
Buttons = &buttonsConfig{}
)
func init() {
AddressableLEDs = &ws2812LEDs{}
}
type mainBattery struct {
}
func (b mainBattery) Configure() {
machine.InitADC()
machine.ADC{Pin: machine.A6}.Configure(machine.ADCConfig{
Samples: 4, // 4 seems to be good enough
})
}
func (b mainBattery) Status() (ChargeState, uint32, int8) {
rawValue := machine.ADC{Pin: machine.A6}.Get()
// Formula to calculate microvolts:
// rawValue * 6600_000 / 0x10000
// Simlified, to fit in 32-bit integers:
// rawValue * 51562 / 512
microvolts := uint32(rawValue) * 51562 / 512
return UnknownBattery, microvolts, lithumBatteryApproximation.approximate(microvolts)
}
type allSensors struct {
baseSensors
accelX, accelY, accelZ int32
}
var accel lis3dh.Device
func (s *allSensors) Configure(which drivers.Measurement) error {
if which&drivers.Acceleration != 0 {
machine.I2C0.Configure(machine.I2CConfig{
Frequency: 400 * machine.KHz,
SCL: machine.SCL_PIN,
SDA: machine.SDA_PIN,
})
accel = lis3dh.New(machine.I2C0)
accel.Configure()
}
return nil
}
func (s *allSensors) Update(which drivers.Measurement) error {
// TODO:
// - read temperature from LIS3DH
// - read brightness value
if which&drivers.Acceleration != 0 {
var err error
s.accelX, s.accelY, s.accelZ, err = accel.ReadAcceleration()
if err != nil {
return err
}
}
return nil
}
func (s *allSensors) Acceleration() (x, y, z int32) {
// Adjust accelerometer to match standard axes.
x = -s.accelX
y = s.accelY
z = -s.accelZ
return
}
type mainDisplay struct{}
func (d mainDisplay) PPI() int {
return 116 // 160px / (35.04mm / 25.4)
}
func (d mainDisplay) Configure() Displayer[pixel.RGB565BE] {
machine.SPI1.Configure(machine.SPIConfig{
SCK: machine.SPI1_SCK_PIN,
SDO: machine.SPI1_SDO_PIN,
SDI: machine.SPI1_SDI_PIN,
Frequency: 15_000_000, // datasheet for st7735 says 66ns (~15.15MHz) is the max speed
})
display := st7735.New(machine.SPI1, machine.TFT_RST, machine.TFT_DC, machine.TFT_CS, machine.TFT_LITE)
display.Configure(st7735.Config{
Rotation: st7735.ROTATION_90,
})
display.EnableBacklight(false)
return &display
}
func (d mainDisplay) MaxBrightness() int {
return 1
}
func (d mainDisplay) SetBrightness(level int) {
machine.TFT_LITE.Set(level > 0)
}
func (d mainDisplay) WaitForVBlank(defaultInterval time.Duration) {
dummyWaitForVBlank(defaultInterval)
}
func (d mainDisplay) ConfigureTouch() TouchInput {
return noTouch{}
}
type buttonsConfig struct {
shifter.Device
lastState, currentState uint8
}
func (b *buttonsConfig) Configure() {
b.Device = shifter.NewButtons()
b.Device.Configure()
}
func (b *buttonsConfig) ReadInput() {
b.currentState, _ = b.Device.ReadInput()
}
var codes = [8]Key{
KeyLeft,
KeyUp,
KeyDown,
KeyRight,
KeySelect,
KeyStart,
KeyA,
KeyB,
}
func (b *buttonsConfig) NextEvent() KeyEvent {
// The xor between the previous state and the current state is the buttons
// that changed.
change := b.currentState ^ b.lastState
if change == 0 {
return NoKeyEvent
}
// Find the index of the button with the lowest index that changed state.
index := bits.TrailingZeros32(uint32(change))
e := KeyEvent(codes[index])
if b.currentState&(1<<index) == 0 {
// The button state change was from 1 to 0, so it was released.
e |= keyReleased
}
// This button event was read, so mark it as such.
// By toggling the bit, the bit will be set to the value that is currently
// in currentState.
b.lastState ^= (1 << index)
return e
}
type ws2812LEDs struct {
data [5]colorGRB
}
func (l *ws2812LEDs) Configure() {
machine.WS2812.Configure(machine.PinConfig{Mode: machine.PinOutput})
}
func (l *ws2812LEDs) Len() int {
return len(l.data)
}
func (l *ws2812LEDs) SetRGB(i int, r, g, b uint8) {
l.data[i] = colorGRB{
R: r,
G: g,
B: b,
}
}
// Send pixel data to the LEDs.
func (l *ws2812LEDs) Update() {
ws := ws2812.Device{Pin: machine.WS2812}
ws.Write(pixelsToBytes(l.data[:]))
}