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:
58:15aa7a785b9f
Parent:
57:bdac7dd17af2
Child:
60:5179449a684f
--- a/xDotBridge/src/main.cpp	Tue Feb 28 09:26:27 2017 -0700
+++ b/xDotBridge/src/main.cpp	Wed Mar 01 17:28:32 2017 -0700
@@ -4,7 +4,9 @@
 #include "dot_util.h"
 #include "RadioEvent.h"
 #include "WinbondSPIFlash.h"
+#include "UserInterface.h"
 //#include <xdot_low_power.h>
+#include "mDot.h"
 
 #include "BaseboardIO.h"
 #include "CommProtocolPeerBrute.h"
@@ -14,7 +16,7 @@
 
 mDot* dot = NULL; // Used by dot-utils
 
-//DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
+DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
 
 volatile bool ccIntFlag;
 volatile bool tamperIntFlag;
@@ -29,10 +31,11 @@
     pairBtnIntFlag = true;
 }
 
-
 int main() {
     CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
     BaseboardIO *bbio = new BaseboardIO();
+    WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
+
     CmdResult result;
     ccIntFlag = false;
     tamperIntFlag = false;
@@ -87,9 +90,9 @@
 //    logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
 //    // 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);
 
-
     // display configuration
 //    display_config();
 
@@ -110,8 +113,15 @@
 
     bbio->sampleUserSwitches();
     bbio->relayNormal(); // Always force relay in known state
+    bbio->regCCInInt(ccInIntObj);
+    bbio->regTamperInt(tamperIntObj);
+    bbio->regPairBtnInt(pairBtnIntObj);
 
-    unsigned int intCnt = 0; // Just a quick temp varaible to keep track of ints
+    // Start flash powered down
+    flash->powerDown();
+
+    unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
+    bool prevCCNormallyOpen;
     /**
      * Main Loop
      */
@@ -120,12 +130,10 @@
         bbio->ledOff();
 
         // Sample IO and update any configuration
-        bool prevCCNormallyOpen = bbio->isCCNO();
+        prevCCNormallyOpen = bbio->isCCNO();
         bbio->sampleUserSwitches();
         if (prevCCNormallyOpen == bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
             bbio->regCCInInt(ccInIntObj);
-            bbio->regTamperInt(tamperIntObj);
-            bbio->regPairBtnInt(pairBtnIntObj);
             bbio->relayNormal();
         }
         if (bbio->isTx()) {
@@ -137,8 +145,8 @@
         // End sample and update
 
 		if (protocol->isTx()) {
-            logInfo("Status #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
-                    intCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
+            logInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
+                    loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
             // TODO add tamper
             if (pairBtnIntFlag) { // Wait up to 1 second for short button hit
                 for (int i=0; i<10;i++) {
@@ -169,21 +177,20 @@
 		    }
 
             bbio->ledOff();
-//            sleep_save_io();
-//            sleep_configure_io();
+            bbio->prepareSleep();
 		    if (bbio->isCCInAlert()) { // Still in alert mode
-                dot->sleep(10, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep and check in 2 secs if CCInAlert is asserted
+                dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep and check in 2 secs if CCInAlert is asserted
 		    }
 		    else {
                 dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until wake button
 		    }
-//            sleep_restore_io();
+		    bbio->exitSleep();
 		}
 
 		if (protocol->isRx()) {
 		    bool msgPending;
 		    protocol->listen(msgPending);
-		    logInfo("Listening.");
+		    logInfo("Loop Cnt %d.  Listening.", loopCnt);
 		    if (msgPending) {
 		        protocol->recv(data);
                 std::string dataStr(data.begin(), data.end());
@@ -193,27 +200,25 @@
                 #if LED_FEEDBACK
                 bbio->ledOn();
                 #endif
-                wait(5.0); // TODO this should be configurable
+                // Hold time for alert
+                // TODO maybe use sleep instead of wait
+                logInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
+                wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
+                bbio->ledOff();
+                bbio->relayNormal();
 		    }
-            bbio->ledOff();
-            bbio->relayNormal();
             logInfo("Sleeping.  Time %d", us_ticker_read());
-//            sleep_save_io();
-//            sleep_configure_io();
-            // TODO maybe add if statement here to prevent double hits by sleeping for a longer time
             protocol->sampleDLC();
+            bbio->prepareSleep();
             dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false);  // Go to sleep until wake button
-//            sleep_restore_io();
+            bbio->exitSleep();
 		}
 
 		// TODO maybe a good place to put pairing logic
 
-        logInfo("================================");
+        logInfo("\r\n================================");
         wait(1.0); // May want to remove
-        // Need to re-implement some of these sleep functions
-//		sleep_save_io();
-//		sleep_configure_io();
-        intCnt++;
+        loopCnt++;
     }
 
     delete protocol;