1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-01 13:48:57 +08:00
hybridgroup.gobot/platforms/gpio/makey_button_driver.go

56 lines
1.1 KiB
Go
Raw Normal View History

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
}
func NewMakeyButtonDriver(a DigitalReader, name string, pin string) *MakeyButtonDriver {
2014-04-27 19:34:16 -07:00
return &MakeyButtonDriver{
Driver: gobot.Driver{
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
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-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
}
}