Wsitch On a LED by BLE

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_LED_IDB0XA1 by ST

Files at this revision

API Documentation at this revision

Comitter:
pinofal
Date:
Sun Jul 01 11:32:06 2018 +0000
Parent:
6:e696cac5ef17
Commit message:
Switch On a LED by BLE

Changed in this revision

BLE_LED_IDB0XA1_Relay.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show diff for this revision Revisions of this file
diff -r e696cac5ef17 -r 79fe72eb3771 BLE_LED_IDB0XA1_Relay.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_LED_IDB0XA1_Relay.cpp	Sun Jul 01 11:32:06 2018 +0000
@@ -0,0 +1,177 @@
+//Tested: NUCLEO-F401RE
+#include "mbed.h"
+
+/****************** START BLE Declaration ****************/
+#include "ble/BLE.h"
+#include "LEDService.h"
+
+DigitalOut Led1(LED1);
+DigitalOut myLED(PB_2);
+DigitalOut myRelay(PA_15);
+DigitalIn myButton(USER_BUTTON);
+Serial pc(USBTX, USBRX); 
+
+
+const static char     DEVICE_NAME[] = "Amaldi_Ex_5";
+static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
+
+LEDService *ledServicePtr;
+/***************** END BLE Declaration ********************/
+
+/**************** START BLE Functions **********************/
+
+//****************************************************************
+//* Disconnection Callback
+//****************************************************************
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    (void)params;
+    BLE::Instance().gap().startAdvertising(); // restart advertising
+    
+     pc.printf("------ Sono qui 3 disconnetrionCallBack \r\n", error);
+}
+
+//****************************************************************************************
+//* This callback allows the LEDService to receive updates to the ledState Characteristic.
+//*
+//* @param[in] params
+//*     Information about the characterisitc being updated.
+//****************************************************************************************
+void onDataWrittenCallback(const GattWriteCallbackParams *params) 
+{
+    if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) 
+    {
+        switch(*(params->data))
+        {
+            case 0:
+            {
+                Led1 = 0x00; // spegni LED  
+                myLED = 0x00; // spegni LED
+            } break;
+            case 1:
+            {
+                Led1 = 0x01; // accendi LED  
+                myLED = 0x00; // spegni LED
+                myRelay=0x00;
+            } break;
+            case 2:
+            {
+                Led1 = 0x00; // spegni LED  
+                myLED = 0x01; // accendi LED
+            } break;
+            case 3:
+            {
+                Led1 = 0x01; // accendi LED  
+                myLED = 0x01; // accendi LED
+                myRelay=0x01;
+            } break;
+            case 4:
+            {
+                myRelay = 0x01; // accendi Relay
+            } break;
+            case 5:
+            {
+                myRelay = 0x00; // spegni Relay
+            } break;
+            
+            default: break;
+        }
+        pc.printf("\n\r---- Ricevuto: %d \n\r",*(params->data));
+        
+        //Led1 = *(params->data); // in params->data riceve il byte inviato dal cellulare
+    }
+}
+
+//************************************************************************** 
+//* This function is called when the ble initialization process has failled 
+//************************************************************************** 
+void onBleInitError(BLE &ble, ble_error_t error) 
+{ 
+    /* Initialization error handling should go here */ 
+} 
+
+//************************************************************************** 
+//* Callback triggered when the ble initialization process has finished 
+//************************************************************************** 
+void bleInitComplete(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 onBleInitError */
+        onBleInitError(ble, error);
+        return;
+    }
+
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) 
+    {
+        return;
+    }
+
+    ble.gap().onDisconnection(disconnectionCallback);
+    ble.gattServer().onDataWritten(onDataWrittenCallback);
+
+    bool initialValueForLEDCharacteristic = true;
+    ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
+
+    /* setup advertising */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().setAdvertisingInterval(1000); // 1000ms.
+    ble.gap().startAdvertising();
+
+    while (true) 
+    {
+        ble.waitForEvent();
+        //pc.printf("-------- Sono qui 1 WaitForEvent() \n\r");
+    }
+}
+/******************* END BLE Functions *************************/
+
+
+/********/
+/* MAIN */
+/********/
+int main(void)
+{
+    // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto
+    pc.baud(921600); //921600 bps
+    // messaggio di benvenuto
+    pc.printf("\r\nHallo Amaldi Students - Exercise 5 \r\n");
+    pc.printf("\r\n*** Bluetooth Driving for LED and Relay ***\r\n");
+    
+    //imposta il funzionamento del pulsante come "PullDown": Aperto = '0'. L'altra modalità di funzinamento è PullUp
+    myButton.mode(PullDown);
+    
+    /* ciclo di prova 
+    while(true)
+    {
+        if(myButton==0x01)
+        {
+            myLED=0x01;
+            //while(myButton !=0x00);
+            Led1=0x01;
+            myRelay=0x01; // accendi relay
+        }
+        else
+        {
+            myLED=0x00;
+            Led1=0x00;
+            myRelay=0x00; // spegni relay
+        }
+    }
+    */
+    /*************** START BLE Main ************/
+    BLE &ble = BLE::Instance();
+    ble.init(bleInitComplete);
+    /*************** END BLE Main ***********/
+    
+    
+    
+}
+
diff -r e696cac5ef17 -r 79fe72eb3771 main.cpp
--- a/main.cpp	Fri Sep 16 10:36:43 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,98 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "mbed.h"
-#include "ble/BLE.h"
-#include "LEDService.h"
-
-DigitalOut actuatedLED(LED1, 0);
-
-const static char     DEVICE_NAME[] = "LED";
-static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
-
-LEDService *ledServicePtr;
-
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    (void)params;
-    BLE::Instance().gap().startAdvertising(); // restart advertising
-}
-
-/**
- * This callback allows the LEDService to receive updates to the ledState Characteristic.
- *
- * @param[in] params
- *     Information about the characterisitc being updated.
- */
-void onDataWrittenCallback(const GattWriteCallbackParams *params) {
-    if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
-        actuatedLED = *(params->data);
-    }
-}
-
-/** 
- * This function is called when the ble initialization process has failled 
- */ 
-void onBleInitError(BLE &ble, ble_error_t error) 
-{ 
-    /* Initialization error handling should go here */ 
-} 
-
-/** 
- * Callback triggered when the ble initialization process has finished 
- */ 
-void bleInitComplete(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 onBleInitError */
-        onBleInitError(ble, error);
-        return;
-    }
-
-    /* Ensure that it is the default instance of BLE */
-    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
-        return;
-    }
-
-    ble.gap().onDisconnection(disconnectionCallback);
-    ble.gattServer().onDataWritten(onDataWrittenCallback);
-
-    bool initialValueForLEDCharacteristic = true;
-    ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
-
-    /* setup advertising */
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
-    ble.gap().startAdvertising();
-
-    while (true) {
-        ble.waitForEvent();
-    }
-}
-
-int main(void)
-{
-    BLE &ble = BLE::Instance();
-    
-    ble.init(bleInitComplete);
-}
-