gobot.io/x/gobot@v1.16.0/examples/edison_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/intel-iot/edison"
    14  )
    15  
    16  func main() {
    17  	e := edison.NewAdaptor()
    18  	sensor := aio.NewAnalogSensorDriver(e, "0")
    19  	led := gpio.NewLedDriver(e, "3")
    20  
    21  	work := func() {
    22  		sensor.On(aio.Data, func(data interface{}) {
    23  			brightness := uint8(
    24  				gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 4096), 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{e},
    34  		[]gobot.Device{sensor, led},
    35  		work,
    36  	)
    37  
    38  	robot.Start()
    39  }