gobot.io/x/gobot@v1.16.0/examples/beaglebone_led_brightness_with_analog_input.go (about) 1 // +build example 2 // 3 // Do not build by default. 4 5 package main 6 7 import ( 8 "fmt" 9 10 "gobot.io/x/gobot" 11 "gobot.io/x/gobot/drivers/aio" 12 "gobot.io/x/gobot/drivers/gpio" 13 "gobot.io/x/gobot/platforms/beaglebone" 14 ) 15 16 func main() { 17 beagleboneAdaptor := beaglebone.NewAdaptor() 18 sensor := aio.NewAnalogSensorDriver(beagleboneAdaptor, "P9_33") 19 led := gpio.NewLedDriver(beagleboneAdaptor, "P9_14") 20 21 work := func() { 22 sensor.On(sensor.Event("data"), func(data interface{}) { 23 brightness := uint8( 24 gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255), 25 ) 26 fmt.Println("sensor", data) 27 fmt.Println("brightness", brightness) 28 led.Brightness(brightness) 29 }) 30 } 31 32 robot := gobot.NewRobot("sensorBot", 33 []gobot.Connection{beagleboneAdaptor}, 34 []gobot.Device{sensor, led}, 35 work, 36 ) 37 38 robot.Start() 39 }