TOMAS GIACAMAN / Mbed 2 deprecated 170316-01_BluNode_V4_lala

Dependencies:   mbed

Fork of 170309-01_BluNode_V4 by TOMAS GIACAMAN

Files at this revision

API Documentation at this revision

Comitter:
tgiacaman
Date:
Sat Feb 04 17:04:14 2017 +0000
Parent:
2:2c98408a0a35
Child:
4:cf72ec8cf6d1
Commit message:
170204_BluNode_V1_02 previo al updateAll

Changed in this revision

Source/BluNodeMain.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Source/BluNodeMain.cpp	Sat Feb 04 17:04:14 2017 +0000
@@ -0,0 +1,347 @@
+//------------------------------------------------------------------------------
+//-- BluNodeV1_02
+//------------------------------------------------------------------------------
+
+#include "mbed.h" 
+#include "BLE.h"
+#include "UARTService.h"
+
+#include "nrf.h"
+#include "nrf_temp.h"
+#include "nrf_gpio.h"
+
+#define NEED_CONSOLE_OUTPUT 0 /*  Set this if you need debug messages on the console;
+                               * it will have an impact on code-size and power consumption. */
+
+#if NEED_CONSOLE_OUTPUT
+#define DEBUG(...) { printf(__VA_ARGS__); }
+#else
+#define DEBUG(...)  
+#endif 
+// Constructors
+UARTService *uartServicePtr;
+BLE ble;
+Ticker t;
+Timeout sleepCall;
+DigitalOut activeLed(LED1, 1);
+DigitalOut connectLed(LED2, 0);
+DigitalIn  button1(BUTTON1);  // used to trigger the time report
+InterruptIn button1Press(BUTTON1);
+//Define
+const static char     DEVICE_NAME[] = "BluNode";
+#define COMPANY_ID 0xCAFE
+//Variables
+int advCount=0;
+bool isConnected=false;
+int advActivePeriod;
+int sleepActivePeriod;
+uint8_t AdvData[] = {(COMPANY_ID & 0xFF), ((COMPANY_ID >> 8) & 0xFF), 0x00, 0x00, 0x00, 0x00,0x00,0x00,0x00,0x00, 0x00, 0x00, 0x00, 0x00}; 
+static int value0,value1,value2,value3; // stored voltage reading from ADC
+char dataOu[30];
+
+
+static uint32_t m_soc_temp;
+
+
+
+
+
+//Connection Call Back
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
+ {
+    DEBUG("Connected \n");
+   
+    connectLed=1;
+    isConnected=true;
+    
+}
+// Disconnection Call Back
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
+    ble.startAdvertising();
+    activeLed=1;
+    connectLed=0;
+    isConnected=false;
+    advCount=0;
+}
+//Analog Initialize
+void my_analogin_init(void)
+{
+
+    NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
+    NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |                                //Configure Resolution to 10 bits
+                      (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |  //Configure ADC input selection and prescalar settings
+                                                                                                    //Internal reference voltage of 1.2V. Onethird prescaling -> 3.6V 
+                      (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |                            //ADC reference selection to internal 1.2V VBG 
+                      (ADC_CONFIG_PSEL_AnalogInput0 << ADC_CONFIG_PSEL_Pos) |                       //Select Pin to be used as ADC input pin -> Disable analog pins
+                      (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);                      //Select external reference pin -> No external reference (we use internal reference of 1.2V)
+}
+
+
+
+
+
+
+
+
+
+//------------------------------------------------------------------------------
+//-- Lectura de temperatura interna del CPU
+//------------------------------------------------------------------------------
+
+void my_soc_temperature_read(void)
+{
+    NRF_TEMP->TASKS_START = 1;
+    while (NRF_TEMP->EVENTS_DATARDY == 0) {}
+    NRF_TEMP->EVENTS_DATARDY = 0;
+    m_soc_temp = NRF_TEMP->TEMP ;//(100*nrf_temp_read() / 4);
+    NRF_TEMP->TASKS_STOP = 1;
+    
+    
+}
+
+
+
+
+
+void my_analogin_read_u16A0(void)
+{ 
+
+      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
+      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput2 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
+      NRF_ADC->TASKS_START = 1;
+      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
+      value0 = (uint16_t) NRF_ADC -> RESULT;
+     
+      DEBUG("A0 Value = %i \n\r",value0);
+}
+void my_analogin_read_u16A1(void)
+{ 
+
+      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
+      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput3 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
+      NRF_ADC->TASKS_START = 1;
+      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
+      value1 = (uint16_t) NRF_ADC -> RESULT;
+      DEBUG("A1 Value = %i \n\r",value1);
+}
+
+void my_analogin_read_u16A2(void)
+{ 
+
+      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
+      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
+      NRF_ADC->TASKS_START = 1;
+      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
+      value2 = (uint16_t) NRF_ADC -> RESULT;
+      DEBUG("A2 Value = %i \n\r",value2);
+}
+
+void my_analogin_read_u16A3(void)
+{ 
+
+      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
+      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput5 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
+      NRF_ADC->TASKS_START = 1;
+      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
+      value3 = (uint16_t) NRF_ADC -> RESULT;
+      DEBUG("A3 Value = %i \n\r",value3);
+}
+
+void advReset(void)
+{ 
+
+ 
+AdvData[3]=(value1  & 0xFF);
+AdvData[2]=((value1 >> 8) & 0xFF);
+
+//AdvData[5]=(value2  & 0xFF);
+//AdvData[4]=((value2 >> 8) & 0xFF);
+
+//AdvData[7]=(value3  & 0xFF);
+//AdvData[6]=((value3 >> 8) & 0xFF);
+
+//AdvData[9]=(value0  & 0xFF);
+//AdvData[8]=((value0 >> 8) & 0xFF);
+
+
+
+AdvData[10] = (uint8_t) (( m_soc_temp & 0xFF000000) >> 24) & 0xFF;
+AdvData[11] = (uint8_t) (( m_soc_temp & 0x00FF0000) >> 16) & 0xFF;
+AdvData[12] = (uint8_t) (( m_soc_temp & 0x0000FF00) >> 8) & 0xFF;
+AdvData[13] = (uint8_t) (( m_soc_temp & 0x000000FF) & 0xFF ); 
+
+    ble.stopAdvertising(); 
+ble.clearAdvertisingPayload(); 
+             ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+            ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+            ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+            ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
+                                     AdvData, sizeof(AdvData));
+    ble.setAdvertisingInterval(600); 
+    ble.startAdvertising();
+}
+
+
+
+
+/* Callback function for taking ADC Sample */
+    
+void my_analogin_read(void)
+{ 
+if(!isConnected)
+{
+advCount++;
+ 
+             wait_ms(100);
+             my_analogin_read_u16A0();
+             wait_ms(100);
+              my_analogin_read_u16A1();
+              wait_ms(100);
+              my_analogin_read_u16A2();
+              wait_ms(100);
+              my_analogin_read_u16A3();
+              wait_ms(100);
+              my_soc_temperature_read();
+              wait_ms(100);
+             advReset();
+            
+             
+             
+             
+             
+             }
+             
+}
+
+void onDataWritten(const GattWriteCallbackParams *params)
+{
+        if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
+       uint16_t bytesRead = params->len;
+        DEBUG("received %u bytes\n\r", bytesRead);
+        if (strncmp("param1=",(const char *)params->data,7)==0)
+        {
+            sscanf((const char *)params->data,"param1=%i",&advActivePeriod);
+            sprintf(dataOu,"Adver=%i sec\n",advActivePeriod);
+            DEBUG("%s",dataOu);
+            ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu , 20);
+            
+            }
+            else if (strncmp("param2=",(const char *)params->data,7)==0)
+            {
+                sscanf((const char *)params->data,"param2=%i",&sleepActivePeriod);
+                sprintf(dataOu,"Sleep=%i sec\n",sleepActivePeriod);
+                DEBUG("%s",dataOu);
+                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
+                
+            }
+            else if (strncmp("scan",(const char *)params->data,4)==0)
+            {
+                DEBUG("Scanning\n");
+                my_analogin_read_u16A0();
+                sprintf(dataOu,"A0=%i \n",value0);
+                DEBUG("%s",dataOu);
+                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
+                wait(5);
+                my_analogin_read_u16A1();
+                sprintf(dataOu,"A1=%i \n",value1);
+                DEBUG("%s",dataOu);
+                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
+                wait(5);
+                my_analogin_read_u16A2();
+                sprintf(dataOu,"A2=%i \n",value2);
+                DEBUG("%s",dataOu);
+                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
+                wait(5);
+                my_analogin_read_u16A3();
+                sprintf(dataOu,"A3=%i \n",value3);
+                DEBUG("%s",dataOu);
+                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
+                
+                
+            }
+            
+            
+        
+    }
+}
+
+void advActiveFunc (void)
+{
+      DEBUG("\n BT WAKE UP!!");
+    activeLed=1;
+    sleepCall.detach();
+    advCount=0;
+    
+    t.attach(my_analogin_read,1); 
+    
+    }
+void SleepFunc(void)
+{
+    t.attach(my_analogin_read,1); //repeatedly read analog every second.
+    DEBUG("\nWAKE UP!!");
+      activeLed=1;
+    
+    
+}
+void BLE_Shutdown()
+{
+    ble.stopAdvertising(); 
+ble.clearAdvertisingPayload();
+}
+
+int main(void)
+{
+
+    /* Inicialización de la medición de temperatura interna */
+    nrf_temp_init();
+    
+    
+    
+    ble.init();
+    
+    ble.onDisconnection(disconnectionCallback);
+   ble.onDataWritten(onDataWritten);
+   ble.onConnection(connectionCallback);
+    /* setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
+                                     AdvData, sizeof(AdvData));
+    ble.setAdvertisingInterval(600);
+    ble.startAdvertising();
+
+    my_analogin_init(); //Initialize ADC settings
+ advActivePeriod=10;
+ sleepActivePeriod=30;
+    t.attach(my_analogin_read,1); //second.
+    UARTService uartService(ble);
+    uartServicePtr = &uartService;
+    button1Press.fall(&advActiveFunc); 
+    /* Infinite Loop*/
+    while(true)
+    {
+        ble.waitForEvent();  
+       
+        if(advCount>advActivePeriod)
+        {
+         
+           wait_ms(50);
+           t.detach (); 
+             activeLed=0;
+    
+           BLE_Shutdown();
+           
+           sleepCall.attach(SleepFunc,sleepActivePeriod);
+           advCount=0;
+           
+          
+           
+           
+        } 
+              
+    }
+}
\ No newline at end of file
--- a/main.cpp	Sat Feb 04 17:01:25 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,347 +0,0 @@
-//------------------------------------------------------------------------------
-//-- BluNodeV1_02
-//------------------------------------------------------------------------------
-
-#include "mbed.h" 
-#include "BLE.h"
-#include "UARTService.h"
-
-#include "nrf.h"
-#include "nrf_temp.h"
-#include "nrf_gpio.h"
-
-#define NEED_CONSOLE_OUTPUT 0 /*  Set this if you need debug messages on the console;
-                               * it will have an impact on code-size and power consumption. */
-
-#if NEED_CONSOLE_OUTPUT
-#define DEBUG(...) { printf(__VA_ARGS__); }
-#else
-#define DEBUG(...)  
-#endif 
-// Constructors
-UARTService *uartServicePtr;
-BLE ble;
-Ticker t;
-Timeout sleepCall;
-DigitalOut activeLed(LED1, 1);
-DigitalOut connectLed(LED2, 0);
-DigitalIn  button1(BUTTON1);  // used to trigger the time report
-InterruptIn button1Press(BUTTON1);
-//Define
-const static char     DEVICE_NAME[] = "BluNode";
-#define COMPANY_ID 0xCAFE
-//Variables
-int advCount=0;
-bool isConnected=false;
-int advActivePeriod;
-int sleepActivePeriod;
-uint8_t AdvData[] = {(COMPANY_ID & 0xFF), ((COMPANY_ID >> 8) & 0xFF), 0x00, 0x00, 0x00, 0x00,0x00,0x00,0x00,0x00, 0x00, 0x00, 0x00, 0x00}; 
-static int value0,value1,value2,value3; // stored voltage reading from ADC
-char dataOu[30];
-
-
-static uint32_t m_soc_temp;
-
-
-
-
-
-//Connection Call Back
-void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
- {
-    DEBUG("Connected \n");
-   
-    connectLed=1;
-    isConnected=true;
-    
-}
-// Disconnection Call Back
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    DEBUG("Disconnected!\n\r");
-    DEBUG("Restarting the advertising process\n\r");
-    ble.startAdvertising();
-    activeLed=1;
-    connectLed=0;
-    isConnected=false;
-    advCount=0;
-}
-//Analog Initialize
-void my_analogin_init(void)
-{
-
-    NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled;
-    NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |                                //Configure Resolution to 10 bits
-                      (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |  //Configure ADC input selection and prescalar settings
-                                                                                                    //Internal reference voltage of 1.2V. Onethird prescaling -> 3.6V 
-                      (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) |                            //ADC reference selection to internal 1.2V VBG 
-                      (ADC_CONFIG_PSEL_AnalogInput0 << ADC_CONFIG_PSEL_Pos) |                       //Select Pin to be used as ADC input pin -> Disable analog pins
-                      (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);                      //Select external reference pin -> No external reference (we use internal reference of 1.2V)
-}
-
-
-
-
-
-
-
-
-
-//------------------------------------------------------------------------------
-//-- Lectura de temperatura interna del CPU
-//------------------------------------------------------------------------------
-
-void my_soc_temperature_read(void)
-{
-    NRF_TEMP->TASKS_START = 1;
-    while (NRF_TEMP->EVENTS_DATARDY == 0) {}
-    NRF_TEMP->EVENTS_DATARDY = 0;
-    m_soc_temp = NRF_TEMP->TEMP ;//(100*nrf_temp_read() / 4);
-    NRF_TEMP->TASKS_STOP = 1;
-    
-    
-}
-
-
-
-
-
-void my_analogin_read_u16A0(void)
-{ 
-
-      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
-      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput2 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
-      NRF_ADC->TASKS_START = 1;
-      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
-      value0 = (uint16_t) NRF_ADC -> RESULT;
-     
-      DEBUG("A0 Value = %i \n\r",value0);
-}
-void my_analogin_read_u16A1(void)
-{ 
-
-      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
-      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput3 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
-      NRF_ADC->TASKS_START = 1;
-      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
-      value1 = (uint16_t) NRF_ADC -> RESULT;
-      DEBUG("A1 Value = %i \n\r",value1);
-}
-
-void my_analogin_read_u16A2(void)
-{ 
-
-      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
-      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
-      NRF_ADC->TASKS_START = 1;
-      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
-      value2 = (uint16_t) NRF_ADC -> RESULT;
-      DEBUG("A2 Value = %i \n\r",value2);
-}
-
-void my_analogin_read_u16A3(void)
-{ 
-
-      NRF_ADC->CONFIG   &= ~ADC_CONFIG_PSEL_Msk;                                                    // Check if Analog input pin has been selected
-      NRF_ADC->CONFIG   |= ADC_CONFIG_PSEL_AnalogInput5 << ADC_CONFIG_PSEL_Pos;                      //Check if Analog Input pin has been selected
-      NRF_ADC->TASKS_START = 1;
-      while (((NRF_ADC->BUSY & ADC_BUSY_BUSY_Msk) >> ADC_BUSY_BUSY_Pos) == ADC_BUSY_BUSY_Busy) {}   //Produce ADC value only when ADC is not busy
-      value3 = (uint16_t) NRF_ADC -> RESULT;
-      DEBUG("A3 Value = %i \n\r",value3);
-}
-
-void advReset(void)
-{ 
-
- 
-AdvData[3]=(value1  & 0xFF);
-AdvData[2]=((value1 >> 8) & 0xFF);
-
-//AdvData[5]=(value2  & 0xFF);
-//AdvData[4]=((value2 >> 8) & 0xFF);
-
-//AdvData[7]=(value3  & 0xFF);
-//AdvData[6]=((value3 >> 8) & 0xFF);
-
-//AdvData[9]=(value0  & 0xFF);
-//AdvData[8]=((value0 >> 8) & 0xFF);
-
-
-
-AdvData[10] = (uint8_t) (( m_soc_temp & 0xFF000000) >> 24) & 0xFF;
-AdvData[11] = (uint8_t) (( m_soc_temp & 0x00FF0000) >> 16) & 0xFF;
-AdvData[12] = (uint8_t) (( m_soc_temp & 0x0000FF00) >> 8) & 0xFF;
-AdvData[13] = (uint8_t) (( m_soc_temp & 0x000000FF) & 0xFF ); 
-
-    ble.stopAdvertising(); 
-ble.clearAdvertisingPayload(); 
-             ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-            ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-            ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-            ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
-                                     AdvData, sizeof(AdvData));
-    ble.setAdvertisingInterval(600); 
-    ble.startAdvertising();
-}
-
-
-
-
-/* Callback function for taking ADC Sample */
-    
-void my_analogin_read(void)
-{ 
-if(!isConnected)
-{
-advCount++;
- 
-             wait_ms(100);
-             my_analogin_read_u16A0();
-             wait_ms(100);
-              my_analogin_read_u16A1();
-              wait_ms(100);
-              my_analogin_read_u16A2();
-              wait_ms(100);
-              my_analogin_read_u16A3();
-              wait_ms(100);
-              my_soc_temperature_read();
-              wait_ms(100);
-             advReset();
-            
-             
-             
-             
-             
-             }
-             
-}
-
-void onDataWritten(const GattWriteCallbackParams *params)
-{
-        if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
-       uint16_t bytesRead = params->len;
-        DEBUG("received %u bytes\n\r", bytesRead);
-        if (strncmp("param1=",(const char *)params->data,7)==0)
-        {
-            sscanf((const char *)params->data,"param1=%i",&advActivePeriod);
-            sprintf(dataOu,"Adver=%i sec\n",advActivePeriod);
-            DEBUG("%s",dataOu);
-            ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu , 20);
-            
-            }
-            else if (strncmp("param2=",(const char *)params->data,7)==0)
-            {
-                sscanf((const char *)params->data,"param2=%i",&sleepActivePeriod);
-                sprintf(dataOu,"Sleep=%i sec\n",sleepActivePeriod);
-                DEBUG("%s",dataOu);
-                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
-                
-            }
-            else if (strncmp("scan",(const char *)params->data,4)==0)
-            {
-                DEBUG("Scanning\n");
-                my_analogin_read_u16A0();
-                sprintf(dataOu,"A0=%i \n",value0);
-                DEBUG("%s",dataOu);
-                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
-                wait(5);
-                my_analogin_read_u16A1();
-                sprintf(dataOu,"A1=%i \n",value1);
-                DEBUG("%s",dataOu);
-                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
-                wait(5);
-                my_analogin_read_u16A2();
-                sprintf(dataOu,"A2=%i \n",value2);
-                DEBUG("%s",dataOu);
-                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
-                wait(5);
-                my_analogin_read_u16A3();
-                sprintf(dataOu,"A3=%i \n",value3);
-                DEBUG("%s",dataOu);
-                ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(const uint8_t *)dataOu, 20);
-                
-                
-            }
-            
-            
-        
-    }
-}
-
-void advActiveFunc (void)
-{
-      DEBUG("\n BT WAKE UP!!");
-    activeLed=1;
-    sleepCall.detach();
-    advCount=0;
-    
-    t.attach(my_analogin_read,1); 
-    
-    }
-void SleepFunc(void)
-{
-    t.attach(my_analogin_read,1); //repeatedly read analog every second.
-    DEBUG("\nWAKE UP!!");
-      activeLed=1;
-    
-    
-}
-void BLE_Shutdown()
-{
-    ble.stopAdvertising(); 
-ble.clearAdvertisingPayload();
-}
-
-int main(void)
-{
-
-    /* Inicialización de la medición de temperatura interna */
-    nrf_temp_init();
-    
-    
-    
-    ble.init();
-    
-    ble.onDisconnection(disconnectionCallback);
-   ble.onDataWritten(onDataWritten);
-   ble.onConnection(connectionCallback);
-    /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
-                                     AdvData, sizeof(AdvData));
-    ble.setAdvertisingInterval(600);
-    ble.startAdvertising();
-
-    my_analogin_init(); //Initialize ADC settings
- advActivePeriod=10;
- sleepActivePeriod=30;
-    t.attach(my_analogin_read,1); //second.
-    UARTService uartService(ble);
-    uartServicePtr = &uartService;
-    button1Press.fall(&advActiveFunc); 
-    /* Infinite Loop*/
-    while(true)
-    {
-        ble.waitForEvent();  
-       
-        if(advCount>advActivePeriod)
-        {
-         
-           wait_ms(50);
-           t.detach (); 
-             activeLed=0;
-    
-           BLE_Shutdown();
-           
-           sleepCall.attach(SleepFunc,sleepActivePeriod);
-           advCount=0;
-           
-          
-           
-           
-        } 
-              
-    }
-}
\ No newline at end of file