2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2018-01-29 20:56:25 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2018-01-29 20:56:25 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
/*
|
|
|
|
How to run
|
|
|
|
Pass serial port to use as the first param:
|
|
|
|
|
|
|
|
go run examples/firmata_adxl345.go /dev/ttyACM0
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/firmata"
|
2018-01-29 20:56:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
firmataAdaptor := firmata.NewAdaptor(os.Args[1])
|
|
|
|
adxl345 := i2c.NewADXL345Driver(firmataAdaptor)
|
|
|
|
|
|
|
|
work := func() {
|
|
|
|
gobot.Every(100*time.Millisecond, func() {
|
|
|
|
x, y, z, _ := adxl345.XYZ()
|
|
|
|
|
|
|
|
fmt.Printf("x: %.7f | y: %.7f | z: %.7f \n", x, y, z)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("adxl345Bot",
|
|
|
|
[]gobot.Connection{firmataAdaptor},
|
|
|
|
[]gobot.Device{adxl345},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2024-02-11 22:34:50 +08:00
|
|
|
if err := robot.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-29 20:56:25 +08:00
|
|
|
}
|