2016-12-20 20:25:22 +08:00
|
|
|
package aio
|
2015-06-30 09:00:05 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2015-06-30 09:00:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*GroveTemperatureSensorDriver)(nil)
|
|
|
|
|
|
|
|
// GroveTemperatureSensorDriver represents a Temperature Sensor
|
2016-10-27 19:21:50 +08:00
|
|
|
// The temperature is reported in degree Celsius
|
2015-06-30 09:00:05 +08:00
|
|
|
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
|
2016-09-25 19:36:18 +08:00
|
|
|
// 10 Milliseconds given an AnalogReader and pin.
|
2015-06-30 09:00:05 +08:00
|
|
|
//
|
2016-07-14 00:44:47 +08:00
|
|
|
// Optionally accepts:
|
2015-06-30 09:00:05 +08:00
|
|
|
// time.Duration: Interval at which the TemperatureSensor is polled for new information
|
|
|
|
//
|
|
|
|
// Adds the following API Commands:
|
|
|
|
// "Read" - See AnalogSensor.Read
|
2016-09-25 19:36:18 +08:00
|
|
|
func NewGroveTemperatureSensorDriver(a AnalogReader, pin string, v ...time.Duration) *GroveTemperatureSensorDriver {
|
2015-06-30 09:00:05 +08:00
|
|
|
d := &GroveTemperatureSensorDriver{
|
2017-02-02 22:32:37 +08:00
|
|
|
name: gobot.DefaultName("GroveTemperatureSensor"),
|
2015-06-30 09:00:05 +08:00
|
|
|
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.
|
2016-11-07 21:55:21 +08:00
|
|
|
func (a *GroveTemperatureSensorDriver) Start() (err error) {
|
2015-06-30 09:00:05 +08:00
|
|
|
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 {
|
2016-09-01 18:17:43 +08:00
|
|
|
a.Publish(Error, err)
|
2015-06-30 09:00:05 +08:00
|
|
|
} else if newValue != a.temperature && newValue != -1 {
|
|
|
|
a.temperature = newValue
|
2016-09-01 18:17:43 +08:00
|
|
|
a.Publish(Data, a.temperature)
|
2015-06-30 09:00:05 +08:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(a.interval):
|
|
|
|
case <-a.halt:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Halt stops polling the analog sensor for new information
|
2016-11-07 21:55:21 +08:00
|
|
|
func (a *GroveTemperatureSensorDriver) Halt() (err error) {
|
2015-06-30 09:00:05 +08:00
|
|
|
a.halt <- true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the GroveTemperatureSensorDrivers name
|
|
|
|
func (a *GroveTemperatureSensorDriver) Name() string { return a.name }
|
|
|
|
|
2016-09-25 19:36:18 +08:00
|
|
|
// SetName sets the GroveTemperatureSensorDrivers name
|
|
|
|
func (a *GroveTemperatureSensorDriver) SetName(n string) { a.name = n }
|
|
|
|
|
2015-06-30 09:00:05 +08:00
|
|
|
// 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())
|
|
|
|
}
|