@@ -25,8 +25,9 @@ type Device struct {
2525
2626// ShiftPin is the implementation of the ShiftPin interface.
2727type 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.
5960func (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.
6768func (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.
7576func (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.
8386func (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.
8891func (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.
9295func (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