EEP fORK
Dependencies: BLE_API mbed nRF51822
Fork of MCS_LRF by
bleHelper.cpp@7:8a23a257b66a, 2015-12-09 (annotated)
- Committer:
- Farshad
- Date:
- Wed Dec 09 03:28:42 2015 +0000
- Revision:
- 7:8a23a257b66a
- Child:
- 9:afd6bd6e88bd
First cut. Testing works with single measurement averaging 10 readings.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Farshad | 7:8a23a257b66a | 1 | |
Farshad | 7:8a23a257b66a | 2 | #include "bleHelper.h" |
Farshad | 7:8a23a257b66a | 3 | |
Farshad | 7:8a23a257b66a | 4 | BLEHelper::BLEHelper(BLEDevice* ble, UARTService* UuartServicePtr) |
Farshad | 7:8a23a257b66a | 5 | { |
Farshad | 7:8a23a257b66a | 6 | blePtr = ble; |
Farshad | 7:8a23a257b66a | 7 | uartServicePtr = UuartServicePtr; |
Farshad | 7:8a23a257b66a | 8 | } |
Farshad | 7:8a23a257b66a | 9 | |
Farshad | 7:8a23a257b66a | 10 | |
Farshad | 7:8a23a257b66a | 11 | void BLEHelper::sendOverBLE(uint16_t cmd, uint8_t data[], uint16_t len, PacketStatus_e status) |
Farshad | 7:8a23a257b66a | 12 | { |
Farshad | 7:8a23a257b66a | 13 | |
Farshad | 7:8a23a257b66a | 14 | if(len > 18) { |
Farshad | 7:8a23a257b66a | 15 | // this is a problem |
Farshad | 7:8a23a257b66a | 16 | } |
Farshad | 7:8a23a257b66a | 17 | |
Farshad | 7:8a23a257b66a | 18 | uint8_t buf[len + 2]; // should set it to the max single packet size |
Farshad | 7:8a23a257b66a | 19 | uint8_t offset = 0; |
Farshad | 7:8a23a257b66a | 20 | uint16_t tmp = cmd | (uint16_t)status; |
Farshad | 7:8a23a257b66a | 21 | memcpy(buf, &tmp, sizeof(uint16_t)); // command |
Farshad | 7:8a23a257b66a | 22 | offset+=sizeof(uint16_t); |
Farshad | 7:8a23a257b66a | 23 | // memcpy(&buf[offset], &len, sizeof(uint16_t)); // length of array |
Farshad | 7:8a23a257b66a | 24 | // offset+=sizeof(uint16_t); |
Farshad | 7:8a23a257b66a | 25 | memcpy(&buf[offset], &data[0], len); // data |
Farshad | 7:8a23a257b66a | 26 | offset+= len; |
Farshad | 7:8a23a257b66a | 27 | blePtr->updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), buf, offset); |
Farshad | 7:8a23a257b66a | 28 | } |
Farshad | 7:8a23a257b66a | 29 | |
Farshad | 7:8a23a257b66a | 30 | void BLEHelper::sendPacketOverBLE(uint16_t cmd, uint8_t data[], uint16_t len) |
Farshad | 7:8a23a257b66a | 31 | { |
Farshad | 7:8a23a257b66a | 32 | uint8_t nPackets = (len / 19) + 1; |
Farshad | 7:8a23a257b66a | 33 | uint8_t remainingPackets = nPackets; |
Farshad | 7:8a23a257b66a | 34 | uint16_t offset = 0; |
Farshad | 7:8a23a257b66a | 35 | bool first = true; |
Farshad | 7:8a23a257b66a | 36 | const uint8_t maxDataPayload = 18; // max is 20 but we are sending a command of 2 bytes always |
Farshad | 7:8a23a257b66a | 37 | |
Farshad | 7:8a23a257b66a | 38 | do { |
Farshad | 7:8a23a257b66a | 39 | if(nPackets == 1) { |
Farshad | 7:8a23a257b66a | 40 | // first and only |
Farshad | 7:8a23a257b66a | 41 | packetStatus = PS_FIRST_AND_ONLY; |
Farshad | 7:8a23a257b66a | 42 | sendOverBLE(cmd, &data[offset], len, packetStatus); |
Farshad | 7:8a23a257b66a | 43 | remainingPackets = 0; |
Farshad | 7:8a23a257b66a | 44 | } else if(remainingPackets == 1) { |
Farshad | 7:8a23a257b66a | 45 | // last one |
Farshad | 7:8a23a257b66a | 46 | packetStatus = PS_LAST; |
Farshad | 7:8a23a257b66a | 47 | sendOverBLE(cmd, &data[offset], len-offset, packetStatus); |
Farshad | 7:8a23a257b66a | 48 | remainingPackets = 0; |
Farshad | 7:8a23a257b66a | 49 | } else if(remainingPackets > 1 && first == true) { |
Farshad | 7:8a23a257b66a | 50 | // first and more to come |
Farshad | 7:8a23a257b66a | 51 | packetStatus = PS_FIRST_AND_NOT_LAST; |
Farshad | 7:8a23a257b66a | 52 | sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); |
Farshad | 7:8a23a257b66a | 53 | offset += maxDataPayload; |
Farshad | 7:8a23a257b66a | 54 | remainingPackets -= 1; |
Farshad | 7:8a23a257b66a | 55 | first = false; |
Farshad | 7:8a23a257b66a | 56 | } else if(remainingPackets > 1 && first == false) { |
Farshad | 7:8a23a257b66a | 57 | // middle |
Farshad | 7:8a23a257b66a | 58 | packetStatus = PS_MIDDLE; |
Farshad | 7:8a23a257b66a | 59 | sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); |
Farshad | 7:8a23a257b66a | 60 | offset += maxDataPayload; |
Farshad | 7:8a23a257b66a | 61 | remainingPackets -= 1; |
Farshad | 7:8a23a257b66a | 62 | } |
Farshad | 7:8a23a257b66a | 63 | |
Farshad | 7:8a23a257b66a | 64 | wait_ms(50); // 40ms delay seems to be a limit |
Farshad | 7:8a23a257b66a | 65 | } while (remainingPackets > 0); |
Farshad | 7:8a23a257b66a | 66 | } |
Farshad | 7:8a23a257b66a | 67 |