Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of UART_TEMPLATE by
main.cpp
00001 #include "mbed.h" 00002 #include "BLEDevice.h" 00003 #include "UARTService.h" 00004 #include "nrf_temp.h" 00005 00006 #define MAX_REPLY_LEN (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN) 00007 #define SENSOR_READ_INTERVAL_S (0.5F) 00008 #define ADV_INTERVAL_MS (1000UL) 00009 #define UART_BAUD_RATE (9600UL) 00010 #define DEVICE_NAME ("MobilMed Tartı") // This can be read AFTER connecting to the device. 00011 #define SHORT_NAME ("Tartı2") // Keep this short: max 8 chars if a 128bit UUID is also advertised. 00012 #define DEBUG(...) { m_serial_port.printf(__VA_ARGS__); } 00013 00014 00015 BLEDevice m_ble; 00016 Serial m_serial_port(P0_3, P0_4); // TX pin, RX pin 00017 DigitalOut m_cmd_led(P0_21); 00018 DigitalOut m_error_led(LED2); 00019 AnalogIn m_analog_in(P0_1); 00020 uint16_t m_analog_in_value; 00021 UARTService *m_uart_service_ptr; 00022 00023 00024 /** 00025 * This callback is used whenever a disconnection occurs. 00026 */ 00027 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00028 { 00029 switch (reason) { 00030 case Gap::REMOTE_USER_TERMINATED_CONNECTION: 00031 DEBUG("Disconnected (REMOTE_USER_TERMINATED_CONNECTION)\n\r"); 00032 break; 00033 case Gap::LOCAL_HOST_TERMINATED_CONNECTION: 00034 DEBUG("Disconnected (LOCAL_HOST_TERMINATED_CONNECTION)\n\r"); 00035 break; 00036 case Gap::CONN_INTERVAL_UNACCEPTABLE: 00037 DEBUG("Disconnected (CONN_INTERVAL_UNACCEPTABLE)\n\r"); 00038 break; 00039 } 00040 00041 DEBUG("Restarting the advertising process\n\r"); 00042 m_ble.startAdvertising(); 00043 } 00044 00045 00046 /** 00047 * This callback is used whenever the host writes data to one of our GATT characteristics. 00048 */ 00049 void dataWrittenCallback(const GattCharacteristicWriteCBParams *params) 00050 { 00051 // Ensure that initialization is finished and the host has written to the TX characteristic. 00052 if ((m_uart_service_ptr != NULL) && (params->charHandle == m_uart_service_ptr->getTXCharacteristicHandle())) { 00053 uint8_t buf[MAX_REPLY_LEN]; 00054 uint32_t len = 0; 00055 00056 if (1 == params->len) { 00057 switch (params->data[0]) { 00058 case '0': 00059 m_cmd_led = 0; 00060 len = snprintf((char*) buf, MAX_REPLY_LEN, "OK"); 00061 break; 00062 case '1': 00063 m_cmd_led = 1; 00064 len = snprintf((char*) buf, MAX_REPLY_LEN, "OK"); 00065 break; 00066 case 'a': 00067 len = snprintf((char*) buf, MAX_REPLY_LEN, "%d", m_analog_in_value); 00068 break; 00069 default: 00070 len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR: Unknown char"); 00071 break; 00072 } 00073 } 00074 else 00075 { 00076 len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR: Invalid len"); 00077 } 00078 00079 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); 00080 00081 DEBUG("%d bytes received from host\n\r", params->len); 00082 } 00083 } 00084 00085 00086 /** 00087 * This callback is used whenever a write to a GATT characteristic causes data to be sent to the host. 00088 */ 00089 void dataSentCallback(unsigned count) 00090 { 00091 // NOTE: The count always seems to be 1 regardless of data. 00092 DEBUG("%d bytes sent to host\n\r", count); 00093 } 00094 00095 00096 /** 00097 * This callback is scheduled to be called periodically via a low-priority interrupt. 00098 */ 00099 void periodicCallback(void) 00100 { 00101 m_analog_in_value = m_analog_in.read_u16(); 00102 } 00103 00104 00105 void error(ble_error_t err, uint32_t line) 00106 { 00107 m_error_led = 1; 00108 DEBUG("Error %d on line number %d\n\r", err, line); 00109 } 00110 00111 00112 int main(void) 00113 { 00114 ble_error_t err; 00115 Ticker ticker; 00116 00117 m_serial_port.baud(UART_BAUD_RATE); 00118 00119 DEBUG("Initialising\n\r"); 00120 00121 m_cmd_led = 0; 00122 m_error_led = 0; 00123 m_analog_in_value = 0; 00124 00125 ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S); 00126 00127 m_ble.init(); 00128 m_ble.onDisconnection(disconnectionCallback); 00129 m_ble.onDataWritten(dataWrittenCallback); 00130 m_ble.onDataSent(dataSentCallback); 00131 00132 // Set the TX power in dBm units. 00133 // Possible values (in decreasing order): 4, 0, -4, -8, -12, -16, -20. 00134 err = m_ble.setTxPower(4); 00135 if (BLE_ERROR_NONE != err) { 00136 error(err, __LINE__); 00137 } 00138 00139 // Setup advertising (GAP stuff). 00140 err = m_ble.setDeviceName(DEVICE_NAME); 00141 if (BLE_ERROR_NONE != err) { 00142 error(err, __LINE__); 00143 } 00144 00145 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00146 if (BLE_ERROR_NONE != err) { 00147 error(err, __LINE__); 00148 } 00149 00150 m_ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00151 00152 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00153 (const uint8_t *)SHORT_NAME, 00154 (sizeof(SHORT_NAME) - 1)); 00155 if (BLE_ERROR_NONE != err) { 00156 error(err, __LINE__); 00157 } 00158 00159 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00160 (const uint8_t *)UARTServiceUUID_reversed, 00161 sizeof(UARTServiceUUID_reversed)); 00162 if (BLE_ERROR_NONE != err) { 00163 error(err, __LINE__); 00164 } 00165 00166 m_ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADV_INTERVAL_MS)); 00167 m_ble.startAdvertising(); 00168 00169 // Create a UARTService object (GATT stuff). 00170 UARTService uartService(m_ble); 00171 m_uart_service_ptr = &uartService; 00172 00173 while (true) { 00174 m_ble.waitForEvent(); 00175 } 00176 }
Generated on Sun Jul 24 2022 02:37:02 by
1.7.2
