Cambio de pin de entrada digital a PA10

Dependencies:   BufferedSerial PinDetect mbed

Fork of DTK-MEGAPACA_NUCLEO by Felícito Manzano

Revision:
6:e0f528571367
Parent:
4:8a4c7f35157c
Child:
7:0ec9cc5b9fe0
--- a/main.cpp	Mon Jul 10 21:54:02 2017 +0000
+++ b/main.cpp	Fri Jan 19 14:51:02 2018 +0000
@@ -1,18 +1,138 @@
-#include "MapleMini.h"
+/*
+ * - Proyecto:      Apagado Temporizado MEGAPACA
+ * - Lenguaje:      ANSI C/C++ (mbed)
+ * - Tarjeta:       NUCLEO F303K8
+ * - Referencias:
+ * - Fecha:         2017/Septiembre
+ * - Autor(es):     Felícito Manzano /
+                    Mario Vargas
+ * - Compañia:      V.S.R. de Centroamérica
+ * - País:          SV / CR
+ */
+
 #include "mbed.h"
-  
+#include "BufferedSerial.h"
+#include "constantes.hpp"
+#include "funciones_basicas.h"
+
+/*  CONFIGURACIÓN DE HARDWARE
+************************************** */
+BufferedSerial  gv300(PA_9, PA_10);
+Serial          pcusb(USBTX, USBRX);
+InterruptIn     input(PB_5);
+DigitalOut      myled(PB_1);
+
+// DECLARACIÓN DE VARIABLES
+//**************************************
+float           tiempo_actual           = 0.0;      // Para almacenar el valor del temporizador
+int             contador_tramas         = 2;        // Para contar las tramas enviadas a SKT300
+int             factor                  = 1;        // Para multiplicar
+int             i                       = 0;        // Uso genérico
+int             apagar                  = 0;
+char            skytrack_frame[128];                // Salida de datos para SKT300
+char            buffered_frame[128];                      // Salida temporal de datos
+
+// DECLARACIÓN DE TEMPORIZADORES
+//**************************************
+Timer           funcionando;            // Temporizador para generar el motivo por tiempo de lectura de la tarjeta NUCLEO
+Timer           apagando_cp;        // Temporizador para consultar el estatus de la puerta.
+
+
+void pressed_f()
+{
+    apagar = 1;
+}
+
 int main() {
-    confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
+    // CONFIGURAR INTERFACES
+    //***********************************
+    gv300.baud(115200);
+    pcusb.baud(115200);
+    input.fall(&pressed_f);
+    wait(1.0);
+    booting_gtdat(&gv300, &pcusb);
     
-    Serial      pc(PA_2, PA_3);
-    DigitalOut  myled(LED1);
+    // Inicio de temporizadores y RTC
+    funcionando.start();
     
     while(1) {
-        myled = 1;      // turn the on-board LED on
-        wait_ms(200);   // wait 200 milliseconds
-        myled = 0;      // turn the on-board LED off
-        wait_ms(1000);  // wait 1000 milliseconds
-        pc.printf("Blink\r\n");
+        myled = !myled;
+        wait(0.5);
+        
+        // CONSULTAR SI SE HA PRESIONADO EL BOTÓN 
+        if (apagar) {
+            pcusb.printf("\r\nBoton de panico presionado.\r\n");
+            pcusb.printf("Apagando en T-15\r\n");
+            apagar = 0;
+            apagando_cp.start();
+            sprintf(skytrack_frame, "%s%s%04X%s\r\n",
+                    ALIVE, FIN_CABECERA_TX, contador_tramas, ULTIMO_CARACTER);
+            gv300.printf("%s\r\n", skytrack_frame);
+            pcusb.printf("Iniciando temporizador 15 min.\r\n");
+            memset(skytrack_frame, '\0', sizeof(skytrack_frame));               // Vaciar el buffer de Skytrack Frame
+            incrementar_trama(&contador_tramas);
+
+        }
+
+        // VALIDAR SI SE DEBE ABORTAR
+        // Leer puerto Serial
+        i = leer_uart(&gv300, buffered_frame);
+        if (i) {
+            pcusb.printf("Trama recibida...\r\n");
+            i = procesar_trama(buffered_frame);
+            if(i == 1) { // 1 = ABORTAR
+                apagando_cp.stop();
+                apagando_cp.reset();
+                factor = 1;
+                gv300.printf("%s\r\n", CANC_SHUTDOWN);
+                pcusb.printf("Apagado por panico ABORTADO.\r\n");
+            } else {
+                pcusb.printf("Trama DESCONOCIDA.\r\n");
+            }
+        }
+
+        // CONSULTAR SI SE DEBE APAGAR
+        tiempo_actual = apagando_cp.read();                              // Leer el temporizador de Heartbeat
+        if (tiempo_actual >= (MINUTO * factor)) {
+            pcusb.printf("Apagando en T-%d\r\n", (T_15 - factor));
+            factor++;
+        }
+
+        if (tiempo_actual >= TIME_SHUTDOWN) {
+            pcusb.printf("Apangado en progreso...\r\n");
+            apagando_cp.stop();
+            apagando_cp.reset();
+            factor = 1;
+            gv300.printf("%s\r\n", SHUTDOWN_TXT);
+            wait_ms(T_TX);
+            // Leer puerto Serial
+            i = leer_uart(&gv300, buffered_frame);
+            if (i) {
+                // Confirmar resultado de Apagado
+                i = procesar_trama(buffered_frame);
+                if (i == 2) { // +ACK
+                    gv300.printf("%s\r\n", SHUTDOWN_DONE);
+                    pcusb.printf("Apagado remoto +EXITOSO!\r\n");
+                } else {
+                    gv300.printf("%s\r\n", SHUTDOWN_ERRO);
+                    pcusb.printf("Apagado remoto -FALLIDO!\r\n");    
+                }
+            }  else { 
+                gv300.printf("%s\r\n", SHUTDOWN_ERRO);
+                pcusb.printf("Apagado remoto -FALLIDO!\r\n");
+            }
+        }
+
+        // ENVIAR HEARTBEAT A SKYTRACK
+        tiempo_actual = funcionando.read();                              // Leer el temporizador de Heartbeat
+        if (tiempo_actual >= TIME_HEARTBEAT) {
+            pcusb.printf("\r\nMultipuertos estable.\r\n");
+            funcionando.reset();
+            sprintf(skytrack_frame, "%s%s%04X%s\r\n",
+                    ALIVE, FIN_CABECERA_TX, contador_tramas, ULTIMO_CARACTER);
+            gv300.printf("%s\r\n", skytrack_frame);
+            memset(skytrack_frame, '\0', sizeof(skytrack_frame));               // Vaciar el buffer de Skytrack Frame
+            incrementar_trama(&contador_tramas);
+        }
     }
-}
- 
\ No newline at end of file
+} 
\ No newline at end of file