exp/sensor: enable magnetometer on darwin{arm,arm64}

Change-Id: Ib188121501ac96cb8743e57f907b23d0fdfc37a0
Reviewed-on: https://go-review.googlesource.com/14292
Reviewed-by: Nigel Tao <nigeltao@golang.org>
This commit is contained in:
Burcu Dogan 2015-09-03 21:40:21 -07:00
parent d4f6353bc3
commit 043428404d
2 changed files with 33 additions and 9 deletions

View File

@ -23,6 +23,10 @@ void GoIOS_startGyro(float interval);
void GoIOS_stopGyro(); void GoIOS_stopGyro();
void GoIOS_readGyro(int64_t* timestamp, float* vector); 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(); void GoIOS_destroyManager();
*/ */
import "C" import "C"
@ -71,17 +75,16 @@ func (m *manager) enable(s Sender, t Type, delay time.Duration) error {
if delay < minDelay { if delay < minDelay {
delay = minDelay delay = minDelay
} }
interval := float64(delay) / float64(time.Second) interval := C.float(float64(delay) / float64(time.Second))
switch t { switch t {
case Accelerometer: case Accelerometer:
C.GoIOS_startAccelerometer(C.float(interval)) C.GoIOS_startAccelerometer(interval)
case Gyroscope: case Gyroscope:
C.GoIOS_startGyro(C.float(interval)) C.GoIOS_startGyro(interval)
case Magnetometer: 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]) go m.pollSensor(s, t, delay, channels.done[t])
return nil return nil
} }
@ -102,8 +105,7 @@ func (m *manager) disable(t Type) error {
case Gyroscope: case Gyroscope:
C.GoIOS_stopGyro() C.GoIOS_stopGyro()
case Magnetometer: case Magnetometer:
default: C.GoIOS_stopMagneto()
return fmt.Errorf("sensor: unknown sensor type: %v", t)
} }
return nil return nil
} }
@ -119,11 +121,16 @@ func (m *manager) pollSensor(s Sender, t Type, d time.Duration, done chan struct
case <-done: case <-done:
return return
default: default:
tp := (*C.int64_t)(unsafe.Pointer(&timestamp))
vp := (*C.float)(unsafe.Pointer(&ev[0]))
switch t { switch t {
case Accelerometer: case Accelerometer:
C.GoIOS_readAccelerometer((*C.int64_t)(unsafe.Pointer(&timestamp)), (*C.float)(unsafe.Pointer(&ev[0]))) C.GoIOS_readAccelerometer(tp, vp)
case Gyroscope: case Gyroscope:
C.GoIOS_readGyro((*C.int64_t)(unsafe.Pointer(&timestamp)), (*C.float)(unsafe.Pointer(&ev[0]))) C.GoIOS_readGyro(tp, vp)
case Magnetometer:
C.GoIOS_readMagneto(tp, vp)
} }
ts := int64(timestamp) ts := int64(timestamp)
if ts > lastTimestamp { if ts > lastTimestamp {

View File

@ -47,6 +47,23 @@ void GoIOS_readGyro(int64_t* timestamp, float* v) {
v[2] = data.rotationRate.z; 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() { void GoIOS_destroyManager() {
[manager release]; [manager release];
manager = nil; manager = nil;