diff --git a/examples/makeybutton/main.go b/examples/makeybutton/main.go index 32e11bb..53e3434 100644 --- a/examples/makeybutton/main.go +++ b/examples/makeybutton/main.go @@ -15,8 +15,8 @@ var ( func main() { led.Configure(machine.PinConfig{Mode: machine.PinOutput}) - button.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) key = makeybutton.NewButton(button) + key.Configure() for { switch key.Get() { @@ -25,6 +25,6 @@ func main() { case makeybutton.Released: led.Low() } - time.Sleep(30 * time.Millisecond) + time.Sleep(100 * time.Millisecond) } } diff --git a/makeybutton/button.go b/makeybutton/button.go index 5cd1e96..74023df 100644 --- a/makeybutton/button.go +++ b/makeybutton/button.go @@ -6,7 +6,10 @@ // package makeybutton -import "machine" +import ( + "machine" + "time" +) // ButtonState represents the state of a MakeyButton. type ButtonState int @@ -36,9 +39,6 @@ type Button struct { // NewButton creates a new Button. func NewButton(pin machine.Pin) *Button { - pin.Configure(machine.PinConfig{Mode: machine.PinInput}) - pin.Set(false) - return &Button{ pin: pin, 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, // and if it has changed by being pressed or released. func (b *Button) Get() ButtonEvent { @@ -61,7 +73,7 @@ func (b *Button) Get() ButtonEvent { b.readings.Put(pressed) switch { - case pressed && avg > -1 * bufferSize - 2: + case pressed && avg > -1*bufferSize+2: if b.state == Press { return NotChanged }