gobot.io/x/gobot/v2@v2.1.0/examples/edison_led_brightness_with_analog_input.go (about)

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