Bluetooth Low Energy enabled device with "Analog" feature, compatible with BlueST Protocol.
Dependencies: X_NUCLEO_LED61A1
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author Davide Aliprandi, STMicroelectronics 00005 * @version V1.0.0 00006 * @date Ocober 31st, 2017 00007 * @brief mbed test application for the STMicroelectronics X-NUCLEO-LED61A1 00008 * LED expansion board and the X-NUCLEO-IDB05A1 Bluetooth expansion 00009 * board. 00010 ****************************************************************************** 00011 * @attention 00012 * 00013 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00014 * 00015 * Redistribution and use in source and binary forms, with or without modification, 00016 * are permitted provided that the following conditions are met: 00017 * 1. Redistributions of source code must retain the above copyright notice, 00018 * this list of conditions and the following disclaimer. 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00023 * may be used to endorse or promote products derived from this software 00024 * without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00029 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00030 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00031 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00032 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00033 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00034 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00035 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE 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 /* Component specific header files. */ 00050 #include "Led6001.h" 00051 00052 00053 /* Definitions ---------------------------------------------------------------*/ 00054 00055 #define BLE_ADVERTISING_INTERVAL_ms 1000 00056 00057 00058 /* Variables -----------------------------------------------------------------*/ 00059 00060 /* Blinky LED to indicate system aliveness. */ 00061 DigitalOut alivenessLED(LED1, 0); 00062 00063 /* Bluetooth. */ 00064 const static char DEVICE_NAME[] = "LED_DEVICE"; 00065 const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x00,0x00,0x20,0x00}; 00066 static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); 00067 CustomService *custom_service; 00068 00069 /* LED Control Component. */ 00070 Led6001 *led; 00071 00072 00073 /* Bluetooth related functions -----------------------------------------------*/ 00074 00075 void on_disconnection_callback(const Gap::DisconnectionCallbackParams_t *params) 00076 { 00077 (void) params; 00078 BLE::Instance().gap().startAdvertising(); 00079 } 00080 00081 void aliveness_callback(void) 00082 { 00083 alivenessLED = !alivenessLED; /* Do blinky to indicate system aliveness. */ 00084 } 00085 00086 /** 00087 * This callback allows the custom service to receive updates for the 00088 * characteristic. 00089 * 00090 * @param[in] params 00091 * Information about the characterisitc being updated. 00092 */ 00093 void on_data_written_callback(const GattWriteCallbackParams *params) { 00094 if ((params->handle == custom_service->getValueHandle())) { 00095 switch ((CustomService::led_commands_t) ((uint8_t *) (params->data))[0]) 00096 { 00097 //Power Off 00098 case CustomService::LED_POWER_OFF: 00099 led->power_off(); 00100 break; 00101 //Power On 00102 case CustomService::LED_POWER_ON: 00103 led->power_on(); 00104 break; 00105 //Other 00106 default: 00107 break; 00108 } 00109 } 00110 } 00111 00112 /** 00113 * This function is called when the ble initialization process has failled 00114 */ 00115 void on_ble_init_error_callback(BLE &ble, ble_error_t error) 00116 { 00117 /* Initialization error handling should go here */ 00118 } 00119 00120 /** 00121 * Callback triggered when the ble initialization process has finished 00122 */ 00123 void ble_init_complete(BLE::InitializationCompleteCallbackContext *params) 00124 { 00125 BLE& ble = params->ble; 00126 ble_error_t error = params->error; 00127 00128 if (error != BLE_ERROR_NONE) { 00129 /* In case of error, forward the error handling to on_ble_init_error_callback */ 00130 on_ble_init_error_callback(ble, error); 00131 return; 00132 } 00133 00134 /* Ensure that it is the default instance of BLE */ 00135 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00136 return; 00137 } 00138 00139 ble.gap().onDisconnection(on_disconnection_callback); 00140 ble.gattServer().onDataWritten(on_data_written_callback); 00141 00142 custom_service = new CustomService(ble); 00143 00144 /* Setup advertising data. */ 00145 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00146 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *) CUSTOM_SERVICE_UUID, sizeof(CUSTOM_SERVICE_UUID)); 00147 ble.gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, MANUFACTURER_SPECIFIC_DATA, sizeof(MANUFACTURER_SPECIFIC_DATA)); 00148 ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME) - 1); 00149 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00150 ble.gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL_ms); 00151 ble.gap().startAdvertising(); 00152 } 00153 00154 void schedule_ble_events_processing(BLE::OnEventsToProcessCallbackContext* context) { 00155 BLE &ble = BLE::Instance(); 00156 event_queue.call(Callback<void()>(&ble, &BLE::processEvents)); 00157 } 00158 00159 00160 /* Custom service related functions ------------------------------------------*/ 00161 00162 /** 00163 * @brief Interrupt Handler for the component's XFAULT interrupt. 00164 * @param None. 00165 * @retval None. 00166 */ 00167 void xfault_irq_handler(void) 00168 { 00169 /* Printing to the console. */ 00170 printf("XFAULT Interrupt detected! Re-initializing LED driver..."); 00171 00172 /* Re-starting-up LED Control Component. */ 00173 led->start_up(); 00174 00175 /* Printing to the console. */ 00176 printf("Done.\r\n\n"); 00177 } 00178 00179 00180 /* Main function -------------------------------------------------------------*/ 00181 00182 int main() 00183 { 00184 /*----- Initialization. -----*/ 00185 00186 /* Printing to the console. */ 00187 printf("LED Node Application Example\r\n\n"); 00188 00189 /* Aliveness callback. */ 00190 event_queue.call_every(500, aliveness_callback); 00191 00192 /* Bluetooth. */ 00193 BLE &ble = BLE::Instance(); 00194 ble.onEventsToProcess(schedule_ble_events_processing); 00195 ble.init(ble_init_complete); 00196 00197 /* Initializing LED Control Component. */ 00198 led = new Led6001(D4, A3, D6, D5); 00199 if (led->init() != COMPONENT_OK) { 00200 exit(EXIT_FAILURE); 00201 } 00202 00203 /* Attaching and enabling interrupt handlers. */ 00204 led->attach_xfault_irq(&xfault_irq_handler); 00205 led->enable_xfault_irq(); 00206 00207 /* Powering OFF. */ 00208 led->power_off(); 00209 00210 /* Start. */ 00211 event_queue.dispatch_forever(); 00212 00213 return 0; 00214 }
Generated on Sat Jul 16 2022 19:14:51 by 1.7.2