gobot.io/x/gobot/v2@v2.1.0/examples/joule_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/drivers/i2c"
    16  	"gobot.io/x/gobot/v2/platforms/intel-iot/joule"
    17  )
    18  
    19  func main() {
    20  	e := joule.NewAdaptor()
    21  	ads1015 := i2c.NewADS1015Driver(e)
    22  	sensor := aio.NewAnalogSensorDriver(ads1015, "0")
    23  	led := gpio.NewLedDriver(e, "J12_26")
    24  
    25  	work := func() {
    26  		sensor.On(aio.Data, func(data interface{}) {
    27  			brightness := uint8(gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1023), 0, 255))
    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{ads1015, sensor, led},
    37  		work,
    38  	)
    39  
    40  	robot.Start()
    41  }