
Example for Healthcare Kit
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* SDT-example-ble-uart-echo 00002 * 00003 * Copyright (c) 2018 Sigma Delta Technologies Inc. 00004 * 00005 * MIT License 00006 * 00007 * Permission is hereby granted, free of charge, to any person 00008 * obtaining a copy of this software and associated documentation 00009 * files (the "Software"), to deal in the Software without 00010 * restriction, including without limitation the rights to use, 00011 * copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the 00013 * Software is furnished to do so, subject to the following 00014 * conditions: 00015 * 00016 * The above copyright notice and this permission notice shall be 00017 * included in all copies or substantial portions of the Software. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00020 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00021 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00022 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00023 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00024 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00025 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00026 * OTHER DEALINGS IN THE SOFTWARE. 00027 */ 00028 00029 #include "mbed.h" 00030 #include "features/FEATURE_BLE/ble/BLE.h" 00031 #include "features/FEATURE_BLE/ble/services/UARTService.h" 00032 00033 /* Serial */ 00034 #define BAUDRATE 9600 00035 Serial g_Serial_pc(USBTX, USBRX, BAUDRATE); 00036 00037 /* DigitalOut */ 00038 #define LED_ON 0 00039 #define LED_OFF 1 00040 DigitalOut g_DO_LedRed(LED_RED, LED_OFF); 00041 DigitalOut g_DO_LedGreen(LED_GREEN, LED_OFF); 00042 DigitalOut g_DO_LedBlue(LED_BLUE, LED_OFF); 00043 DigitalOut* g_pDO_Led = &g_DO_LedBlue; 00044 00045 /* Ticker */ 00046 Ticker g_Ticker; 00047 00048 /* BLE */ 00049 #define BLE_DEVICE_NAME "SDT Device" 00050 BLE& g_pBle = BLE::Instance(); // you can't use this name, 'ble', because this name is already declared in UARTService.h 00051 00052 /* UART service */ 00053 UARTService* g_pUartService; 00054 00055 /* Variable */ 00056 bool g_b_BleConnect = false; 00057 00058 00059 00060 void callbackTicker(void) { 00061 g_Serial_pc.printf("LED Toggle\n"); 00062 // *g_pDO_Led = !(*g_pDO_Led); 00063 } 00064 00065 void setLED(char data) { 00066 if (data <= 0) { 00067 return; 00068 } 00069 00070 if (data == 'R' || data == 'r') { 00071 g_DO_LedRed = 0; g_DO_LedGreen = 1; g_DO_LedBlue = 1; 00072 } 00073 else if (data == 'G' || data == 'g') { 00074 g_DO_LedRed = 1; g_DO_LedGreen = 0; g_DO_LedBlue = 1; 00075 } 00076 else if (data == 'B' || data == 'b') { 00077 g_DO_LedRed = 1; g_DO_LedGreen = 1; g_DO_LedBlue = 0; 00078 } 00079 else { 00080 g_DO_LedRed = 1; g_DO_LedGreen = 1; g_DO_LedBlue = 1; 00081 } 00082 } 00083 00084 void callbackBleDataWritten(const GattWriteCallbackParams* params) { 00085 if ((g_pUartService != NULL) && (params->handle == g_pUartService->getTXCharacteristicHandle())) { 00086 uint16_t bytesRead = params->len; 00087 const uint8_t* pBleRxBuf = params->data; 00088 g_Serial_pc.printf("data from BLE: %s\r\n", pBleRxBuf); 00089 00090 g_pBle.gattServer().write(g_pUartService->getRXCharacteristicHandle(), pBleRxBuf, bytesRead); 00091 setLED(pBleRxBuf[0]); 00092 } 00093 } 00094 00095 void callbackBleConnection(const Gap::ConnectionCallbackParams_t* params) { 00096 g_Serial_pc.printf("Connected!\n"); 00097 g_b_BleConnect = true; 00098 *g_pDO_Led = LED_OFF; 00099 g_pDO_Led = &g_DO_LedGreen; 00100 } 00101 00102 void callbackBleDisconnection(const Gap::DisconnectionCallbackParams_t* params) { 00103 g_Serial_pc.printf("Disconnected!\n"); 00104 g_Serial_pc.printf("Restarting the advertising process\n\r"); 00105 g_b_BleConnect = false; 00106 *g_pDO_Led = LED_OFF; 00107 g_pDO_Led = &g_DO_LedBlue; 00108 g_pBle.gap().startAdvertising(); 00109 } 00110 00111 void callbackBleInitComplete(BLE::InitializationCompleteCallbackContext* params) { 00112 BLE& ble = params->ble; // 'ble' equals g_pBle declared in global 00113 ble_error_t error = params->error; // 'error' has BLE_ERROR_NONE if the initialization procedure started successfully. 00114 00115 if (error == BLE_ERROR_NONE) { 00116 g_Serial_pc.printf("Initialization completed successfully\n"); 00117 } 00118 else { 00119 /* In case of error, forward the error handling to onBleInitError */ 00120 g_Serial_pc.printf("Initialization failled\n"); 00121 return; 00122 } 00123 00124 /* Ensure that it is the default instance of BLE */ 00125 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00126 g_Serial_pc.printf("ID of BLE instance is not DEFAULT_INSTANCE\n"); 00127 return; 00128 } 00129 00130 /* Setup UARTService */ 00131 g_pUartService = new UARTService(ble); 00132 00133 /* Setup and start advertising */ 00134 ble.gattServer().onDataWritten(callbackBleDataWritten); 00135 ble.gap().onConnection(callbackBleConnection); 00136 ble.gap().onDisconnection(callbackBleDisconnection); 00137 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00138 ble.gap().setAdvertisingInterval(1000); // Advertising interval in units of milliseconds 00139 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00140 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)BLE_DEVICE_NAME, sizeof(BLE_DEVICE_NAME) - 1); 00141 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); 00142 ble.gap().startAdvertising(); 00143 g_Serial_pc.printf("Start advertising\n"); 00144 } 00145 00146 int main(void) { 00147 g_Serial_pc.printf("< Sigma Delta Technologies Inc. >\n\r"); 00148 00149 /* Init BLE */ 00150 // g_pBle.onEventsToProcess(callbackEventsToProcess); 00151 g_pBle.init(callbackBleInitComplete); 00152 00153 /* Check whether IC is running or not */ 00154 g_Ticker.attach(callbackTicker, 1); 00155 00156 while (true) { 00157 g_pBle.waitForEvent(); 00158 } 00159 00160 return 0; 00161 }
Generated on Tue Jul 12 2022 19:38:11 by
