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

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  /*
     6   How to run
     7   Pass serial port to use as the first param:
     8  
     9  	go run examples/firmata_mma7660.go /dev/ttyACM0
    10  */
    11  
    12  package main
    13  
    14  import (
    15  	"fmt"
    16  	"os"
    17  	"time"
    18  
    19  	"gobot.io/x/gobot"
    20  	"gobot.io/x/gobot/drivers/i2c"
    21  	"gobot.io/x/gobot/platforms/firmata"
    22  )
    23  
    24  func main() {
    25  	firmataAdaptor := firmata.NewAdaptor(os.Args[1])
    26  	mma7660 := i2c.NewMMA7660Driver(firmataAdaptor)
    27  
    28  	work := func() {
    29  		gobot.Every(500*time.Millisecond, func() {
    30  			if x, y, z, err := mma7660.XYZ(); err == nil {
    31  				fmt.Println(x, y, z)
    32  				fmt.Println(mma7660.Acceleration(x, y, z))
    33  			} else {
    34  				fmt.Println(err)
    35  			}
    36  		})
    37  	}
    38  
    39  	robot := gobot.NewRobot("mma76602Bot",
    40  		[]gobot.Connection{firmataAdaptor},
    41  		[]gobot.Device{mma7660},
    42  		work,
    43  	)
    44  
    45  	robot.Start()
    46  }