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:
69:eb391644b346
Parent:
68:51c25f4f6d9a
Child:
71:2b57529df137
--- a/xDotBridge/src/main.cpp	Tue Mar 21 14:29:32 2017 -0600
+++ b/xDotBridge/src/main.cpp	Fri Mar 24 08:15:37 2017 -0600
@@ -8,10 +8,15 @@
 //#include <xdot_low_power.h>
 #include "mDot.h"
 #include "MyLog.h"
+#include "SimpleRxSeqLog.h"
 
 #include "BaseboardIO.h"
 #include "CommProtocolPeerBrute.h"
 
+#define RX_SEQ_LOG 1
+
+const float BridgeVersion = 0.2;
+
 #ifndef __TEST__ // Exclude code for tests
 Serial pc(USBTX, USBRX);
 
@@ -42,6 +47,10 @@
     pc.baud(115200);
 
     RadioEvent events;  // Custom event handler for automatically displaying RX data
+#if RX_SEQ_LOG
+    SimpleRxSeqLog rxSeqLog(0x0000);
+    rxSeqLog.read();
+#endif
 
     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
     MyLog::setLogLevel(MyLog::DEBUG_LEVEL);
@@ -53,6 +62,7 @@
 
     myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
     myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
+    myLogInfo("Vortex Wireless Bridge SW Version %0.2f", BridgeVersion);
 
     // attach the custom events handler
     dot->setEvents(&events);  // Little bonus event debug information
@@ -113,13 +123,13 @@
     LedPatterns ledPatterns(bbio);
 
     Callback<void()> ccInIntObj (&ccInIntCallback);
-    Callback<void()> tamperIntObj (&tamperIntCallback);
+//    Callback<void()> tamperIntObj (&tamperIntCallback);
     Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
 
     bbio->sampleUserSwitches();
     bbio->relayNormal(); // Always force relay in known state
     bbio->regCCInInt(ccInIntObj);
-    bbio->regTamperInt(tamperIntObj);
+//    bbio->regTamperInt(tamperIntObj);
     bbio->regPairBtnInt(pairBtnIntObj);
 
     if (bbio->isTx()) {
@@ -132,15 +142,16 @@
     // Start flash powered down
     flash->powerDown();
 
-    unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
+    unsigned int loopCnt = 0; // Just a quick temp variable to keep track of loopNums
     bool prevCCNormallyOpen;
     PairBtnState pairBtnState;
     uint32_t rxMsgCnt = 0;
+    uint32_t maxSeenMsgSeqNum = 0;
     /**
      * Main Loop
      */
     while (true) {
-        myLogInfo("Start of loop time %d", us_ticker_read());
+//        myLogInfo("Start of loop time %d", us_ticker_read());
         ledPatterns.turnOff();
 
         // Pair logic and switch sampling
@@ -210,15 +221,19 @@
                 }
             }
         }
+        else {
+            pairBtnState = pairBtnNoPress;
+        }
+        pairBtnIntFlag = false;
 
+        myLogInfo("Loop #%d. isTX %d, CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnState %d",
+                loopCnt, protocol->isTx(), ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnState);
         // Alert code
 		if (protocol->isTx()) {
-            myLogInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
-                    loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
             // TODO add tamper
 		    if (ccIntFlag || // If contact closure in
 		       bbio->isCCInAlert() || // If closure remains in effect
-		       (pairBtnIntFlag && (pairBtnState == pairBtnShortPress))) {
+		       pairBtnState == pairBtnShortPress) {
                 ccIntFlag = false;
                 ledPatterns.turnOn();
 
@@ -228,8 +243,10 @@
 		    }
 
             ledPatterns.turnOff();
-            pairBtnIntFlag = false;
-            tamperIntFlag = false;
+//            tamperIntFlag = false;
+            if (pairBtnIntFlag) {// An Interrupt happened during transmission
+                continue; // Go straight to pair handling
+            }
             bbio->prepareSleep();
 		    if (bbio->isCCInAlert()) { // Still in alert mode
 		        // Sleep for 5 seconds to ensure that receiver does not miss a message sequence
@@ -243,19 +260,20 @@
 
 		if (protocol->isRx()) {
             ccIntFlag = false;
-            pairBtnIntFlag = false;
-            tamperIntFlag = false;
-            myLogInfo("Before listen time %d", us_ticker_read());
+//            tamperIntFlag = false;
+//            myLogInfo("Before listen time %d", us_ticker_read());
 		    bool msgPending;
 		    protocol->listen(msgPending);
-		    myLogInfo("Loop Cnt %d.  Listening.", loopCnt);
 		    if (msgPending) {
 		        std::vector<uint8_t> txEui;
 		        txEui.reserve(8);
 		        uint16_t data;
 		        uint32_t msgSeqNum;
 		        protocol->recvAlert(txEui, data, msgSeqNum);
-                myLogInfo("Got msg #%d,  with Seqnum: %d", rxMsgCnt, msgSeqNum);
+		        if (msgSeqNum > maxSeenMsgSeqNum)
+		            maxSeenMsgSeqNum = msgSeqNum;
+		        rxMsgCnt++; // for message
+                myLogInfo("Got rxMsgCnt #%d,  with Seqnum: %d", rxMsgCnt, msgSeqNum);
                 bbio->relayAlert();
                 ledPatterns.turnOn();
                 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
@@ -265,6 +283,23 @@
                 ledPatterns.turnOff();
                 bbio->relayNormal();
 		    }
+            if (pairBtnIntFlag) {// An Interrupt happened during reception
+                continue; // Go straight to pair handling
+            }
+            //// TEMP LOGGING
+#if RX_SEQ_LOG
+            if ((loopCnt % 1000 == 0) &&
+                rxMsgCnt > rxSeqLog.rxMsgCount()) {
+                rxSeqLog.setLoopCount(loopCnt);
+                rxSeqLog.setRxMsgCount(rxMsgCnt);
+                rxSeqLog.setMaxSeenMsgSeqNum(maxSeenMsgSeqNum);
+                rxSeqLog.save();
+                myLogInfo("EEPROM Saved.");
+            }
+            myLogInfo("NVM Log: Loop Cnt: %d, RxMsgCnt %d, MaxSeenSeqNum %d",
+                    rxSeqLog.loopCount(), rxSeqLog.rxMsgCount(), rxSeqLog.maxSeenMsgSeqNum());
+#endif
+
             myLogInfo("Sleeping.  Time %d", us_ticker_read());
             bbio->prepareSleep();
             dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep until wake button