BLE to Digi-Dot-Booster bridge. Send commands to DD-Booster over Bluetooth using BLE Nordic UART service
Dependencies: BLE_API DD-Booster-mbed mbed nRF51822
main.cpp@1:841b99be017c, 2017-03-06 (annotated)
- Committer:
- Gamadril
- Date:
- Mon Mar 06 14:39:55 2017 +0000
- Revision:
- 1:841b99be017c
- Parent:
- 0:972386bde852
added license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Gamadril | 0:972386bde852 | 1 | #include "mbed.h" |
Gamadril | 0:972386bde852 | 2 | #include "BLE.h" |
Gamadril | 0:972386bde852 | 3 | #include "UARTService.h" |
Gamadril | 0:972386bde852 | 4 | #include "DDBooster.h" |
Gamadril | 0:972386bde852 | 5 | |
Gamadril | 0:972386bde852 | 6 | const static char DEVICE_NAME[] = "DD-Booster"; |
Gamadril | 0:972386bde852 | 7 | |
Gamadril | 0:972386bde852 | 8 | #define SPI_MOSI P0_3 |
Gamadril | 0:972386bde852 | 9 | #define SPI_SCK P0_4 |
Gamadril | 0:972386bde852 | 10 | #define SPI_CS P0_2 |
Gamadril | 0:972386bde852 | 11 | #define RESET P0_1 |
Gamadril | 0:972386bde852 | 12 | |
Gamadril | 0:972386bde852 | 13 | UARTService *uart = NULL; |
Gamadril | 0:972386bde852 | 14 | DDBooster booster(SPI_MOSI, SPI_SCK, SPI_CS, RESET); |
Gamadril | 0:972386bde852 | 15 | |
Gamadril | 0:972386bde852 | 16 | void onConnected(const Gap::ConnectionCallbackParams_t *params) |
Gamadril | 0:972386bde852 | 17 | { |
Gamadril | 0:972386bde852 | 18 | Gap::ConnectionParams_t fast; |
Gamadril | 0:972386bde852 | 19 | BLE &ble = BLE::Instance(); |
Gamadril | 0:972386bde852 | 20 | |
Gamadril | 0:972386bde852 | 21 | ble.getPreferredConnectionParams(&fast); |
Gamadril | 0:972386bde852 | 22 | fast.minConnectionInterval = 8; |
Gamadril | 0:972386bde852 | 23 | fast.maxConnectionInterval = 16; |
Gamadril | 0:972386bde852 | 24 | fast.slaveLatency = 0; |
Gamadril | 0:972386bde852 | 25 | ble.setPreferredConnectionParams(&fast); |
Gamadril | 0:972386bde852 | 26 | } |
Gamadril | 0:972386bde852 | 27 | |
Gamadril | 0:972386bde852 | 28 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Gamadril | 0:972386bde852 | 29 | { |
Gamadril | 0:972386bde852 | 30 | BLE &ble = BLE::Instance(); |
Gamadril | 0:972386bde852 | 31 | ble.startAdvertising(); |
Gamadril | 0:972386bde852 | 32 | } |
Gamadril | 0:972386bde852 | 33 | |
Gamadril | 0:972386bde852 | 34 | void onDataWritten(const GattWriteCallbackParams *params) |
Gamadril | 0:972386bde852 | 35 | { |
Gamadril | 0:972386bde852 | 36 | if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { |
Gamadril | 0:972386bde852 | 37 | booster.sendRawBytes(params->data, params->len); |
Gamadril | 0:972386bde852 | 38 | } |
Gamadril | 0:972386bde852 | 39 | } |
Gamadril | 0:972386bde852 | 40 | |
Gamadril | 0:972386bde852 | 41 | |
Gamadril | 0:972386bde852 | 42 | int main(void) |
Gamadril | 0:972386bde852 | 43 | { |
Gamadril | 0:972386bde852 | 44 | booster.reset(); |
Gamadril | 0:972386bde852 | 45 | |
Gamadril | 0:972386bde852 | 46 | BLE &ble = BLE::Instance(); |
Gamadril | 0:972386bde852 | 47 | ble.init(); |
Gamadril | 0:972386bde852 | 48 | ble.onConnection(onConnected); |
Gamadril | 0:972386bde852 | 49 | ble.onDisconnection(disconnectionCallback); |
Gamadril | 0:972386bde852 | 50 | ble.onDataWritten(onDataWritten); |
Gamadril | 0:972386bde852 | 51 | |
Gamadril | 0:972386bde852 | 52 | uart = new UARTService(ble); |
Gamadril | 0:972386bde852 | 53 | |
Gamadril | 0:972386bde852 | 54 | /* setup advertising */ |
Gamadril | 0:972386bde852 | 55 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Gamadril | 0:972386bde852 | 56 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Gamadril | 0:972386bde852 | 57 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
Gamadril | 0:972386bde852 | 58 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
Gamadril | 0:972386bde852 | 59 | |
Gamadril | 0:972386bde852 | 60 | ble.setAdvertisingInterval(500); |
Gamadril | 0:972386bde852 | 61 | ble.startAdvertising(); |
Gamadril | 0:972386bde852 | 62 | |
Gamadril | 0:972386bde852 | 63 | while (true) { |
Gamadril | 0:972386bde852 | 64 | ble.waitForEvent(); |
Gamadril | 0:972386bde852 | 65 | } |
Gamadril | 0:972386bde852 | 66 | } |