curie: tap detect implemented
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
b18d4c5506
commit
b91b680673
|
@ -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()
|
||||
}
|
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue