1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-11 19:29:20 +08:00

curie: step count implemented

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-06-14 12:18:30 +02:00
parent 78993b4454
commit b18d4c5506
2 changed files with 65 additions and 1 deletions

View File

@ -0,0 +1,41 @@
// +build example
//
// Do not build by default.
package main
import (
"log"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/platforms/firmata"
"gobot.io/x/gobot/platforms/intel-iot/curie"
)
func main() {
firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0")
led := gpio.NewLedDriver(firmataAdaptor, "13")
imu := curie.NewIMUDriver(firmataAdaptor)
work := func() {
imu.On("Steps", func(data interface{}) {
log.Println("Steps", data)
})
gobot.Every(1*time.Second, func() {
led.Toggle()
})
imu.EnableStepCounter(true)
}
robot := gobot.NewRobot("blinkmBot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{imu, led},
work,
)
robot.Start()
}

View File

@ -75,6 +75,10 @@ func (imu *IMUDriver) Start() (err error) {
val, _ := parseShockData(data)
imu.Publish("Shock", val)
case CURIE_IMU_STEP_COUNTER:
val, _ := parseStepData(data)
imu.Publish("Steps", val)
}
}
})
@ -113,7 +117,7 @@ func (imu *IMUDriver) ReadTemperature() error {
return imu.connection.WriteSysex([]byte{CURIE_IMU, CURIE_IMU_READ_TEMP})
}
// EnableShockDetection turns on the Curie's built-in shock detection.
// EnableShockDetection turns on/off the Curie's built-in shock detection.
// The result will be returned by the Sysex response message
func (imu *IMUDriver) EnableShockDetection(detect bool) error {
var d byte
@ -123,6 +127,16 @@ func (imu *IMUDriver) EnableShockDetection(detect bool) error {
return imu.connection.WriteSysex([]byte{CURIE_IMU, CURIE_IMU_SHOCK_DETECT, d})
}
// EnableStepCounter turns on/off the Curie's built-in step counter.
// The result will be returned by the Sysex response message
func (imu *IMUDriver) EnableStepCounter(count bool) error {
var c byte
if count {
c = 1
}
return imu.connection.WriteSysex([]byte{CURIE_IMU, CURIE_IMU_STEP_COUNTER, c})
}
func parseAccelerometerData(data []byte) (*AccelerometerData, error) {
if len(data) < 9 {
return nil, errors.New("Invalid data")
@ -166,3 +180,12 @@ func parseShockData(data []byte) (*ShockData, error) {
res := &ShockData{Axis: data[3], Direction: data[4]}
return res, nil
}
func parseStepData(data []byte) (int16, error) {
if len(data) < 6 {
return 0, errors.New("Invalid data")
}
res := int16(uint16(data[3]) | uint16(data[4])<<7)
return res, nil
}