Application running on nRF51822 PCA10001
Dependencies: BLE_API MMA8652 nRF51822 mbed-src
BuddiService.cpp@3:596283411a00, 2014-08-29 (annotated)
- Committer:
- rosterloh84
- Date:
- Fri Aug 29 12:36:01 2014 +0000
- Revision:
- 3:596283411a00
- Child:
- 4:630f1560a0f3
Started seperating out services and refactored due to library updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rosterloh84 | 3:596283411a00 | 1 | #include "mbed.h" |
rosterloh84 | 3:596283411a00 | 2 | #include "BuddiService.h" |
rosterloh84 | 3:596283411a00 | 3 | #include "Logger.h" |
rosterloh84 | 3:596283411a00 | 4 | #include "Configuration.h" |
rosterloh84 | 3:596283411a00 | 5 | |
rosterloh84 | 3:596283411a00 | 6 | namespace Nudge { |
rosterloh84 | 3:596283411a00 | 7 | // Buddi base UUID: 92D7DC20-xxxx-4DF8-84B1-AD8AF6E1EA4A |
rosterloh84 | 3:596283411a00 | 8 | #define buddi_UUID(x) {0x92, 0xD7, 0xDC, 0x20, (((x) & 0xFF00) >> 8), ((x) & 0xFF), 0x4D, 0xF8, 0x82, 0xB1, 0xAD, 0x8A, 0xF6, 0xE1, 0xEA, 0x4A} |
rosterloh84 | 3:596283411a00 | 9 | // UUID byte arrays |
rosterloh84 | 3:596283411a00 | 10 | static const uint8_t buddiServiceUUID[] = buddi_UUID(0xBA55); |
rosterloh84 | 3:596283411a00 | 11 | static const uint8_t commandUUID[16] = buddi_UUID(0xBB01); |
rosterloh84 | 3:596283411a00 | 12 | static const uint8_t notificationUUID[] = buddi_UUID(0xBB02); |
rosterloh84 | 3:596283411a00 | 13 | |
rosterloh84 | 3:596283411a00 | 14 | // Storage for the value of the characteristics |
rosterloh84 | 3:596283411a00 | 15 | struct command_t { |
rosterloh84 | 3:596283411a00 | 16 | uint8_t module; |
rosterloh84 | 3:596283411a00 | 17 | uint8_t opcode; |
rosterloh84 | 3:596283411a00 | 18 | uint8_t data[15]; |
rosterloh84 | 3:596283411a00 | 19 | }; |
rosterloh84 | 3:596283411a00 | 20 | static struct command_t commandPayload; |
rosterloh84 | 3:596283411a00 | 21 | //uint8_t commandPayload[17] = {0,}; |
rosterloh84 | 3:596283411a00 | 22 | |
rosterloh84 | 3:596283411a00 | 23 | // Other things needed for operation |
rosterloh84 | 3:596283411a00 | 24 | static BLEDevice* ble; |
rosterloh84 | 3:596283411a00 | 25 | |
rosterloh84 | 3:596283411a00 | 26 | static GattCharacteristic commandCharacteristic(commandUUID, |
rosterloh84 | 3:596283411a00 | 27 | (uint8_t *)&commandPayload, |
rosterloh84 | 3:596283411a00 | 28 | sizeof(commandPayload), //1, |
rosterloh84 | 3:596283411a00 | 29 | sizeof(commandPayload), //8, |
rosterloh84 | 3:596283411a00 | 30 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | |
rosterloh84 | 3:596283411a00 | 31 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
rosterloh84 | 3:596283411a00 | 32 | |
rosterloh84 | 3:596283411a00 | 33 | uint8_t notificationPayload[8] = {0,}; |
rosterloh84 | 3:596283411a00 | 34 | |
rosterloh84 | 3:596283411a00 | 35 | static GattCharacteristic notificationCharacteristic(notificationUUID, |
rosterloh84 | 3:596283411a00 | 36 | notificationPayload, |
rosterloh84 | 3:596283411a00 | 37 | 1, |
rosterloh84 | 3:596283411a00 | 38 | 8, |
rosterloh84 | 3:596283411a00 | 39 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
rosterloh84 | 3:596283411a00 | 40 | |
rosterloh84 | 3:596283411a00 | 41 | static GattCharacteristic *allChars[] = {&commandCharacteristic, ¬ificationCharacteristic}; |
rosterloh84 | 3:596283411a00 | 42 | static GattService buddiService(buddiServiceUUID, allChars, sizeof(allChars) / sizeof(GattCharacteristic *)); |
rosterloh84 | 3:596283411a00 | 43 | |
rosterloh84 | 3:596283411a00 | 44 | PwmOut ledr(p22); /* Red LED on PCA10000 */ |
rosterloh84 | 3:596283411a00 | 45 | PwmOut ledg(p23); /* Green LED on PCA10000 */ |
rosterloh84 | 3:596283411a00 | 46 | PwmOut ledb(p21); /* Blue LED on PCA10000 */ |
rosterloh84 | 3:596283411a00 | 47 | |
rosterloh84 | 3:596283411a00 | 48 | void init(BLEDevice &bleDevice) |
rosterloh84 | 3:596283411a00 | 49 | { |
rosterloh84 | 3:596283411a00 | 50 | ledr = 1.0f; |
rosterloh84 | 3:596283411a00 | 51 | ledg = 1.0f; |
rosterloh84 | 3:596283411a00 | 52 | ledb = 1.0f; |
rosterloh84 | 3:596283411a00 | 53 | ble = &bleDevice; |
rosterloh84 | 3:596283411a00 | 54 | ble->addService(buddiService); |
rosterloh84 | 3:596283411a00 | 55 | DEBUG("Added buddi service\r\n"); |
rosterloh84 | 3:596283411a00 | 56 | } |
rosterloh84 | 3:596283411a00 | 57 | |
rosterloh84 | 3:596283411a00 | 58 | const uint8_t *getServiceUUIDp() |
rosterloh84 | 3:596283411a00 | 59 | { |
rosterloh84 | 3:596283411a00 | 60 | return buddiServiceUUID; |
rosterloh84 | 3:596283411a00 | 61 | } |
rosterloh84 | 3:596283411a00 | 62 | |
rosterloh84 | 3:596283411a00 | 63 | void setLEDValue(uint8_t led, uint8_t value) |
rosterloh84 | 3:596283411a00 | 64 | { |
rosterloh84 | 3:596283411a00 | 65 | float val = ((float)value)/255; |
rosterloh84 | 3:596283411a00 | 66 | DEBUG("Setting LED %u to %1.3f\r\n", led, val); |
rosterloh84 | 3:596283411a00 | 67 | switch(led) |
rosterloh84 | 3:596283411a00 | 68 | { |
rosterloh84 | 3:596283411a00 | 69 | case 0: |
rosterloh84 | 3:596283411a00 | 70 | ledg = val; |
rosterloh84 | 3:596283411a00 | 71 | break; |
rosterloh84 | 3:596283411a00 | 72 | case 1: |
rosterloh84 | 3:596283411a00 | 73 | ledr = val; |
rosterloh84 | 3:596283411a00 | 74 | break; |
rosterloh84 | 3:596283411a00 | 75 | case 2: |
rosterloh84 | 3:596283411a00 | 76 | ledb = val; |
rosterloh84 | 3:596283411a00 | 77 | break; |
rosterloh84 | 3:596283411a00 | 78 | default: |
rosterloh84 | 3:596283411a00 | 79 | DEBUG("Unknown LED %u selected\r\n", led); |
rosterloh84 | 3:596283411a00 | 80 | break; |
rosterloh84 | 3:596283411a00 | 81 | } |
rosterloh84 | 3:596283411a00 | 82 | } |
rosterloh84 | 3:596283411a00 | 83 | |
rosterloh84 | 3:596283411a00 | 84 | void handleDataWritten(uint16_t handle) |
rosterloh84 | 3:596283411a00 | 85 | { |
rosterloh84 | 3:596283411a00 | 86 | if (!ble) { |
rosterloh84 | 3:596283411a00 | 87 | return; |
rosterloh84 | 3:596283411a00 | 88 | } |
rosterloh84 | 3:596283411a00 | 89 | |
rosterloh84 | 3:596283411a00 | 90 | if (handle == commandCharacteristic.getValueAttribute().getHandle()) { |
rosterloh84 | 3:596283411a00 | 91 | uint16_t len = sizeof(commandPayload); |
rosterloh84 | 3:596283411a00 | 92 | ble->readCharacteristicValue(handle, (uint8_t *) &commandPayload, &len); |
rosterloh84 | 3:596283411a00 | 93 | |
rosterloh84 | 3:596283411a00 | 94 | DEBUG("RX: MOD: 0x%X OP: 0x%X D0: 0x%X D1: 0x%X", commandPayload.module, commandPayload.opcode, commandPayload.data[0], commandPayload.data[1]); |
rosterloh84 | 3:596283411a00 | 95 | //ble->updateCharacteristicValue(notificationCharacteristic.getValueAttribute().getHandle(), commandPayload, bytesRead); |
rosterloh84 | 3:596283411a00 | 96 | switch(commandPayload.module) { |
rosterloh84 | 3:596283411a00 | 97 | case MODULE_NEO_PIXEL: |
rosterloh84 | 3:596283411a00 | 98 | setLEDValue(0, commandPayload.data[2]); |
rosterloh84 | 3:596283411a00 | 99 | setLEDValue(1, commandPayload.data[3]); |
rosterloh84 | 3:596283411a00 | 100 | setLEDValue(2, commandPayload.data[4]); |
rosterloh84 | 3:596283411a00 | 101 | break; |
rosterloh84 | 3:596283411a00 | 102 | default: |
rosterloh84 | 3:596283411a00 | 103 | DEBUG("Module %u not implemented yet", commandPayload.module); |
rosterloh84 | 3:596283411a00 | 104 | break; |
rosterloh84 | 3:596283411a00 | 105 | } |
rosterloh84 | 3:596283411a00 | 106 | } else { |
rosterloh84 | 3:596283411a00 | 107 | DEBUG("Got data on unexpected characteristic handle %u!\r\n", handle); |
rosterloh84 | 3:596283411a00 | 108 | } |
rosterloh84 | 3:596283411a00 | 109 | } |
rosterloh84 | 3:596283411a00 | 110 | |
rosterloh84 | 3:596283411a00 | 111 | } // namespace nudge |