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: X_NUCLEO_IKS01A2
Fork of Node_BLE_Switch_Device by
Bluetooth Low Energy enabled device with environmental and inertial features, i.e. "Pressure", "Humidity", "Temperature", "Accelerometer", "Gyroscope", and "Magnetometer", plus "Switch" feature, compatible with BlueST Protocol.
source/main.cpp@7:2b5ed57b088c, 2018-08-06 (annotated)
- Committer:
- Davidroid
- Date:
- Mon Aug 06 09:57:49 2018 +0000
- Revision:
- 7:2b5ed57b088c
- Parent:
- 6:01d49589410e
Correct Custom Service UUID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:6b34c49b5285 | 1 | /** |
Davidroid | 0:6b34c49b5285 | 2 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 3 | * @file main.cpp |
Davidroid | 0:6b34c49b5285 | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:6b34c49b5285 | 5 | * @version V1.0.0 |
Davidroid | 0:6b34c49b5285 | 6 | * @date March 31st, 2018 |
Davidroid | 0:6b34c49b5285 | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IDB05A1 |
Davidroid | 0:6b34c49b5285 | 8 | * Bluetooth Low energy Expansion Board. |
Davidroid | 0:6b34c49b5285 | 9 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 10 | * @attention |
Davidroid | 0:6b34c49b5285 | 11 | * |
Davidroid | 0:6b34c49b5285 | 12 | * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> |
Davidroid | 0:6b34c49b5285 | 13 | * |
Davidroid | 0:6b34c49b5285 | 14 | * Redistribution and use in source and binary forms, with or without |
Davidroid | 0:6b34c49b5285 | 15 | * modification, are permitted provided that the following conditions are met: |
Davidroid | 0:6b34c49b5285 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:6b34c49b5285 | 17 | * this list of conditions and the following disclaimer. |
Davidroid | 0:6b34c49b5285 | 18 | * 2. Redistributions in binary form must reproduce the above copyright |
Davidroid | 0:6b34c49b5285 | 19 | * notice, this list of conditions and the following disclaimer in the |
Davidroid | 0:6b34c49b5285 | 20 | * documentation and/or other materials provided with the distribution. |
Davidroid | 0:6b34c49b5285 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its |
Davidroid | 0:6b34c49b5285 | 22 | * contributors may be used to endorse or promote products derived from |
Davidroid | 0:6b34c49b5285 | 23 | * this software without specific prior written permission. |
Davidroid | 0:6b34c49b5285 | 24 | * |
Davidroid | 0:6b34c49b5285 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:6b34c49b5285 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:6b34c49b5285 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
Davidroid | 0:6b34c49b5285 | 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
Davidroid | 0:6b34c49b5285 | 29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
Davidroid | 0:6b34c49b5285 | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
Davidroid | 0:6b34c49b5285 | 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
Davidroid | 0:6b34c49b5285 | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
Davidroid | 0:6b34c49b5285 | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
Davidroid | 0:6b34c49b5285 | 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Davidroid | 0:6b34c49b5285 | 35 | * POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:6b34c49b5285 | 36 | * |
Davidroid | 0:6b34c49b5285 | 37 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 38 | */ |
Davidroid | 0:6b34c49b5285 | 39 | |
Davidroid | 0:6b34c49b5285 | 40 | |
Davidroid | 0:6b34c49b5285 | 41 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 42 | |
Davidroid | 0:6b34c49b5285 | 43 | /* mbed specific header files. */ |
Davidroid | 0:6b34c49b5285 | 44 | #include <events/mbed_events.h> |
Davidroid | 0:6b34c49b5285 | 45 | #include <mbed.h> |
Davidroid | 0:6b34c49b5285 | 46 | #include "ble/BLE.h" |
Davidroid | 0:6b34c49b5285 | 47 | #include "CustomService.h" |
Davidroid | 4:a10e2c94c8c8 | 48 | #include "XNucleoIKS01A2.h" |
Davidroid | 0:6b34c49b5285 | 49 | |
Davidroid | 0:6b34c49b5285 | 50 | |
Davidroid | 0:6b34c49b5285 | 51 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 52 | |
Davidroid | 0:6b34c49b5285 | 53 | #define BLE_ADVERTISING_INTERVAL_ms 1000 |
Davidroid | 0:6b34c49b5285 | 54 | #define BLUETOOTH_PERIODIC_CALLBACK_ms 500 |
Davidroid | 0:6b34c49b5285 | 55 | #define DELAY_1 100 |
Davidroid | 0:6b34c49b5285 | 56 | |
Davidroid | 0:6b34c49b5285 | 57 | |
Davidroid | 0:6b34c49b5285 | 58 | /* Types ---------------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 59 | |
Davidroid | 0:6b34c49b5285 | 60 | /* Toggle state. */ |
Davidroid | 0:6b34c49b5285 | 61 | typedef enum |
Davidroid | 0:6b34c49b5285 | 62 | { |
Davidroid | 0:6b34c49b5285 | 63 | TOGGLE_OFF = 0, |
Davidroid | 0:6b34c49b5285 | 64 | TOGGLE_ON |
Davidroid | 0:6b34c49b5285 | 65 | } toggle_t; |
Davidroid | 0:6b34c49b5285 | 66 | |
Davidroid | 0:6b34c49b5285 | 67 | |
Davidroid | 0:6b34c49b5285 | 68 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 69 | |
Davidroid | 7:2b5ed57b088c | 70 | /* Switch and toggle state. */ |
Davidroid | 7:2b5ed57b088c | 71 | CustomService::switch_state_t switch_state; |
Davidroid | 0:6b34c49b5285 | 72 | bool toggle_state; |
Davidroid | 0:6b34c49b5285 | 73 | |
Davidroid | 0:6b34c49b5285 | 74 | /* LED to indicate system state. */ |
Davidroid | 0:6b34c49b5285 | 75 | DigitalOut led_state(LED1); //Conflicts with SPI CLK on D13. |
Davidroid | 0:6b34c49b5285 | 76 | |
Davidroid | 0:6b34c49b5285 | 77 | /* Bluetooth. */ |
Davidroid | 6:01d49589410e | 78 | const static char DEVICE_NAME[] = "IOT_DEVICE"; |
Davidroid | 6:01d49589410e | 79 | const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x20,0xfc,0x00,0x00}; |
Davidroid | 0:6b34c49b5285 | 80 | static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); |
Davidroid | 0:6b34c49b5285 | 81 | CustomService *custom_service; |
Davidroid | 0:6b34c49b5285 | 82 | |
Davidroid | 0:6b34c49b5285 | 83 | /* Button event. */ |
Davidroid | 0:6b34c49b5285 | 84 | InterruptIn event(USER_BUTTON); |
Davidroid | 0:6b34c49b5285 | 85 | |
Davidroid | 4:a10e2c94c8c8 | 86 | /* Instantiate the expansion board */ |
Davidroid | 4:a10e2c94c8c8 | 87 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
Davidroid | 4:a10e2c94c8c8 | 88 | |
Davidroid | 4:a10e2c94c8c8 | 89 | /* Components. */ |
Davidroid | 6:01d49589410e | 90 | static HTS221Sensor *hum_tem = mems_expansion_board->ht_sensor; |
Davidroid | 6:01d49589410e | 91 | static LPS22HBSensor *pre_tem = mems_expansion_board->pt_sensor; |
Davidroid | 6:01d49589410e | 92 | static LSM6DSLSensor *acc_gyr = mems_expansion_board->acc_gyro; |
Davidroid | 6:01d49589410e | 93 | static LSM303AGRMagSensor *mag = mems_expansion_board->magnetometer; |
Davidroid | 6:01d49589410e | 94 | //static LSM303AGRAccSensor *acc = mems_expansion_board->accelerometer; |
Davidroid | 4:a10e2c94c8c8 | 95 | |
Davidroid | 6:01d49589410e | 96 | /* Timestamp. */ |
Davidroid | 4:a10e2c94c8c8 | 97 | static uint16_t time_stamp = 0; |
Davidroid | 4:a10e2c94c8c8 | 98 | |
Davidroid | 4:a10e2c94c8c8 | 99 | |
Davidroid | 4:a10e2c94c8c8 | 100 | /* Custom service related functions ------------------------------------------*/ |
Davidroid | 4:a10e2c94c8c8 | 101 | |
Davidroid | 4:a10e2c94c8c8 | 102 | void button_callback(void) { |
Davidroid | 7:2b5ed57b088c | 103 | switch_state = (CustomService::switch_state_t) (CustomService::SWITCH_ON - switch_state); |
Davidroid | 4:a10e2c94c8c8 | 104 | toggle_state = true; |
Davidroid | 4:a10e2c94c8c8 | 105 | } |
Davidroid | 4:a10e2c94c8c8 | 106 | |
Davidroid | 6:01d49589410e | 107 | void get_and_send_env_sensors() { |
Davidroid | 6:01d49589410e | 108 | float pressure, humidity, temperature; |
Davidroid | 6:01d49589410e | 109 | |
Davidroid | 6:01d49589410e | 110 | pre_tem->get_pressure(&pressure); |
Davidroid | 6:01d49589410e | 111 | hum_tem->get_humidity(&humidity); |
Davidroid | 6:01d49589410e | 112 | hum_tem->get_temperature(&temperature); |
Davidroid | 6:01d49589410e | 113 | |
Davidroid | 6:01d49589410e | 114 | event_queue.call(Callback<void( |
Davidroid | 6:01d49589410e | 115 | uint16_t, |
Davidroid | 6:01d49589410e | 116 | float, float, float |
Davidroid | 6:01d49589410e | 117 | )>(custom_service, &CustomService::send_env_sensors), |
Davidroid | 6:01d49589410e | 118 | time_stamp++, |
Davidroid | 6:01d49589410e | 119 | pressure, humidity, temperature |
Davidroid | 6:01d49589410e | 120 | ); |
Davidroid | 6:01d49589410e | 121 | //printf("Env Sensors: (%d) %d %d %d\r\n", time_stamp, pressure, humidity, temperature); |
Davidroid | 6:01d49589410e | 122 | } |
Davidroid | 6:01d49589410e | 123 | |
Davidroid | 6:01d49589410e | 124 | void get_and_send_ine_sensors() { |
Davidroid | 6:01d49589410e | 125 | static axes_t accelerometer, gyroscope, magnetometer; |
Davidroid | 6:01d49589410e | 126 | |
Davidroid | 6:01d49589410e | 127 | acc_gyr->get_x_axes((int32_t *) &accelerometer); |
Davidroid | 6:01d49589410e | 128 | acc_gyr->get_g_axes((int32_t *) &gyroscope); |
Davidroid | 6:01d49589410e | 129 | mag->get_m_axes((int32_t *) &magnetometer); |
Davidroid | 6:01d49589410e | 130 | //accelerometer->get_x_axes((int32_t *) (&accelerometer)); |
Davidroid | 6:01d49589410e | 131 | |
Davidroid | 6:01d49589410e | 132 | event_queue.call(Callback<void( |
Davidroid | 6:01d49589410e | 133 | uint16_t, |
Davidroid | 6:01d49589410e | 134 | axes_t*, axes_t*, axes_t* |
Davidroid | 6:01d49589410e | 135 | )>(custom_service, &CustomService::send_ine_sensors), |
Davidroid | 6:01d49589410e | 136 | time_stamp++, |
Davidroid | 6:01d49589410e | 137 | &accelerometer, &gyroscope, &magnetometer |
Davidroid | 6:01d49589410e | 138 | ); |
Davidroid | 6:01d49589410e | 139 | //printf("Ine Sensors: (%d) %d %d %d %d %d %d %d %d %d\r\n", time_stamp, accelerometer.x, accelerometer.y, accelerometer.z, gyroscope.x, gyroscope.y, gyroscope.z, magnetometer.x, magnetometer.y, magnetometer.z); |
Davidroid | 4:a10e2c94c8c8 | 140 | } |
Davidroid | 4:a10e2c94c8c8 | 141 | |
Davidroid | 0:6b34c49b5285 | 142 | |
Davidroid | 0:6b34c49b5285 | 143 | /* Bluetooth related functions -----------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 144 | |
Davidroid | 0:6b34c49b5285 | 145 | void on_disconnection_callback(const Gap::DisconnectionCallbackParams_t *params) |
Davidroid | 0:6b34c49b5285 | 146 | { |
Davidroid | 0:6b34c49b5285 | 147 | (void) params; |
Davidroid | 0:6b34c49b5285 | 148 | BLE::Instance().gap().startAdvertising(); |
Davidroid | 0:6b34c49b5285 | 149 | } |
Davidroid | 0:6b34c49b5285 | 150 | |
Davidroid | 0:6b34c49b5285 | 151 | /** |
Davidroid | 0:6b34c49b5285 | 152 | * This callback allows the custom service to read updates from the |
Davidroid | 0:6b34c49b5285 | 153 | * characteristic. |
Davidroid | 0:6b34c49b5285 | 154 | * |
Davidroid | 0:6b34c49b5285 | 155 | * @param[in] params |
Davidroid | 0:6b34c49b5285 | 156 | * Information about the characterisitc being updated. |
Davidroid | 0:6b34c49b5285 | 157 | */ |
Davidroid | 0:6b34c49b5285 | 158 | void on_data_read_callback(const GattReadCallbackParams *params) { |
Davidroid | 4:a10e2c94c8c8 | 159 | /* Reading characteristic and sending it via bluetooth. */ |
Davidroid | 7:2b5ed57b088c | 160 | /* |
Davidroid | 7:2b5ed57b088c | 161 | if (params->handle == custom_service->get_state_command_handle()) { |
Davidroid | 7:2b5ed57b088c | 162 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 7:2b5ed57b088c | 163 | 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); |
Davidroid | 7:2b5ed57b088c | 164 | } |
Davidroid | 7:2b5ed57b088c | 165 | } else |
Davidroid | 7:2b5ed57b088c | 166 | */ |
Davidroid | 6:01d49589410e | 167 | if (params->handle == custom_service->get_env_sensors_handle()) { |
Davidroid | 0:6b34c49b5285 | 168 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 6:01d49589410e | 169 | get_and_send_env_sensors(); |
Davidroid | 6:01d49589410e | 170 | } |
Davidroid | 6:01d49589410e | 171 | } else if (params->handle == custom_service->get_ine_sensors_handle()) { |
Davidroid | 6:01d49589410e | 172 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 6:01d49589410e | 173 | get_and_send_ine_sensors(); |
Davidroid | 0:6b34c49b5285 | 174 | } |
Davidroid | 0:6b34c49b5285 | 175 | } |
Davidroid | 0:6b34c49b5285 | 176 | } |
Davidroid | 0:6b34c49b5285 | 177 | |
Davidroid | 0:6b34c49b5285 | 178 | /** |
Davidroid | 0:6b34c49b5285 | 179 | * This callback allows the custom service to write updates to the |
Davidroid | 0:6b34c49b5285 | 180 | * characteristic. |
Davidroid | 0:6b34c49b5285 | 181 | * |
Davidroid | 0:6b34c49b5285 | 182 | * @param[in] params |
Davidroid | 0:6b34c49b5285 | 183 | * Information about the characterisitc being updated. |
Davidroid | 0:6b34c49b5285 | 184 | */ |
Davidroid | 0:6b34c49b5285 | 185 | void on_data_written_callback(const GattWriteCallbackParams *params) { |
Davidroid | 4:a10e2c94c8c8 | 186 | /* Receiving data via bluetooth and writing it to the characteristic. */ |
Davidroid | 4:a10e2c94c8c8 | 187 | if (params->handle == custom_service->get_state_command_handle()) { |
Davidroid | 7:2b5ed57b088c | 188 | led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[SWITCH_DATA_INDEX])); |
Davidroid | 4:a10e2c94c8c8 | 189 | } |
Davidroid | 0:6b34c49b5285 | 190 | } |
Davidroid | 0:6b34c49b5285 | 191 | |
Davidroid | 0:6b34c49b5285 | 192 | /** |
Davidroid | 4:a10e2c94c8c8 | 193 | * This function is called periodically and sends notifications. |
Davidroid | 0:6b34c49b5285 | 194 | */ |
Davidroid | 0:6b34c49b5285 | 195 | void periodic_callback(void) |
Davidroid | 0:6b34c49b5285 | 196 | { |
Davidroid | 4:a10e2c94c8c8 | 197 | /* Reading toggle command and sending it via bluetooth, if needed. */ |
Davidroid | 0:6b34c49b5285 | 198 | if (toggle_state) { |
Davidroid | 0:6b34c49b5285 | 199 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 7:2b5ed57b088c | 200 | event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, switch_state); |
Davidroid | 0:6b34c49b5285 | 201 | toggle_state = false; |
Davidroid | 0:6b34c49b5285 | 202 | } |
Davidroid | 0:6b34c49b5285 | 203 | } |
Davidroid | 4:a10e2c94c8c8 | 204 | |
Davidroid | 4:a10e2c94c8c8 | 205 | /* Reading sensors and sending data via bluetooth. */ |
Davidroid | 4:a10e2c94c8c8 | 206 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 6:01d49589410e | 207 | get_and_send_env_sensors(); |
Davidroid | 6:01d49589410e | 208 | get_and_send_ine_sensors(); |
Davidroid | 4:a10e2c94c8c8 | 209 | } |
Davidroid | 0:6b34c49b5285 | 210 | } |
Davidroid | 0:6b34c49b5285 | 211 | |
Davidroid | 0:6b34c49b5285 | 212 | /** |
Davidroid | 0:6b34c49b5285 | 213 | * This function is called when the ble initialization process has failled. |
Davidroid | 0:6b34c49b5285 | 214 | */ |
Davidroid | 0:6b34c49b5285 | 215 | void on_ble_init_error_callback(BLE &ble, ble_error_t error) |
Davidroid | 0:6b34c49b5285 | 216 | { |
Davidroid | 0:6b34c49b5285 | 217 | /* Initialization error handling should go here */ |
Davidroid | 0:6b34c49b5285 | 218 | } |
Davidroid | 0:6b34c49b5285 | 219 | |
Davidroid | 0:6b34c49b5285 | 220 | /** |
Davidroid | 0:6b34c49b5285 | 221 | * Callback triggered when the ble initialization process has finished. |
Davidroid | 0:6b34c49b5285 | 222 | */ |
Davidroid | 0:6b34c49b5285 | 223 | void ble_init_complete(BLE::InitializationCompleteCallbackContext *params) |
Davidroid | 0:6b34c49b5285 | 224 | { |
Davidroid | 0:6b34c49b5285 | 225 | BLE& ble = params->ble; |
Davidroid | 0:6b34c49b5285 | 226 | ble_error_t error = params->error; |
Davidroid | 0:6b34c49b5285 | 227 | |
Davidroid | 0:6b34c49b5285 | 228 | if (error != BLE_ERROR_NONE) { |
Davidroid | 0:6b34c49b5285 | 229 | /* In case of error, forward the error handling to on_ble_init_error_callback */ |
Davidroid | 0:6b34c49b5285 | 230 | on_ble_init_error_callback(ble, error); |
Davidroid | 0:6b34c49b5285 | 231 | return; |
Davidroid | 0:6b34c49b5285 | 232 | } |
Davidroid | 0:6b34c49b5285 | 233 | |
Davidroid | 0:6b34c49b5285 | 234 | /* Ensure that it is the default instance of BLE */ |
Davidroid | 0:6b34c49b5285 | 235 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Davidroid | 0:6b34c49b5285 | 236 | return; |
Davidroid | 0:6b34c49b5285 | 237 | } |
Davidroid | 0:6b34c49b5285 | 238 | |
Davidroid | 0:6b34c49b5285 | 239 | ble.gap().onDisconnection(on_disconnection_callback); |
Davidroid | 0:6b34c49b5285 | 240 | ble.gattServer().onDataWritten(on_data_written_callback); |
Davidroid | 4:a10e2c94c8c8 | 241 | ble.gattServer().onDataRead(on_data_read_callback); |
Davidroid | 0:6b34c49b5285 | 242 | |
Davidroid | 0:6b34c49b5285 | 243 | custom_service = new CustomService(ble); |
Davidroid | 0:6b34c49b5285 | 244 | |
Davidroid | 0:6b34c49b5285 | 245 | /* Setup advertising data. */ |
Davidroid | 0:6b34c49b5285 | 246 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Davidroid | 4:a10e2c94c8c8 | 247 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *) CUSTOM_SERVICE_UUID, sizeof(CUSTOM_SERVICE_UUID)); |
Davidroid | 0:6b34c49b5285 | 248 | ble.gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, MANUFACTURER_SPECIFIC_DATA, sizeof(MANUFACTURER_SPECIFIC_DATA)); |
Davidroid | 3:b880b9a9ccdf | 249 | ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
Davidroid | 0:6b34c49b5285 | 250 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Davidroid | 0:6b34c49b5285 | 251 | ble.gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL_ms); |
Davidroid | 0:6b34c49b5285 | 252 | ble.gap().startAdvertising(); |
Davidroid | 0:6b34c49b5285 | 253 | } |
Davidroid | 0:6b34c49b5285 | 254 | |
Davidroid | 0:6b34c49b5285 | 255 | void schedule_ble_events_processing(BLE::OnEventsToProcessCallbackContext* context) { |
Davidroid | 0:6b34c49b5285 | 256 | BLE &ble = BLE::Instance(); |
Davidroid | 0:6b34c49b5285 | 257 | event_queue.call(Callback<void()>(&ble, &BLE::processEvents)); |
Davidroid | 0:6b34c49b5285 | 258 | } |
Davidroid | 0:6b34c49b5285 | 259 | |
Davidroid | 0:6b34c49b5285 | 260 | |
Davidroid | 0:6b34c49b5285 | 261 | /* Main function -------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 262 | |
Davidroid | 0:6b34c49b5285 | 263 | int main() |
Davidroid | 0:6b34c49b5285 | 264 | { |
Davidroid | 0:6b34c49b5285 | 265 | /*----- Initialization. -----*/ |
Davidroid | 0:6b34c49b5285 | 266 | |
Davidroid | 0:6b34c49b5285 | 267 | /* Printing to the console. */ |
Davidroid | 4:a10e2c94c8c8 | 268 | printf("Sensors Node Application Example\r\n\n"); |
Davidroid | 0:6b34c49b5285 | 269 | |
Davidroid | 0:6b34c49b5285 | 270 | /* Bluetooth. */ |
Davidroid | 0:6b34c49b5285 | 271 | event_queue.call_every(BLUETOOTH_PERIODIC_CALLBACK_ms, periodic_callback); |
Davidroid | 0:6b34c49b5285 | 272 | BLE &ble = BLE::Instance(); |
Davidroid | 0:6b34c49b5285 | 273 | ble.onEventsToProcess(schedule_ble_events_processing); |
Davidroid | 0:6b34c49b5285 | 274 | ble.init(ble_init_complete); |
Davidroid | 0:6b34c49b5285 | 275 | |
Davidroid | 0:6b34c49b5285 | 276 | /* Attaching and enabling interrupt handlers. */ |
Davidroid | 0:6b34c49b5285 | 277 | event.fall(button_callback); |
Davidroid | 0:6b34c49b5285 | 278 | |
Davidroid | 0:6b34c49b5285 | 279 | /* Setting up initial state: led OFF, no toggle state. */ |
Davidroid | 0:6b34c49b5285 | 280 | led_state.write(CustomService::SWITCH_OFF); |
Davidroid | 0:6b34c49b5285 | 281 | toggle_state = false; |
Davidroid | 4:a10e2c94c8c8 | 282 | |
Davidroid | 4:a10e2c94c8c8 | 283 | /* Enabling sensors. */ |
Davidroid | 6:01d49589410e | 284 | hum_tem->enable(); |
Davidroid | 6:01d49589410e | 285 | pre_tem->enable(); |
Davidroid | 6:01d49589410e | 286 | acc_gyr->enable_x(); |
Davidroid | 6:01d49589410e | 287 | acc_gyr->enable_g(); |
Davidroid | 6:01d49589410e | 288 | mag->enable(); |
Davidroid | 6:01d49589410e | 289 | //acc->enable(); |
Davidroid | 0:6b34c49b5285 | 290 | |
Davidroid | 0:6b34c49b5285 | 291 | /* Start. */ |
Davidroid | 0:6b34c49b5285 | 292 | event_queue.dispatch_forever(); |
Davidroid | 0:6b34c49b5285 | 293 | |
Davidroid | 0:6b34c49b5285 | 294 | return 0; |
Davidroid | 0:6b34c49b5285 | 295 | } |