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.
Fork of nRF51822_SimpleControls by
Revision 9:501a9e6710d2, committed 2016-09-02
- Comitter:
- Gustavo_Eduardo338
- Date:
- Fri Sep 02 15:12:42 2016 +0000
- Parent:
- 8:c4130ea78471
- Commit message:
- Simple Control Caracteristicas NRF51822
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file | 
--- a/main.cpp	Wed Aug 17 22:54:54 2016 +0000
+++ b/main.cpp	Fri Sep 02 15:12:42 2016 +0000
@@ -101,13 +101,13 @@
     ble.startAdvertising();
     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.
@@ -133,17 +133,17 @@
         memcpy(txPayload, buf, TXRX_BUF_LEN);
 
         //por dixys
-        pc.printf("Buffer recibido desde Android: \r\n");
+        //pc.printf("Buffer recibido desde Android: \r\n");
 
         for(index=0; index<bytesRead; index++)
             pc.putc(buf[index]);
 
-        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("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");
+        //}
+        //pc.printf("Fin de la Lectura: \r\n");
 
         // Desde el telefono desactiva el envio de tramas de las siguientes tramas.
         if (buf[0] == 0xDC && buf[1] == 0xC1) {              //  Dato Configuracion
@@ -160,7 +160,7 @@
             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
@@ -170,13 +170,13 @@
         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
         }
-        
+
         //  Maneja las Tramas de confirmacion
         //  PWD
         if (buf[0] == 0xBB && buf[1] == 0xA0 && buf[2] == 40) {
@@ -185,7 +185,7 @@
         //  Actuador la Bomba
         if (buf[0] == 0xBB && buf[1] == 0xD0 && buf[2] == 80) {
             resp_digital_out_4 = buf[3];      // Hace que ya no se vuelva a enviar este tipo de configuracion
-        
+
         }
         //  Actuador Foco
         if (buf[0] == 0xBB && buf[1] == 0xD0 && buf[2] == 10) {
@@ -196,24 +196,26 @@
         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;
-                    resp_digital_out_7 = buf[1];        //  Paquete id
-                } else {
-                    LED_SET_D7 = 0;
+                if(buf[3] == 0x07) {
+                    if (buf[4] == 0x01) {
+                        LED_SET_D7 = 1;
+                    } else {
+                        LED_SET_D7 = 0;
+                    }
                     resp_digital_out_7 = buf[1];        //  Paquete id
                 }
 
-                if(buf[3] == 0x04 && buf[4] == 0x01) {
-                    LED_SET_D4 = 1;
-                    analog_enabled = 1;
-                    resp_digital_out_4 = buf[1];        //  Paquete id
-                } else {
-                    LED_SET_D4 = 0;
-                    analog_enabled = 0;
+                if(buf[3] == 0x04 ) {
+                    if (buf[4] == 0x01) {
+                        LED_SET_D4 = 1;
+                        analog_enabled = 1;
+                    } else {
+                        LED_SET_D4 = 0;
+                        analog_enabled = 0;
+                    }
                     resp_digital_out_4 = buf[1];        //  Paquete id
                 }
-            // Verifico si es un signal Analog out
+                // Verifico si es un signal Analog out
             } else if(buf[2] == 0xA0) {
                 if(buf[3] == 0x06) {
                     float value = (float)buf[4]/255;
@@ -222,7 +224,7 @@
                     resp_analog_out_6 = buf[1];        //  Paquete id
                 }
             }
-        
+
         } else if(buf[0] == 0xA0) {
             if(buf[1] == 0x01) {
                 analog_enabled = 1;
@@ -234,7 +236,7 @@
                 //por dixys
                 pc.printf("ANALOG DISAABLE \r\n");
                 pc.printf("valor : %d", analog_enabled);
-            }
+            } 
         } else if(buf[0] == 0x02) {
             float value = (float)buf[1]/255;
             PWM = value;
@@ -262,23 +264,26 @@
 void m_status_check_handle(void)
 {
     uint8_t tiempo_entre_envio_conf = 200;
-    uint8_t buf[5], conf_02[5];
+    uint8_t buf[6], 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] = (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.
+        buf[4] = (value >> 8);                   //  Valor
+        buf[5] = (value);                   //  Valor
+        
+        //pc.printf("buf[4]: %d\r\n", buf[4]);
+        //pc.printf("buf[5]: %d\r\n", buf[5]);
+        
+        //pc.printf("value pt 1000: %d\r\n", value);  // Imprimo en terminal lo que esta enviando desde el mote.
 
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5);    // Para el RTD
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 6);    // Para el RTD
     }
 
     // If digital in changes, report the state
@@ -287,15 +292,16 @@
         buf[0] = (0xDD);                    //  Codigo de Lecturas de Datos.
         buf[1] = (0x00);                    //  Paquete
         buf[2] = (0xD1);                    //  A1| A0| D1| D0
-        buf[3] = (10);                       //  Posicion
-        if (BUTTON == 1) {
-            pc.printf("BUTTON == 1\r\n");
+        buf[3] = (10);                      //  Posicion
+        
+        if (BUTTON == 1) {                  //  Estado natural (no pulsado)
+            pc.printf("BUTTON == 0\r\n");
             LED_SET_D7 = 0;
-            buf[4] = (0x00);    //  Valor
-        } else {
-            pc.printf("else BUTTON == 1 \r\n");
+            buf[4] = 0;                     //  Apagado
+        } else {                            //  Pulsado
+            pc.printf("BUTTON == 1 \r\n");
             LED_SET_D7 = 1;
-            buf[4] = (0x01);    //  Valor
+            buf[4] = 1;                     //  Encendido
         }
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5);
     }
@@ -306,7 +312,7 @@
         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)
@@ -317,7 +323,7 @@
         pc.printf("Configuracion Detallada por Pin PT1000\r\n");
         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)
@@ -328,7 +334,7 @@
         pc.printf("Configuracion Detallada por sensor de luz\r\n");
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
     }
-    
+
     //  Envia la configuracion del PWD.
     if (act_analog_out_6 == 1) {
         conf_02[1] = (0xA);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
@@ -339,7 +345,7 @@
         pc.printf("Configuracion Detallada por PWD\r\n");
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
     }
-    
+
     //  Envia la configuracion del Actuador la Bomba.
     if (act_analog_out_4 == 1) {
         conf_02[1] = (0xA);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
@@ -350,7 +356,7 @@
         pc.printf("Configuracion Detallada por Actuador la Bomba \r\n");
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
     }
-    
+
     //  Envia la configuracion del Actuador Foco.
     if (act_analog_out_7 == 1) {
         conf_02[1] = (0xA);                 //  Categoria puede ser: A (Actuador) | 5 (Sensor)
@@ -361,9 +367,9 @@
         pc.printf("Configuracion Detallada por Actuador Foco \r\n");
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), conf_02, 5);
     }
-    
+
     // Envia las tramas de confirmacion hacia el gateway.
-    if (resp_analog_out_6 != 0){
+    if (resp_analog_out_6 != 0) {
         // Envia trama de lectura
         buf[0] = (0xDD);                    //  Codigo
         buf[1] = resp_analog_out_6;         //  paquete id
@@ -374,8 +380,8 @@
 
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5);    // Para el RTD
     }
-    
-    if (resp_digital_out_4 != 0){
+
+    if (resp_digital_out_4 != 0) {
         // Envia trama de lectura
         buf[0] = (0xDD);                    //  Codigo
         buf[1] = resp_digital_out_4;         //  paquete id
@@ -386,8 +392,8 @@
 
         ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 5);    // Para el RTD
     }
-    
-    if (resp_digital_out_7 != 0){
+
+    if (resp_digital_out_7 != 0) {
         // Envia trama de lectura
         buf[0] = (0xDD);                    //  Codigo
         buf[1] = resp_digital_out_7;         //  paquete id
    