CUED IIA Project
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_Example by
gloves.cpp@24:baa43caa2f3d, 2017-06-03 (annotated)
- Committer:
- AidasL
- Date:
- Sat Jun 03 21:14:42 2017 +0000
- Revision:
- 24:baa43caa2f3d
Saturday updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AidasL | 24:baa43caa2f3d | 1 | #include "mbed.h" |
AidasL | 24:baa43caa2f3d | 2 | #include "ble/BLE.h" |
AidasL | 24:baa43caa2f3d | 3 | #include "sensors.h" |
AidasL | 24:baa43caa2f3d | 4 | |
AidasL | 24:baa43caa2f3d | 5 | |
AidasL | 24:baa43caa2f3d | 6 | |
AidasL | 24:baa43caa2f3d | 7 | DigitalOut led(LED1); |
AidasL | 24:baa43caa2f3d | 8 | uint16_t customServiceUUID = 0xABCD; |
AidasL | 24:baa43caa2f3d | 9 | uint16_t readCharUUID = 0xABCE; |
AidasL | 24:baa43caa2f3d | 10 | uint16_t writeCharUUID = 0xABCF; |
AidasL | 24:baa43caa2f3d | 11 | |
AidasL | 24:baa43caa2f3d | 12 | static volatile bool triggerSensorPolling = false; |
AidasL | 24:baa43caa2f3d | 13 | |
AidasL | 24:baa43caa2f3d | 14 | //Serial pc(USBTX, USBRX); |
AidasL | 24:baa43caa2f3d | 15 | // pc.baud(9600); |
AidasL | 24:baa43caa2f3d | 16 | // pc.printf("IIC Demo Start \r\n"); |
AidasL | 24:baa43caa2f3d | 17 | // For debugging into PC terminal |
AidasL | 24:baa43caa2f3d | 18 | |
AidasL | 24:baa43caa2f3d | 19 | |
AidasL | 24:baa43caa2f3d | 20 | |
AidasL | 24:baa43caa2f3d | 21 | const static char DEVICE_NAME[] = "WorkGloves"; // change this |
AidasL | 24:baa43caa2f3d | 22 | static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development |
AidasL | 24:baa43caa2f3d | 23 | |
AidasL | 24:baa43caa2f3d | 24 | /* Set Up custom Characteristics */ |
AidasL | 24:baa43caa2f3d | 25 | static uint16_t readValue[26] = {0x55, 0x33}; |
AidasL | 24:baa43caa2f3d | 26 | ReadOnlyArrayGattCharacteristic<uint16_t, sizeof(readValue)> readChar(readCharUUID, readValue); |
AidasL | 24:baa43caa2f3d | 27 | |
AidasL | 24:baa43caa2f3d | 28 | static uint8_t writeValue[8] = {0x00}; |
AidasL | 24:baa43caa2f3d | 29 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
AidasL | 24:baa43caa2f3d | 30 | |
AidasL | 24:baa43caa2f3d | 31 | |
AidasL | 24:baa43caa2f3d | 32 | /* Set up custom service */ |
AidasL | 24:baa43caa2f3d | 33 | GattCharacteristic *characteristics[] = {&readChar, &writeChar}; |
AidasL | 24:baa43caa2f3d | 34 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
AidasL | 24:baa43caa2f3d | 35 | |
AidasL | 24:baa43caa2f3d | 36 | void datapackettoarray(Datapacket todump, uint16_t *dest) |
AidasL | 24:baa43caa2f3d | 37 | { //Need a uint16_t array[26] input for correct functioning |
AidasL | 24:baa43caa2f3d | 38 | uint8_t j,k; |
AidasL | 24:baa43caa2f3d | 39 | |
AidasL | 24:baa43caa2f3d | 40 | for (j=0; j<12; j++) |
AidasL | 24:baa43caa2f3d | 41 | { |
AidasL | 24:baa43caa2f3d | 42 | *dest=todump.flex[j]; |
AidasL | 24:baa43caa2f3d | 43 | dest++; |
AidasL | 24:baa43caa2f3d | 44 | } |
AidasL | 24:baa43caa2f3d | 45 | for(k=0; k<7; k++) |
AidasL | 24:baa43caa2f3d | 46 | { |
AidasL | 24:baa43caa2f3d | 47 | *dest=todump.acc[0][k]; |
AidasL | 24:baa43caa2f3d | 48 | dest++; |
AidasL | 24:baa43caa2f3d | 49 | } |
AidasL | 24:baa43caa2f3d | 50 | |
AidasL | 24:baa43caa2f3d | 51 | for(k=0; k<7; j++, k++) |
AidasL | 24:baa43caa2f3d | 52 | { |
AidasL | 24:baa43caa2f3d | 53 | *dest=todump.acc[1][k]; |
AidasL | 24:baa43caa2f3d | 54 | dest++; |
AidasL | 24:baa43caa2f3d | 55 | } |
AidasL | 24:baa43caa2f3d | 56 | |
AidasL | 24:baa43caa2f3d | 57 | |
AidasL | 24:baa43caa2f3d | 58 | |
AidasL | 24:baa43caa2f3d | 59 | } |
AidasL | 24:baa43caa2f3d | 60 | |
AidasL | 24:baa43caa2f3d | 61 | |
AidasL | 24:baa43caa2f3d | 62 | /* |
AidasL | 24:baa43caa2f3d | 63 | * Restart advertising when phone app disconnects |
AidasL | 24:baa43caa2f3d | 64 | */ |
AidasL | 24:baa43caa2f3d | 65 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
AidasL | 24:baa43caa2f3d | 66 | { |
AidasL | 24:baa43caa2f3d | 67 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); |
AidasL | 24:baa43caa2f3d | 68 | } |
AidasL | 24:baa43caa2f3d | 69 | |
AidasL | 24:baa43caa2f3d | 70 | void periodicCallback(void) |
AidasL | 24:baa43caa2f3d | 71 | { |
AidasL | 24:baa43caa2f3d | 72 | led = !led; /* Do blinky on LED1 while we're waiting for BLE events */ |
AidasL | 24:baa43caa2f3d | 73 | |
AidasL | 24:baa43caa2f3d | 74 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
AidasL | 24:baa43caa2f3d | 75 | * heavy-weight sensor polling from the main thread. */ |
AidasL | 24:baa43caa2f3d | 76 | triggerSensorPolling = true; |
AidasL | 24:baa43caa2f3d | 77 | } |
AidasL | 24:baa43caa2f3d | 78 | |
AidasL | 24:baa43caa2f3d | 79 | /* |
AidasL | 24:baa43caa2f3d | 80 | * Handle writes to writeCharacteristic for screen data from phone |
AidasL | 24:baa43caa2f3d | 81 | */ |
AidasL | 24:baa43caa2f3d | 82 | void writeCharCallback(const GattWriteCallbackParams *params) |
AidasL | 24:baa43caa2f3d | 83 | { |
AidasL | 24:baa43caa2f3d | 84 | /* Check to see what characteristic was written, by handle */ |
AidasL | 24:baa43caa2f3d | 85 | if(params->handle == writeChar.getValueHandle()) { |
AidasL | 24:baa43caa2f3d | 86 | /* Update the readChar with the value of writeChar */ |
AidasL | 24:baa43caa2f3d | 87 | // BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len); |
AidasL | 24:baa43caa2f3d | 88 | } |
AidasL | 24:baa43caa2f3d | 89 | } |
AidasL | 24:baa43caa2f3d | 90 | /* |
AidasL | 24:baa43caa2f3d | 91 | * Initialization callback |
AidasL | 24:baa43caa2f3d | 92 | */ |
AidasL | 24:baa43caa2f3d | 93 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
AidasL | 24:baa43caa2f3d | 94 | { |
AidasL | 24:baa43caa2f3d | 95 | BLE &ble = params->ble; |
AidasL | 24:baa43caa2f3d | 96 | ble_error_t error = params->error; |
AidasL | 24:baa43caa2f3d | 97 | |
AidasL | 24:baa43caa2f3d | 98 | if (error != BLE_ERROR_NONE) { |
AidasL | 24:baa43caa2f3d | 99 | return; |
AidasL | 24:baa43caa2f3d | 100 | } |
AidasL | 24:baa43caa2f3d | 101 | |
AidasL | 24:baa43caa2f3d | 102 | ble.gap().onDisconnection(disconnectionCallback); |
AidasL | 24:baa43caa2f3d | 103 | ble.gattServer().onDataWritten(writeCharCallback); // TODO: update to flush screen !!! |
AidasL | 24:baa43caa2f3d | 104 | |
AidasL | 24:baa43caa2f3d | 105 | /* Setup advertising */ |
AidasL | 24:baa43caa2f3d | 106 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
AidasL | 24:baa43caa2f3d | 107 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
AidasL | 24:baa43caa2f3d | 108 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
AidasL | 24:baa43caa2f3d | 109 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet |
AidasL | 24:baa43caa2f3d | 110 | ble.gap().setAdvertisingInterval(250); // 250ms. |
AidasL | 24:baa43caa2f3d | 111 | |
AidasL | 24:baa43caa2f3d | 112 | /* Add our custom service */ |
AidasL | 24:baa43caa2f3d | 113 | ble.addService(customService); |
AidasL | 24:baa43caa2f3d | 114 | |
AidasL | 24:baa43caa2f3d | 115 | /* Start advertising */ |
AidasL | 24:baa43caa2f3d | 116 | ble.gap().startAdvertising(); |
AidasL | 24:baa43caa2f3d | 117 | } |
AidasL | 24:baa43caa2f3d | 118 | |
AidasL | 24:baa43caa2f3d | 119 | /* |
AidasL | 24:baa43caa2f3d | 120 | * Main loop |
AidasL | 24:baa43caa2f3d | 121 | */ |
AidasL | 24:baa43caa2f3d | 122 | int main(void) |
AidasL | 24:baa43caa2f3d | 123 | { |
AidasL | 24:baa43caa2f3d | 124 | /* initialize stuff */ |
AidasL | 24:baa43caa2f3d | 125 | // printf("\n\r********* Starting Main Loop *********\n\r"); |
AidasL | 24:baa43caa2f3d | 126 | Datapacket readings; |
AidasL | 24:baa43caa2f3d | 127 | setupI2C(); |
AidasL | 24:baa43caa2f3d | 128 | setupacc(ACC_LEFT); |
AidasL | 24:baa43caa2f3d | 129 | setupacc(ACC_RIGHT); |
AidasL | 24:baa43caa2f3d | 130 | led = 1; |
AidasL | 24:baa43caa2f3d | 131 | Ticker ticker; |
AidasL | 24:baa43caa2f3d | 132 | ticker.attach(periodicCallback, 0.2); // blink LED every 0.2 second |
AidasL | 24:baa43caa2f3d | 133 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
AidasL | 24:baa43caa2f3d | 134 | ble.init(bleInitComplete); |
AidasL | 24:baa43caa2f3d | 135 | uint16_t array[26]; |
AidasL | 24:baa43caa2f3d | 136 | // uint8_t data[52]={0,0,0,0,0,0,0,0,0,0,0,0,0}; |
AidasL | 24:baa43caa2f3d | 137 | /* SpinWait for initialization to complete. This is necessary because the |
AidasL | 24:baa43caa2f3d | 138 | * BLE object is used in the main loop below. */ |
AidasL | 24:baa43caa2f3d | 139 | while (ble.hasInitialized() == false) { /* spin loop */ } |
AidasL | 24:baa43caa2f3d | 140 | // uint16_t data[24] = {0x11, 0x22}; |
AidasL | 24:baa43caa2f3d | 141 | /* Infinite loop waiting for BLE interrupt events */ |
AidasL | 24:baa43caa2f3d | 142 | while (1) { |
AidasL | 24:baa43caa2f3d | 143 | // check for trigger// from periodicCallback() |
AidasL | 24:baa43caa2f3d | 144 | if (triggerSensorPolling && ble.getGapState().connected) { |
AidasL | 24:baa43caa2f3d | 145 | triggerSensorPolling = false; |
AidasL | 24:baa43caa2f3d | 146 | // data[0] = data[0]+1;// |
AidasL | 24:baa43caa2f3d | 147 | readflexs(&readings); |
AidasL | 24:baa43caa2f3d | 148 | readacc(&readings, ACC_LEFT ); |
AidasL | 24:baa43caa2f3d | 149 | readacc(&readings, ACC_RIGHT ); |
AidasL | 24:baa43caa2f3d | 150 | datapackettoarray(readings, array); |
AidasL | 24:baa43caa2f3d | 151 | BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), (uint8_t*)array, 52 ); |
AidasL | 24:baa43caa2f3d | 152 | // BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), (uint8_t*)data, 52 ); |
AidasL | 24:baa43caa2f3d | 153 | |
AidasL | 24:baa43caa2f3d | 154 | |
AidasL | 24:baa43caa2f3d | 155 | } |
AidasL | 24:baa43caa2f3d | 156 | else { |
AidasL | 24:baa43caa2f3d | 157 | ble.waitForEvent(); // low power wait for event |
AidasL | 24:baa43caa2f3d | 158 | } |
AidasL | 24:baa43caa2f3d | 159 | } |
AidasL | 24:baa43caa2f3d | 160 | } |