curie: tap detect implemented

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-06-14 12:23:08 +02:00
parent b18d4c5506
commit b91b680673
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("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()
}

View File

@ -35,6 +35,11 @@ type ShockData struct {
Direction byte Direction byte
} }
type TapData struct {
Axis byte
Direction byte
}
// IMUDriver represents the IMU that is built-in to the Curie // IMUDriver represents the IMU that is built-in to the Curie
type IMUDriver struct { type IMUDriver struct {
name string name string
@ -79,6 +84,10 @@ func (imu *IMUDriver) Start() (err error) {
val, _ := parseStepData(data) val, _ := parseStepData(data)
imu.Publish("Steps", val) imu.Publish("Steps", val)
case CURIE_IMU_TAP_DETECT:
val, _ := parseTapData(data)
imu.Publish("Tap", val)
} }
} }
}) })
@ -137,6 +146,16 @@ func (imu *IMUDriver) EnableStepCounter(count bool) error {
return imu.connection.WriteSysex([]byte{CURIE_IMU, CURIE_IMU_STEP_COUNTER, c}) 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) { func parseAccelerometerData(data []byte) (*AccelerometerData, error) {
if len(data) < 9 { if len(data) < 9 {
return nil, errors.New("Invalid data") return nil, errors.New("Invalid data")
@ -189,3 +208,12 @@ func parseStepData(data []byte) (int16, error) {
res := int16(uint16(data[3]) | uint16(data[4])<<7) res := int16(uint16(data[3]) | uint16(data[4])<<7)
return res, nil 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
}