2014-04-28 10:34:16 +08:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
2016-08-30 19:27:50 +08:00
|
|
|
"time"
|
2016-09-25 19:36:18 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2014-04-28 10:34:16 +08:00
|
|
|
)
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// ButtonDriver Represents a digital Button
|
2014-04-28 10:34:16 +08:00
|
|
|
type ButtonDriver struct {
|
2014-11-23 04:21:28 +08:00
|
|
|
Active bool
|
|
|
|
pin string
|
|
|
|
name string
|
2015-01-03 02:42:53 +08:00
|
|
|
halt chan bool
|
2014-11-29 10:44:52 +08:00
|
|
|
interval time.Duration
|
2014-11-30 03:02:10 +08:00
|
|
|
connection DigitalReader
|
2014-11-23 04:21:28 +08:00
|
|
|
gobot.Eventer
|
2014-04-28 10:34:16 +08:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// NewButtonDriver returns a new ButtonDriver with a polling interval of
|
2016-09-25 19:36:18 +08:00
|
|
|
// 10 Milliseconds given a DigitalReader and pin.
|
2015-01-03 02:42:53 +08:00
|
|
|
//
|
2016-07-14 00:44:47 +08:00
|
|
|
// Optionally accepts:
|
2015-01-03 02:42:53 +08:00
|
|
|
// time.Duration: Interval at which the ButtonDriver is polled for new information
|
2016-09-25 19:36:18 +08:00
|
|
|
func NewButtonDriver(a DigitalReader, pin string, v ...time.Duration) *ButtonDriver {
|
2014-07-04 10:14:04 +08:00
|
|
|
b := &ButtonDriver{
|
2017-02-02 22:37:09 +08:00
|
|
|
name: gobot.DefaultName("Button"),
|
2014-11-30 03:02:10 +08:00
|
|
|
connection: a,
|
2014-11-23 04:21:28 +08:00
|
|
|
pin: pin,
|
|
|
|
Active: false,
|
|
|
|
Eventer: gobot.NewEventer(),
|
2014-11-29 10:44:52 +08:00
|
|
|
interval: 10 * time.Millisecond,
|
2015-01-03 02:42:53 +08:00
|
|
|
halt: make(chan bool),
|
2014-11-29 10:44:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(v) > 0 {
|
|
|
|
b.interval = v[0]
|
2014-04-28 10:34:16 +08:00
|
|
|
}
|
2014-07-04 10:14:04 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
b.AddEvent(ButtonPush)
|
|
|
|
b.AddEvent(ButtonRelease)
|
2014-11-30 03:02:10 +08:00
|
|
|
b.AddEvent(Error)
|
2014-07-04 10:14:04 +08:00
|
|
|
|
|
|
|
return b
|
2014-04-28 10:34:16 +08:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Start starts the ButtonDriver and polls the state of the button at the given interval.
|
2014-09-28 02:34:13 +08:00
|
|
|
//
|
|
|
|
// Emits the Events:
|
2015-01-03 02:42:53 +08:00
|
|
|
// Push int - On button push
|
|
|
|
// Release int - On button release
|
|
|
|
// Error error - On button error
|
2016-11-07 21:55:21 +08:00
|
|
|
func (b *ButtonDriver) Start() (err error) {
|
2014-04-28 10:34:16 +08:00
|
|
|
state := 0
|
2014-11-17 05:45:18 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2014-11-30 03:02:10 +08:00
|
|
|
newValue, err := b.connection.DigitalRead(b.Pin())
|
2014-11-17 05:45:18 +08:00
|
|
|
if err != nil {
|
2016-09-01 18:17:43 +08:00
|
|
|
b.Publish(Error, err)
|
2014-11-17 05:45:18 +08:00
|
|
|
} else if newValue != state && newValue != -1 {
|
|
|
|
state = newValue
|
|
|
|
b.update(newValue)
|
|
|
|
}
|
2015-01-03 02:42:53 +08:00
|
|
|
select {
|
|
|
|
case <-time.After(b.interval):
|
|
|
|
case <-b.halt:
|
|
|
|
return
|
|
|
|
}
|
2014-04-28 10:34:16 +08:00
|
|
|
}
|
2014-11-17 05:45:18 +08:00
|
|
|
}()
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-28 10:34:16 +08:00
|
|
|
}
|
2014-09-28 02:34:13 +08:00
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Halt stops polling the button for new information
|
2016-11-07 21:55:21 +08:00
|
|
|
func (b *ButtonDriver) Halt() (err error) {
|
2015-01-03 02:42:53 +08:00
|
|
|
b.halt <- true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the ButtonDrivers name
|
|
|
|
func (b *ButtonDriver) Name() string { return b.name }
|
|
|
|
|
2016-09-25 19:36:18 +08:00
|
|
|
// SetName sets the ButtonDrivers name
|
|
|
|
func (b *ButtonDriver) SetName(n string) { b.name = n }
|
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Pin returns the ButtonDrivers pin
|
|
|
|
func (b *ButtonDriver) Pin() string { return b.pin }
|
2014-04-28 10:34:16 +08:00
|
|
|
|
2015-01-03 02:42:53 +08:00
|
|
|
// Connection returns the ButtonDrivers Connection
|
2014-11-30 03:02:10 +08:00
|
|
|
func (b *ButtonDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }
|
2014-04-28 10:34:16 +08:00
|
|
|
|
2014-11-17 05:45:18 +08:00
|
|
|
func (b *ButtonDriver) update(newValue int) {
|
|
|
|
if newValue == 1 {
|
2014-04-28 10:34:16 +08:00
|
|
|
b.Active = true
|
2016-09-01 18:17:43 +08:00
|
|
|
b.Publish(ButtonPush, newValue)
|
2014-04-28 10:34:16 +08:00
|
|
|
} else {
|
|
|
|
b.Active = false
|
2016-09-01 18:17:43 +08:00
|
|
|
b.Publish(ButtonRelease, newValue)
|
2014-04-28 10:34:16 +08:00
|
|
|
}
|
|
|
|
}
|