gobot.io/x/gobot/v2@v2.1.0/examples/wifi_firmata_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  	"os"
    12  
    13  	"gobot.io/x/gobot/v2"
    14  	"gobot.io/x/gobot/v2/drivers/aio"
    15  	"gobot.io/x/gobot/v2/platforms/firmata"
    16  )
    17  
    18  func main() {
    19  	firmataAdaptor := firmata.NewTCPAdaptor(os.Args[1])
    20  	sensor := aio.NewAnalogSensorDriver(firmataAdaptor, "A0")
    21  
    22  	work := func() {
    23  		sensor.On(aio.Data, func(data interface{}) {
    24  			brightness := uint8(
    25  				gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255),
    26  			)
    27  			fmt.Println("sensor", data)
    28  			fmt.Println("brightness", brightness)
    29  		})
    30  	}
    31  
    32  	robot := gobot.NewRobot("sensorBot",
    33  		[]gobot.Connection{firmataAdaptor},
    34  		[]gobot.Device{sensor},
    35  		work,
    36  	)
    37  
    38  	robot.Start()
    39  }