forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grove_temperature_sensor_driver.go
107 lines (89 loc) · 2.88 KB
/
grove_temperature_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
100
101
102
103
104
105
106
107
package gpio
import (
"math"
"time"
"github.com/hybridgroup/gobot"
)
var _ gobot.Driver = (*GroveTemperatureSensorDriver)(nil)
// GroveTemperatureSensorDriver represents a Temperature Sensor
type GroveTemperatureSensorDriver struct {
name string
pin string
halt chan bool
temperature float64
interval time.Duration
connection AnalogReader
gobot.Eventer
}
// NewGroveTemperatureSensorDriver returns a new GroveTemperatureSensorDriver with a polling interval of
// 10 Milliseconds given an AnalogReader, name and pin.
//
// Optinally accepts:
// time.Duration: Interval at which the TemperatureSensor is polled for new information
//
// Adds the following API Commands:
// "Read" - See AnalogSensor.Read
func NewGroveTemperatureSensorDriver(a AnalogReader, name string, pin string, v ...time.Duration) *GroveTemperatureSensorDriver {
d := &GroveTemperatureSensorDriver{
name: name,
connection: a,
pin: pin,
Eventer: gobot.NewEventer(),
interval: 10 * time.Millisecond,
halt: make(chan bool),
}
if len(v) > 0 {
d.interval = v[0]
}
d.AddEvent(Data)
d.AddEvent(Error)
return d
}
// Start starts the GroveTemperatureSensorDriver and reads the Sensor at the given interval.
// Emits the Events:
// Data int - Event is emitted on change and represents the current temperature in celsius from the sensor.
// Error error - Event is emitted on error reading from the sensor.
func (a *GroveTemperatureSensorDriver) Start() (errs []error) {
thermistor := 3975.0
a.temperature = 0
go func() {
for {
rawValue, err := a.Read()
resistance := float64(1023.0-rawValue) * 10000 / float64(rawValue)
newValue := 1/(math.Log(resistance/10000.0)/thermistor+1/298.15) - 273.15
if err != nil {
gobot.Publish(a.Event(Error), err)
} else if newValue != a.temperature && newValue != -1 {
a.temperature = newValue
gobot.Publish(a.Event(Data), a.temperature)
}
select {
case <-time.After(a.interval):
case <-a.halt:
return
}
}
}()
return
}
// Halt stops polling the analog sensor for new information
func (a *GroveTemperatureSensorDriver) Halt() (errs []error) {
a.halt <- true
return
}
// Name returns the GroveTemperatureSensorDrivers name
func (a *GroveTemperatureSensorDriver) Name() string { return a.name }
// Pin returns the GroveTemperatureSensorDrivers pin
func (a *GroveTemperatureSensorDriver) Pin() string { return a.pin }
// Connection returns the GroveTemperatureSensorDrivers Connection
func (a *GroveTemperatureSensorDriver) Connection() gobot.Connection {
return a.connection.(gobot.Connection)
}
// Read returns the current Temperature from the Sensor
func (a *GroveTemperatureSensorDriver) Temperature() (val float64) {
return a.temperature
}
// Read returns the raw reading from the Sensor
func (a *GroveTemperatureSensorDriver) Read() (val int, err error) {
return a.connection.AnalogRead(a.Pin())
}