2014-04-27 19:34:16 -07:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MakeyButtonDriver struct {
|
|
|
|
gobot.Driver
|
|
|
|
Adaptor DigitalReader
|
|
|
|
Active bool
|
|
|
|
data []int
|
|
|
|
}
|
|
|
|
|
2014-06-06 14:44:51 -07:00
|
|
|
func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButtonDriver {
|
2014-04-27 19:34:16 -07:00
|
|
|
return &MakeyButtonDriver{
|
|
|
|
Driver: gobot.Driver{
|
2014-06-06 14:44:51 -07:00
|
|
|
Name: name,
|
|
|
|
Pin: pin,
|
2014-06-11 11:37:20 -07:00
|
|
|
Events: map[string]*gobot.Event{
|
|
|
|
"push": gobot.NewEvent(),
|
|
|
|
"release": gobot.NewEvent(),
|
2014-04-27 19:34:16 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Active: false,
|
|
|
|
Adaptor: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MakeyButtonDriver) Start() bool {
|
|
|
|
state := 0
|
2014-06-06 14:44:51 -07:00
|
|
|
gobot.Every(m.Interval, func() {
|
2014-06-10 15:16:11 -07:00
|
|
|
newValue := m.readState()
|
|
|
|
if newValue != state && newValue != -1 {
|
|
|
|
state = newValue
|
|
|
|
m.update(newValue)
|
2014-04-27 19:34:16 -07:00
|
|
|
}
|
2014-06-06 14:44:51 -07:00
|
|
|
})
|
2014-04-27 19:34:16 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
func (m *MakeyButtonDriver) Halt() bool { return true }
|
|
|
|
func (m *MakeyButtonDriver) Init() bool { return true }
|
|
|
|
|
|
|
|
func (m *MakeyButtonDriver) readState() int {
|
|
|
|
return m.Adaptor.DigitalRead(m.Pin)
|
|
|
|
}
|
|
|
|
|
2014-06-10 15:16:11 -07:00
|
|
|
func (m *MakeyButtonDriver) update(newVal int) {
|
|
|
|
if newVal == 0 {
|
2014-04-27 19:34:16 -07:00
|
|
|
m.Active = true
|
2014-06-10 15:16:11 -07:00
|
|
|
gobot.Publish(m.Events["push"], newVal)
|
2014-04-27 19:34:16 -07:00
|
|
|
} else {
|
|
|
|
m.Active = false
|
2014-06-10 15:16:11 -07:00
|
|
|
gobot.Publish(m.Events["release"], newVal)
|
2014-04-27 19:34:16 -07:00
|
|
|
}
|
|
|
|
}
|