Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822 MPU6050_lib
main.cpp
- Committer:
- fruediger
- Date:
- 2015-06-24
- Revision:
- 1:6a40ad9ac7e5
- Parent:
- 0:23fd064af409
- Child:
- 3:d72d9195dc26
File content as of revision 1:6a40ad9ac7e5:
/** Includes */ #include "common.h" #include "mbed.h" #include "BLE.h" #include "MotionOrientationService.h" /** Constants */ static const char deviceName[] = "nano bear " XSTRING_(MBED_BUILD_TIMESTAMP); static const uint16_t minimumConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(20); // 20ms static const uint16_t maximumConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(40); // 40ms; static const uint16_t slaveLatency = 0; static const float tickerTimeout = 0.2f; static const uint8_t connectedBlinkSequ[] = {1, 1, 1, 0}; static const uint8_t disconnectedBlinkSequ[] = {1, 0, 1, 0, 1, 0, 0, 0}; /** Global variables */ BLE ble; Gap::ConnectionParams_t fast; MotionOrientationService *mos; DigitalOut led(LED1); Ticker ticker; /** Helper functions */ inline Gap &gap() { // get us the corresponding Gap instance only once and then save and reuse that instance // (the underlying Gap instance shouldn't change on the BLE object) static Gap &gap = ble.gap(); return gap; } void blink(const uint8_t sequence[]) { static int n = -1; n = ((n + 1) % sizeof(sequence)); led = sequence[n]; } /** Callback functions */ void periodicCallback() { blink(gap().getState().connected ? connectedBlinkSequ : disconnectedBlinkSequ); } void timeoutCallback(Gap::TimeoutSource_t) { gap().startAdvertising(); } void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { gap().startAdvertising(); } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { // update the connection parameters with the tuned fast ones ble.updateConnectionParams(params->handle, &fast); // start polling and sending sensor data mos->start(); } /** Main */ int main() { led = 0; ble.init(); ticker.attach(periodicCallback, tickerTimeout); gap().onTimeout(timeoutCallback); gap().onDisconnection(disconnectionCallback); gap().onConnection(connectionCallback); gap().setDeviceName((const uint8_t*)deviceName); gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t*)deviceName, sizeof(deviceName)); gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); //TODO: DECISION: advertising at minimal interval may consumes to much power, but is way faster gap().setAdvertisingInterval(gap().getMinAdvertisingInterval()); // tune the preferred connection parameters to enable transfer more often // TODO: DECISION: also waht about power consumption? gap().getPreferredConnectionParams(&fast); fast.minConnectionInterval = minimumConnectionInterval; fast.maxConnectionInterval = maximumConnectionInterval; fast.slaveLatency = slaveLatency; gap().setPreferredConnectionParams(&fast); gap().startAdvertising(); MotionOrientationService _mos(ble); mos = &_mos; while (true) ble.waitForEvent(); }