a fork to test out advertising process
Dependencies: BLE_API TxIR mbed nRF51822
Fork of ir-puck by
main.cpp@1:6ba27220d1da, 2014-07-08 (annotated)
- Committer:
- stiaje
- Date:
- Tue Jul 08 09:38:31 2014 +0000
- Revision:
- 1:6ba27220d1da
- Parent:
- 0:c94311378ec1
Rewrite all the things!; ; printing and advertising mostly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sigveseb | 0:c94311378ec1 | 1 | #include "mbed.h" |
sigveseb | 0:c94311378ec1 | 2 | #include "BLEDevice.h" |
sigveseb | 0:c94311378ec1 | 3 | #include "IR.h" |
sigveseb | 0:c94311378ec1 | 4 | #include "nRF51822n.h" |
sigveseb | 0:c94311378ec1 | 5 | |
sigveseb | 0:c94311378ec1 | 6 | BLEDevice ble; |
sigveseb | 0:c94311378ec1 | 7 | |
sigveseb | 0:c94311378ec1 | 8 | DigitalOut myled(LED1); |
sigveseb | 0:c94311378ec1 | 9 | DigitalOut yourled(LED2); |
sigveseb | 0:c94311378ec1 | 10 | |
stiaje | 1:6ba27220d1da | 11 | #define DEBUG 1 |
stiaje | 1:6ba27220d1da | 12 | #ifdef DEBUG |
stiaje | 1:6ba27220d1da | 13 | Serial py(USBTX, USBRX); |
stiaje | 1:6ba27220d1da | 14 | #define LOG(args...) py.printf(args) |
stiaje | 1:6ba27220d1da | 15 | #else |
stiaje | 1:6ba27220d1da | 16 | #define LOG(args...) |
stiaje | 1:6ba27220d1da | 17 | #endif |
sigveseb | 0:c94311378ec1 | 18 | |
sigveseb | 0:c94311378ec1 | 19 | const static uint8_t beaconPayload[] = { |
sigveseb | 0:c94311378ec1 | 20 | 0x00, 0x4C, // Company identifier code (0x004C == Apple) |
sigveseb | 0:c94311378ec1 | 21 | 0x02, // ID |
sigveseb | 0:c94311378ec1 | 22 | 0x15, // length of the remaining payload |
sigveseb | 0:c94311378ec1 | 23 | 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // UUID |
sigveseb | 0:c94311378ec1 | 24 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, |
sigveseb | 0:c94311378ec1 | 25 | 0x13, 0x37, // the major value to differenciate a location |
stiaje | 1:6ba27220d1da | 26 | 0xBA, 0xCE, // the minor value to differenciate a location |
sigveseb | 0:c94311378ec1 | 27 | 0xC8 // 2's complement of the Tx power (-56dB) |
sigveseb | 0:c94311378ec1 | 28 | }; |
sigveseb | 0:c94311378ec1 | 29 | |
sigveseb | 0:c94311378ec1 | 30 | extern GattService ir_service; |
sigveseb | 0:c94311378ec1 | 31 | extern GattCharacteristic header, one, zero, ptrail, predata, code; |
sigveseb | 0:c94311378ec1 | 32 | |
sigveseb | 0:c94311378ec1 | 33 | void onDataWritten(uint16_t handle) |
sigveseb | 0:c94311378ec1 | 34 | { |
stiaje | 1:6ba27220d1da | 35 | LOG("Data written! %i\n", handle); |
sigveseb | 0:c94311378ec1 | 36 | for (int i = 0; i < ir_service.getCharacteristicCount(); i++) { |
sigveseb | 0:c94311378ec1 | 37 | GattCharacteristic* characteristic = ir_service.getCharacteristic(i); |
sigveseb | 0:c94311378ec1 | 38 | characteristic->getMaxLength(); |
sigveseb | 0:c94311378ec1 | 39 | if (characteristic->getHandle() == handle) { |
sigveseb | 0:c94311378ec1 | 40 | uint16_t max_length = characteristic->getMaxLength(); |
sigveseb | 0:c94311378ec1 | 41 | ble.readCharacteristicValue(handle, characteristic->getValuePtr(), &max_length); |
sigveseb | 0:c94311378ec1 | 42 | break; |
sigveseb | 0:c94311378ec1 | 43 | } |
sigveseb | 0:c94311378ec1 | 44 | } |
sigveseb | 0:c94311378ec1 | 45 | |
sigveseb | 0:c94311378ec1 | 46 | if (code.getHandle() == handle) { |
sigveseb | 0:c94311378ec1 | 47 | fireIRCode(header.getValuePtr(), one.getValuePtr(), zero.getValuePtr(), ptrail.getValuePtr(), predata.getValuePtr(), code.getValuePtr()); |
sigveseb | 0:c94311378ec1 | 48 | } |
sigveseb | 0:c94311378ec1 | 49 | } |
sigveseb | 0:c94311378ec1 | 50 | |
sigveseb | 0:c94311378ec1 | 51 | void disconnectionCallback(void) |
sigveseb | 0:c94311378ec1 | 52 | { |
stiaje | 1:6ba27220d1da | 53 | LOG("Disconnected!\n"); |
stiaje | 1:6ba27220d1da | 54 | LOG("Restarting the advertising process\n"); |
sigveseb | 0:c94311378ec1 | 55 | ble.startAdvertising(); |
sigveseb | 0:c94311378ec1 | 56 | } |
sigveseb | 0:c94311378ec1 | 57 | |
sigveseb | 0:c94311378ec1 | 58 | void connectionCallback(void) |
sigveseb | 0:c94311378ec1 | 59 | { |
stiaje | 1:6ba27220d1da | 60 | LOG("Connected!\n"); |
sigveseb | 0:c94311378ec1 | 61 | } |
sigveseb | 0:c94311378ec1 | 62 | |
sigveseb | 0:c94311378ec1 | 63 | void onDataSent(uint16_t data) |
sigveseb | 0:c94311378ec1 | 64 | { |
stiaje | 1:6ba27220d1da | 65 | LOG("onDataSent!\n"); |
sigveseb | 0:c94311378ec1 | 66 | } |
sigveseb | 0:c94311378ec1 | 67 | |
sigveseb | 0:c94311378ec1 | 68 | int main() { |
sigveseb | 0:c94311378ec1 | 69 | ble.init(); |
sigveseb | 0:c94311378ec1 | 70 | ble.onConnection(connectionCallback); |
sigveseb | 0:c94311378ec1 | 71 | ble.onDisconnection(disconnectionCallback); |
sigveseb | 0:c94311378ec1 | 72 | ble.onDataWritten(onDataWritten); |
sigveseb | 0:c94311378ec1 | 73 | ble.onDataSent(onDataSent); |
sigveseb | 0:c94311378ec1 | 74 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
sigveseb | 0:c94311378ec1 | 75 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
sigveseb | 0:c94311378ec1 | 76 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
sigveseb | 0:c94311378ec1 | 77 | |
stiaje | 1:6ba27220d1da | 78 | uint8_t uuid_array_service[16] = {'b', 'f', 't', 'j', ' ', 'i', 'r', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; |
stiaje | 1:6ba27220d1da | 79 | ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, |
stiaje | 1:6ba27220d1da | 80 | uuid_array_service, sizeof(uuid_array_service)); |
sigveseb | 0:c94311378ec1 | 81 | |
sigveseb | 0:c94311378ec1 | 82 | ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, |
sigveseb | 0:c94311378ec1 | 83 | beaconPayload, sizeof(beaconPayload)); |
sigveseb | 0:c94311378ec1 | 84 | |
sigveseb | 0:c94311378ec1 | 85 | ble.startAdvertising(); |
stiaje | 1:6ba27220d1da | 86 | |
sigveseb | 0:c94311378ec1 | 87 | ble.addService(ir_service); |
sigveseb | 0:c94311378ec1 | 88 | |
sigveseb | 0:c94311378ec1 | 89 | myled = 1; |
sigveseb | 0:c94311378ec1 | 90 | |
stiaje | 1:6ba27220d1da | 91 | LOG("Starting up.\n"); |
sigveseb | 0:c94311378ec1 | 92 | |
sigveseb | 0:c94311378ec1 | 93 | |
sigveseb | 0:c94311378ec1 | 94 | |
sigveseb | 0:c94311378ec1 | 95 | while (true) { |
sigveseb | 0:c94311378ec1 | 96 | ble.waitForEvent(); |
sigveseb | 0:c94311378ec1 | 97 | myled = !myled; |
sigveseb | 0:c94311378ec1 | 98 | } |
sigveseb | 0:c94311378ec1 | 99 | } |