This is a simple UART chat that will be used to read sensors in Sand Molds.
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat by
main.cpp@4:378d43fa5d20, 2017-01-14 (annotated)
- Committer:
- emacdonald
- Date:
- Sat Jan 14 18:06:41 2017 +0000
- Revision:
- 4:378d43fa5d20
- Parent:
- 3:b3f6c612b603
Ben, here is a simple terminal chat progam for our YSU project.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 1:1c058e553423 | 1 | |
RedBearLab | 1:1c058e553423 | 2 | /* |
RedBearLab | 1:1c058e553423 | 3 | * The application works with the BLEController iOS/Android App. |
RedBearLab | 1:1c058e553423 | 4 | * Type something from the Terminal to send |
RedBearLab | 1:1c058e553423 | 5 | * to the BLEController App or vice verse. |
RedBearLab | 1:1c058e553423 | 6 | * Characteristics received from App will print on Terminal. |
RedBearLab | 1:1c058e553423 | 7 | */ |
RedBearLab | 1:1c058e553423 | 8 | |
RedBearLab | 0:cffe8ac1bdf0 | 9 | #include "mbed.h" |
RedBearLab | 2:4b66b69c7ecb | 10 | #include "ble/BLE.h" |
RedBearLab | 0:cffe8ac1bdf0 | 11 | |
RedBearLab | 0:cffe8ac1bdf0 | 12 | |
RedBearLab | 0:cffe8ac1bdf0 | 13 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:cffe8ac1bdf0 | 14 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 15 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 16 | |
RedBearLab | 0:cffe8ac1bdf0 | 17 | #define TXRX_BUF_LEN 20 |
RedBearLab | 0:cffe8ac1bdf0 | 18 | |
RedBearLab | 2:4b66b69c7ecb | 19 | BLE ble; |
emacdonald | 4:378d43fa5d20 | 20 | DigitalOut led1(LED1); |
RedBearLab | 0:cffe8ac1bdf0 | 21 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:cffe8ac1bdf0 | 22 | |
RedBearLab | 0:cffe8ac1bdf0 | 23 | |
RedBearLab | 0:cffe8ac1bdf0 | 24 | // The Nordic UART Service |
RedBearLab | 0:cffe8ac1bdf0 | 25 | static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 26 | static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 27 | static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 28 | static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; |
RedBearLab | 0:cffe8ac1bdf0 | 29 | |
RedBearLab | 0:cffe8ac1bdf0 | 30 | |
RedBearLab | 0:cffe8ac1bdf0 | 31 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 32 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 33 | |
RedBearLab | 0:cffe8ac1bdf0 | 34 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 35 | static uint8_t rx_len=0; |
RedBearLab | 0:cffe8ac1bdf0 | 36 | |
RedBearLab | 0:cffe8ac1bdf0 | 37 | |
RedBearLab | 0:cffe8ac1bdf0 | 38 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
RedBearLab | 0:cffe8ac1bdf0 | 39 | |
RedBearLab | 0:cffe8ac1bdf0 | 40 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
RedBearLab | 0:cffe8ac1bdf0 | 41 | |
RedBearLab | 0:cffe8ac1bdf0 | 42 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
RedBearLab | 0:cffe8ac1bdf0 | 43 | |
RedBearLab | 0:cffe8ac1bdf0 | 44 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:cffe8ac1bdf0 | 45 | |
RedBearLab | 0:cffe8ac1bdf0 | 46 | |
RedBearLab | 0:cffe8ac1bdf0 | 47 | |
RedBearLab | 3:b3f6c612b603 | 48 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
RedBearLab | 0:cffe8ac1bdf0 | 49 | { |
RedBearLab | 0:cffe8ac1bdf0 | 50 | pc.printf("Disconnected \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 51 | pc.printf("Restart advertising \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 52 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 53 | } |
RedBearLab | 0:cffe8ac1bdf0 | 54 | |
emacdonald | 4:378d43fa5d20 | 55 | // Serial Write from BLE Read |
RedBearLab | 2:4b66b69c7ecb | 56 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
RedBearLab | 0:cffe8ac1bdf0 | 57 | { |
RedBearLab | 0:cffe8ac1bdf0 | 58 | uint8_t buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 59 | uint16_t bytesRead, index; |
RedBearLab | 0:cffe8ac1bdf0 | 60 | |
RedBearLab | 2:4b66b69c7ecb | 61 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) |
RedBearLab | 0:cffe8ac1bdf0 | 62 | { |
emacdonald | 4:378d43fa5d20 | 63 | led1 = !led1; |
RedBearLab | 0:cffe8ac1bdf0 | 64 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 65 | memset(txPayload, 0, TXRX_BUF_LEN); |
RedBearLab | 0:cffe8ac1bdf0 | 66 | memcpy(txPayload, buf, TXRX_BUF_LEN); |
RedBearLab | 0:cffe8ac1bdf0 | 67 | pc.printf("WriteHandler \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 68 | pc.printf("Length: "); |
RedBearLab | 0:cffe8ac1bdf0 | 69 | pc.putc(bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 70 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 71 | pc.printf("Data: "); |
RedBearLab | 0:cffe8ac1bdf0 | 72 | for(index=0; index<bytesRead; index++) |
RedBearLab | 0:cffe8ac1bdf0 | 73 | { |
RedBearLab | 0:cffe8ac1bdf0 | 74 | pc.putc(txPayload[index]); |
RedBearLab | 0:cffe8ac1bdf0 | 75 | } |
RedBearLab | 0:cffe8ac1bdf0 | 76 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 77 | } |
RedBearLab | 0:cffe8ac1bdf0 | 78 | } |
RedBearLab | 0:cffe8ac1bdf0 | 79 | |
RedBearLab | 0:cffe8ac1bdf0 | 80 | void uartCB(void) |
RedBearLab | 0:cffe8ac1bdf0 | 81 | { |
RedBearLab | 0:cffe8ac1bdf0 | 82 | while(pc.readable()) |
RedBearLab | 0:cffe8ac1bdf0 | 83 | { |
emacdonald | 4:378d43fa5d20 | 84 | rx_buf[rx_len++] = pc.getc(); |
emacdonald | 4:378d43fa5d20 | 85 | led1 = !led1; |
RedBearLab | 0:cffe8ac1bdf0 | 86 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') |
RedBearLab | 0:cffe8ac1bdf0 | 87 | { |
RedBearLab | 0:cffe8ac1bdf0 | 88 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
RedBearLab | 0:cffe8ac1bdf0 | 89 | pc.printf("RecHandler \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 90 | pc.printf("Length: "); |
RedBearLab | 0:cffe8ac1bdf0 | 91 | pc.putc(rx_len); |
RedBearLab | 0:cffe8ac1bdf0 | 92 | pc.printf("\r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 93 | rx_len = 0; |
RedBearLab | 0:cffe8ac1bdf0 | 94 | break; |
RedBearLab | 0:cffe8ac1bdf0 | 95 | } |
RedBearLab | 0:cffe8ac1bdf0 | 96 | } |
RedBearLab | 0:cffe8ac1bdf0 | 97 | } |
RedBearLab | 0:cffe8ac1bdf0 | 98 | |
emacdonald | 4:378d43fa5d20 | 99 | void periodicCallback(void) |
emacdonald | 4:378d43fa5d20 | 100 | { |
emacdonald | 4:378d43fa5d20 | 101 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
emacdonald | 4:378d43fa5d20 | 102 | |
emacdonald | 4:378d43fa5d20 | 103 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
emacdonald | 4:378d43fa5d20 | 104 | * heavy-weight sensor polling from the main thread. */ |
emacdonald | 4:378d43fa5d20 | 105 | } |
emacdonald | 4:378d43fa5d20 | 106 | |
RedBearLab | 0:cffe8ac1bdf0 | 107 | int main(void) |
RedBearLab | 0:cffe8ac1bdf0 | 108 | { |
emacdonald | 4:378d43fa5d20 | 109 | led1 = 1; |
emacdonald | 4:378d43fa5d20 | 110 | Ticker ticker; |
emacdonald | 4:378d43fa5d20 | 111 | ticker.attach(periodicCallback, 5.0); // blink LED every second |
RedBearLab | 0:cffe8ac1bdf0 | 112 | ble.init(); |
RedBearLab | 0:cffe8ac1bdf0 | 113 | ble.onDisconnection(disconnectionCallback); |
RedBearLab | 0:cffe8ac1bdf0 | 114 | ble.onDataWritten(WrittenHandler); |
RedBearLab | 0:cffe8ac1bdf0 | 115 | |
RedBearLab | 0:cffe8ac1bdf0 | 116 | pc.baud(9600); |
RedBearLab | 0:cffe8ac1bdf0 | 117 | pc.printf("SimpleChat Init \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 118 | |
RedBearLab | 0:cffe8ac1bdf0 | 119 | pc.attach( uartCB , pc.RxIrq); |
RedBearLab | 0:cffe8ac1bdf0 | 120 | // setup advertising |
RedBearLab | 0:cffe8ac1bdf0 | 121 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:cffe8ac1bdf0 | 122 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:cffe8ac1bdf0 | 123 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
emacdonald | 4:378d43fa5d20 | 124 | (const uint8_t *)"Bennett", sizeof("Bennett") - 1); |
RedBearLab | 0:cffe8ac1bdf0 | 125 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
RedBearLab | 0:cffe8ac1bdf0 | 126 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
RedBearLab | 0:cffe8ac1bdf0 | 127 | // 100ms; in multiples of 0.625ms. |
RedBearLab | 0:cffe8ac1bdf0 | 128 | ble.setAdvertisingInterval(160); |
RedBearLab | 0:cffe8ac1bdf0 | 129 | |
RedBearLab | 0:cffe8ac1bdf0 | 130 | ble.addService(uartService); |
RedBearLab | 0:cffe8ac1bdf0 | 131 | |
RedBearLab | 0:cffe8ac1bdf0 | 132 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 133 | pc.printf("Advertising Start \r\n"); |
RedBearLab | 0:cffe8ac1bdf0 | 134 | |
RedBearLab | 0:cffe8ac1bdf0 | 135 | while(1) |
RedBearLab | 0:cffe8ac1bdf0 | 136 | { |
RedBearLab | 0:cffe8ac1bdf0 | 137 | ble.waitForEvent(); |
RedBearLab | 0:cffe8ac1bdf0 | 138 | } |
RedBearLab | 0:cffe8ac1bdf0 | 139 | } |
RedBearLab | 0:cffe8ac1bdf0 | 140 | |
RedBearLab | 0:cffe8ac1bdf0 | 141 | |
RedBearLab | 0:cffe8ac1bdf0 | 142 | |
RedBearLab | 0:cffe8ac1bdf0 | 143 | |
RedBearLab | 0:cffe8ac1bdf0 | 144 | |
RedBearLab | 0:cffe8ac1bdf0 | 145 | |
RedBearLab | 0:cffe8ac1bdf0 | 146 | |
RedBearLab | 0:cffe8ac1bdf0 | 147 | |
RedBearLab | 0:cffe8ac1bdf0 | 148 | |
RedBearLab | 0:cffe8ac1bdf0 | 149 | |
RedBearLab | 0:cffe8ac1bdf0 | 150 | |
RedBearLab | 0:cffe8ac1bdf0 | 151 | |
RedBearLab | 0:cffe8ac1bdf0 | 152 | |
RedBearLab | 0:cffe8ac1bdf0 | 153 | |
RedBearLab | 0:cffe8ac1bdf0 | 154 | |
RedBearLab | 0:cffe8ac1bdf0 | 155 |