curie: shock detect implemented

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-06-14 11:41:22 +02:00
parent 30345764c7
commit 78993b4454
2 changed files with 69 additions and 0 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("Shock", func(data interface{}) {
log.Println("Shock", data)
})
gobot.Every(1*time.Second, func() {
led.Toggle()
})
imu.EnableShockDetection(true)
}
robot := gobot.NewRobot("blinkmBot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{imu, led},
work,
)
robot.Start()
}

View File

@ -30,6 +30,11 @@ type GyroscopeData struct {
Z int16
}
type ShockData struct {
Axis byte
Direction byte
}
// IMUDriver represents the IMU that is built-in to the Curie
type IMUDriver struct {
name string
@ -66,6 +71,10 @@ func (imu *IMUDriver) Start() (err error) {
val, _ := parseTemperatureData(data)
imu.Publish("Temperature", val)
case CURIE_IMU_SHOCK_DETECT:
val, _ := parseShockData(data)
imu.Publish("Shock", val)
}
}
})
@ -104,6 +113,16 @@ 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.
// The result will be returned by the Sysex response message
func (imu *IMUDriver) EnableShockDetection(detect bool) error {
var d byte
if detect {
d = 1
}
return imu.connection.WriteSysex([]byte{CURIE_IMU, CURIE_IMU_SHOCK_DETECT, d})
}
func parseAccelerometerData(data []byte) (*AccelerometerData, error) {
if len(data) < 9 {
return nil, errors.New("Invalid data")
@ -138,3 +157,12 @@ func parseTemperatureData(data []byte) (float32, error) {
res := (float32(t1+(t2*8)) / 512.0) + 23.0
return res, nil
}
func parseShockData(data []byte) (*ShockData, error) {
if len(data) < 6 {
return nil, errors.New("Invalid data")
}
res := &ShockData{Axis: data[3], Direction: data[4]}
return res, nil
}