Fork to see if I can get working

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

Fork of xDotBridge_update_test20180823 by Matt Briggs

Files at this revision

API Documentation at this revision

Comitter:
Matt Briggs
Date:
Wed May 10 13:54:25 2017 -0600
Parent:
74:dc969906f1f7
Child:
76:d1b20a259d8f
Commit message:
Skeleton version of terminal. Compiled version of prototype code

Changed in this revision

xDotBridge/config.h Show annotated file Show diff for this revision Revisions of this file
xDotBridge/inc/SerialTermMgr.h Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/BaseboardIO.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/CommProtocolPeerBrute.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/SerialTermMgr.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/main.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge_20170508_v1_01_fixWaitTime.bin Show annotated file Show diff for this revision Revisions of this file
--- a/xDotBridge/config.h	Thu May 04 11:32:24 2017 -0600
+++ b/xDotBridge/config.h	Wed May 10 13:54:25 2017 -0600
@@ -1,13 +1,10 @@
-/**
- * FIXME
- */
-
 #ifndef CONFIG_H_
 #define CONFIG_H_
 
 //#define __TEST__ 1 // Comment out for main application
 
 // Manual test.  Comment out for main application.  Or uncomment for individual test.
+
 //#define __TEST_BBIO__
 //#define __TEST_LRR__
 //#define __TEST_PVD__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xDotBridge/inc/SerialTermMgr.h	Wed May 10 13:54:25 2017 -0600
@@ -0,0 +1,52 @@
+/*
+ * SerialTermMgr.h
+ *
+ *  Created on: May 8, 2017
+ *      Author: mbriggs
+ */
+
+#ifndef XDOTBRIDGE_INC_SERIALTERMMGR_H_
+#define XDOTBRIDGE_INC_SERIALTERMMGR_H_
+
+#include "mbed.h"
+#include "BaseboardIO.h"
+
+const uint8_t TERM_TIMEOUT = 30; // In seconds
+
+enum ScreenId {
+    mainScreenId,
+    genInfoScreenId,
+    settingsScreenId,
+    statsScreenId,
+    errorLogScreenId,
+    liveLogScreenId,
+    enterSerialBridgeScreenId,
+    enterProgModeScreenId
+};
+
+
+class SerialTermMgr
+{
+private:
+    ScreenId mCurrScreen; // Current selection
+    float mFwVersion;
+    BaseboardIO *mBbio; // Handle for reading states
+    Serial *mPc;
+
+    bool inputMainPage(char in);
+    void printMainScreen();
+    void inputGenInfo (char in);
+    void printGenInfo();
+public:
+    SerialTermMgr(BaseboardIO *bbio, float fwVersion);
+    void regSerial (Serial *pc) {
+        mPc = pc;
+    }
+    ScreenId getCurrentScreenId() {
+        return mCurrScreen;
+    }
+    void printScreen();
+    bool input();
+};
+
+#endif /* XDOTBRIDGE_INC_SERIALTERMMGR_H_ */
--- a/xDotBridge/src/BaseboardIO.cpp	Thu May 04 11:32:24 2017 -0600
+++ b/xDotBridge/src/BaseboardIO.cpp	Wed May 10 13:54:25 2017 -0600
@@ -6,7 +6,7 @@
  */
 
 #include "BaseboardIO.h"
-#include "dot_util.h" // FIXME just need the reference to dot somehow
+#include "dot_util.h" // XXX just need the reference to dot somehow
 #include "xdot_low_power.h"
 #include "MyLog.h"
 
@@ -597,13 +597,14 @@
     // Try doing nothing so that the GPIO3 is still and active output
 
 
-    if ((CCInPinName != UART1_RX && TamperPinName != UART1_RX && PairBtnPinName != UART1_RX)
-            || dot->getWakeMode() == mDot::RTC_ALARM) {
-        GPIO_InitStruct.Pin = GPIO_PIN_10;
-        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
-        GPIO_InitStruct.Pull = GPIO_NOPULL;
-        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-    }
+    // Cannot do this and have serial data
+//    if ((CCInPinName != UART1_RX && TamperPinName != UART1_RX && PairBtnPinName != UART1_RX)
+//            || dot->getWakeMode() == mDot::RTC_ALARM) {
+//        GPIO_InitStruct.Pin = GPIO_PIN_10;
+//        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+//        GPIO_InitStruct.Pull = GPIO_NOPULL;
+//        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+//    }
     if ((CCInPinName != UART_CTS && TamperPinName != UART_CTS && PairBtnPinName != UART_CTS)
             || dot->getWakeMode() == mDot::RTC_ALARM) {
         GPIO_InitStruct.Pin = GPIO_PIN_11;
--- a/xDotBridge/src/CommProtocolPeerBrute.cpp	Thu May 04 11:32:24 2017 -0600
+++ b/xDotBridge/src/CommProtocolPeerBrute.cpp	Wed May 10 13:54:25 2017 -0600
@@ -292,7 +292,7 @@
 
     wait(TX_TIME/1000.0); // Wait TX_TIME
 
-    mPrevDownLinkCnt = 0;  // FIXME just test code
+    mPrevDownLinkCnt = 0;  // XXX improve how we keep track of new messages.  Maybe use lib callbacks.
     if (mPrevDownLinkCnt < dot->getDownLinkCounter()) {
         msgPending = true;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xDotBridge/src/SerialTermMgr.cpp	Wed May 10 13:54:25 2017 -0600
@@ -0,0 +1,183 @@
+/*
+ * SerialTermMgr.cpp
+ *
+ *  Created on: May 8, 2017
+ *      Author: mbriggs
+ */
+
+#include "SerialTermMgr.h"
+#include "UserInterface.h"
+
+SerialTermMgr::SerialTermMgr(BaseboardIO *bbio, float fwVersion)
+{
+    mPc = NULL;
+    mFwVersion = fwVersion;
+    mCurrScreen = mainScreenId;
+    mBbio = bbio;
+}
+void SerialTermMgr::printScreen()
+{
+    switch (mCurrScreen) {
+    case mainScreenId:
+        printMainScreen();
+        break;
+    case genInfoScreenId:
+        printGenInfo();
+        break;
+    case settingsScreenId:
+    case statsScreenId:
+    case errorLogScreenId:
+    case liveLogScreenId:
+    case enterSerialBridgeScreenId:
+    case enterProgModeScreenId:
+        printMainScreen();
+    }
+}
+bool SerialTermMgr::input()
+{
+    if (mPc == NULL) {
+        return true;
+    }
+    bool quit = false;
+    char c = 10;
+    if (mPc->readable()) {
+        c = mPc->getc();
+    }
+    else {
+        return false;  // Do nothing if there is no input
+    }
+    switch (mCurrScreen) {
+    case mainScreenId:
+        quit = inputMainPage(c);
+        break;
+    case genInfoScreenId:
+        inputGenInfo(c);
+        break;
+    case settingsScreenId:
+    case statsScreenId:
+    case errorLogScreenId:
+    case liveLogScreenId:
+    case enterSerialBridgeScreenId:
+    case enterProgModeScreenId:
+        inputMainPage(c);
+    }
+    return quit;
+}
+
+// private methods
+bool SerialTermMgr::inputMainPage (char in) {
+    bool quit = false;
+    switch (in) {
+    case '1':
+        mCurrScreen = genInfoScreenId;
+        break;
+    case '2':
+        mPc->printf("\r\nNot implemented yet.\r\n");
+//        mCurrScreen = settingsScreenId;
+        break;
+    case '3':
+        mPc->printf("\r\nNot implemented yet.\r\n");
+//        mCurrScreen = statsScreenId;
+        break;
+    case '4':
+        mPc->printf("\r\nNot implemented yet.\r\n");
+//        mCurrScreen = errorLogScreenId;
+        break;
+    case '5':
+        mPc->printf("\r\nNot implemented yet.\r\n");
+//        mCurrScreen = liveLogScreenId;
+        break;
+    case '6':
+        mPc->printf("\r\nNot implemented yet.\r\n");
+//        mCurrScreen = enterSerialBridgeScreenId;
+        break;
+    case '7':
+        mPc->printf("\r\nNot implemented yet.\r\n");
+//        mCurrScreen = enterProgModeScreenId;
+        break;
+    case 0x11: // ctrl-q
+        quit=true;
+        break;
+    default:
+        mCurrScreen = mainScreenId;
+    }
+    if (!quit) {
+        printScreen();
+    }
+    return quit;
+}
+
+void SerialTermMgr::printMainScreen()
+{
+    if (mPc == NULL) {
+        return;
+    }
+    mPc->printf("\r\n\r\n");
+    mPc->printf("===============================================\r\n");
+    mPc->printf("= Wireless Bridge Terminal (Firmware: v%0.2f)  =\r\n", mFwVersion);
+    mPc->printf("===============================================\r\n");
+    mPc->printf("===============================================\r\n");
+    mPc->printf("= Selection Options                           =\r\n");
+    mPc->printf("===============================================\r\n");
+    mPc->printf("= 0: Refresh (Enter and space also work)      =\r\n");
+    mPc->printf("= 1: General information                      =\r\n");
+    mPc->printf("= 2: Settings                                 =\r\n");
+    mPc->printf("= 3: Statistics                               =\r\n");
+    mPc->printf("= 4: Error log                                =\r\n");
+    mPc->printf("= 5: Live log                                 =\r\n");
+    mPc->printf("= 6: Enter serial bridge mode                 =\r\n");
+    mPc->printf("= 7: Enter programming mode                   =\r\n");
+    mPc->printf("= <ctrl>-q: Exit terminal                     =\r\n");
+    mPc->printf("===============================================\r\n");
+}
+
+void SerialTermMgr::inputGenInfo (char in) {
+    if (mPc == NULL) {
+        return;
+    }
+    switch (in) {
+    case 0x1B:
+        mCurrScreen = mainScreenId;
+        break;
+    case 'r':
+        mBbio->sampleUserSwitches();
+        mPc->printf("User switches sampled.\r\n");
+        break;
+    default:
+        mPc->printf("Invalid key.\r\n");
+    }
+    printScreen();
+}
+
+void SerialTermMgr::printGenInfo()
+{
+    if (mPc == NULL) {
+        return;
+    }
+    mPc->printf("\r\n\r\n");
+    mPc->printf("===============================================\r\n");
+    mPc->printf("= General Info (ESC to return to main menu)   =\r\n", mFwVersion);
+    mPc->printf("===============================================\r\n");
+    mPc->printf("= Press r to sample user switch values        =\r\n"); // FIXME
+    mPc->printf("===============================================\r\n");
+    mPc->printf("===============================================\r\n");
+    mPc->printf("= Firmware version: v%0.2f                     =\r\n", mFwVersion);
+    //TODO
+//    mPc->printf("= Radio EUI: %s =\r\n", mts::Text::bin2hexString(dot->getDeviceId()).c_str());
+    mPc->printf("= Contact closure: %s            =\r\n", mBbio->isCCNO() ? "Normally open  " :
+                                                                            "Normally closed");
+    mPc->printf("= WB Mode: %s                        =\r\n", mBbio->isTx() ? "Transmitter" :
+                                                                              "Receiver   ");
+    mPc->printf("= Comm Mode: %s                     =\r\n", mBbio->isLoRaWANMode() ? "LoRaWAN     " :
+                                                                                      "Peer-to-peer");
+    mPc->printf("= Serial Mode: %s                       =\r\n", mBbio->isSerialEnabled() ? "Enabled " :
+                                                                                            "Disabled");
+    if (mBbio->isTx()) {
+        mPc->printf("= Currently no rotary switches apply for TX   =\r\n");
+    }
+    else {
+        mPc->printf("= Rotary 1 hold setting is %0.1f seconds        =\r\n", HoldTimeSetting::rotVal2Sec(mBbio->rotarySwitch1()));
+        mPc->printf("= Rotary 2 currently does not apply for RX    =\r\n");
+    }
+    mPc->printf("===============================================\r\n");
+}
--- 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();
 		}
Binary file xDotBridge_20170508_v1_01_fixWaitTime.bin has changed