Diseño de Firmaware Diseañado en base a los requerimientos definidos durante la etapa de analisis.

Dependencies:   BLE_API mbed

Fork of nRF51822_SimpleControls by Gustavo Belduma

Revision:
7:fb4c6c1dd3a9
Parent:
6:84154a9a8717
Child:
8:c4130ea78471
--- a/main.cpp	Fri Jul 01 15:30:22 2016 +0000
+++ b/main.cpp	Wed Aug 10 06:47:28 2016 +0000
@@ -18,7 +18,6 @@
 
 #include "mbed.h"
 #include "ble/BLE.h"
-//#include "Servo.h"
 #include "GattCallbackParamTypes.h"
 
 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
@@ -28,44 +27,24 @@
 #define TXRX_BUF_LEN                     20
 
 #define DIGITAL_OUT_7                    P0_17  //D7
-#define DIGITAL_OUT_5                    P0_23  //D5
+#define DIGITAL_OUT_8                    P0_19  //D8
+#define DIGITAL_OUT_11                   P0_12  //D11
 #define DIGITAL_OUT_4                    P0_21  //D4
 
 #define DIGITAL_IN_PIN                   P0_5   //A4
 
 #define PWM_PIN                          P0_16  //D6
-//#define SERVO_PIN                        P0_14  //D10
 #define ANALOG_IN_PIN                    P0_6   //A5
 
 // Declarando los pines
-static int32_t A_0 = 0x00;
-static int32_t A_1 = 0x00;
-static int32_t A_2 = 0x00;
-static int32_t A_3 = 0x00;
-static int32_t A_4 = 0x01;
-static int32_t A_5 = 0x01;
+//static int32_t send_config = 0;
+static int32_t enviar_config_01 = 0;
 
-static int32_t D_0 = 0x00;
-static int32_t D_1 = 0x00;
-static int32_t D_2 = 0x00;
-static int32_t D_3 = 0x00;
-static int32_t D_4 = 0x01;
-static int32_t D_5 = 0x01;
-static int32_t D_6 = 0x01;
-static int32_t D_7 = 0x01;
-static int32_t D_8 = 0x00;
-static int32_t D_9 = 0x00;
-static int32_t D_10 = 0x00;
-static int32_t D_11 = 0x00;
-static int32_t D_12 = 0x00;
-static int32_t D_13 = 0x00;
-static int32_t D_14 = 0x00;
-static int32_t D_15 = 0x00;
-static int32_t D_16 = 0x00;
-static int32_t D_17 = 0x00;
-static int32_t D_18 = 0x00;
-
-static int32_t send_config = 0;
+static int32_t sen_analog_in_5 = 0;
+static int32_t sen_digital_in_7 = 0;
+static int32_t act_analog_out_6 = 0;
+static int32_t act_analog_out_4 = 0;
+static int32_t act_analog_out_7 = 0;
 
 BLE ble;
 
@@ -74,18 +53,17 @@
 
 // Digital OUT
 DigitalOut      LED_SET_D7(DIGITAL_OUT_7);
-DigitalOut      LED_SET_D5(DIGITAL_OUT_5);
+DigitalOut      LED_SET_D8(DIGITAL_OUT_8);
+DigitalOut      LED_SET_D11(DIGITAL_OUT_11);
 DigitalOut      LED_SET_D4(DIGITAL_OUT_4);
 
 // Analog IN
 AnalogIn        ANALOG(ANALOG_IN_PIN);
 
 // Analog OUT
-
+PwmOut          PWM(PWM_PIN);
 
-PwmOut          PWM(PWM_PIN);
-//Servo           MYSERVO(SERVO_PIN);
-
+// Permite imprimir mensajes en la consola
 Serial pc(USBTX, USBRX);
 
 static uint8_t analog_enabled = 0;
@@ -97,9 +75,8 @@
 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
 
-// Declarando los vectores de configuracion para el mote.
-static const uint8_t di_conf  [] = {0xA1, 0x04, 0xA0, 0x0A, 0xD1, 0x04, 0xD0, 0x01};        // length: 10
-
+// Trama de Configuracion de los Pines (a).
+static const uint8_t di_conf  [] = {0xC01,0xA1, 20, 0xA0, 40, 0xD1, 8, 0xD0, 90};        // length: 9
 
 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
@@ -109,16 +86,6 @@
 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
-/*
-void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
-{
-    pc.printf("Disconnected \r\n");
-    pc.printf("Restart advertising \r\n");
-    ble.startAdvertising();
-}
-*/
-
-// La de arriba era la original, pero se cambio para que funcione, solucion sacada de la web
 // https://developer.mbed.org/forum/repo-61676-BLE_GAP_Example-community/topic/17193/
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
 {
@@ -126,16 +93,25 @@
     pc.printf("Disconnected \r\n");
     pc.printf("Restart advertising \r\n");
     ble.startAdvertising();
-    LED_SET_D7 = 0;
-    send_config = 0;
+    LED_SET_D11 = 0;
+    analog_enabled = 0;         //  Deja que envie lecturas el PT 1000
+    
+    sen_analog_in_5 = 0;
+    sen_digital_in_7 = 0;
+    act_analog_out_6 = 0;
+    act_analog_out_4 = 0;
+    act_analog_out_7 = 0;
+    
+    enviar_config_01 = 0;
 }
 // Ingresa por este metdo unicamente la primera vez que se conecta al mote.
 // Tomado desde: https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_LEDBlinker/file/dc392bde2b3c/main.cpp
 void connectionCallback(const Gap::ConnectionCallbackParams_t *)
 {
     pc.printf("connectionCallback \r\n");
-    LED_SET_D7 = 1;
-    send_config = 2;
+    LED_SET_D11 = 1;                //  Enciendo led que indica que alguien se conecto
+    enviar_config_01 = 1;
+    //send_config = 1;
 }
 
 // Recepta las caracteristicas que se desea escribir en el mote.
@@ -156,20 +132,72 @@
         for(index=0; index<bytesRead; index++)
             pc.putc(buf[index]);
 
-        if(buf[0] == 0x01) {
-            if(buf[1] == 0x01) {
-                LED_SET_D7 = 1;
-                LED_SET_D5 = 1;
-                LED_SET_D4 = 1;
-                //por dixys
-                pc.printf("LED SET 1 \r\n");
-            } else {
-                LED_SET_D7 = 0;
-                LED_SET_D5 = 0;
-                LED_SET_D4 = 0;
-                //por dixys
-                pc.printf("LED SET 0 \r\n");
+        pc.printf("Leemos la trama: \r\n");
+        for(index=0; index<bytesRead; index++) {
+            pc.printf("buf[%02x]: %02x\r\n", index, buf[index]);
+
+        }
+        pc.printf("Fin de la Lectura: \r\n");
+
+        // Desde el telefono desactiva el envio de tramas de la Configuracion general del mote.
+        if (buf[0] == 0xDC && buf[1] == 0xC1) {              //  Dato Configuracion
+            enviar_config_01 = buf[2];           // Debe ser cero, hace que ya no se vuelva a enviar la conf general del mote
+            sen_analog_in_5 = 1;            // habilita al Sensor PT1000, para que envie su configuracion
+        }
+        //  PT 1000
+        if (buf[0] == 0xDC && buf[1] == 0x05 && buf[2] == 0xAA && buf[3] == 20) {
+            sen_analog_in_5 = buf[4];       // Hace que ya no se vuelva a enviar este tipo de configuracion
+            sen_digital_in_7 = 1;           //  habilita al Sensor de Luz, para que envie su configuracion
+        }
+        //  sensor de luz
+        if (buf[0] == 0xDC && buf[1] == 0x05 && buf[2] == 0xDD && buf[3] == 10) {
+            sen_digital_in_7 = buf[4];       // Hace que ya no se vuelva a enviar este tipo de configuracion
+            act_analog_out_6 = 1;           //  habilita al atuador PWD, para que envie su configuracion
+        }
+        
+        //  PWD
+        if (buf[0] == 0xDC && buf[1] == 0xA && buf[2] == 0xAA && buf[3] == 40) {
+            act_analog_out_6 = buf[4];       // Hace que ya no se vuelva a enviar este tipo de configuracion
+            act_analog_out_4 = 1;           //  habilita al atuador Bomba, para que envie su configuracion
+        }
+        //  Actuador la Bomba
+        if (buf[0] == 0xDC && buf[1] == 0xA && buf[2] == 0x0A && buf[3] == 80) {
+            act_analog_out_4 = buf[4];       // Hace que ya no se vuelva a enviar este tipo de configuracion
+            act_analog_out_7 = 1;           //  habilita al atuador Foco, para que envie su configuracion
+        
+        }
+        //  Actuador Foco
+        if (buf[0] == 0xDC && buf[1] == 0xA && buf[2] == 0x0A && buf[3] == 10) {
+            act_analog_out_7 = buf[4];       // Hace que ya no se vuelva a enviar este tipo de configuracion
+        }
+
+
+        // Verifico si es una trama de Escitura.
+        if(buf[0] == 0xEE) {
+            // Verifico si es un signal Digital Out
+            if(buf[2] == 0xD0) {
+                if(buf[3] == 0x07 && buf[4] == 0x01) {
+                    LED_SET_D7 = 1;
+                } else {
+                    LED_SET_D7 = 0;
+                }
+
+                if(buf[3] == 0x04 && buf[4] == 0x01) {
+                    LED_SET_D4 = 1;
+                    analog_enabled = 1;
+                } else {
+                    LED_SET_D4 = 0;
+                    analog_enabled = 0;
+                }
+                // Verifico si es un signal Analog out
+            } else if(buf[2] == 0xA0) {
+                if(buf[3] == 0x06) {
+                    float value = (float)buf[4]/255;
+                    PWM = value;
+                    pc.printf("PWM = %f  \r\n", value);
+                }
             }
+
         } else if(buf[0] == 0xA0) {
             if(buf[1] == 0x01) {
                 analog_enabled = 1;
@@ -202,70 +230,120 @@
         }
     }
 }
+
 /*
-void uartCB(void)
-{
-    while(pc.readable())
-    {
-        rx_buf[rx_len++] = pc.getc();
-        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
-        {
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
-            pc.putc(rx_len);
-            pc.printf("\r\n");
-            rx_len = 0;
-            break;
-        }
-    }
-}
+* Desde este metodo envia las tramas al Gateway.
 */
 void m_status_check_handle(void)
 {
-    uint8_t buf2[12];
-    uint8_t buf[3];
-
-    if(send_config > 0) {
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), di_conf, 8);
-        send_config -= 1;
-        pc.printf("value: %d\r\n", send_config);
-    }
+    uint8_t tiempo_entre_envio_conf = 200;
+    uint8_t buf[5], conf_02[5];
+    conf_02[0] = (0xC02);               //  Codigo que indica que la configuracion sera por cada Pin.
 
     if (analog_enabled) { // if analog reading enabled
+        pc.printf("analog_enabled: %d\r\n", analog_enabled);
         // Read and send out
         float s = ANALOG;
+        pc.printf("value pt 1000 (s): %d\r\n", s);
         uint16_t value = s*1024;
-        buf[0] = (0x0B);
-        buf[1] = (value >> 8);
-        buf[2] = (value);
-        pc.printf("value: %d\r\n", value); // Imprimo en terminal lo que esta enviando desde el mote.
+        buf[0] = (0xDD);                    //  Codigo
+        buf[1] = (0x00);                    //  paquete id
+        buf[2] = (0xA1);                    //  A1| A0| D1| D0
+        buf[3] = (20);                      //  Posicion
+        buf[4] = (value);                   //  Valor
+        pc.printf("value pt 1000: %d\r\n", value);  // Imprimo en terminal lo que esta enviando desde el mote.
 
-        buf2[0] = 0xA1;
-        buf2[1] = 0x01;
-        buf2[2] = 0x97;
-        buf2[3] = 0x00;
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf2, 4);
-
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5);    // Para el RTD
     }
 
     // If digital in changes, report the state
     if (BUTTON != old_state) {
         old_state = BUTTON;
-
+        buf[0] = (0xDD);                    //  Codigo de Lecturas de Datos.
+        buf[1] = (0x00);                    //  Paquete
+        buf[2] = (0xD1);                    //  A1| A0| D1| D0
+        buf[3] = (8);                       //  Posicion
         if (BUTTON == 1) {
-            buf[0] = (0x0A);
-            buf[1] = (0x01);
-            buf[2] = (0x00);
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
+            pc.printf("BUTTON == 1\r\n");
+            LED_SET_D7 = 0;
+            buf[4] = (0x00);    //  Valor
         } else {
-            buf[0] = (0x0A);
-            buf[1] = (0x00);
-            buf[2] = (0x00);
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
+            pc.printf("else BUTTON == 1 \r\n");
+            LED_SET_D7 = 1;
+            buf[4] = (0x01);    //  Valor
         }
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5);
     }
+
+    //pc.printf("enviar_config %d \r\n", enviar_config_01);
+    if (enviar_config_01 == 1) {
+        //  Envia la configuracion Genaral del Mote.
+        //wait_ms(tiempo_entre_envio_conf);
+        pc.printf("enviar_config_01 %d \r\n", enviar_config_01);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), di_conf, 9);
+    }
+    
+    //  Envia la configuracion del PT 1000.
+    if (sen_analog_in_5 == 1) {
+        conf_02[1] = (0x05);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
+        conf_02[2] = (0xAA);                 //  Tipo de Signal AA | DD
+        conf_02[3] = (0);                    //  Codigo del Ted
+        conf_02[4] = (20);                   //  Posicion que ocupa en el mote
+
+        pc.printf("Configuracion Detallada por Pin PT1000\r\n");
+        //wait_ms(tiempo_entre_envio_conf);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
+    }
+    
+    //  Envia la configuracion del sensor de luz.
+    if (sen_digital_in_7 == 1) {
+        conf_02[1] = (0x05);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
+        conf_02[2] = (0xDD);                 //  Tipo de Signal AA | DD
+        conf_02[3] = (0);                    //  Codigo del Ted
+        conf_02[4] = (10);                   //  Posicion que ocupa en el mote
+
+        pc.printf("Configuracion Detallada por sensor de luz\r\n");
+        //wait_ms(tiempo_entre_envio_conf);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
+    }
+    
+    //  Envia la configuracion del PWD una sola vez.
+    if (act_analog_out_6 == 1) {
+        conf_02[1] = (0xA);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
+        conf_02[2] = (0xAA);                 //  Tipo de Signal AA | DD
+        conf_02[3] = (0);                    //  Codigo del Ted
+        conf_02[4] = (40);                   //  Posicion que ocupa en el mote
+
+        pc.printf("Configuracion Detallada por PWD\r\n");
+        //wait_ms(tiempo_entre_envio_conf);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
+    }
+    
+    //  Envia la configuracion del Actuador la Bomba una sola vez.
+    if (act_analog_out_4 == 1) {
+        conf_02[1] = (0xA);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
+        conf_02[2] = (0x0A);                 //  Tipo de Signal AA | DD
+        conf_02[3] = (0);                    //  Codigo del Ted
+        conf_02[4] = (80);                   //  Posicion que ocupa en el mote
+
+        pc.printf("Configuracion Detallada por Actuador la Bomba \r\n");
+        //wait_ms(tiempo_entre_envio_conf);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
+    }
+    
+    //  Envia la configuracion del Actuador Foco una sola vez.
+    if (act_analog_out_7 == 1) {
+        conf_02[1] = (0xA);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
+        conf_02[2] = (0x0A);                 //  Tipo de Signal AA | DD
+        conf_02[3] = (0);                    //  Codigo del Ted
+        conf_02[4] = (10);                   //  Posicion que ocupa en el mote
+
+        pc.printf("Configuracion Detallada por Actuador Foco \r\n");
+        //wait_ms(tiempo_entre_envio_conf);
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
+    }
+
+    wait_ms(100);
 }
 
 int main(void)
@@ -280,14 +358,12 @@
 
     pc.baud(9600);
     pc.printf("SimpleChat Init \r\n");
-
     //pc.attach( uartCB , pc.RxIrq);
 
     // setup advertising
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                     //(const uint8_t *)"RedBearLab_1", sizeof("RedBearLab_1") - 1);   // Original:  Biscuit
                                      (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);   // Original:  Biscuit
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));