Skip to content

Commit 41c6e3b

Browse files
authored
shifter: simplify API surface for PyBadge (#137)
* shifter: simplify API surface and use build directive to directly match the PyBadge Signed-off-by: Ron Evans <[email protected]> * shifter: further simplify API for PyBadge Signed-off-by: Ron Evans <[email protected]>
1 parent 583e800 commit 41c6e3b

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

examples/shifter/main.go

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,42 @@
1+
// This example is designed to implement the button shifter for a PyBadge.
12
package main
23

34
import (
4-
"machine"
55
"time"
66

77
"tinygo.org/x/drivers/shifter"
88
)
99

10-
const (
11-
BUTTON_LEFT = iota
12-
BUTTON_UP
13-
BUTTON_DOWN
14-
BUTTON_RIGHT
15-
BUTTON_SELECT
16-
BUTTON_START
17-
BUTTON_A
18-
BUTTON_B
19-
)
20-
2110
func main() {
22-
buttons := shifter.New(shifter.EIGHT_BITS, machine.BUTTON_LATCH, machine.BUTTON_CLK, machine.BUTTON_OUT)
11+
buttons := shifter.NewButtons()
2312
buttons.Configure()
2413

2514
for {
2615
// Update the pins state, to later be returned by .Get()
27-
buttons.Read8Input()
16+
buttons.ReadInput()
2817

29-
if buttons.Pins[BUTTON_LEFT].Get() {
18+
if buttons.Pins[shifter.BUTTON_LEFT].Get() {
3019
println("Button LEFT pressed")
3120
}
32-
if buttons.Pins[BUTTON_UP].Get() {
21+
if buttons.Pins[shifter.BUTTON_UP].Get() {
3322
println("Button UP pressed")
3423
}
35-
if buttons.Pins[BUTTON_DOWN].Get() {
24+
if buttons.Pins[shifter.BUTTON_DOWN].Get() {
3625
println("Button DOWN pressed")
3726
}
38-
if buttons.Pins[BUTTON_RIGHT].Get() {
27+
if buttons.Pins[shifter.BUTTON_RIGHT].Get() {
3928
println("Button RIGHT pressed")
4029
}
41-
if buttons.Pins[BUTTON_SELECT].Get() {
30+
if buttons.Pins[shifter.BUTTON_SELECT].Get() {
4231
println("Button SELECT pressed")
4332
}
44-
if buttons.Pins[BUTTON_START].Get() {
33+
if buttons.Pins[shifter.BUTTON_START].Get() {
4534
println("Button START pressed")
4635
}
47-
if buttons.Pins[BUTTON_A].Get() {
36+
if buttons.Pins[shifter.BUTTON_A].Get() {
4837
println("Button A pressed")
4938
}
50-
if buttons.Pins[BUTTON_B].Get() {
39+
if buttons.Pins[shifter.BUTTON_B].Get() {
5140
println("Button B pressed")
5241
}
5342
time.Sleep(100 * time.Millisecond)

shifter/pybadge.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// +build pybadge
2+
3+
package shifter
4+
5+
import "machine"
6+
7+
const (
8+
BUTTON_LEFT = 0
9+
BUTTON_UP = 1
10+
BUTTON_DOWN = 2
11+
BUTTON_RIGHT = 3
12+
BUTTON_SELECT = 4
13+
BUTTON_START = 5
14+
BUTTON_A = 6
15+
BUTTON_B = 7
16+
)
17+
18+
// NewButtons returns a new shifter device for the buttons on an AdaFruit PyBadge
19+
func NewButtons() Device {
20+
return Device{
21+
latch: machine.BUTTON_LATCH,
22+
clk: machine.BUTTON_CLK,
23+
out: machine.BUTTON_OUT,
24+
Pins: make([]ShiftPin, int(EIGHT_BITS)),
25+
bits: EIGHT_BITS,
26+
}
27+
}
28+
29+
// ReadInput returns the latest input readings from the PyBadge.
30+
func (d *Device) ReadInput() (uint8, error) {
31+
return d.Read8Input()
32+
}

shifter/shifter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type ShiftPin struct {
3030
pressed bool
3131
}
3232

33-
// New returns a new thermistor driver given an ADC pin.
33+
// New returns a new shifter driver given the correct pins.
3434
func New(numBits NumberBit, latch, clk, out machine.Pin) Device {
3535
return Device{
3636
latch: latch,

0 commit comments

Comments
 (0)