mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-12 19:03:42 +00:00
makeybutton: revise to better match the algorithm defined by the original
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
+34
-20
@@ -4,6 +4,8 @@
|
||||
// Inspired by the amazing MakeyMakey
|
||||
// https://makeymakey.com/
|
||||
//
|
||||
// This code is a reinterpretation of
|
||||
// https://github.com/sparkfun/MaKeyMaKey/blob/master/firmware/Arduino/makey_makey/makey_makey.ino
|
||||
package makeybutton
|
||||
|
||||
import (
|
||||
@@ -11,6 +13,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
pressThreshold int = 1
|
||||
releaseThreshold int = 0
|
||||
)
|
||||
|
||||
// ButtonState represents the state of a MakeyButton.
|
||||
type ButtonState int
|
||||
|
||||
@@ -33,6 +40,7 @@ const (
|
||||
type Button struct {
|
||||
pin machine.Pin
|
||||
state ButtonState
|
||||
pressed bool
|
||||
readings *Buffer
|
||||
HighMeansPressed bool
|
||||
}
|
||||
@@ -49,7 +57,7 @@ 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,
|
||||
// Note that on AVR 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)
|
||||
@@ -62,30 +70,36 @@ func (b *Button) Configure() error {
|
||||
// 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 {
|
||||
// if pin is pulled up, a low value means the key is pressed
|
||||
pressed := !b.pin.Get()
|
||||
if b.HighMeansPressed {
|
||||
// otherwise, a high value means the key is pressed
|
||||
pressed = !pressed
|
||||
}
|
||||
b.update()
|
||||
|
||||
avg := b.readings.Avg()
|
||||
b.readings.Put(pressed)
|
||||
|
||||
switch {
|
||||
case pressed && avg > -1*bufferSize+2:
|
||||
if b.state == Press {
|
||||
return NotChanged
|
||||
}
|
||||
|
||||
b.state = Press
|
||||
return Pressed
|
||||
case !pressed:
|
||||
if b.state == Press {
|
||||
if b.pressed {
|
||||
// the button had previously been pressed,
|
||||
// but now appears to have been released.
|
||||
if b.readings.Sum() <= releaseThreshold {
|
||||
b.pressed = false
|
||||
b.state = Release
|
||||
return Released
|
||||
}
|
||||
} else {
|
||||
// the button had previously not been pressed,
|
||||
// but now appears to have been pressed.
|
||||
if b.readings.Sum() >= pressThreshold {
|
||||
b.pressed = true
|
||||
b.state = Press
|
||||
return Pressed
|
||||
}
|
||||
}
|
||||
|
||||
return NotChanged
|
||||
}
|
||||
|
||||
func (b *Button) update() {
|
||||
// if pin is pulled up, a low value means the key is pressed
|
||||
press := !b.pin.Get()
|
||||
if b.HighMeansPressed {
|
||||
// otherwise, a high value means the key is pressed
|
||||
press = !press
|
||||
}
|
||||
|
||||
b.readings.Put(press)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user