Application running on nRF51822 PCA10001
Dependencies: BLE_API MMA8652 nRF51822 mbed-src
Diff: main.cpp
- Revision:
- 2:2ddac99c3bde
- Parent:
- 1:1c52fb502f6b
- Child:
- 3:596283411a00
--- a/main.cpp Fri Jul 25 14:57:16 2014 +0000 +++ b/main.cpp Mon Jul 28 07:45:00 2014 +0000 @@ -15,7 +15,7 @@ * *******************************************************************************/ /** \file main.cpp - * \brief This module contains the + * \brief This module contains the main function and setup */ /****************************************************************************** * Includes @@ -54,9 +54,9 @@ DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */ //AnalogIn adc1(p0_0); //MMA8652 acc1(I2C_SDA0, I2C_SCL0); -PwmOut ledr(p21); -PwmOut ledg(p22); -PwmOut ledb(p23); +PwmOut ledr(p21); /* Red LED on PCA10000 */ +PwmOut ledg(p22); /* Green LED on PCA10000 */ +PwmOut ledb(p23); /* Blue LED on PCA10000 */ const static char DEVICE_NAME[] = "Buddi Blueband"; @@ -124,6 +124,10 @@ // (const uint8_t *)buddi_service_uuid_rev, // (const uint8_t *)buddi_service_uuid_rev}; +static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that + * polling is necessary. */ +static Gap::ConnectionParams_t connectionParams; + float r = 0.0f; float g = 0.0f; float b = 0.0f; @@ -140,16 +144,22 @@ DEBUG("Timeout!\r\n"); } -void connectionCallback(void) +void connectionCallback(Gap::Handle_t handle) { - DEBUG("Connection\n\r"); + DEBUG("connected. Got handle %u\r\n", handle); + + connectionParams.slaveLatency = 1; + if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { + DEBUG("failed to update connection paramter\r\n"); + } + advertisingStateLed = 0; } -void disconnectionCallback(void) +void disconnectionCallback(Gap::Handle_t handle) { - DEBUG("Disconnected!\r\n"); - DEBUG("Restarting the advertising process\r\n"); + DEBUG("Disconnected handle %u!\n\r", handle); + DEBUG("Restarting the advertising process\n\r"); ble.startAdvertising(); advertisingStateLed = 1; } @@ -171,37 +181,10 @@ void periodicCallback(void) { oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */ - - if (ble.getGapState().connected) { - /* Update the battery measurement */ - //batteryLevel = adc1.read_u16()&0x0FFF) * 3.3/4096; - batteryLevel--; - if (batteryLevel == 1) { - batteryLevel = 100; - } - //ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel)); - } - - if(r <= 1.0f) { - r += 0.1f; - } else { - r = 0.0f; - } - ledr = r; - - if(g <= 1.0f) { - g += 0.2f; - } else { - g = 0.0f; - } - ledg = g; - - if(b <= 1.0f) { - b += 0.3f; - } else { - b = 0.0f; - } - ledb = b; + triggerSensorPolling = true; /* Note that the periodicCallback() executes in + * interrupt context, so it is safer to do + * heavy-weight sensor polling from the main + * thread.*/ } int main(void) @@ -228,6 +211,8 @@ //ble.onUpdatesEnabled(onUpdatesEnabled); //ble.onUpdatesDisabled(onUpdatesDisabled); //ble.onConfirmationReceived(onConfirmationReceived); + + ble.getPreferredConnectionParams(&connectionParams); /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); @@ -247,6 +232,43 @@ ble.addService(buddiService); while (true) { - ble.waitForEvent(); + if (triggerSensorPolling) { + triggerSensorPolling = false; + + /* Do blocking calls or whatever is necessary for sensor polling. */ + if (ble.getGapState().connected) { + /* Update the battery measurement */ + //batteryLevel = adc1.read_u16()&0x0FFF) * 3.3/4096; + batteryLevel--; + if (batteryLevel == 1) { + batteryLevel = 100; + } + //ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel)); + } + + if(r <= 1.0f) { + r += 0.1f; + } else { + r = 0.0f; + } + ledr = r; + + if(g <= 1.0f) { + g += 0.2f; + } else { + g = 0.0f; + } + ledg = g; + + if(b <= 1.0f) { + b += 0.3f; + } else { + b = 0.0f; + } + ledb = b; + + } else { + ble.waitForEvent(); + } } }