1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00
hybridgroup.gobot/drivers/gpio/button_driver.go

118 lines
2.5 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import (
"time"
"gobot.io/x/gobot/v2"
2014-04-27 19:34:16 -07:00
)
// ButtonDriver Represents a digital Button
2014-04-27 19:34:16 -07:00
type ButtonDriver struct {
*Driver
gobot.Eventer
pin string
active bool
defaultState int
halt chan bool
interval time.Duration
2014-04-27 19:34:16 -07:00
}
// NewButtonDriver returns a new ButtonDriver with a polling interval of
// 10 Milliseconds given a DigitalReader and pin.
//
2016-07-13 10:44:47 -06:00
// Optionally accepts:
//
// time.Duration: Interval at which the ButtonDriver is polled for new information
func NewButtonDriver(a DigitalReader, pin string, v ...time.Duration) *ButtonDriver {
//nolint:forcetypeassert // no error return value, so there is no better way
d := &ButtonDriver{
Driver: NewDriver(a.(gobot.Connection), "Button"),
Eventer: gobot.NewEventer(),
pin: pin,
active: false,
defaultState: 0,
interval: 10 * time.Millisecond,
halt: make(chan bool),
2014-11-28 18:44:52 -08:00
}
d.afterStart = d.initialize
d.beforeHalt = d.shutdown
2014-11-28 18:44:52 -08:00
if len(v) > 0 {
d.interval = v[0]
2014-04-27 19:34:16 -07:00
}
d.AddEvent(ButtonPush)
d.AddEvent(ButtonRelease)
d.AddEvent(Error)
return d
2014-04-27 19:34:16 -07:00
}
// Pin returns the ButtonDrivers pin
func (d *ButtonDriver) Pin() string { return d.pin }
// Active gets the current state
func (d *ButtonDriver) Active() bool {
// ensure that read and write can not interfere
d.mutex.Lock()
defer d.mutex.Unlock()
return d.active
}
// SetDefaultState for the next start.
func (d *ButtonDriver) SetDefaultState(s int) {
// ensure that read and write can not interfere
d.mutex.Lock()
defer d.mutex.Unlock()
d.defaultState = s
}
// initialize the ButtonDriver and polls the state of the button at the given interval.
2014-09-27 11:34:13 -07:00
//
// Emits the Events:
//
// Push int - On button push
// Release int - On button release
// Error error - On button error
func (d *ButtonDriver) initialize() error {
state := d.defaultState
go func() {
for {
newValue, err := d.connection.(DigitalReader).DigitalRead(d.Pin())
if err != nil {
d.Publish(Error, err)
} else if newValue != state && newValue != -1 {
state = newValue
d.update(newValue)
}
select {
case <-time.After(d.interval):
case <-d.halt:
return
}
2014-04-27 19:34:16 -07:00
}
}()
return nil
2014-04-27 19:34:16 -07:00
}
2014-09-27 11:34:13 -07:00
func (d *ButtonDriver) shutdown() error {
d.halt <- true
return nil
}
func (d *ButtonDriver) update(newValue int) {
// ensure that read and write can not interfere
d.mutex.Lock()
defer d.mutex.Unlock()
if newValue != d.defaultState {
d.active = true
d.Publish(ButtonPush, newValue)
2014-04-27 19:34:16 -07:00
} else {
d.active = false
d.Publish(ButtonRelease, newValue)
2014-04-27 19:34:16 -07:00
}
}