39 lines
848 B
Go
39 lines
848 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hybridgroup/gobot"
|
|
"github.com/hybridgroup/gobot/platforms/firmata"
|
|
"github.com/hybridgroup/gobot/platforms/gpio"
|
|
)
|
|
|
|
func main() {
|
|
gbot := gobot.NewGobot()
|
|
|
|
firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0")
|
|
sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "sensor", "0")
|
|
led := gpio.NewLedDriver(firmataAdaptor, "led", "3")
|
|
|
|
work := func() {
|
|
gobot.On(sensor.Event("data"), func(data interface{}) {
|
|
brightness := uint8(
|
|
gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255),
|
|
)
|
|
fmt.Println("sensor", data)
|
|
fmt.Println("brightness", brightness)
|
|
led.Brightness(brightness)
|
|
})
|
|
}
|
|
|
|
robot := gobot.NewRobot("sensorBot",
|
|
[]gobot.Connection{firmataAdaptor},
|
|
[]gobot.Device{sensor, led},
|
|
work,
|
|
)
|
|
|
|
gbot.AddRobot(robot)
|
|
|
|
gbot.Start()
|
|
}
|