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:
50:e89647e77fd5
Parent:
48:bab9f747d9ed
Child:
52:64a2c71c7c49
--- a/xDotBridge/src/main.cpp	Fri Feb 17 08:06:37 2017 -0700
+++ b/xDotBridge/src/main.cpp	Fri Feb 17 11:58:40 2017 -0700
@@ -16,14 +16,29 @@
 
 DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal
 
+volatile bool ccIntFlag;
+volatile bool tamperIntFlag;
+volatile bool pairBtnIntFlag;
+void ccInIntCallback () {
+    ccIntFlag = true;
+}
+void tamperIntCallback () {
+    tamperIntFlag = true;
+}
+void pairBtnIntCallback () {
+    pairBtnIntFlag = true;
+}
+
+
 int main() {
+    ccIntFlag = false;
+    tamperIntFlag = false;
+    pairBtnIntFlag = false;
     pc.baud(115200);
 
     CommProtocolPeerBrute protocol;
     BaseboardIO bbio;
     RadioEvent events;  // Custom event handler for automatically displaying RX data
-    WinbondSPIFlash flash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
-//    flash.frequency(48e6); // TODO try overridding for faster freq (Default 1MHz)
 
     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
 
@@ -49,42 +64,14 @@
     // PVD_LEVEL4 Falling 2.64
     // PVD_LEVEL5 Falling 2.84
     // PVD_LEVEL6 Falling 3.05
-    PWR_PVDTypeDef pvdConfig;
-    pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
-    pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
-
-    HAL_PWR_ConfigPVD(&pvdConfig);
-    HAL_PWR_EnablePVD();
-    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
-
-    // Appears to work but do not have a good way to measure power
-    for (int i=0; i<8; i++) {
-        int data = flash.readByte(0x000000+i);
-        logInfo("Idx: %d: %02X", i, data);
-    }
-    logInfo("Powering Down");
-    flash.powerDown();
-    wait(1.0);
-    for (int i=0; i<8; i++) {
-        int data = flash.readByte(0x000000+i);
-        logInfo("Idx: %d: %02X", i, data);
-    }
-    flash.releaseFromPowerDown();
-    logInfo("Waking up");
-    wait(1.0);
-    for (int i=0; i<8; i++) {
-        int data = flash.readByte(0x000000+i);
-        logInfo("Idx: %d: %02X", i, data);
-    }
-    return 0;
-
-#if BRIDGE_TX_BRUTE
-    protocol.setTx(true);
-#else
-    protocol.setTx(false);
-#endif
-    protocol.init();
+//    PWR_PVDTypeDef pvdConfig;
+//    pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
+//    pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
+//
+//    HAL_PWR_ConfigPVD(&pvdConfig);
+//    HAL_PWR_EnablePVD();
+//    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->setWakePin(WAKE);
 
@@ -98,6 +85,15 @@
     display_config();
 
     uint16_t seqNum=0;
+    Callback<void()> ccInIntObj (&ccInIntCallback);
+    Callback<void()> tamperIntObj (&tamperIntCallback);
+    Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
+    bbio.regCCInInt(ccInIntObj);
+    bbio.regTamperInt(tamperIntObj);
+    bbio.regPairBtnInt(pairBtnIntObj);
+
+    bbio.sampleUserSwitches();
+    bbio.relayNormal(); // Always force relay in known state
 
     /**
      * Main Loop
@@ -106,20 +102,38 @@
         std::vector<uint8_t> data;
         bbio.ledOff();
 
-		if (protocol.isTx()) {
-#if LED_FEEDBACK
-		    bbio.ledOn();
-#endif
-            // TODO check for CC_IN
+        // Sample IO and update any configuration
+        bool prevCCNormallyOpen = bbio.isCCNO();
+        bbio.sampleUserSwitches();
+        if (prevCCNormallyOpen == bbio.isCCNO()) { // Only activate the coil if the DIP SW has changed
+            bbio.relayNormal();
+        }
+        if (bbio.isTx()) {
+            protocol.setTx(true);
+        }
+        else { // RX
+            protocol.setTx(false);
+        }
+        // End sample and update
 
-            data.push_back((seqNum >> 8) & 0xFF);
-            data.push_back(seqNum & 0xFF);
-            protocol.send(data);
-            seqNum++;
+		if (protocol.isTx()) {
+		    if (ccIntFlag || // If contact closure in
+		       (tamperIntFlag && (bbio.isPairBtn() == false))) { // If short pair btn hit
 
-#if LED_FEEDBACK
+                ccIntFlag = false;
+                tamperIntFlag = false;
+                #if LED_FEEDBACK
+                bbio.ledOn();
+                #endif
+
+                data.push_back((seqNum >> 8) & 0xFF);
+                data.push_back(seqNum & 0xFF);
+                protocol.send(data);
+                seqNum++;
+                wait(0.5); // Leave the LED on so a person can see it.
+		    }
+
             bbio.ledOff();
-#endif
             sleep_save_io();
             sleep_configure_io();
             dot->sleep(0, mDot::INTERRUPT, false);  // Go to sleep until wake button
@@ -133,14 +147,15 @@
 		        protocol.recv(data);
                 std::string dataStr(data.begin(), data.end());
                 logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str());
-                // TODO add CC_OUT code here
+                bbio.relayAlert();
                 seqNum++;
-#if LED_FEEDBACK
+                #if LED_FEEDBACK
                 bbio.ledOn();
-                wait(0.5);
-#endif
+                #endif
+                wait(0.5); // TODO this should be configurable
 		    }
             bbio.ledOff();
+            bbio.relayNormal();
             logInfo("Sleeping.  Time %d", us_ticker_read());
             sleep_save_io();
             sleep_configure_io();
@@ -149,6 +164,7 @@
             sleep_restore_io();
 		}
 
+		// TODO maybe a good place to put pairing logic
 
         logInfo("================================");
         wait(1.0);