Add analog sensor data event
This commit is contained in:
parent
634756bbb0
commit
34ccc13ecb
|
@ -5,7 +5,6 @@ import (
|
||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
"github.com/hybridgroup/gobot/platforms/firmata"
|
"github.com/hybridgroup/gobot/platforms/firmata"
|
||||||
"github.com/hybridgroup/gobot/platforms/gpio"
|
"github.com/hybridgroup/gobot/platforms/gpio"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -15,10 +14,9 @@ func main() {
|
||||||
led := gpio.NewLedDriver(firmataAdaptor, "led", "3")
|
led := gpio.NewLedDriver(firmataAdaptor, "led", "3")
|
||||||
|
|
||||||
work := func() {
|
work := func() {
|
||||||
gobot.Every(100*time.Millisecond, func() {
|
gobot.On(sensor.Events["data"], func(data interface{}) {
|
||||||
val := sensor.Read()
|
brightness := uint8(gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255))
|
||||||
brightness := uint8(gobot.ToScale(gobot.FromScale(float64(val), 0, 1024), 0, 255))
|
fmt.Println("sensor", data)
|
||||||
fmt.Println("sensor", val)
|
|
||||||
fmt.Println("brightness", brightness)
|
fmt.Println("brightness", brightness)
|
||||||
led.Brightness(brightness)
|
led.Brightness(brightness)
|
||||||
})
|
})
|
||||||
|
|
|
@ -11,8 +11,11 @@ type AnalogSensorDriver struct {
|
||||||
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
|
func NewAnalogSensorDriver(a AnalogReader, name string, pin string) *AnalogSensorDriver {
|
||||||
d := &AnalogSensorDriver{
|
d := &AnalogSensorDriver{
|
||||||
Driver: gobot.Driver{
|
Driver: gobot.Driver{
|
||||||
Name: name,
|
Name: name,
|
||||||
Pin: pin,
|
Pin: pin,
|
||||||
|
Events: map[string]*gobot.Event{
|
||||||
|
"data": gobot.NewEvent(),
|
||||||
|
},
|
||||||
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
Commands: make(map[string]func(map[string]interface{}) interface{}),
|
||||||
Adaptor: a.(gobot.AdaptorInterface),
|
Adaptor: a.(gobot.AdaptorInterface),
|
||||||
},
|
},
|
||||||
|
@ -27,9 +30,19 @@ func (a *AnalogSensorDriver) adaptor() AnalogReader {
|
||||||
return a.Driver.Adaptor.(AnalogReader)
|
return a.Driver.Adaptor.(AnalogReader)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AnalogSensorDriver) Start() bool { return true }
|
func (a *AnalogSensorDriver) Start() bool {
|
||||||
func (a *AnalogSensorDriver) Init() bool { return true }
|
value := 0
|
||||||
func (a *AnalogSensorDriver) Halt() bool { return true }
|
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 }
|
||||||
|
|
||||||
func (a *AnalogSensorDriver) Read() int {
|
func (a *AnalogSensorDriver) Read() int {
|
||||||
return a.adaptor().AnalogRead(a.Pin)
|
return a.adaptor().AnalogRead(a.Pin)
|
||||||
|
|
Loading…
Reference in New Issue