gobot.io/x/gobot/v2@v2.1.0/examples/beaglebone_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/beaglebone"
    16  )
    17  
    18  func main() {
    19  	beagleboneAdaptor := beaglebone.NewAdaptor()
    20  	sensor := aio.NewAnalogSensorDriver(beagleboneAdaptor, "P9_33")
    21  	led := gpio.NewLedDriver(beagleboneAdaptor, "P9_14")
    22  
    23  	work := func() {
    24  		sensor.On(sensor.Event("data"), func(data interface{}) {
    25  			brightness := uint8(
    26  				gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 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{beagleboneAdaptor},
    36  		[]gobot.Device{sensor, led},
    37  		work,
    38  	)
    39  
    40  	robot.Start()
    41  }