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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"time"
    10  
    11  	"gobot.io/x/gobot"
    12  	"gobot.io/x/gobot/drivers/i2c"
    13  	"gobot.io/x/gobot/platforms/chip"
    14  )
    15  
    16  func main() {
    17  	board := chip.NewAdaptor()
    18  	luxSensor := i2c.NewTSL2561Driver(board, i2c.WithTSL2561Gain16X)
    19  
    20  	work := func() {
    21  		gobot.Every(1*time.Second, func() {
    22  			broadband, ir, err := luxSensor.GetLuminocity()
    23  
    24  			if err != nil {
    25  				fmt.Println("Err:", err)
    26  			} else {
    27  				light := luxSensor.CalculateLux(broadband, ir)
    28  				fmt.Printf("BB: %v, IR: %v, Lux: %v\n", broadband, ir, light)
    29  			}
    30  		})
    31  	}
    32  
    33  	robot := gobot.NewRobot("tsl2561Bot",
    34  		[]gobot.Connection{board},
    35  		[]gobot.Device{luxSensor},
    36  		work,
    37  	)
    38  
    39  	robot.Start()
    40  }