Application running on nRF51822 PCA10001

Dependencies:   BLE_API MMA8652 nRF51822 mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BuddiService.cpp Source File

BuddiService.cpp

00001 #include "mbed.h"
00002 #include "BuddiService.h"
00003 #include "Logger.h"
00004 #include "Configuration.h"
00005 
00006 namespace Nudge {
00007 // Buddi base UUID: 92D7DC20-xxxx-4DF8-84B1-AD8AF6E1EA4A
00008 #define buddi_UUID(x)   {0x92, 0xD7, 0xDC, 0x20, (((x) & 0xFF00) >> 8), ((x) & 0xFF), 0x4D, 0xF8, 0x82, 0xB1, 0xAD, 0x8A, 0xF6, 0xE1, 0xEA, 0x4A}
00009 // UUID byte arrays
00010 static const uint8_t buddiServiceUUID[]     = buddi_UUID(0xBA55);
00011 static const uint8_t commandUUID[16]        = buddi_UUID(0xBB01);
00012 static const uint8_t notificationUUID[]     = buddi_UUID(0xBB02);
00013 
00014 // Storage for the value of the characteristics
00015 typedef struct {
00016     uint8_t module;
00017     uint8_t opcode;
00018     uint8_t data[15];
00019 } Command_t;
00020 static Command_t commandPayload;
00021 //uint8_t commandPayload[17] = {0,};
00022 
00023 // Other things needed for operation
00024 static BLEDevice*                   ble;
00025 
00026 static GattCharacteristic commandCharacteristic(commandUUID, 
00027                                                 (uint8_t *)&commandPayload, 
00028                                                 sizeof(Command_t), //1, 
00029                                                 sizeof(Command_t), //8, 
00030                                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 
00031                                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
00032 
00033 uint8_t notificationPayload[8] = {0,};
00034 
00035 static GattCharacteristic notificationCharacteristic(notificationUUID, 
00036                                                     notificationPayload, 
00037                                                     1, 
00038                                                     8, 
00039                                                     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00040 
00041 static GattCharacteristic *allChars[] = {&commandCharacteristic, &notificationCharacteristic};
00042 static GattService buddiService(buddiServiceUUID, allChars, sizeof(allChars) / sizeof(GattCharacteristic *));
00043 
00044 PwmOut      ledr(p22);                       /* Red LED on PCA10000 */
00045 PwmOut      ledg(p23);                       /* Green LED on PCA10000 */ 
00046 PwmOut      ledb(p21);                       /* Blue LED on PCA10000 */
00047 
00048 void init(BLEDevice &bleDevice)
00049 {
00050     ledr               = 1.0f;
00051     ledg               = 1.0f;
00052     ledb               = 1.0f;
00053     ble                = &bleDevice;
00054     ble->addService(buddiService);
00055     DEBUG("Added buddi service\r\n");
00056 }
00057 
00058 const uint8_t *getServiceUUIDp()
00059 {
00060     return buddiServiceUUID;
00061 }
00062 
00063 void setLEDValue(uint8_t led, uint8_t value)
00064 {
00065     float val = ((float)value)/255;
00066     DEBUG("Setting LED %u to %1.3f\r\n", led, val);
00067     switch(led)
00068     {
00069         case 0:
00070             ledg = val;
00071             break;
00072         case 1:
00073             ledr = val;
00074             break;
00075         case 2:
00076             ledb = val;
00077             break;
00078         default:
00079             DEBUG("Unknown LED %u selected\r\n", led);
00080             break;
00081     }
00082 }
00083 
00084 void handleDataWritten(uint16_t handle, const GattCharacteristicWriteCBParams *params)
00085 {
00086     if (!ble) {
00087         return;
00088     }
00089 
00090     if (handle == commandCharacteristic.getValueAttribute().getHandle()) {
00091         //uint16_t len = sizeof(commandPayload);
00092         //ble->readCharacteristicValue(handle, (uint8_t *) &commandPayload, &len);
00093         if (params->len != sizeof(Command_t)) {
00094             DEBUG("invalid write into fileInfo characteristic\r\n");
00095             return;
00096         }
00097         memcpy(&commandPayload, params->data, params->len);
00098         
00099         DEBUG("RX: MOD: 0x%X OP: 0x%X D0: 0x%X D1: 0x%X", commandPayload.module, commandPayload.opcode, commandPayload.data[0], commandPayload.data[1]);
00100         //ble->updateCharacteristicValue(notificationCharacteristic.getValueAttribute().getHandle(), commandPayload, bytesRead);
00101         switch(commandPayload.module) {
00102             case MODULE_NEO_PIXEL:
00103                 setLEDValue(0, commandPayload.data[2]);
00104                 setLEDValue(1, commandPayload.data[3]);
00105                 setLEDValue(2, commandPayload.data[4]);
00106                 break;
00107             default:
00108                 DEBUG("Module %u not implemented yet", commandPayload.module);
00109                 break;
00110         }
00111     } else {
00112         DEBUG("Got data on unexpected characteristic handle %u!\r\n", handle);
00113     }
00114 }
00115 
00116 } // namespace nudge