-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboard-badger2040.go
139 lines (115 loc) · 2.98 KB
/
board-badger2040.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
//go:build badger2040
package board
import (
"machine"
"math/bits"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/pixel"
"tinygo.org/x/drivers/uc8151"
)
const (
Name = "badger2040"
)
var (
Power = dummyBattery{state: UnknownBattery}
Sensors = baseSensors{}
Display = mainDisplay{}
Buttons = &gpioButtons{}
)
type mainDisplay struct{}
func (d mainDisplay) PPI() int {
return 102 // 296px wide display / 2.9 inches wide display
}
func (d mainDisplay) Configure() Displayer[pixel.Monochrome] {
machine.ENABLE_3V3.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.ENABLE_3V3.High()
machine.SPI0.Configure(machine.SPIConfig{
Frequency: 12 * machine.MHz,
SCK: machine.EPD_SCK_PIN,
SDO: machine.EPD_SDO_PIN,
})
display := uc8151.New(machine.SPI0, machine.EPD_CS_PIN, machine.EPD_DC_PIN, machine.EPD_RESET_PIN, machine.EPD_BUSY_PIN)
display.Configure(uc8151.Config{
Rotation: drivers.Rotation270,
Speed: uc8151.TURBO,
FlickerFree: true,
Blocking: false,
})
display.ClearDisplay()
return &display
}
func (d mainDisplay) MaxBrightness() int {
return 1
}
func (d mainDisplay) SetBrightness(level int) {
// Nothing to do here.
}
func (d mainDisplay) WaitForVBlank(defaultInterval time.Duration) {
dummyWaitForVBlank(defaultInterval)
}
func (d mainDisplay) ConfigureTouch() TouchInput {
return noTouch{}
}
type gpioButtons struct {
state uint8
previousState uint8
}
func (b *gpioButtons) Configure() {
machine.BUTTON_A.Configure(machine.PinConfig{Mode: machine.PinInput})
machine.BUTTON_B.Configure(machine.PinConfig{Mode: machine.PinInput})
machine.BUTTON_C.Configure(machine.PinConfig{Mode: machine.PinInput})
machine.BUTTON_UP.Configure(machine.PinConfig{Mode: machine.PinInput})
machine.BUTTON_DOWN.Configure(machine.PinConfig{Mode: machine.PinInput})
machine.BUTTON_USER.Configure(machine.PinConfig{Mode: machine.PinInput})
}
func (b *gpioButtons) ReadInput() {
state := uint8(0)
if !machine.BUTTON_A.Get() {
state |= 1
}
if !machine.BUTTON_B.Get() {
state |= 2
}
if !machine.BUTTON_C.Get() {
state |= 4
}
if !machine.BUTTON_UP.Get() {
state |= 8
}
if !machine.BUTTON_DOWN.Get() {
state |= 16
}
if !machine.BUTTON_USER.Get() {
state |= 32
}
b.state = state
}
var codes = [8]Key{
KeyA,
KeyB,
KeyRight,
KeyUp,
KeyDown,
KeyLeft,
}
func (b *gpioButtons) NextEvent() KeyEvent {
// The xor between the previous state and the current state is the buttons
// that changed.
change := b.state ^ b.previousState
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.state&(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 b.state.
b.previousState ^= (1 << index)
return e
}