Created
April 19, 2016 16:03
-
-
Save ciniml/f1a8cba2ebdaca0ba327219894a2d4ad to your computer and use it in GitHub Desktop.
Sends Genuino IMU data via BLE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CurieBLE.h> | |
#include <CurieIMU.h> | |
#include <CurieTimerOne.h> | |
#include <stdint.h> | |
BLEPeripheral blePeripheral; | |
BLEService imuService("FFF0"); | |
static constexpr size_t AccelometerCharacteristicSize = sizeof(int) * 3; | |
static constexpr size_t GyroCharacteristicSize = sizeof(int) * 3; | |
BLECharacteristic accelometerCharacteristic("FFF1", BLERead | BLENotify, AccelometerCharacteristicSize); | |
BLECharacteristic gyroCharacteristic("FFF2", BLERead | BLENotify, GyroCharacteristicSize); | |
static volatile int accelometerValues[3]; | |
static volatile int gyroValues[3]; | |
static volatile bool valueUpdated = false; | |
void timerSampleValuesIsr() | |
{ | |
if (valueUpdated) return; | |
int values[3]; | |
CurieIMU.readAccelerometer(values[0], values[1], values[2]); | |
for (int i = 0; i < 3; ++i) accelometerValues[i] = values[i]; | |
CurieIMU.readGyro(values[0], values[1], values[2]); | |
for (int i = 0; i < 3; ++i) gyroValues[i] = values[i]; | |
valueUpdated = true; | |
} | |
void setup() { | |
pinMode(13, OUTPUT); | |
blePeripheral.setLocalName("IMU_BLE"); | |
blePeripheral.setAdvertisedServiceUuid(imuService.uuid()); | |
blePeripheral.addAttribute(imuService); | |
blePeripheral.addAttribute(accelometerCharacteristic); | |
blePeripheral.addAttribute(gyroCharacteristic); | |
const uint8_t initialValue[AccelometerCharacteristicSize] = { 0 }; | |
accelometerCharacteristic.setValue(initialValue, AccelometerCharacteristicSize); | |
gyroCharacteristic.setValue(initialValue, GyroCharacteristicSize); | |
blePeripheral.begin(); | |
// Initialize IMU | |
CurieIMU.begin(); | |
CurieIMU.setAccelerometerRange(2); // Max: 2G | |
CurieIMU.setGyroRange(250); // Max: 250 [deg/s] | |
} | |
void loop() { | |
BLECentral central = blePeripheral.central(); | |
if (central) { | |
digitalWrite(13, HIGH); | |
valueUpdated = false; | |
CurieTimerOne.start(100000u, timerSampleValuesIsr); | |
while (central.connected()) { | |
if (valueUpdated) | |
{ | |
accelometerCharacteristic.setValue(reinterpret_cast<uint8_t*>(const_cast<int*>(accelometerValues)), AccelometerCharacteristicSize); | |
gyroCharacteristic.setValue(reinterpret_cast<uint8_t*>(const_cast<int*>(gyroValues)), GyroCharacteristicSize); | |
valueUpdated = false; | |
} | |
} | |
CurieTimerOne.stop(); | |
digitalWrite(13, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment