Skip to content

Commit

Permalink
curie: tap detect implemented
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jun 15, 2017
1 parent b18d4c5 commit b91b680
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/firmata_curie_imu_tap_detect.go
Original file line number Diff line number Diff line change
@@ -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("Tap", func(data interface{}) {
log.Println("Tap", data)
})

gobot.Every(1*time.Second, func() {
led.Toggle()
})

imu.EnableTapDetection(true)
}

robot := gobot.NewRobot("blinkmBot",
[]gobot.Connection{firmataAdaptor},
[]gobot.Device{imu, led},
work,
)

robot.Start()
}
28 changes: 28 additions & 0 deletions platforms/intel-iot/curie/imu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type ShockData struct {
Direction byte
}

type TapData struct {
Axis byte
Direction byte
}

// IMUDriver represents the IMU that is built-in to the Curie
type IMUDriver struct {
name string
Expand Down Expand Up @@ -79,6 +84,10 @@ func (imu *IMUDriver) Start() (err error) {
val, _ := parseStepData(data)
imu.Publish("Steps", val)

case CURIE_IMU_TAP_DETECT:
val, _ := parseTapData(data)
imu.Publish("Tap", val)

}
}
})
Expand Down Expand Up @@ -137,6 +146,16 @@ func (imu *IMUDriver) EnableStepCounter(count bool) error {
return imu.connection.WriteSysex([]byte{CURIE_IMU, CURIE_IMU_STEP_COUNTER, c})
}

// EnableTapDetection turns on/off the Curie's built-in tap detection.
// The result will be returned by the Sysex response message
func (imu *IMUDriver) EnableTapDetection(detect bool) error {
var d byte
if detect {
d = 1
}
return imu.connection.WriteSysex([]byte{CURIE_IMU, CURIE_IMU_TAP_DETECT, d})
}

func parseAccelerometerData(data []byte) (*AccelerometerData, error) {
if len(data) < 9 {
return nil, errors.New("Invalid data")
Expand Down Expand Up @@ -189,3 +208,12 @@ func parseStepData(data []byte) (int16, error) {
res := int16(uint16(data[3]) | uint16(data[4])<<7)
return res, nil
}

func parseTapData(data []byte) (*TapData, error) {
if len(data) < 6 {
return nil, errors.New("Invalid data")
}

res := &TapData{Axis: data[3], Direction: data[4]}
return res, nil
}

0 comments on commit b91b680

Please sign in to comment.