gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_mpl115a2.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  	"time"
    12  
    13  	"gobot.io/x/gobot/v2"
    14  	"gobot.io/x/gobot/v2/drivers/i2c"
    15  	"gobot.io/x/gobot/v2/platforms/tinkerboard"
    16  )
    17  
    18  // Wiring
    19  // PWR  Tinkerboard: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND)
    20  // I2C1 Tinkerboard: 3 (SDA-ws), 5 (SCL-gn)
    21  // MPL115A2 plate: VDD (2.375..5.5V), GND, SDL, SDA
    22  func main() {
    23  	board := tinkerboard.NewAdaptor()
    24  	mpl115a2 := i2c.NewMPL115A2Driver(board)
    25  
    26  	work := func() {
    27  		gobot.Every(2*time.Second, func() {
    28  			if press, err := mpl115a2.Pressure(); err != nil {
    29  				fmt.Println(err)
    30  			} else {
    31  				fmt.Println("Pressure [kPa]", press)
    32  			}
    33  
    34  			if temp, err := mpl115a2.Temperature(); err != nil {
    35  				fmt.Println(err)
    36  			} else {
    37  				fmt.Println("Temperature [°C]", temp)
    38  			}
    39  
    40  			fmt.Println("-------------")
    41  		})
    42  	}
    43  
    44  	robot := gobot.NewRobot("mpl115Bot",
    45  		[]gobot.Connection{board},
    46  		[]gobot.Device{mpl115a2},
    47  		work,
    48  	)
    49  
    50  	err := robot.Start()
    51  	if err != nil {
    52  		fmt.Println(err)
    53  	}
    54  }