diff --git a/examples/firmata_curie_imu_shock_detect.go b/examples/firmata_curie_imu_shock_detect.go new file mode 100644 index 00000000..686b629e --- /dev/null +++ b/examples/firmata_curie_imu_shock_detect.go @@ -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() +} diff --git a/platforms/intel-iot/curie/imu_driver.go b/platforms/intel-iot/curie/imu_driver.go index e8415577..d8db8443 100644 --- a/platforms/intel-iot/curie/imu_driver.go +++ b/platforms/intel-iot/curie/imu_driver.go @@ -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 +}