From 043428404dd7b7ead495718485dcaac8faaa4244 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Thu, 3 Sep 2015 21:40:21 -0700 Subject: [PATCH] exp/sensor: enable magnetometer on darwin{arm,arm64} Change-Id: Ib188121501ac96cb8743e57f907b23d0fdfc37a0 Reviewed-on: https://go-review.googlesource.com/14292 Reviewed-by: Nigel Tao --- exp/sensor/darwin_armx.go | 25 ++++++++++++++++--------- exp/sensor/darwin_armx.m | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/exp/sensor/darwin_armx.go b/exp/sensor/darwin_armx.go index 612882e..94c3f89 100644 --- a/exp/sensor/darwin_armx.go +++ b/exp/sensor/darwin_armx.go @@ -23,6 +23,10 @@ void GoIOS_startGyro(float interval); void GoIOS_stopGyro(); void GoIOS_readGyro(int64_t* timestamp, float* vector); +void GoIOS_startMagneto(float interval); +void GoIOS_stopMagneto(); +void GoIOS_readMagneto(int64_t* timestamp, float* vector); + void GoIOS_destroyManager(); */ import "C" @@ -71,17 +75,16 @@ func (m *manager) enable(s Sender, t Type, delay time.Duration) error { if delay < minDelay { delay = minDelay } - interval := float64(delay) / float64(time.Second) + interval := C.float(float64(delay) / float64(time.Second)) switch t { case Accelerometer: - C.GoIOS_startAccelerometer(C.float(interval)) + C.GoIOS_startAccelerometer(interval) case Gyroscope: - C.GoIOS_startGyro(C.float(interval)) + C.GoIOS_startGyro(interval) case Magnetometer: - return fmt.Errorf("sensor: %v is not supported yet", t) + C.GoIOS_startMagneto(interval) } - go m.pollSensor(s, t, delay, channels.done[t]) return nil } @@ -102,8 +105,7 @@ func (m *manager) disable(t Type) error { case Gyroscope: C.GoIOS_stopGyro() case Magnetometer: - default: - return fmt.Errorf("sensor: unknown sensor type: %v", t) + C.GoIOS_stopMagneto() } return nil } @@ -119,11 +121,16 @@ func (m *manager) pollSensor(s Sender, t Type, d time.Duration, done chan struct case <-done: return default: + tp := (*C.int64_t)(unsafe.Pointer(×tamp)) + vp := (*C.float)(unsafe.Pointer(&ev[0])) + switch t { case Accelerometer: - C.GoIOS_readAccelerometer((*C.int64_t)(unsafe.Pointer(×tamp)), (*C.float)(unsafe.Pointer(&ev[0]))) + C.GoIOS_readAccelerometer(tp, vp) case Gyroscope: - C.GoIOS_readGyro((*C.int64_t)(unsafe.Pointer(×tamp)), (*C.float)(unsafe.Pointer(&ev[0]))) + C.GoIOS_readGyro(tp, vp) + case Magnetometer: + C.GoIOS_readMagneto(tp, vp) } ts := int64(timestamp) if ts > lastTimestamp { diff --git a/exp/sensor/darwin_armx.m b/exp/sensor/darwin_armx.m index a775a46..d67f812 100644 --- a/exp/sensor/darwin_armx.m +++ b/exp/sensor/darwin_armx.m @@ -47,6 +47,23 @@ void GoIOS_readGyro(int64_t* timestamp, float* v) { v[2] = data.rotationRate.z; } +void GoIOS_startMagneto(float interval) { + manager.magnetometerUpdateInterval = interval; + [manager startMagnetometerUpdates]; +} + +void GoIOS_stopMagneto() { + [manager stopMagnetometerUpdates]; +} + +void GoIOS_readMagneto(int64_t* timestamp, float* v) { + CMMagnetometerData* data = manager.magnetometerData; + *timestamp = (int64_t)(data.timestamp * 1000 * 1000); + v[0] = data.magneticField.x; + v[1] = data.magneticField.y; + v[2] = data.magneticField.z; +} + void GoIOS_destroyManager() { [manager release]; manager = nil;