makeybutton: move pin config where it needs to be

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2022-07-27 21:22:58 +02:00
committed by Daniel Esteban
parent 1f54c7dd23
commit 71c77d4b53
2 changed files with 19 additions and 7 deletions
+2 -2
View File
@@ -15,8 +15,8 @@ var (
func main() { func main() {
led.Configure(machine.PinConfig{Mode: machine.PinOutput}) led.Configure(machine.PinConfig{Mode: machine.PinOutput})
button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
key = makeybutton.NewButton(button) key = makeybutton.NewButton(button)
key.Configure()
for { for {
switch key.Get() { switch key.Get() {
@@ -25,6 +25,6 @@ func main() {
case makeybutton.Released: case makeybutton.Released:
led.Low() led.Low()
} }
time.Sleep(30 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
} }
+17 -5
View File
@@ -6,7 +6,10 @@
// //
package makeybutton package makeybutton
import "machine" import (
"machine"
"time"
)
// ButtonState represents the state of a MakeyButton. // ButtonState represents the state of a MakeyButton.
type ButtonState int type ButtonState int
@@ -36,9 +39,6 @@ type Button struct {
// NewButton creates a new Button. // NewButton creates a new Button.
func NewButton(pin machine.Pin) *Button { func NewButton(pin machine.Pin) *Button {
pin.Configure(machine.PinConfig{Mode: machine.PinInput})
pin.Set(false)
return &Button{ return &Button{
pin: pin, pin: pin,
state: NeverPressed, state: NeverPressed,
@@ -47,6 +47,18 @@ func NewButton(pin machine.Pin) *Button {
} }
} }
// Configure configures the Makey Button pin to have the correct settings to detect touches.
func (b *Button) Configure() error {
// Note that we have to first turn on the pullup, and then turn off the pullup,
// in order for the pin to be properly floating.
b.pin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
time.Sleep(10 * time.Millisecond)
b.pin.Configure(machine.PinConfig{Mode: machine.PinInput})
b.pin.Set(false)
return nil
}
// Get returns a ButtonEvent based on the most recent state of the button, // Get returns a ButtonEvent based on the most recent state of the button,
// and if it has changed by being pressed or released. // and if it has changed by being pressed or released.
func (b *Button) Get() ButtonEvent { func (b *Button) Get() ButtonEvent {
@@ -61,7 +73,7 @@ func (b *Button) Get() ButtonEvent {
b.readings.Put(pressed) b.readings.Put(pressed)
switch { switch {
case pressed && avg > -1 * bufferSize - 2: case pressed && avg > -1*bufferSize+2:
if b.state == Press { if b.state == Press {
return NotChanged return NotChanged
} }