github.com/SahandAslani/gomobile@v0.0.0-20210909130135-2cb2d44c09b2/exp/sensor/darwin_armx.m (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin
     6  // +build arm arm64
     7  
     8  #import <CoreMotion/CoreMotion.h>
     9  
    10  CMMotionManager* manager = nil;
    11  
    12  void GoIOS_createManager() {
    13    manager = [[CMMotionManager alloc] init];
    14  }
    15  
    16  void GoIOS_startAccelerometer(float interval) {
    17    manager.accelerometerUpdateInterval = interval;
    18    [manager startAccelerometerUpdates];
    19  }
    20  
    21  void GoIOS_stopAccelerometer() {
    22    [manager stopAccelerometerUpdates];
    23  }
    24  
    25  void GoIOS_readAccelerometer(int64_t* timestamp, float* v) {
    26    CMAccelerometerData* data = manager.accelerometerData;
    27    *timestamp = (int64_t)(data.timestamp * 1000 * 1000);
    28    v[0] = data.acceleration.x;
    29    v[1] = data.acceleration.y;
    30    v[2] = data.acceleration.z;
    31  }
    32  
    33  void GoIOS_startGyro(float interval) {
    34    manager.gyroUpdateInterval = interval;
    35    [manager startGyroUpdates];
    36  }
    37  
    38  void GoIOS_stopGyro() {
    39    [manager stopGyroUpdates];
    40  }
    41  
    42  void GoIOS_readGyro(int64_t* timestamp, float* v) {
    43    CMGyroData* data = manager.gyroData;
    44    *timestamp = (int64_t)(data.timestamp * 1000 * 1000);
    45    v[0] = data.rotationRate.x;
    46    v[1] = data.rotationRate.y;
    47    v[2] = data.rotationRate.z;
    48  }
    49  
    50  void GoIOS_startMagneto(float interval) {
    51    manager.magnetometerUpdateInterval = interval;
    52    [manager startMagnetometerUpdates];
    53  }
    54  
    55  void GoIOS_stopMagneto() {
    56    [manager stopMagnetometerUpdates];
    57  }
    58  
    59  void GoIOS_readMagneto(int64_t* timestamp, float* v) {
    60    CMMagnetometerData* data = manager.magnetometerData;
    61    *timestamp = (int64_t)(data.timestamp * 1000 * 1000);
    62    v[0] = data.magneticField.x;
    63    v[1] = data.magneticField.y;
    64    v[2] = data.magneticField.z;
    65  }
    66  
    67  void GoIOS_destroyManager() {
    68    [manager release];
    69    manager = nil;
    70  }