1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/gpio/makey_button_driver.go

56 lines
1.0 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 NewMakeyButton(a DigitalReader) *MakeyButtonDriver {
return &MakeyButtonDriver{
Driver: gobot.Driver{
Events: map[string]chan interface{}{
"push": make(chan interface{}),
"release": make(chan interface{}),
},
},
Active: false,
Adaptor: a,
}
}
func (m *MakeyButtonDriver) Start() bool {
state := 0
go func() {
for {
new_value := m.readState()
if new_value != state && new_value != -1 {
state = new_value
m.update(new_value)
}
}
}()
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)
}
func (m *MakeyButtonDriver) update(new_val int) {
if new_val == 0 {
m.Active = true
gobot.Publish(m.Events["push"], new_val)
} else {
m.Active = false
gobot.Publish(m.Events["release"], new_val)
}
}