Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Revision:
75:600cb3a9f126
Parent:
71:2b57529df137
Child:
76:d1b20a259d8f
--- a/xDotBridge/src/main.cpp	Thu May 04 11:32:24 2017 -0600
+++ b/xDotBridge/src/main.cpp	Wed May 10 13:54:25 2017 -0600
@@ -12,19 +12,69 @@
 
 #include "BaseboardIO.h"
 #include "CommProtocolPeerBrute.h"
+#include "SerialTermMgr.h"
 
 #define RX_SEQ_LOG 1
 
-const float BridgeVersion = 0.3;
+const float BridgeVersion = 1.01;
 
 #ifndef __TEST__ // Exclude code for tests
 Serial pc(USBTX, USBRX);
+//RawSerial outPc(UART_TX, UART_RX);
+Serial *outPc;
+InterruptIn uart1RxIntIn (UART_RX);
+
+//// Serial interrupts / buffer code
+//const int buffer_size = 255;
+//// might need to increase buffer size for high baud rates
+//char tx_buffer[buffer_size+1];
+//char rx_buffer[buffer_size+1];
+//// Circular buffer pointers
+//// volatile makes read-modify-write atomic
+//volatile int tx_in=0;
+//volatile int tx_out=0;
+//volatile int rx_in=0;
+//volatile int rx_out=0;
+//
+//// Interupt Routine to read in data from serial port
+//void Rx_interrupt() {
+//    led1=1;
+//// Loop just in case more than one character is in UART's receive FIFO buffer
+//// Stop if buffer full
+//    while ((outPc.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
+//        rx_buffer[rx_in] = outPc.getc();
+//// Uncomment to Echo to USB serial to watch data flow
+////        monitor_device.putc(rx_buffer[rx_in]);
+//        rx_in = (rx_in + 1) % buffer_size;
+//    }
+//    led1=0;
+//    return;
+//}
+//
+//// Interupt Routine to write out data to serial port
+//void Tx_interrupt() {
+//    led2=1;
+//// Loop to fill more than one character in UART's transmit FIFO buffer
+//// Stop if buffer empty
+//    while ((outPc.writeable()) && (tx_in != tx_out)) {
+//        outPc.putc(tx_buffer[tx_out]);
+//        tx_out = (tx_out + 1) % buffer_size;
+//    }
+//    led2=0;
+//    return;
+//}
 
 mDot* dot = NULL; // Used by dot-utils
 
+volatile bool uartRxFlag;
 volatile bool ccIntFlag;
 volatile bool tamperIntFlag;
 volatile bool pairBtnIntFlag;
+void uart1RxCallback () {
+    uartRxFlag = true;
+//    char c = outPc.getc();
+//    printf("Got %d, %c", c, c);
+}
 void ccInIntCallback () {
     ccIntFlag = true;
 }
@@ -39,12 +89,15 @@
     CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
     BaseboardIO *bbio = new BaseboardIO();
     WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
+    SerialTermMgr serialTermMgr(bbio, BridgeVersion);
 
+    pc.baud(115200);
+//    outPc.baud(9600);
     CmdResult result;
+    uartRxFlag = false;
     ccIntFlag = false;
     tamperIntFlag = false;
     pairBtnIntFlag = false;
-    pc.baud(115200);
 
     RadioEvent events;  // Custom event handler for automatically displaying RX data
 #if RX_SEQ_LOG
@@ -105,7 +158,8 @@
 //    // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
 
     dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
-    dot->setWakePin(UART1_RX);
+    dot->setWakePin(GPIO0);
+//    dot->setWakePin(UART_RX);
 
     display_config();  // Print configuration for now
 
@@ -122,6 +176,11 @@
     }
     LedPatterns ledPatterns(bbio);
 
+//    outPc.attach(&uart1RxCallback, Serial::RxIrq);
+//    outPc.attach(&uart1RxCallback);
+//    uart1RxIntIn.rise(&uart1RxCallback);
+    uart1RxIntIn.fall(&uart1RxCallback);
+    uart1RxIntIn.enable_irq();
     Callback<void()> ccInIntObj (&ccInIntCallback);
 //    Callback<void()> tamperIntObj (&tamperIntCallback);
     Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
@@ -236,6 +295,42 @@
         }
         pairBtnIntFlag = false;
 
+        // Serial Terminal
+        if (uartRxFlag) {
+            uart1RxIntIn.disable_irq();
+//            pc.printf("Got uart flag!!!\r\n");
+            uartRxFlag = false;
+            outPc = new Serial(UART1_TX, UART1_RX);
+            serialTermMgr.regSerial(outPc);
+            char c;
+            if (outPc->readable()) {
+                c =  outPc->getc(); // Throw away the first char
+                pc.printf("Got %d, %c\r\n", c, c);
+            }
+
+            outPc->printf("Starting Terminal...\r\n");
+            serialTermMgr.printScreen();
+            time_t termLastAction = time(NULL);
+            bool quit = false;
+            while(!quit && (time(NULL) < (termLastAction + TERM_TIMEOUT))) {
+                if (outPc->readable()) {
+//                    pc.printf("Got %d\r\n", outPc->getc());
+                    termLastAction = time(NULL);
+                    quit = serialTermMgr.input();
+                }
+            }
+            if (quit) {
+                outPc->printf("Terminal quit resuming operation <press any key to reactivate>...\r\n");
+            }
+            else {
+                outPc->printf("Terminal timeout resuming operation <press any key to reactivate>...\r\n");
+            }
+            serialTermMgr.regSerial(NULL);
+            delete outPc;
+            uart1RxIntIn.enable_irq();
+        }
+        wait(0.1);
+
         myLogInfo("Loop #%d. isTX %d, CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnState %d",
                 loopCnt, protocol->isTx(), ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnState);
         // Alert code
@@ -260,11 +355,11 @@
             bbio->prepareSleep();
 		    if (bbio->isCCInAlert()) { // Still in alert mode
 		        // Sleep for 5 seconds to ensure that receiver does not miss a message sequence
-//                dot->sleep(5, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep and check in 5 secs if CCInAlert is asserted
-                dot->sleep(13, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Just for test FIXME
+                dot->sleep(5, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep and check in 5 secs if CCInAlert is asserted
 		    }
 		    else {
-                dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until wake button
+                dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until interrupt event
+//		        wait(0.5);
 		    }
 		    bbio->exitSleep();
 		}