A basic demo where a message sent over a BLE UART gets displayed on a low-power eInk display.
Dependencies: BLE_API SharpLCD mbed nRF51822
Revision 4:0241d6beea9d, committed 2014-10-07
- Comitter:
- rgrover1
- Date:
- Tue Oct 07 09:11:57 2014 +0000
- Parent:
- 3:fc93699018c9
- Child:
- 5:96a3b298c4f9
- Commit message:
- updating to use the UARTService
Changed in this revision
--- a/BLE_API.lib Thu Aug 07 14:29:13 2014 +0000 +++ b/BLE_API.lib Tue Oct 07 09:11:57 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#189ff241dae1 +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#4b68a819ab4f
--- a/main.cpp Thu Aug 07 14:29:13 2014 +0000 +++ b/main.cpp Tue Oct 07 09:11:57 2014 +0000 @@ -16,64 +16,41 @@ #include "mbed.h" #include "BLEDevice.h" +#include "UARTService.h" + #include "SharpLCD.hpp" #include "font.h" -#define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */ -#define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ -#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ - -#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; - * it will have an impact on code-size and power consumption. */ - -#if NEED_CONSOLE_OUTPUT -Serial pc(USBTX, USBRX); -#define DEBUG(...) { pc.printf(__VA_ARGS__); } -#else -#define DEBUG(...) /* nothing */ -#endif /* #if NEED_CONSOLE_OUTPUT */ - BLEDevice ble; DigitalOut led1(LED1); -// The Nordic UART Service -const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5, 0x0e, 0x24, 0xdc, 0xca, 0x9e}; -const uint8_t uart_tx_uuid[] = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5, 0x0e, 0x24, 0xdc, 0xca, 0x9e}; -const uint8_t uart_rx_uuid[] = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5, 0x0e, 0x24, 0xdc, 0xca, 0x9e}; -const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e}; - bool rxPayloadUpdated = false; -uint8_t rxPayload[20] = {0,}; -uint8_t txPayload[20] = {0,}; -GattCharacteristic rxCharacteristic (uart_tx_uuid, rxPayload, 1, sizeof(rxPayload), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); -GattCharacteristic txCharacteristic (uart_rx_uuid, txPayload, 1, sizeof(txPayload), - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); -GattCharacteristic *uartChars[] = {&rxCharacteristic, &txCharacteristic}; -GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); - +const static unsigned MAX_SIZEOF_RX_PAYLOAD = 20; +char rxPayload[MAX_SIZEOF_RX_PAYLOAD] = {0,}; uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC]; -void disconnectionCallback(uint16_t handle) +UARTService *uartServicePtr; + +const char *deviceName = "lcdDemo"; +SharpLCD lcd(p0 /* display enable */, SPI_PSELSS0, SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0, NC); +SharpLCD::FrameBuffer fb(framebuffer); + +void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { - DEBUG("Disconnected!\n\r"); - DEBUG("Restarting the advertising process\n\r"); ble.startAdvertising(); } -void onDataWritten(uint16_t charHandle) +void onDataWritten(const GattCharacteristicWriteCBParams *params) { - if (charHandle == rxCharacteristic.getHandle()) { - DEBUG("onDataWritten()\n\r"); - uint16_t bytesRead; - ble.readCharacteristicValue(rxCharacteristic.getHandle(), rxPayload, &bytesRead); - if (bytesRead < sizeof(rxPayload)) { - rxPayload[bytesRead] = 0; + if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { + uint16_t bytesRead = params->len; + if (bytesRead < MAX_SIZEOF_RX_PAYLOAD) { + strncpy(rxPayload, (const char *)params->data, MAX_SIZEOF_RX_PAYLOAD - 1); + rxPayload[bytesRead] = '\0'; + rxPayloadUpdated = true; + ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); } - DEBUG("ECHO: %s\n\r", (char *)rxPayload); - ble.updateCharacteristicValue(txCharacteristic.getHandle(), rxPayload, bytesRead); - rxPayloadUpdated = true; } } @@ -88,7 +65,6 @@ Ticker ticker; ticker.attach(periodicCallback, 1); - DEBUG("Initialising the nRF51822\n\r"); ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(onDataWritten); @@ -96,18 +72,13 @@ /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); - ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); - ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); + ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName)); ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ ble.startAdvertising(); - ble.addService(uartService); - - SharpLCD lcd(p21 /* display enable */, SPI_PSELSS0, SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0, NC); - SharpLCD::FrameBuffer fb(framebuffer); + UARTService uartService(ble); + uartServicePtr = &uartService; lcd.enableDisplay(); lcd.clear();
--- a/mbed.bld Thu Aug 07 14:29:13 2014 +0000 +++ b/mbed.bld Tue Oct 07 09:11:57 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file
--- a/nRF51822.lib Thu Aug 07 14:29:13 2014 +0000 +++ b/nRF51822.lib Tue Oct 07 09:11:57 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#1e5c300cec7f +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#936d81c963fe