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
- Committer:
- Davidroid
- Date:
- 2018-05-18
- Revision:
- 5:445024130101
- Parent:
- 4:a10e2c94c8c8
- Child:
- 6:01d49589410e
File content as of revision 5:445024130101:
/** ******************************************************************************* * @file main.cpp * @author Davide Aliprandi, STMicroelectronics * @version V1.0.0 * @date March 31st, 2018 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IDB05A1 * Bluetooth Low energy Expansion Board. ******************************************************************************* * @attention * * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ******************************************************************************* */ /* Includes ------------------------------------------------------------------*/ /* mbed specific header files. */ #include <events/mbed_events.h> #include <mbed.h> #include "ble/BLE.h" #include "CustomService.h" #include "XNucleoIKS01A2.h" /* Definitions ---------------------------------------------------------------*/ #define BLE_ADVERTISING_INTERVAL_ms 1000 #define BLUETOOTH_PERIODIC_CALLBACK_ms 500 #define DELAY_1 100 /* Types ---------------------------------------------------------------------*/ /* Toggle state. */ typedef enum { TOGGLE_OFF = 0, TOGGLE_ON } toggle_t; /* Variables -----------------------------------------------------------------*/ /* Button state. */ bool toggle_state; /* LED to indicate system state. */ DigitalOut led_state(LED1); //Conflicts with SPI CLK on D13. /* Bluetooth. */ const static char DEVICE_NAME[] = "SENSORS_DEVICE"; const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x20,0x1c,0x00,0x00}; static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); CustomService *custom_service; /* Button event. */ InterruptIn event(USER_BUTTON); /* Instantiate the expansion board */ static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); /* Components. */ //static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; //static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; //static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; static uint16_t time_stamp = 0; /* Custom service related functions ------------------------------------------*/ void button_callback(void) { toggle_state = true; } void get_sensors(float *pressure, float *humidity, float *temperature) { press_temp->get_pressure(pressure); hum_temp->get_humidity(humidity); hum_temp->get_temperature(temperature); } /* Bluetooth related functions -----------------------------------------------*/ void on_disconnection_callback(const Gap::DisconnectionCallbackParams_t *params) { (void) params; BLE::Instance().gap().startAdvertising(); } /** * This callback allows the custom service to read updates from the * characteristic. * * @param[in] params * Information about the characterisitc being updated. */ void on_data_read_callback(const GattReadCallbackParams *params) { /* Reading characteristic and sending it via bluetooth. */ if (params->handle == custom_service->get_sensors_handle()) { if (BLE::Instance().getGapState().connected) { float pressure, humidity, temperature; get_sensors(&pressure, &humidity, &temperature); event_queue.call(Callback<void(uint16_t, uint32_t, uint16_t, uint16_t)>(custom_service, &CustomService::send_sensors), time_stamp++, (uint32_t) (pressure * 10), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10)); //printf("Sensors: (%d) %d %d %d\r\n", time_stamp, (uint32_t) (pressure * 100), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10)); } } } /** * This callback allows the custom service to write updates to the * characteristic. * * @param[in] params * Information about the characterisitc being updated. */ void on_data_written_callback(const GattWriteCallbackParams *params) { /* Receiving data via bluetooth and writing it to the characteristic. */ if (params->handle == custom_service->get_state_command_handle()) { led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[0])); } } /** * This function is called periodically and sends notifications. */ void periodic_callback(void) { /* Reading toggle command and sending it via bluetooth, if needed. */ if (toggle_state) { if (BLE::Instance().getGapState().connected) { event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, toggle_state ? TOGGLE_ON : TOGGLE_OFF); toggle_state = false; } } /* Reading sensors and sending data via bluetooth. */ if (BLE::Instance().getGapState().connected) { float pressure, humidity, temperature; get_sensors(&pressure, &humidity, &temperature); event_queue.call(Callback<void(uint16_t, uint32_t, uint16_t, uint16_t)>(custom_service, &CustomService::send_sensors), time_stamp++, (uint32_t) (pressure * 10), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10)); //printf("Sensors: (%d) %d %d %d\r\n", time_stamp, (uint32_t) (pressure * 100), (uint16_t) (humidity * 10), (uint16_t) (temperature * 10)); } } /** * This function is called when the ble initialization process has failled. */ void on_ble_init_error_callback(BLE &ble, ble_error_t error) { /* Initialization error handling should go here */ } /** * Callback triggered when the ble initialization process has finished. */ void ble_init_complete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; ble_error_t error = params->error; if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to on_ble_init_error_callback */ on_ble_init_error_callback(ble, error); return; } /* Ensure that it is the default instance of BLE */ if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } ble.gap().onDisconnection(on_disconnection_callback); ble.gattServer().onDataWritten(on_data_written_callback); ble.gattServer().onDataRead(on_data_read_callback); custom_service = new CustomService(ble); /* Setup advertising data. */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *) CUSTOM_SERVICE_UUID, sizeof(CUSTOM_SERVICE_UUID)); ble.gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, MANUFACTURER_SPECIFIC_DATA, sizeof(MANUFACTURER_SPECIFIC_DATA)); ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME) - 1); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL_ms); ble.gap().startAdvertising(); } void schedule_ble_events_processing(BLE::OnEventsToProcessCallbackContext* context) { BLE &ble = BLE::Instance(); event_queue.call(Callback<void()>(&ble, &BLE::processEvents)); } /* Main function -------------------------------------------------------------*/ int main() { /*----- Initialization. -----*/ /* Printing to the console. */ printf("Sensors Node Application Example\r\n\n"); /* Bluetooth. */ event_queue.call_every(BLUETOOTH_PERIODIC_CALLBACK_ms, periodic_callback); BLE &ble = BLE::Instance(); ble.onEventsToProcess(schedule_ble_events_processing); ble.init(ble_init_complete); /* Attaching and enabling interrupt handlers. */ event.fall(button_callback); /* Setting up initial state: led OFF, no toggle state. */ led_state.write(CustomService::SWITCH_OFF); toggle_state = false; /* Enabling sensors. */ hum_temp->enable(); press_temp->enable(); /* Start. */ event_queue.dispatch_forever(); return 0; }