forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analog_sensor_driver.go
99 lines (84 loc) · 2.53 KB
/
analog_sensor_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gpio
import (
"time"
"github.com/hybridgroup/gobot"
)
var _ gobot.Driver = (*AnalogSensorDriver)(nil)
// AnalogSensorDriver represents an Analog Sensor
type AnalogSensorDriver struct {
name string
pin string
halt chan bool
interval time.Duration
connection AnalogReader
gobot.Eventer
gobot.Commander
}
// NewAnalogSensorDriver returns a new AnalogSensorDriver with a polling interval of
// 10 Milliseconds given an AnalogReader, name and pin.
//
// Optinally accepts:
// time.Duration: Interval at which the AnalogSensor is polled for new information
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
func NewAnalogSensorDriver(a AnalogReader, name string, pin string, v ...time.Duration) *AnalogSensorDriver {
d := &AnalogSensorDriver{
name: name,
connection: a,
pin: pin,
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
interval: 10 * time.Millisecond,
halt: make(chan bool),
}
if len(v) > 0 {
d.interval = v[0]
}
d.AddEvent(Data)
d.AddEvent(Error)
d.AddCommand("Read", func(params map[string]interface{}) interface{} {
val, err := d.Read()
return map[string]interface{}{"val": val, "err": err}
})
return d
}
// Start starts the AnalogSensorDriver and reads the Analog Sensor at the given interval.
// Emits the Events:
// Data int - Event is emitted on change and represents the current reading from the sensor.
// Error error - Event is emitted on error reading from the sensor.
func (a *AnalogSensorDriver) Start() (errs []error) {
value := 0
go func() {
for {
newValue, err := a.Read()
if err != nil {
gobot.Publish(a.Event(Error), err)
} else if newValue != value && newValue != -1 {
value = newValue
gobot.Publish(a.Event(Data), value)
}
select {
case <-time.After(a.interval):
case <-a.halt:
return
}
}
}()
return
}
// Halt stops polling the analog sensor for new information
func (a *AnalogSensorDriver) Halt() (errs []error) {
a.halt <- true
return
}
// Name returns the AnalogSensorDrivers name
func (a *AnalogSensorDriver) Name() string { return a.name }
// Pin returns the AnalogSensorDrivers pin
func (a *AnalogSensorDriver) Pin() string { return a.pin }
// Connection returns the AnalogSensorDrivers Connection
func (a *AnalogSensorDriver) Connection() gobot.Connection { return a.connection.(gobot.Connection) }
// Read returns the current reading from the Analog Sensor
func (a *AnalogSensorDriver) Read() (val int, err error) {
return a.connection.AnalogRead(a.Pin())
}