Skip to content

Commit 583e800

Browse files
conejoninjadeadprogram
authored andcommitted
new API for shifter driver.
this doesn't break existing code, but it might no longer work as expected
1 parent c7cbd7c commit 583e800

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

examples/shifter/main.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,47 @@ import (
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+
1021
func main() {
1122
buttons := shifter.New(shifter.EIGHT_BITS, machine.BUTTON_LATCH, machine.BUTTON_CLK, machine.BUTTON_OUT)
1223
buttons.Configure()
1324

1425
for {
15-
// Slower
16-
for i := 0; i < 8; i++ {
17-
if buttons.Pins[i].Get() {
18-
println("Button", i, "pressed")
19-
}
20-
}
26+
// Update the pins state, to later be returned by .Get()
27+
buttons.Read8Input()
2128

22-
// Faster
23-
pressed, _ := buttons.Read8Input()
24-
if pressed&machine.BUTTON_LEFT_MASK > 0 {
29+
if buttons.Pins[BUTTON_LEFT].Get() {
2530
println("Button LEFT pressed")
2631
}
27-
if pressed&machine.BUTTON_UP_MASK > 0 {
32+
if buttons.Pins[BUTTON_UP].Get() {
2833
println("Button UP pressed")
2934
}
30-
if pressed&machine.BUTTON_DOWN_MASK > 0 {
35+
if buttons.Pins[BUTTON_DOWN].Get() {
3136
println("Button DOWN pressed")
3237
}
33-
if pressed&machine.BUTTON_RIGHT_MASK > 0 {
38+
if buttons.Pins[BUTTON_RIGHT].Get() {
3439
println("Button RIGHT pressed")
3540
}
36-
if pressed&machine.BUTTON_SELECT_MASK > 0 {
41+
if buttons.Pins[BUTTON_SELECT].Get() {
3742
println("Button SELECT pressed")
3843
}
39-
if pressed&machine.BUTTON_START_MASK > 0 {
44+
if buttons.Pins[BUTTON_START].Get() {
4045
println("Button START pressed")
4146
}
42-
if pressed&machine.BUTTON_A_MASK > 0 {
47+
if buttons.Pins[BUTTON_A].Get() {
4348
println("Button A pressed")
4449
}
45-
if pressed&machine.BUTTON_B_MASK > 0 {
50+
if buttons.Pins[BUTTON_B].Get() {
4651
println("Button B pressed")
4752
}
4853
time.Sleep(100 * time.Millisecond)

shifter/shifter.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ type Device struct {
2525

2626
// ShiftPin is the implementation of the ShiftPin interface.
2727
type ShiftPin struct {
28-
pin machine.Pin
29-
d *Device
28+
pin machine.Pin
29+
d *Device
30+
pressed bool
3031
}
3132

3233
// New returns a new thermistor driver given an ADC pin.
@@ -55,47 +56,52 @@ func (d *Device) GetShiftPin(input int) ShiftPin {
5556
return ShiftPin{pin: machine.Pin(input), d: d}
5657
}
5758

58-
// Read8Input reads the 8 inputs and return an uint8
59+
// Read8Input updates the internal pins' states and returns it as an uint8.
5960
func (d *Device) Read8Input() (uint8, error) {
6061
if d.bits != EIGHT_BITS {
6162
return 0, errors.New("wrong amount of registers")
6263
}
6364
return uint8(d.readInput(EIGHT_BITS)), nil
6465
}
6566

66-
// Read16Input reads the 16 inputs and return an uint16
67+
// Read16Input updates the internal pins' states and returns it as an uint16.
6768
func (d *Device) Read16Input() (uint16, error) {
6869
if d.bits != SIXTEEN_BITS {
6970
return 0, errors.New("wrong amount of registers")
7071
}
7172
return uint16(d.readInput(SIXTEEN_BITS)), nil
7273
}
7374

74-
// Read32Input reads the 32 inputs and return an uint32
75+
// Read32Input updates the internal pins' states and returns it as an uint32.
7576
func (d *Device) Read32Input() (uint32, error) {
7677
if d.bits != THIRTYTWO_BITS {
7778
return 0, errors.New("wrong amount of registers")
7879
}
7980
return d.readInput(THIRTYTWO_BITS), nil
8081
}
8182

82-
// Get the current reading for a specific ShiftPin.
83+
// Get the pin's state for a specific ShiftPin.
84+
// Read{8|16|32}Input should be called before to update the state. Read{8|16|32}Input updates
85+
// all the pins, no need to call it for each pin individually.
8386
func (p ShiftPin) Get() bool {
84-
return (p.d.readInput(p.d.bits) & (1 << int(p.pin))) > 0
87+
return p.pressed
8588
}
8689

8790
// Configure here just for interface compatibility.
8891
func (p ShiftPin) Configure() {
8992
}
9093

91-
// readInput reads howMany bits from the shift register
94+
// readInput reads howMany bits from the shift register and updates the internal pins' states.
9295
func (d *Device) readInput(howMany NumberBit) uint32 {
9396
d.latch.High()
9497
var data uint32
9598
for i := howMany - 1; i >= 0; i-- {
9699
d.clk.Low()
97100
if d.out.Get() {
98101
data |= 1 << i
102+
d.Pins[i].pressed = true
103+
} else {
104+
d.Pins[i].pressed = false
99105
}
100106
d.clk.High()
101107
}

0 commit comments

Comments
 (0)