gobot.io/x/gobot@v1.16.0/examples/wifi_firmata_analog_input.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/drivers/aio"
    13  	"gobot.io/x/gobot/platforms/firmata"
    14  )
    15  
    16  func main() {
    17  	firmataAdaptor := firmata.NewTCPAdaptor(os.Args[1])
    18  	sensor := aio.NewAnalogSensorDriver(firmataAdaptor, "A0")
    19  
    20  	work := func() {
    21  		sensor.On(aio.Data, func(data interface{}) {
    22  			brightness := uint8(
    23  				gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255),
    24  			)
    25  			fmt.Println("sensor", data)
    26  			fmt.Println("brightness", brightness)
    27  		})
    28  	}
    29  
    30  	robot := gobot.NewRobot("sensorBot",
    31  		[]gobot.Connection{firmataAdaptor},
    32  		[]gobot.Device{sensor},
    33  		work,
    34  	)
    35  
    36  	robot.Start()
    37  }