Application running on nRF51822 PCA10001
Dependencies: BLE_API MMA8652 nRF51822 mbed-src
Diff: main.cpp
- Revision:
- 1:1c52fb502f6b
- Parent:
- 0:90c13be263a0
- Child:
- 2:2ddac99c3bde
diff -r 90c13be263a0 -r 1c52fb502f6b main.cpp --- a/main.cpp Tue Jul 22 14:54:44 2014 +0000 +++ b/main.cpp Fri Jul 25 14:57:16 2014 +0000 @@ -1,5 +1,5 @@ /******************************************************************************* -* Title : System Initialization +* Title : System Initialisation * Filename : main.cpp * Author : Richard Osterloh * Origin Date : 22/07/2014 @@ -48,11 +48,15 @@ /****************************************************************************** * Module Variable Definitions *******************************************************************************/ -nRF51822n nrf; -BLEDevice ble; -DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */ -DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */ +nRF51822n nrf; +BLEDevice ble; +DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */ +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); const static char DEVICE_NAME[] = "Buddi Blueband"; @@ -95,9 +99,35 @@ GattCharacteristic *batteryChars[] = {&batteryPercentage }; GattService batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *)); +// Custom Buddi Service 0x92d7dc20-ba55-4df8-84b1-ad8af6e1ea4a +static const uint8_t buddi_service_uuid[] = {0x92, 0xd7, 0xdc, 0x20, 0xba, 0x55, 0x4d, 0xf8, 0x84, 0xb1, 0xad, 0x8a, 0xf6, 0xe1, 0xea, 0x4a}; +// Command Characteristic +static const uint8_t command_uuid[] = {0x92, 0xd7, 0xdc, 0x20, 0xbb, 0x01, 0x4d, 0xf8, 0x84, 0xb1, 0xad, 0x8a, 0xf6, 0xe1, 0xea, 0x4a}; +uint8_t commandPayload[8] = {0,}; +GattCharacteristic commandCharacteristic (command_uuid, commandPayload, 1, 8, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); +// Notification Characteristic +static const uint8_t notification_uuid[] = {0x92, 0xd7, 0xdc, 0x20, 0xbb, 0x02, 0x4d, 0xf8, 0x84, 0xb1, 0xad, 0x8a, 0xf6, 0xe1, 0xea, 0x4a}; +uint8_t notificationPayload[8] = {0,}; +GattCharacteristic notificationCharacteristic (notification_uuid, notificationPayload, 1, 8, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); +GattCharacteristic *buddiChars[] = {&commandCharacteristic, ¬ificationCharacteristic}; +GattService buddiService(buddi_service_uuid, buddiChars, sizeof(buddiChars) / sizeof(GattCharacteristic *)); + +//static const uint8_t dev_info_uuid_rev[] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x0a, 0x18, 0x00, 0x00}; // 0000180a-0000-1000-8000-00805f9b34fb +//static const uint8_t battery_uuid_rev[] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x0f, 0x18, 0x00, 0x00}; // 0000180f-0000-1000-8000-00805f9b34fb +//static const uint8_t buddi_service_uuid_rev[] = {0x4a, 0xea, 0xe1, 0xf6, 0x8a, 0xad, 0xb1, 0x84, 0xf8, 0x4d, 0x55, 0xba, 0x20, 0xdc, 0xd7, 0x92}; // 92d7dc20-ba55-4df8-84b1-ad8af6e1ea4a + static const uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE, GattService::UUID_BATTERY_SERVICE}; +//static const unit8_t uuid128_list[] = {(const uint8_t *)buddi_service_uuid_rev, +// (const uint8_t *)buddi_service_uuid_rev, +// (const uint8_t *)buddi_service_uuid_rev}; +float r = 0.0f; +float g = 0.0f; +float b = 0.0f; + /****************************************************************************** * Function Prototypes *******************************************************************************/ @@ -124,6 +154,17 @@ advertisingStateLed = 1; } +void onDataWritten(uint16_t charHandle) +{ + if (charHandle == commandCharacteristic.getHandle()) { + DEBUG("onDataWritten()\n\r"); + uint16_t bytesRead; + ble.readCharacteristicValue(commandCharacteristic.getHandle(), commandPayload, &bytesRead); + DEBUG("ECHO: %s\n\r", (char *)commandPayload); + ble.updateCharacteristicValue(notificationCharacteristic.getHandle(), commandPayload, bytesRead); + } +} + /** * Triggered periodically by the 'ticker' interrupt */ @@ -133,12 +174,34 @@ 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)); + //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; } int main(void) @@ -160,20 +223,19 @@ //ble.onTimeout(timeoutCallback); ble.onConnection(connectionCallback); ble.onDisconnection(disconnectionCallback); - //ble.onDataSent(GattServer::ServerEventCallback_t callback); - //ble.onDataWritten(GattServer::EventCallback_t callback); - //ble.onUpdatesEnabled(GattServer::EventCallback_t callback); - //ble.onUpdatesDisabled(GattServer::EventCallback_t callback); - //ble.onConfirmationReceived(GattServer::EventCallback_t callback); + //ble.onDataSent(onDataSent); + ble.onDataWritten(onDataWritten); + //ble.onUpdatesEnabled(onUpdatesEnabled); + //ble.onUpdatesDisabled(onUpdatesDisabled); + //ble.onConfirmationReceived(onConfirmationReceived); /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); + ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_WATCH); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); - //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - // (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); + //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t*)uuid128_list, sizeof(uuid128_list)); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ @@ -182,6 +244,7 @@ ble.addService(devInfoService); ble.addService(batteryService); + ble.addService(buddiService); while (true) { ble.waitForEvent();