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