2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2017-07-13 08:08:46 +08:00
|
|
|
/*
|
|
|
|
How to run
|
|
|
|
Pass serial port to use as the first param:
|
|
|
|
|
|
|
|
go run examples/firmata_led_brightness_with_analog_input.go /dev/ttyACM0
|
|
|
|
*/
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-13 08:08:46 +08:00
|
|
|
"os"
|
2014-07-10 07:51:00 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
2017-04-04 19:38:51 +08:00
|
|
|
"gobot.io/x/gobot/drivers/aio"
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot/drivers/gpio"
|
|
|
|
"gobot.io/x/gobot/platforms/firmata"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-07-13 08:08:46 +08:00
|
|
|
firmataAdaptor := firmata.NewAdaptor(os.Args[1])
|
2017-04-04 19:38:51 +08:00
|
|
|
sensor := aio.NewAnalogSensorDriver(firmataAdaptor, "0")
|
2016-10-03 22:58:43 +08:00
|
|
|
led := gpio.NewLedDriver(firmataAdaptor, "3")
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
work := func() {
|
2017-04-04 19:38:51 +08:00
|
|
|
sensor.On(aio.Data, func(data interface{}) {
|
2014-07-11 08:21:21 +08:00
|
|
|
brightness := uint8(
|
|
|
|
gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255),
|
|
|
|
)
|
2014-06-29 08:18:16 +08:00
|
|
|
fmt.Println("sensor", data)
|
2014-04-26 18:11:51 +08:00
|
|
|
fmt.Println("brightness", brightness)
|
|
|
|
led.Brightness(brightness)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-09 09:36:14 +08:00
|
|
|
robot := gobot.NewRobot("sensorBot",
|
|
|
|
[]gobot.Connection{firmataAdaptor},
|
|
|
|
[]gobot.Device{sensor, led},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|