SDT Inc.
/
SDT-example-ble-uart-echo
Example for Healthcare Kit
main.cpp@1:41735bbc31af, 2018-10-12 (annotated)
- Committer:
- hyungo
- Date:
- Fri Oct 12 09:01:09 2018 +0000
- Revision:
- 1:41735bbc31af
- Parent:
- 0:5ed42babfd71
Need to update "readme"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 1 | /* SDT-example-ble-uart-echo |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 2 | * |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 3 | * Copyright (c) 2018 Sigma Delta Technologies Inc. |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 4 | * |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 5 | * MIT License |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 6 | * |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 7 | * Permission is hereby granted, free of charge, to any person |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 8 | * obtaining a copy of this software and associated documentation |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 9 | * files (the "Software"), to deal in the Software without |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 10 | * restriction, including without limitation the rights to use, |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 11 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 12 | * copies of the Software, and to permit persons to whom the |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 13 | * Software is furnished to do so, subject to the following |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 14 | * conditions: |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 15 | * |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 16 | * The above copyright notice and this permission notice shall be |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 17 | * included in all copies or substantial portions of the Software. |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 18 | * |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 27 | */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 28 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 29 | #include "mbed.h" |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 30 | #include "features/FEATURE_BLE/ble/BLE.h" |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 31 | #include "features/FEATURE_BLE/ble/services/UARTService.h" |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 32 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 33 | /* Serial */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 34 | #define BAUDRATE 9600 |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 35 | Serial g_Serial_pc(USBTX, USBRX, BAUDRATE); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 36 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 37 | /* DigitalOut */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 38 | #define LED_ON 0 |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 39 | #define LED_OFF 1 |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 40 | DigitalOut g_DO_LedRed(LED_RED, LED_OFF); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 41 | DigitalOut g_DO_LedGreen(LED_GREEN, LED_OFF); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 42 | DigitalOut g_DO_LedBlue(LED_BLUE, LED_OFF); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 43 | DigitalOut* g_pDO_Led = &g_DO_LedBlue; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 44 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 45 | /* Ticker */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 46 | Ticker g_Ticker; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 47 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 48 | /* BLE */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 49 | #define BLE_DEVICE_NAME "SDT Device" |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 50 | BLE& g_pBle = BLE::Instance(); // you can't use this name, 'ble', because this name is already declared in UARTService.h |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 51 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 52 | /* UART service */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 53 | UARTService* g_pUartService; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 54 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 55 | /* Variable */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 56 | bool g_b_BleConnect = false; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 57 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 58 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 59 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 60 | void callbackTicker(void) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 61 | g_Serial_pc.printf("LED Toggle\n"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 62 | // *g_pDO_Led = !(*g_pDO_Led); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 63 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 64 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 65 | void setLED(char data) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 66 | if (data <= 0) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 67 | return; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 68 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 69 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 70 | if (data == 'R' || data == 'r') { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 71 | g_DO_LedRed = 0; g_DO_LedGreen = 1; g_DO_LedBlue = 1; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 72 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 73 | else if (data == 'G' || data == 'g') { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 74 | g_DO_LedRed = 1; g_DO_LedGreen = 0; g_DO_LedBlue = 1; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 75 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 76 | else if (data == 'B' || data == 'b') { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 77 | g_DO_LedRed = 1; g_DO_LedGreen = 1; g_DO_LedBlue = 0; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 78 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 79 | else { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 80 | g_DO_LedRed = 1; g_DO_LedGreen = 1; g_DO_LedBlue = 1; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 81 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 82 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 83 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 84 | void callbackBleDataWritten(const GattWriteCallbackParams* params) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 85 | if ((g_pUartService != NULL) && (params->handle == g_pUartService->getTXCharacteristicHandle())) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 86 | uint16_t bytesRead = params->len; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 87 | const uint8_t* pBleRxBuf = params->data; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 88 | g_Serial_pc.printf("data from BLE: %s\r\n", pBleRxBuf); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 89 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 90 | g_pBle.gattServer().write(g_pUartService->getRXCharacteristicHandle(), pBleRxBuf, bytesRead); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 91 | setLED(pBleRxBuf[0]); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 92 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 93 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 94 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 95 | void callbackBleConnection(const Gap::ConnectionCallbackParams_t* params) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 96 | g_Serial_pc.printf("Connected!\n"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 97 | g_b_BleConnect = true; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 98 | *g_pDO_Led = LED_OFF; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 99 | g_pDO_Led = &g_DO_LedGreen; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 100 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 101 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 102 | void callbackBleDisconnection(const Gap::DisconnectionCallbackParams_t* params) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 103 | g_Serial_pc.printf("Disconnected!\n"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 104 | g_Serial_pc.printf("Restarting the advertising process\n\r"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 105 | g_b_BleConnect = false; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 106 | *g_pDO_Led = LED_OFF; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 107 | g_pDO_Led = &g_DO_LedBlue; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 108 | g_pBle.gap().startAdvertising(); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 109 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 110 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 111 | void callbackBleInitComplete(BLE::InitializationCompleteCallbackContext* params) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 112 | BLE& ble = params->ble; // 'ble' equals g_pBle declared in global |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 113 | ble_error_t error = params->error; // 'error' has BLE_ERROR_NONE if the initialization procedure started successfully. |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 114 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 115 | if (error == BLE_ERROR_NONE) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 116 | g_Serial_pc.printf("Initialization completed successfully\n"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 117 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 118 | else { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 119 | /* In case of error, forward the error handling to onBleInitError */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 120 | g_Serial_pc.printf("Initialization failled\n"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 121 | return; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 122 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 123 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 124 | /* Ensure that it is the default instance of BLE */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 125 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 126 | g_Serial_pc.printf("ID of BLE instance is not DEFAULT_INSTANCE\n"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 127 | return; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 128 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 129 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 130 | /* Setup UARTService */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 131 | g_pUartService = new UARTService(ble); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 132 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 133 | /* Setup and start advertising */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 134 | ble.gattServer().onDataWritten(callbackBleDataWritten); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 135 | ble.gap().onConnection(callbackBleConnection); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 136 | ble.gap().onDisconnection(callbackBleDisconnection); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 137 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 138 | ble.gap().setAdvertisingInterval(1000); // Advertising interval in units of milliseconds |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 139 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 140 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)BLE_DEVICE_NAME, sizeof(BLE_DEVICE_NAME) - 1); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 141 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 142 | ble.gap().startAdvertising(); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 143 | g_Serial_pc.printf("Start advertising\n"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 144 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 145 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 146 | int main(void) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 147 | g_Serial_pc.printf("< Sigma Delta Technologies Inc. >\n\r"); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 148 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 149 | /* Init BLE */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 150 | // g_pBle.onEventsToProcess(callbackEventsToProcess); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 151 | g_pBle.init(callbackBleInitComplete); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 152 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 153 | /* Check whether IC is running or not */ |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 154 | g_Ticker.attach(callbackTicker, 1); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 155 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 156 | while (true) { |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 157 | g_pBle.waitForEvent(); |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 158 | } |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 159 | |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 160 | return 0; |
group-Sigma-Delta-Technologies | 0:5ed42babfd71 | 161 | } |