forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makey_button_driver.go
69 lines (59 loc) · 1.49 KB
/
makey_button_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gpio
import (
"github.com/hybridgroup/gobot"
)
// Represents a Makey Button
type MakeyButtonDriver struct {
gobot.Driver
Active bool
data []int
}
// NewMakeyButtonDriver returns a new MakeyButtonDriver given a DigitalRead, name and pin.
func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButtonDriver {
m := &MakeyButtonDriver{
Driver: *gobot.NewDriver(
name,
"MakeyButtonDriver",
a.(gobot.AdaptorInterface),
pin,
),
Active: false,
}
m.AddEvent("push")
m.AddEvent("release")
return m
}
func (b *MakeyButtonDriver) adaptor() DigitalReader {
return b.Adaptor().(DigitalReader)
}
// Starts the MakeyButtonDriver and reads the state of the button at the given Driver.Interval().
// Returns true on successful start of the driver.
//
// Emits the Events:
// "push" int - On button push
// "release" int - On button release
func (m *MakeyButtonDriver) Start() bool {
state := 0
gobot.Every(m.Interval(), func() {
newValue := m.readState()
if newValue != state && newValue != -1 {
state = newValue
m.update(newValue)
}
})
return true
}
// Halt returns true on a successful halt of the driver
func (m *MakeyButtonDriver) Halt() bool { return true }
func (m *MakeyButtonDriver) readState() int {
return m.adaptor().DigitalRead(m.Pin())
}
func (m *MakeyButtonDriver) update(newVal int) {
if newVal == 0 {
m.Active = true
gobot.Publish(m.Event("push"), newVal)
} else {
m.Active = false
gobot.Publish(m.Event("release"), newVal)
}
}