hybridgroup.gobot/platforms/gpio/analog_sensor_driver.go

50 lines
1.1 KiB
Go
Raw Normal View History

2014-04-28 10:34:16 +08:00
package gpio
import (
"github.com/hybridgroup/gobot"
)
type AnalogSensorDriver struct {
gobot.Driver
}
2014-06-07 07:58:58 +08:00
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
2014-06-12 08:41:04 +08:00
d := &AnalogSensorDriver{
2014-04-28 10:34:16 +08:00
Driver: gobot.Driver{
2014-06-29 08:18:16 +08:00
Name: name,
Pin: pin,
Events: map[string]*gobot.Event{
"data": gobot.NewEvent(),
},
2014-06-12 08:41:04 +08:00
Commands: make(map[string]func(map[string]interface{}) interface{}),
Adaptor: a.(gobot.AdaptorInterface),
2014-04-28 10:34:16 +08:00
},
}
2014-06-12 08:41:04 +08:00
d.Driver.AddCommand("Read", func(params map[string]interface{}) interface{} {
return d.Read()
})
return d
2014-04-28 10:34:16 +08:00
}
func (a *AnalogSensorDriver) adaptor() AnalogReader {
return a.Driver.Adaptor.(AnalogReader)
}
2014-06-29 08:18:16 +08:00
func (a *AnalogSensorDriver) Start() bool {
value := 0
gobot.Every(a.Interval, func() {
newValue := a.Read()
if newValue != value && newValue != -1 {
value = newValue
gobot.Publish(a.Events["data"], value)
}
})
return true
}
func (a *AnalogSensorDriver) Init() bool { return true }
func (a *AnalogSensorDriver) Halt() bool { return true }
2014-04-28 10:34:16 +08:00
func (a *AnalogSensorDriver) Read() int {
return a.adaptor().AnalogRead(a.Pin)
2014-04-28 10:34:16 +08:00
}