![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/src/main.cpp@71:2b57529df137, 2017-03-27 (annotated)
- Committer:
- Matt Briggs
- Date:
- Mon Mar 27 13:04:39 2017 -0600
- Revision:
- 71:2b57529df137
- Parent:
- 69:eb391644b346
- Child:
- 75:600cb3a9f126
Version modified for testing on 2017/03/27
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 34:5618603e5fc3 | 1 | #include <math.h> |
Matt Briggs | 22:9453658b8d4b | 2 | #include "config.h" |
Matt Briggs | 30:2e673a672884 | 3 | #include "xdot_flash.h" |
Mike Fiore |
11:d2e31743433a | 4 | #include "dot_util.h" |
Mike Fiore |
14:19fae4509473 | 5 | #include "RadioEvent.h" |
Matt Briggs | 47:a68747642a7a | 6 | #include "WinbondSPIFlash.h" |
Matt Briggs | 58:15aa7a785b9f | 7 | #include "UserInterface.h" |
Matt Briggs | 41:9ef4c4d77711 | 8 | //#include <xdot_low_power.h> |
Matt Briggs | 58:15aa7a785b9f | 9 | #include "mDot.h" |
Matt Briggs | 63:e1efbe3402d9 | 10 | #include "MyLog.h" |
Matt Briggs | 69:eb391644b346 | 11 | #include "SimpleRxSeqLog.h" |
mfiore | 17:d4f82e16de5f | 12 | |
Matt Briggs | 47:a68747642a7a | 13 | #include "BaseboardIO.h" |
Matt Briggs | 41:9ef4c4d77711 | 14 | #include "CommProtocolPeerBrute.h" |
Matt Briggs | 31:9c535a708ae9 | 15 | |
Matt Briggs | 69:eb391644b346 | 16 | #define RX_SEQ_LOG 1 |
Matt Briggs | 69:eb391644b346 | 17 | |
Matt Briggs | 71:2b57529df137 | 18 | const float BridgeVersion = 0.3; |
Matt Briggs | 69:eb391644b346 | 19 | |
Matt Briggs | 47:a68747642a7a | 20 | #ifndef __TEST__ // Exclude code for tests |
Mike Fiore |
11:d2e31743433a | 21 | Serial pc(USBTX, USBRX); |
Mike Fiore |
11:d2e31743433a | 22 | |
Matt Briggs | 47:a68747642a7a | 23 | mDot* dot = NULL; // Used by dot-utils |
Matt Briggs | 39:64f79fa6e3cc | 24 | |
Matt Briggs | 50:e89647e77fd5 | 25 | volatile bool ccIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 26 | volatile bool tamperIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 27 | volatile bool pairBtnIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 28 | void ccInIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 29 | ccIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 30 | } |
Matt Briggs | 50:e89647e77fd5 | 31 | void tamperIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 32 | tamperIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 33 | } |
Matt Briggs | 50:e89647e77fd5 | 34 | void pairBtnIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 35 | pairBtnIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 36 | } |
Matt Briggs | 50:e89647e77fd5 | 37 | |
Mike Fiore |
11:d2e31743433a | 38 | int main() { |
Matt Briggs | 53:a1563574a980 | 39 | CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute(); |
Matt Briggs | 53:a1563574a980 | 40 | BaseboardIO *bbio = new BaseboardIO(); |
Matt Briggs | 58:15aa7a785b9f | 41 | WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS); |
Matt Briggs | 58:15aa7a785b9f | 42 | |
Matt Briggs | 52:64a2c71c7c49 | 43 | CmdResult result; |
Matt Briggs | 50:e89647e77fd5 | 44 | ccIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 45 | tamperIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 46 | pairBtnIntFlag = false; |
Matt Briggs | 48:bab9f747d9ed | 47 | pc.baud(115200); |
Matt Briggs | 48:bab9f747d9ed | 48 | |
Matt Briggs | 31:9c535a708ae9 | 49 | RadioEvent events; // Custom event handler for automatically displaying RX data |
Matt Briggs | 69:eb391644b346 | 50 | #if RX_SEQ_LOG |
Matt Briggs | 69:eb391644b346 | 51 | SimpleRxSeqLog rxSeqLog(0x0000); |
Matt Briggs | 69:eb391644b346 | 52 | rxSeqLog.read(); |
Matt Briggs | 69:eb391644b346 | 53 | #endif |
Mike Fiore |
11:d2e31743433a | 54 | |
Matt Briggs | 27:6b68ff715ae1 | 55 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Matt Briggs | 66:bcaa6dbf538a | 56 | MyLog::setLogLevel(MyLog::DEBUG_LEVEL); |
Matt Briggs | 47:a68747642a7a | 57 | |
Mike Fiore |
11:d2e31743433a | 58 | dot = mDot::getInstance(); |
Mike Fiore |
11:d2e31743433a | 59 | |
Mike Fiore |
11:d2e31743433a | 60 | // make sure library logging is turned on |
Matt Briggs | 66:bcaa6dbf538a | 61 | dot->setLogLevel(mts::MTSLog::WARNING_LEVEL); |
Mike Fiore |
11:d2e31743433a | 62 | |
Matt Briggs | 63:e1efbe3402d9 | 63 | myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Matt Briggs | 63:e1efbe3402d9 | 64 | myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str()); |
Matt Briggs | 69:eb391644b346 | 65 | myLogInfo("Vortex Wireless Bridge SW Version %0.2f", BridgeVersion); |
Matt Briggs | 62:9751a8504c82 | 66 | |
Mike Fiore |
11:d2e31743433a | 67 | // attach the custom events handler |
Matt Briggs | 54:c04d7b6fa075 | 68 | dot->setEvents(&events); // Little bonus event debug information |
Matt Briggs | 53:a1563574a980 | 69 | |
Matt Briggs | 53:a1563574a980 | 70 | // Finish radio init |
Matt Briggs | 63:e1efbe3402d9 | 71 | myLogInfo("= Protocol Init Starting =\r\n"); |
Matt Briggs | 53:a1563574a980 | 72 | protocol->init(); |
Matt Briggs | 62:9751a8504c82 | 73 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 74 | myLogInfo("= Protocol Init Finished Successfully =\r\n"); |
Matt Briggs | 62:9751a8504c82 | 75 | } |
Matt Briggs | 62:9751a8504c82 | 76 | else { |
Matt Briggs | 63:e1efbe3402d9 | 77 | myLogInfo("= Protocol Init Finished with Error =\r\n"); |
Matt Briggs | 62:9751a8504c82 | 78 | } |
Matt Briggs | 63:e1efbe3402d9 | 79 | protocol->printDotConfig(); |
Matt Briggs | 53:a1563574a980 | 80 | |
Matt Briggs | 53:a1563574a980 | 81 | // save changes to configuration |
Matt Briggs | 53:a1563574a980 | 82 | // logInfo("saving configuration"); |
Matt Briggs | 53:a1563574a980 | 83 | // core_util_critical_section_enter(); |
Matt Briggs | 53:a1563574a980 | 84 | // bool dotConfigSaved = dot->saveConfig(); |
Matt Briggs | 53:a1563574a980 | 85 | // core_util_critical_section_exit(); |
Matt Briggs | 53:a1563574a980 | 86 | // if (!dotConfigSaved) { |
Matt Briggs | 53:a1563574a980 | 87 | // logError("failed to save configuration"); |
Matt Briggs | 53:a1563574a980 | 88 | // } |
Mike Fiore |
11:d2e31743433a | 89 | |
Matt Briggs | 31:9c535a708ae9 | 90 | // Setup programmable voltage detector |
Matt Briggs | 31:9c535a708ae9 | 91 | // PVD_LEVEL0 Falling 1.85 |
Matt Briggs | 31:9c535a708ae9 | 92 | // PVD_LEVEL1 Falling 2.04 |
Matt Briggs | 31:9c535a708ae9 | 93 | // PVD_LEVEL2 Falling 2.24 |
Matt Briggs | 31:9c535a708ae9 | 94 | // PVD_LEVEL3 Falling 2.44 |
Matt Briggs | 31:9c535a708ae9 | 95 | // PVD_LEVEL4 Falling 2.64 |
Matt Briggs | 31:9c535a708ae9 | 96 | // PVD_LEVEL5 Falling 2.84 |
Matt Briggs | 31:9c535a708ae9 | 97 | // PVD_LEVEL6 Falling 3.05 |
Matt Briggs | 50:e89647e77fd5 | 98 | // PWR_PVDTypeDef pvdConfig; |
Matt Briggs | 50:e89647e77fd5 | 99 | // pvdConfig.Mode = PWR_PVD_MODE_NORMAL; |
Matt Briggs | 50:e89647e77fd5 | 100 | // pvdConfig.PVDLevel = PWR_PVDLEVEL_5; |
Matt Briggs | 50:e89647e77fd5 | 101 | // |
Matt Briggs | 50:e89647e77fd5 | 102 | // HAL_PWR_ConfigPVD(&pvdConfig); |
Matt Briggs | 50:e89647e77fd5 | 103 | // HAL_PWR_EnablePVD(); |
Matt Briggs | 50:e89647e77fd5 | 104 | // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel); |
Matt Briggs | 50:e89647e77fd5 | 105 | // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation |
Mike Fiore |
11:d2e31743433a | 106 | |
Matt Briggs | 58:15aa7a785b9f | 107 | dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT); |
Matt Briggs | 54:c04d7b6fa075 | 108 | dot->setWakePin(UART1_RX); |
Matt Briggs | 24:fdf87e4b72e5 | 109 | |
Matt Briggs | 62:9751a8504c82 | 110 | display_config(); // Print configuration for now |
Matt Briggs | 62:9751a8504c82 | 111 | |
Mike Fiore |
11:d2e31743433a | 112 | // display configuration |
Matt Briggs | 53:a1563574a980 | 113 | // display_config(); |
Matt Briggs | 52:64a2c71c7c49 | 114 | |
Matt Briggs | 63:e1efbe3402d9 | 115 | myLogInfo("= Baseboard Init Starting =\r\n"); |
Matt Briggs | 53:a1563574a980 | 116 | result = bbio->init(); |
Matt Briggs | 52:64a2c71c7c49 | 117 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 118 | myLogInfo("= Baseboard Init Finished Successfully =\r\n"); |
Matt Briggs | 52:64a2c71c7c49 | 119 | } |
Matt Briggs | 52:64a2c71c7c49 | 120 | else { |
Matt Briggs | 63:e1efbe3402d9 | 121 | myLogInfo("= Baseboard Init Finished with Error =\r\n"); |
Matt Briggs | 52:64a2c71c7c49 | 122 | } |
Matt Briggs | 64:46c8819c07cc | 123 | LedPatterns ledPatterns(bbio); |
Mike Fiore |
11:d2e31743433a | 124 | |
Matt Briggs | 50:e89647e77fd5 | 125 | Callback<void()> ccInIntObj (&ccInIntCallback); |
Matt Briggs | 69:eb391644b346 | 126 | // Callback<void()> tamperIntObj (&tamperIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 127 | Callback<void()> pairBtnIntObj (&pairBtnIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 128 | |
Matt Briggs | 53:a1563574a980 | 129 | bbio->sampleUserSwitches(); |
Matt Briggs | 53:a1563574a980 | 130 | bbio->relayNormal(); // Always force relay in known state |
Matt Briggs | 58:15aa7a785b9f | 131 | bbio->regCCInInt(ccInIntObj); |
Matt Briggs | 69:eb391644b346 | 132 | // bbio->regTamperInt(tamperIntObj); |
Matt Briggs | 58:15aa7a785b9f | 133 | bbio->regPairBtnInt(pairBtnIntObj); |
Matt Briggs | 26:9411b26a5084 | 134 | |
Matt Briggs | 66:bcaa6dbf538a | 135 | if (bbio->isTx()) { |
Matt Briggs | 66:bcaa6dbf538a | 136 | protocol->setTx(true); |
Matt Briggs | 66:bcaa6dbf538a | 137 | } |
Matt Briggs | 66:bcaa6dbf538a | 138 | else { // RX |
Matt Briggs | 66:bcaa6dbf538a | 139 | protocol->setTx(false); |
Matt Briggs | 66:bcaa6dbf538a | 140 | } |
Matt Briggs | 66:bcaa6dbf538a | 141 | |
Matt Briggs | 58:15aa7a785b9f | 142 | // Start flash powered down |
Matt Briggs | 58:15aa7a785b9f | 143 | flash->powerDown(); |
Matt Briggs | 58:15aa7a785b9f | 144 | |
Matt Briggs | 69:eb391644b346 | 145 | unsigned int loopCnt = 0; // Just a quick temp variable to keep track of loopNums |
Matt Briggs | 58:15aa7a785b9f | 146 | bool prevCCNormallyOpen; |
Matt Briggs | 61:8d9efd33cac9 | 147 | PairBtnState pairBtnState; |
Matt Briggs | 67:2115a2f1b945 | 148 | uint32_t rxMsgCnt = 0; |
Matt Briggs | 69:eb391644b346 | 149 | uint32_t maxSeenMsgSeqNum = 0; |
Matt Briggs | 47:a68747642a7a | 150 | /** |
Matt Briggs | 47:a68747642a7a | 151 | * Main Loop |
Matt Briggs | 47:a68747642a7a | 152 | */ |
Mike Fiore |
11:d2e31743433a | 153 | while (true) { |
Matt Briggs | 69:eb391644b346 | 154 | // myLogInfo("Start of loop time %d", us_ticker_read()); |
Matt Briggs | 64:46c8819c07cc | 155 | ledPatterns.turnOff(); |
Matt Briggs | 47:a68747642a7a | 156 | |
Matt Briggs | 66:bcaa6dbf538a | 157 | // Pair logic and switch sampling |
Matt Briggs | 61:8d9efd33cac9 | 158 | if (pairBtnIntFlag) { |
Matt Briggs | 61:8d9efd33cac9 | 159 | pairBtnState = PairBtnInterp::read(bbio); |
Matt Briggs | 66:bcaa6dbf538a | 160 | |
Matt Briggs | 66:bcaa6dbf538a | 161 | // Sample IO and update any configuration |
Matt Briggs | 66:bcaa6dbf538a | 162 | prevCCNormallyOpen = bbio->isCCNO(); |
Matt Briggs | 66:bcaa6dbf538a | 163 | bbio->sampleUserSwitches(); |
Matt Briggs | 66:bcaa6dbf538a | 164 | if (prevCCNormallyOpen != bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed |
Matt Briggs | 66:bcaa6dbf538a | 165 | bbio->regCCInInt(ccInIntObj); |
Matt Briggs | 66:bcaa6dbf538a | 166 | bbio->relayNormal(); |
Matt Briggs | 66:bcaa6dbf538a | 167 | } |
Matt Briggs | 66:bcaa6dbf538a | 168 | if (bbio->isTx()) { |
Matt Briggs | 66:bcaa6dbf538a | 169 | protocol->setTx(true); |
Matt Briggs | 66:bcaa6dbf538a | 170 | } |
Matt Briggs | 66:bcaa6dbf538a | 171 | else { // RX |
Matt Briggs | 66:bcaa6dbf538a | 172 | protocol->setTx(false); |
Matt Briggs | 66:bcaa6dbf538a | 173 | } |
Matt Briggs | 61:8d9efd33cac9 | 174 | if (protocol->isTx()) { |
Matt Briggs | 61:8d9efd33cac9 | 175 | if (pairBtnState == pairBtnMediumPress) { |
Matt Briggs | 62:9751a8504c82 | 176 | protocol->configForPairingNetwork(); |
Matt Briggs | 61:8d9efd33cac9 | 177 | protocol->sendPairReq(); |
Matt Briggs | 63:e1efbe3402d9 | 178 | myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME); |
Matt Briggs | 61:8d9efd33cac9 | 179 | result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME); |
Matt Briggs | 61:8d9efd33cac9 | 180 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 181 | myLogInfo("Got accept"); |
Matt Briggs | 64:46c8819c07cc | 182 | ledPatterns.turnOff(); |
Matt Briggs | 64:46c8819c07cc | 183 | wait(0.5); |
Matt Briggs | 64:46c8819c07cc | 184 | ledPatterns.tripleBlink(); |
Matt Briggs | 61:8d9efd33cac9 | 185 | } |
Matt Briggs | 61:8d9efd33cac9 | 186 | else { |
Matt Briggs | 63:e1efbe3402d9 | 187 | myLogInfo("Did not receive accept"); |
Matt Briggs | 61:8d9efd33cac9 | 188 | } |
Matt Briggs | 62:9751a8504c82 | 189 | protocol->configForSavedNetwork(); |
Matt Briggs | 63:e1efbe3402d9 | 190 | protocol->printDotConfig(); |
Matt Briggs | 64:46c8819c07cc | 191 | protocol->resetCounters(); |
Matt Briggs | 71:2b57529df137 | 192 | |
Matt Briggs | 71:2b57529df137 | 193 | // Clear test log |
Matt Briggs | 71:2b57529df137 | 194 | rxSeqLog.clear(); |
Matt Briggs | 71:2b57529df137 | 195 | rxSeqLog.save(); |
Matt Briggs | 71:2b57529df137 | 196 | myLogInfo("NVM Cleared Seq Log"); |
Matt Briggs | 61:8d9efd33cac9 | 197 | } |
Matt Briggs | 61:8d9efd33cac9 | 198 | } |
Matt Briggs | 61:8d9efd33cac9 | 199 | if (protocol->isRx()) { |
Matt Briggs | 61:8d9efd33cac9 | 200 | if (pairBtnState == pairBtnMediumPress) { |
Matt Briggs | 62:9751a8504c82 | 201 | protocol->configForPairingNetwork(); |
Matt Briggs | 64:46c8819c07cc | 202 | ledPatterns.turnOn(); |
Matt Briggs | 63:e1efbe3402d9 | 203 | myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME); |
Matt Briggs | 61:8d9efd33cac9 | 204 | result = protocol->waitForPairing(RX_PAIR_WAIT_TIME); |
Matt Briggs | 64:46c8819c07cc | 205 | ledPatterns.turnOff(); |
Matt Briggs | 61:8d9efd33cac9 | 206 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 207 | myLogInfo("Got pair request and responded"); |
Matt Briggs | 64:46c8819c07cc | 208 | ledPatterns.tripleBlink(); |
Matt Briggs | 61:8d9efd33cac9 | 209 | } |
Matt Briggs | 61:8d9efd33cac9 | 210 | else if (result == cmdTimeout) { |
Matt Briggs | 63:e1efbe3402d9 | 211 | myLogInfo("Did not receive request"); |
Matt Briggs | 61:8d9efd33cac9 | 212 | } |
Matt Briggs | 61:8d9efd33cac9 | 213 | else { |
Matt Briggs | 63:e1efbe3402d9 | 214 | myLogInfo("Unknown pair error"); |
Matt Briggs | 61:8d9efd33cac9 | 215 | } |
Matt Briggs | 62:9751a8504c82 | 216 | protocol->configForSavedNetwork(); |
Matt Briggs | 63:e1efbe3402d9 | 217 | protocol->printDotConfig(); |
Matt Briggs | 64:46c8819c07cc | 218 | protocol->resetCounters(); |
Matt Briggs | 71:2b57529df137 | 219 | |
Matt Briggs | 71:2b57529df137 | 220 | // Clear test log |
Matt Briggs | 71:2b57529df137 | 221 | rxSeqLog.clear(); |
Matt Briggs | 71:2b57529df137 | 222 | rxSeqLog.save(); |
Matt Briggs | 71:2b57529df137 | 223 | myLogInfo("NVM Cleared Seq Log"); |
Matt Briggs | 61:8d9efd33cac9 | 224 | } |
Matt Briggs | 61:8d9efd33cac9 | 225 | else if (pairBtnState == pairBtnLongPress) { |
Matt Briggs | 63:e1efbe3402d9 | 226 | myLogInfo("Clearing pair values and generating new ones."); |
Matt Briggs | 61:8d9efd33cac9 | 227 | protocol->clearPair(); |
Matt Briggs | 63:e1efbe3402d9 | 228 | protocol->printDotConfig(); |
Matt Briggs | 64:46c8819c07cc | 229 | ledPatterns.tenBlinks(); |
Matt Briggs | 64:46c8819c07cc | 230 | protocol->resetCounters(); |
Matt Briggs | 61:8d9efd33cac9 | 231 | } |
Matt Briggs | 61:8d9efd33cac9 | 232 | } |
Matt Briggs | 61:8d9efd33cac9 | 233 | } |
Matt Briggs | 69:eb391644b346 | 234 | else { |
Matt Briggs | 69:eb391644b346 | 235 | pairBtnState = pairBtnNoPress; |
Matt Briggs | 69:eb391644b346 | 236 | } |
Matt Briggs | 69:eb391644b346 | 237 | pairBtnIntFlag = false; |
Matt Briggs | 47:a68747642a7a | 238 | |
Matt Briggs | 69:eb391644b346 | 239 | myLogInfo("Loop #%d. isTX %d, CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnState %d", |
Matt Briggs | 69:eb391644b346 | 240 | loopCnt, protocol->isTx(), ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnState); |
Matt Briggs | 61:8d9efd33cac9 | 241 | // Alert code |
Matt Briggs | 53:a1563574a980 | 242 | if (protocol->isTx()) { |
Matt Briggs | 53:a1563574a980 | 243 | // TODO add tamper |
Matt Briggs | 50:e89647e77fd5 | 244 | if (ccIntFlag || // If contact closure in |
Matt Briggs | 57:bdac7dd17af2 | 245 | bbio->isCCInAlert() || // If closure remains in effect |
Matt Briggs | 69:eb391644b346 | 246 | pairBtnState == pairBtnShortPress) { |
Matt Briggs | 50:e89647e77fd5 | 247 | ccIntFlag = false; |
Matt Briggs | 64:46c8819c07cc | 248 | ledPatterns.turnOn(); |
Matt Briggs | 50:e89647e77fd5 | 249 | |
Matt Briggs | 66:bcaa6dbf538a | 250 | myLogInfo("Sending msg num: %d.", protocol->getSeqNum()); |
Matt Briggs | 65:d546060aa03d | 251 | protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper |
Matt Briggs | 64:46c8819c07cc | 252 | ledPatterns.turnOff(); |
Matt Briggs | 50:e89647e77fd5 | 253 | } |
Matt Briggs | 50:e89647e77fd5 | 254 | |
Matt Briggs | 64:46c8819c07cc | 255 | ledPatterns.turnOff(); |
Matt Briggs | 69:eb391644b346 | 256 | // tamperIntFlag = false; |
Matt Briggs | 69:eb391644b346 | 257 | if (pairBtnIntFlag) {// An Interrupt happened during transmission |
Matt Briggs | 69:eb391644b346 | 258 | continue; // Go straight to pair handling |
Matt Briggs | 69:eb391644b346 | 259 | } |
Matt Briggs | 58:15aa7a785b9f | 260 | bbio->prepareSleep(); |
Matt Briggs | 57:bdac7dd17af2 | 261 | if (bbio->isCCInAlert()) { // Still in alert mode |
Matt Briggs | 68:51c25f4f6d9a | 262 | // Sleep for 5 seconds to ensure that receiver does not miss a message sequence |
Matt Briggs | 71:2b57529df137 | 263 | // dot->sleep(5, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 5 secs if CCInAlert is asserted |
Matt Briggs | 71:2b57529df137 | 264 | dot->sleep(13, mDot::RTC_ALARM_OR_INTERRUPT, false); // Just for test FIXME |
Matt Briggs | 57:bdac7dd17af2 | 265 | } |
Matt Briggs | 57:bdac7dd17af2 | 266 | else { |
Matt Briggs | 57:bdac7dd17af2 | 267 | dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 57:bdac7dd17af2 | 268 | } |
Matt Briggs | 58:15aa7a785b9f | 269 | bbio->exitSleep(); |
Matt Briggs | 47:a68747642a7a | 270 | } |
Mike Fiore |
11:d2e31743433a | 271 | |
Matt Briggs | 53:a1563574a980 | 272 | if (protocol->isRx()) { |
Matt Briggs | 66:bcaa6dbf538a | 273 | ccIntFlag = false; |
Matt Briggs | 69:eb391644b346 | 274 | // tamperIntFlag = false; |
Matt Briggs | 69:eb391644b346 | 275 | // myLogInfo("Before listen time %d", us_ticker_read()); |
Matt Briggs | 47:a68747642a7a | 276 | bool msgPending; |
Matt Briggs | 53:a1563574a980 | 277 | protocol->listen(msgPending); |
Matt Briggs | 47:a68747642a7a | 278 | if (msgPending) { |
Matt Briggs | 65:d546060aa03d | 279 | std::vector<uint8_t> txEui; |
Matt Briggs | 65:d546060aa03d | 280 | txEui.reserve(8); |
Matt Briggs | 65:d546060aa03d | 281 | uint16_t data; |
Matt Briggs | 65:d546060aa03d | 282 | uint32_t msgSeqNum; |
Matt Briggs | 65:d546060aa03d | 283 | protocol->recvAlert(txEui, data, msgSeqNum); |
Matt Briggs | 69:eb391644b346 | 284 | if (msgSeqNum > maxSeenMsgSeqNum) |
Matt Briggs | 69:eb391644b346 | 285 | maxSeenMsgSeqNum = msgSeqNum; |
Matt Briggs | 69:eb391644b346 | 286 | rxMsgCnt++; // for message |
Matt Briggs | 69:eb391644b346 | 287 | myLogInfo("Got rxMsgCnt #%d, with Seqnum: %d", rxMsgCnt, msgSeqNum); |
Matt Briggs | 53:a1563574a980 | 288 | bbio->relayAlert(); |
Matt Briggs | 64:46c8819c07cc | 289 | ledPatterns.turnOn(); |
Matt Briggs | 66:bcaa6dbf538a | 290 | myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1())); |
Matt Briggs | 58:15aa7a785b9f | 291 | // Hold time for alert |
Matt Briggs | 58:15aa7a785b9f | 292 | // TODO maybe use sleep instead of wait |
Matt Briggs | 58:15aa7a785b9f | 293 | wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1())); |
Matt Briggs | 64:46c8819c07cc | 294 | ledPatterns.turnOff(); |
Matt Briggs | 58:15aa7a785b9f | 295 | bbio->relayNormal(); |
Matt Briggs | 47:a68747642a7a | 296 | } |
Matt Briggs | 69:eb391644b346 | 297 | if (pairBtnIntFlag) {// An Interrupt happened during reception |
Matt Briggs | 69:eb391644b346 | 298 | continue; // Go straight to pair handling |
Matt Briggs | 69:eb391644b346 | 299 | } |
Matt Briggs | 69:eb391644b346 | 300 | //// TEMP LOGGING |
Matt Briggs | 69:eb391644b346 | 301 | #if RX_SEQ_LOG |
Matt Briggs | 69:eb391644b346 | 302 | if ((loopCnt % 1000 == 0) && |
Matt Briggs | 69:eb391644b346 | 303 | rxMsgCnt > rxSeqLog.rxMsgCount()) { |
Matt Briggs | 69:eb391644b346 | 304 | rxSeqLog.setLoopCount(loopCnt); |
Matt Briggs | 69:eb391644b346 | 305 | rxSeqLog.setRxMsgCount(rxMsgCnt); |
Matt Briggs | 69:eb391644b346 | 306 | rxSeqLog.setMaxSeenMsgSeqNum(maxSeenMsgSeqNum); |
Matt Briggs | 69:eb391644b346 | 307 | rxSeqLog.save(); |
Matt Briggs | 69:eb391644b346 | 308 | myLogInfo("EEPROM Saved."); |
Matt Briggs | 69:eb391644b346 | 309 | } |
Matt Briggs | 69:eb391644b346 | 310 | myLogInfo("NVM Log: Loop Cnt: %d, RxMsgCnt %d, MaxSeenSeqNum %d", |
Matt Briggs | 69:eb391644b346 | 311 | rxSeqLog.loopCount(), rxSeqLog.rxMsgCount(), rxSeqLog.maxSeenMsgSeqNum()); |
Matt Briggs | 69:eb391644b346 | 312 | #endif |
Matt Briggs | 69:eb391644b346 | 313 | |
Matt Briggs | 63:e1efbe3402d9 | 314 | myLogInfo("Sleeping. Time %d", us_ticker_read()); |
Matt Briggs | 58:15aa7a785b9f | 315 | bbio->prepareSleep(); |
Matt Briggs | 47:a68747642a7a | 316 | dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 58:15aa7a785b9f | 317 | bbio->exitSleep(); |
Matt Briggs | 47:a68747642a7a | 318 | } |
Matt Briggs | 31:9c535a708ae9 | 319 | |
Matt Briggs | 64:46c8819c07cc | 320 | protocol->resetCounters(); |
Matt Briggs | 63:e1efbe3402d9 | 321 | myLogInfo("\r\n================================"); |
Matt Briggs | 58:15aa7a785b9f | 322 | loopCnt++; |
Mike Fiore |
11:d2e31743433a | 323 | } |
Matt Briggs | 47:a68747642a7a | 324 | |
Matt Briggs | 55:79ab0bbc5008 | 325 | delete protocol; |
Matt Briggs | 55:79ab0bbc5008 | 326 | delete bbio; |
Mike Fiore |
11:d2e31743433a | 327 | return 0; |
Mike Fiore |
11:d2e31743433a | 328 | } |
Matt Briggs | 47:a68747642a7a | 329 | #endif |
Mike Fiore |
11:d2e31743433a | 330 | |
Matt Briggs | 27:6b68ff715ae1 | 331 |