ST / Mbed OS Node_BLE_Led_Device

Dependencies:   X_NUCLEO_LED61A1

Bluetooth Low Energy enabled device with "Analog" feature, compatible with BlueST Protocol.

Files at this revision

API Documentation at this revision

Comitter:
Davidroid
Date:
Thu May 10 12:52:16 2018 +0000
Child:
1:71b8b1f19322
Commit message:
Initial release, not completely implementing the "Analog feature"

Changed in this revision

X_NUCLEO_LED61A1.lib Show annotated file Show diff for this revision Revisions of this file
img/LED_OFF.png Show annotated file Show diff for this revision Revisions of this file
img/LED_ON.png Show annotated file Show diff for this revision Revisions of this file
img/connection.png Show annotated file Show diff for this revision Revisions of this file
img/discovery.png Show annotated file Show diff for this revision Revisions of this file
img/scan_results.png Show annotated file Show diff for this revision Revisions of this file
img/start_scan.png Show annotated file Show diff for this revision Revisions of this file
img/write_characteristic.png Show annotated file Show diff for this revision Revisions of this file
img/write_pannel.png Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
module.json Show annotated file Show diff for this revision Revisions of this file
readme.md Show annotated file Show diff for this revision Revisions of this file
shields/TARGET_ST_BLUENRG.lib Show annotated file Show diff for this revision Revisions of this file
source/CustomService.h Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_LED61A1.lib	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/X_NUCLEO_LED61A1/#8cd869d64965
Binary file img/LED_OFF.png has changed
Binary file img/LED_ON.png has changed
Binary file img/connection.png has changed
Binary file img/discovery.png has changed
Binary file img/scan_results.png has changed
Binary file img/start_scan.png has changed
Binary file img/write_characteristic.png has changed
Binary file img/write_pannel.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e62a1b9236b44e70ae3b0902dc538481c04d455b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,16 @@
+{
+    "target_overrides": {
+        "K64F": {
+            "target.features_add": ["BLE"],
+            "target.extra_labels_add": ["ST_BLUENRG"]
+        },
+        "NUCLEO_F401RE": {
+            "target.features_add": ["BLE"],
+            "target.extra_labels_add": ["ST_BLUENRG"]
+        },
+        "DISCO_L475VG_IOT01A": {
+            "target.features_add": ["BLE"],
+            "target.extra_labels_add": ["ST_BLUENRG"]
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/module.json	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,16 @@
+{
+  "name": "ble-led",
+  "version": "0.0.1",
+  "description": "A simple service that demonstrates the use of a read-write characteristic to control a LED",
+  "licenses": [
+    {
+      "url": "https://spdx.org/licenses/Apache-2.0",
+      "type": "Apache-2.0"
+    }
+  ],
+  "dependencies": {
+    "ble": "^2.0.0"
+  },
+  "targetDependencies": {},
+  "bin": "./source"
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.md	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,93 @@
+To help you create your own BLE services, we have created this service template.
+The LED example demonstrates the use of a read-write characteristic to control a
+LED through a phone app.
+
+The template covers:
+
+* Setting up advertising and connection states.
+* Assigning UUIDs to the service and its characteristic.
+* Creating an input characteristic: read-write, boolean. This characteristic offers control of the LED.
+* Constructing a service class and adding it to the BLE stack.
+
+# Running the application
+
+## Requirements
+
+The sample application can be seen on any BLE scanner on a smartphone. If you don't have a scanner on your phone, please install :
+
+- [nRF Master Control Panel](https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp) for Android.
+
+- [LightBlue](https://itunes.apple.com/gb/app/lightblue-bluetooth-low-energy/id557428110?mt=8) for iPhone.
+
+Hardware requirements are in the [main readme](https://github.com/ARMmbed/mbed-os-example-ble/blob/master/README.md).
+
+*NOTE:* If you have more than a single mbed board (e.g. nrf51dk or mkit) you can
+run the BLE_LED and BLE_LEDBlinker at the same time. For more information please
+refer to the BLE_LEDBlinker demo.
+
+## Building instructions
+
+Building instructions for all samples are in the [main readme](https://github.com/ARMmbed/mbed-os-example-ble/blob/master/README.md).
+
+## Checking for success
+
+**Note:** Screens captures depicted below show what is expected from this example if the scanner used is *nRF Master Control Panel* version 4.0.5. If you encounter any difficulties consider trying another scanner or another version of nRF Master Control Panel. Alternative scanners may require reference to their manuals.
+
+
+1. Build the application and install it on your board as explained in the building instructions.
+1. Open the BLE scanner on your phone.
+
+1. Start a scan.
+
+    ![](img/start_scan.png)
+
+    **figure 1** How to start scan using nRF Master Control Panel 4.0.5
+
+1. Find your device; it should be named `LED`.
+
+    ![](img/scan_results.png)
+
+    **figure 2** Scan results using nRF Master Control Panel 4.0.5
+
+1. Establish a connection with your device.
+
+    ![](img/connection.png)
+
+    **figure 3**  How to establish a connection using Master Control Panel 4.0.5
+
+1. Discover the services and the characteristics on the device. The *LED service* has the UUID `0xA000` and includes the *LED state characteristic* which has the UUID `0xA001`. Depending on your scanner, non standard 16-bit UUID's can be displayed as 128-bit UUID's. If it is the case the following format will be used: `0000XXXX-0000-1000-8000-00805F9B34FB` where `XXXX` is the hexadecimal representation of the 16-bit UUID value.
+
+    ![](img/discovery.png)
+
+    **figure 4** Representation of the Led service using Master Control Panel 4.0.5
+
+1. Open the write pannel of the *LED state* characteristic.
+
+    ![](img/write_characteristic.png)
+
+    **figure 5** How to read and write a characteristic value using Master Control Panel 4.0.5
+
+
+1. The characteristic accept a 1 byte value:
+
+    ![](img/write_pannel.png)
+
+    **figure 6** Write characteristic pannel using Master Control Panel 4.0.5
+
+    * `0x00`: LED ON
+
+        ![](img/LED_ON.png)
+
+        **figure 6** Write characteristic pannel to set the LED on using Master Control Panel 4.0.5
+
+
+    * `0x01`: LED OFF
+
+        ![](img/LED_OFF.png)
+
+        **figure 6** Write characteristic pannel to set the LED off using Master Control Panel 4.0.5
+
+
+1. Toggle the LED characteristic value and see the LED turn ON or turn OFF according to the value you set.
+
+If you can see the characteristic, and the LED is turned on/off as you toggle its value, the application is working properly.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/shields/TARGET_ST_BLUENRG.lib	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/ble-x-nucleo-idb0xa1/#bcef7fa68b2deaa8ae7b2dbbe2648fdc8bae290b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/CustomService.h	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,85 @@
+/**
+ *******************************************************************************
+ * @file    CustomService.h
+ * @author  Davide Aliprandi, STMicroelectronics
+ * @version V1.0.0
+ * @date    October 31st, 2017
+ * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
+ *          Motor Control Expansion Board and the X-NUCLEO-IDB05A1 Bluetooth
+ *          Low energy Expansion Board.
+ *******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; 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.
+ *
+ *******************************************************************************
+ */
+ 
+
+#ifndef __BLE_CUSTOM_SERVICE_H__
+#define __BLE_CUSTOM_SERVICE_H__
+
+#include "ble_utils.h"
+
+#define COMMAND_DATA_LENGTH (sizeof(uint8_t))
+#define MAX_DATA_LENGTH     (COMMAND_DATA_LENGTH)
+
+const UUID::LongUUIDBytes_t CUSTOM_LED_SERVICE_UUID        = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+const UUID::LongUUIDBytes_t CUSTOM_LED_CHARACTERISTIC_UUID = {0x80,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b};
+
+class CustomService {
+public:
+
+    /* Led commands. */
+    typedef enum
+    {
+        LED_POWER_OFF = 0,  // Led Off.
+        LED_POWER_ON        // Led On.
+    } led_commands_t;
+
+    CustomService(BLEDevice &_ble) :
+        ble(_ble),
+        command(CUSTOM_LED_CHARACTERISTIC_UUID, packed_command, MAX_DATA_LENGTH, MAX_DATA_LENGTH,
+            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)
+    {
+        GattCharacteristic *char_table[] = {&command};
+        GattService led_service(CUSTOM_LED_SERVICE_UUID, char_table, sizeof(char_table) / sizeof(GattCharacteristic *));
+        ble.addService(led_service);
+        memset (packed_command, 0, MAX_DATA_LENGTH);
+    }
+
+    GattAttribute::Handle_t getValueHandle() const
+    {
+        return command.getValueAttribute().getHandle();
+    }
+
+private:
+    BLEDevice &ble;
+    GattCharacteristic command;
+    uint8_t packed_command[MAX_DATA_LENGTH];
+};
+
+#endif /* #ifndef __BLE_CUSTOM_SERVICE_H__ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/main.cpp	Thu May 10 12:52:16 2018 +0000
@@ -0,0 +1,214 @@
+/**
+ ******************************************************************************
+ * @file    main.cpp
+ * @author  Davide Aliprandi, STMicroelectronics
+ * @version V1.0.0
+ * @date    Ocober 31st, 2017
+ * @brief   mbed test application for the STMicroelectronics X-NUCLEO-LED61A1
+ *          LED expansion board and the X-NUCLEO-IDB05A1 Bluetooth expansion
+ *          board.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT(c) 2015 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 BE 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"
+
+/* Component specific header files. */
+#include "Led6001.h"
+
+
+/* Definitions ---------------------------------------------------------------*/
+
+#define BLE_ADVERTISING_INTERVAL_ms 1000
+
+
+/* Variables -----------------------------------------------------------------*/
+
+/* Blinky LED to indicate system aliveness. */
+DigitalOut alivenessLED(LED1, 0);
+
+/* Bluetooth. */
+const static char    DEVICE_NAME[] = "LED_DEVICE";
+const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x00,0x00,0x20,0x00};
+static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
+CustomService *custom_service;
+
+/* LED Control Component. */
+Led6001 *led;
+
+
+/* Bluetooth related functions -----------------------------------------------*/
+
+void on_disconnection_callback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    (void) params;
+    BLE::Instance().gap().startAdvertising();
+}
+
+void aliveness_callback(void)
+{
+    alivenessLED = !alivenessLED; /* Do blinky to indicate system aliveness. */
+}
+
+/**
+ * This callback allows the custom service to receive updates for the
+ * characteristic.
+ *
+ * @param[in] params
+ *     Information about the characterisitc being updated.
+ */
+void on_data_written_callback(const GattWriteCallbackParams *params) {
+    if ((params->handle == custom_service->getValueHandle())) {
+        switch ((CustomService::led_commands_t) ((uint8_t *) (params->data))[0])
+        {
+            //Power Off
+            case CustomService::LED_POWER_OFF:
+                led->power_off();
+                break;
+            //Power On
+            case CustomService::LED_POWER_ON:
+                led->power_on();
+                break;
+            //Other
+            default:
+                break;
+        }
+    }
+}
+
+/**
+ * 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);
+
+    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_LED_SERVICE_UUID, sizeof(CUSTOM_LED_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));
+}
+
+
+/* Custom service related functions ------------------------------------------*/
+
+/**
+ * @brief  Interrupt Handler for the component's XFAULT interrupt.
+ * @param  None.
+ * @retval None.
+ */
+void xfault_irq_handler(void)
+{
+    /* Printing to the console. */
+    printf("XFAULT Interrupt detected! Re-initializing LED driver...");
+
+    /* Re-starting-up LED Control Component. */
+    led->start_up();
+
+    /* Printing to the console. */
+    printf("Done.\r\n\n");
+}
+
+
+/* Main function -------------------------------------------------------------*/
+
+int main()
+{
+    /*----- Initialization. -----*/
+
+    /* Printing to the console. */
+    printf("LED Node Application Example\r\n\n");
+
+    /* Aliveness callback. */
+    event_queue.call_every(500, aliveness_callback);
+
+    /* Bluetooth. */
+    BLE &ble = BLE::Instance();
+    ble.onEventsToProcess(schedule_ble_events_processing);
+    ble.init(ble_init_complete);
+    
+    /* Initializing LED Control Component. */
+    led = new Led6001(D4, A3, D6, D5);
+    if (led->init() != COMPONENT_OK) {
+        exit(EXIT_FAILURE);
+    }
+
+    /* Attaching and enabling interrupt handlers. */
+    led->attach_xfault_irq(&xfault_irq_handler);
+    led->enable_xfault_irq();
+
+    /* Powering OFF. */
+    led->power_off();
+
+    /* Start. */
+    event_queue.dispatch_forever();
+
+    return 0;
+}