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.
main.cpp
00001 /** 00002 ******************************************************************************* 00003 * @file main.cpp 00004 * @author Davide Aliprandi, STMicroelectronics 00005 * @version V1.0.0 00006 * @date March 31st, 2018 00007 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IDB05A1 00008 * Bluetooth Low energy Expansion Board. 00009 ******************************************************************************* 00010 * @attention 00011 * 00012 * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 2. Redistributions in binary form must reproduce the above copyright 00019 * notice, this list of conditions and the following disclaimer in the 00020 * documentation and/or other materials provided with the distribution. 00021 * 3. Neither the name of STMicroelectronics nor the names of its 00022 * contributors may be used to endorse or promote products derived from 00023 * this software without specific prior written permission. 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00026 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00028 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 00029 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00030 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00031 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00032 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00033 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00034 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00035 * POSSIBILITY OF SUCH DAMAGE. 00036 * 00037 ******************************************************************************* 00038 */ 00039 00040 00041 /* Includes ------------------------------------------------------------------*/ 00042 00043 /* mbed specific header files. */ 00044 #include <events/mbed_events.h> 00045 #include <mbed.h> 00046 #include "ble/BLE.h" 00047 #include "CustomService.h" 00048 00049 00050 /* Definitions ---------------------------------------------------------------*/ 00051 00052 #define BLE_ADVERTISING_INTERVAL_ms 1000 00053 #define BLUETOOTH_PERIODIC_CALLBACK_ms 500 00054 #define DELAY_1 100 00055 00056 00057 /* Variables -----------------------------------------------------------------*/ 00058 00059 /* Switch and toggle state. */ 00060 CustomService::switch_state_t switch_state; 00061 bool toggle_state; 00062 00063 /* LED to indicate system state. */ 00064 DigitalOut led_state(LED1); //Conflicts with SPI CLK on D13. 00065 00066 /* Bluetooth. */ 00067 const static char DEVICE_NAME[] = "SWITCH_DEVICE"; 00068 const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x20,0x00,0x00,0x00}; 00069 static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); 00070 CustomService *custom_service; 00071 00072 /* Button event. */ 00073 InterruptIn event(USER_BUTTON); 00074 00075 00076 /* Bluetooth related functions -----------------------------------------------*/ 00077 00078 void on_disconnection_callback(const Gap::DisconnectionCallbackParams_t *params) 00079 { 00080 (void) params; 00081 BLE::Instance().gap().startAdvertising(); 00082 } 00083 00084 /** 00085 * This callback allows the custom service to read updates from the 00086 * characteristic. 00087 * 00088 * @param[in] params 00089 * Information about the characterisitc being updated. 00090 */ 00091 /* 00092 void on_data_read_callback(const GattReadCallbackParams *params) { 00093 static uint16_t time_stamp = 0; 00094 00095 if (params->handle == custom_service->getValueHandle()) { 00096 // Reading led state and sending it via bluetooth. 00097 if (BLE::Instance().getGapState().connected) { 00098 event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF); 00099 } 00100 } 00101 } 00102 */ 00103 00104 /** 00105 * This callback allows the custom service to write updates to the 00106 * characteristic. 00107 * 00108 * @param[in] params 00109 * Information about the characterisitc being updated. 00110 */ 00111 void on_data_written_callback(const GattWriteCallbackParams *params) { 00112 00113 if (params->handle == custom_service->getValueHandle()) { 00114 /* Receiving command via bluetooth and writing it to the led state. */ 00115 led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[SWITCH_DATA_INDEX])); 00116 } 00117 } 00118 00119 /** 00120 * This function is called periodically. 00121 */ 00122 void periodic_callback(void) 00123 { 00124 static uint16_t time_stamp = 0; 00125 00126 /* Reading toggle command and sending it via bluetooth. */ 00127 if (toggle_state) { 00128 if (BLE::Instance().getGapState().connected) { 00129 event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, switch_state); 00130 toggle_state = false; 00131 } 00132 } 00133 } 00134 00135 /** 00136 * This function is called when the ble initialization process has failled. 00137 */ 00138 void on_ble_init_error_callback(BLE &ble, ble_error_t error) 00139 { 00140 /* Initialization error handling should go here */ 00141 } 00142 00143 /** 00144 * Callback triggered when the ble initialization process has finished. 00145 */ 00146 void ble_init_complete(BLE::InitializationCompleteCallbackContext *params) 00147 { 00148 BLE& ble = params->ble; 00149 ble_error_t error = params->error; 00150 00151 if (error != BLE_ERROR_NONE) { 00152 /* In case of error, forward the error handling to on_ble_init_error_callback */ 00153 on_ble_init_error_callback(ble, error); 00154 return; 00155 } 00156 00157 /* Ensure that it is the default instance of BLE */ 00158 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00159 return; 00160 } 00161 00162 ble.gap().onDisconnection(on_disconnection_callback); 00163 ble.gattServer().onDataWritten(on_data_written_callback); 00164 //ble.gattServer().onDataRead(on_data_read_callback); 00165 00166 custom_service = new CustomService(ble); 00167 00168 /* Setup advertising data. */ 00169 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00170 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *) CUSTOM_SERVICE_UUID, sizeof(CUSTOM_SERVICE_UUID)); 00171 ble.gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, MANUFACTURER_SPECIFIC_DATA, sizeof(MANUFACTURER_SPECIFIC_DATA)); 00172 ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME) - 1); 00173 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00174 ble.gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL_ms); 00175 ble.gap().startAdvertising(); 00176 } 00177 00178 void schedule_ble_events_processing(BLE::OnEventsToProcessCallbackContext* context) { 00179 BLE &ble = BLE::Instance(); 00180 event_queue.call(Callback<void()>(&ble, &BLE::processEvents)); 00181 } 00182 00183 00184 /* Custom service related functions ------------------------------------------*/ 00185 00186 void button_callback(void) { 00187 switch_state = (CustomService::switch_state_t) (CustomService::SWITCH_ON - switch_state); 00188 toggle_state = true; 00189 } 00190 00191 00192 /* Main function -------------------------------------------------------------*/ 00193 00194 int main() 00195 { 00196 /*----- Initialization. -----*/ 00197 00198 /* Printing to the console. */ 00199 printf("Switch Node Application Example\r\n\n"); 00200 00201 /* Bluetooth. */ 00202 event_queue.call_every(BLUETOOTH_PERIODIC_CALLBACK_ms, periodic_callback); 00203 BLE &ble = BLE::Instance(); 00204 ble.onEventsToProcess(schedule_ble_events_processing); 00205 ble.init(ble_init_complete); 00206 00207 /* Attaching and enabling interrupt handlers. */ 00208 event.fall(button_callback); 00209 00210 /* Setting up initial state: switch feature OFF, i.e. led OFF, and no toggle 00211 state triggered. */ 00212 switch_state = CustomService::SWITCH_OFF; 00213 led_state.write(switch_state); 00214 toggle_state = false; 00215 00216 /* Start. */ 00217 event_queue.dispatch_forever(); 00218 00219 return 0; 00220 }
Generated on Tue Jul 19 2022 06:41:04 by
1.7.2