![](/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@77:176e3eb8f712, 2017-05-17 (annotated)
- Committer:
- Matt Briggs
- Date:
- Wed May 17 16:41:56 2017 -0600
- Revision:
- 77:176e3eb8f712
- Parent:
- 76:d1b20a259d8f
- Child:
- 78:43f074baac34
Added serial terminal and Buffered lib
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 | 77:176e3eb8f712 | 6 | #include "BufferedSerial.h" |
Matt Briggs | 47:a68747642a7a | 7 | #include "WinbondSPIFlash.h" |
Matt Briggs | 58:15aa7a785b9f | 8 | #include "UserInterface.h" |
Matt Briggs | 41:9ef4c4d77711 | 9 | //#include <xdot_low_power.h> |
Matt Briggs | 58:15aa7a785b9f | 10 | #include "mDot.h" |
Matt Briggs | 63:e1efbe3402d9 | 11 | #include "MyLog.h" |
Matt Briggs | 69:eb391644b346 | 12 | #include "SimpleRxSeqLog.h" |
mfiore | 17:d4f82e16de5f | 13 | |
Matt Briggs | 47:a68747642a7a | 14 | #include "BaseboardIO.h" |
Matt Briggs | 41:9ef4c4d77711 | 15 | #include "CommProtocolPeerBrute.h" |
Matt Briggs | 75:600cb3a9f126 | 16 | #include "SerialTermMgr.h" |
Matt Briggs | 31:9c535a708ae9 | 17 | |
Matt Briggs | 69:eb391644b346 | 18 | #define RX_SEQ_LOG 1 |
Matt Briggs | 69:eb391644b346 | 19 | |
Matt Briggs | 75:600cb3a9f126 | 20 | const float BridgeVersion = 1.01; |
Matt Briggs | 69:eb391644b346 | 21 | |
Matt Briggs | 47:a68747642a7a | 22 | #ifndef __TEST__ // Exclude code for tests |
Mike Fiore |
11:d2e31743433a | 23 | Serial pc(USBTX, USBRX); |
Matt Briggs | 75:600cb3a9f126 | 24 | //RawSerial outPc(UART_TX, UART_RX); |
Matt Briggs | 77:176e3eb8f712 | 25 | //Serial *outPc; |
Matt Briggs | 77:176e3eb8f712 | 26 | BufferedSerial *outPc; |
Matt Briggs | 75:600cb3a9f126 | 27 | InterruptIn uart1RxIntIn (UART_RX); |
Matt Briggs | 75:600cb3a9f126 | 28 | |
Matt Briggs | 75:600cb3a9f126 | 29 | //// Serial interrupts / buffer code |
Matt Briggs | 75:600cb3a9f126 | 30 | //const int buffer_size = 255; |
Matt Briggs | 75:600cb3a9f126 | 31 | //// might need to increase buffer size for high baud rates |
Matt Briggs | 75:600cb3a9f126 | 32 | //char tx_buffer[buffer_size+1]; |
Matt Briggs | 75:600cb3a9f126 | 33 | //char rx_buffer[buffer_size+1]; |
Matt Briggs | 75:600cb3a9f126 | 34 | //// Circular buffer pointers |
Matt Briggs | 75:600cb3a9f126 | 35 | //// volatile makes read-modify-write atomic |
Matt Briggs | 75:600cb3a9f126 | 36 | //volatile int tx_in=0; |
Matt Briggs | 75:600cb3a9f126 | 37 | //volatile int tx_out=0; |
Matt Briggs | 75:600cb3a9f126 | 38 | //volatile int rx_in=0; |
Matt Briggs | 75:600cb3a9f126 | 39 | //volatile int rx_out=0; |
Matt Briggs | 75:600cb3a9f126 | 40 | // |
Matt Briggs | 75:600cb3a9f126 | 41 | //// Interupt Routine to read in data from serial port |
Matt Briggs | 75:600cb3a9f126 | 42 | //void Rx_interrupt() { |
Matt Briggs | 75:600cb3a9f126 | 43 | // led1=1; |
Matt Briggs | 75:600cb3a9f126 | 44 | //// Loop just in case more than one character is in UART's receive FIFO buffer |
Matt Briggs | 75:600cb3a9f126 | 45 | //// Stop if buffer full |
Matt Briggs | 75:600cb3a9f126 | 46 | // while ((outPc.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) { |
Matt Briggs | 75:600cb3a9f126 | 47 | // rx_buffer[rx_in] = outPc.getc(); |
Matt Briggs | 75:600cb3a9f126 | 48 | //// Uncomment to Echo to USB serial to watch data flow |
Matt Briggs | 75:600cb3a9f126 | 49 | //// monitor_device.putc(rx_buffer[rx_in]); |
Matt Briggs | 75:600cb3a9f126 | 50 | // rx_in = (rx_in + 1) % buffer_size; |
Matt Briggs | 75:600cb3a9f126 | 51 | // } |
Matt Briggs | 75:600cb3a9f126 | 52 | // led1=0; |
Matt Briggs | 75:600cb3a9f126 | 53 | // return; |
Matt Briggs | 75:600cb3a9f126 | 54 | //} |
Matt Briggs | 75:600cb3a9f126 | 55 | // |
Matt Briggs | 75:600cb3a9f126 | 56 | //// Interupt Routine to write out data to serial port |
Matt Briggs | 75:600cb3a9f126 | 57 | //void Tx_interrupt() { |
Matt Briggs | 75:600cb3a9f126 | 58 | // led2=1; |
Matt Briggs | 75:600cb3a9f126 | 59 | //// Loop to fill more than one character in UART's transmit FIFO buffer |
Matt Briggs | 75:600cb3a9f126 | 60 | //// Stop if buffer empty |
Matt Briggs | 75:600cb3a9f126 | 61 | // while ((outPc.writeable()) && (tx_in != tx_out)) { |
Matt Briggs | 75:600cb3a9f126 | 62 | // outPc.putc(tx_buffer[tx_out]); |
Matt Briggs | 75:600cb3a9f126 | 63 | // tx_out = (tx_out + 1) % buffer_size; |
Matt Briggs | 75:600cb3a9f126 | 64 | // } |
Matt Briggs | 75:600cb3a9f126 | 65 | // led2=0; |
Matt Briggs | 75:600cb3a9f126 | 66 | // return; |
Matt Briggs | 75:600cb3a9f126 | 67 | //} |
Mike Fiore |
11:d2e31743433a | 68 | |
Matt Briggs | 47:a68747642a7a | 69 | mDot* dot = NULL; // Used by dot-utils |
Matt Briggs | 39:64f79fa6e3cc | 70 | |
Matt Briggs | 75:600cb3a9f126 | 71 | volatile bool uartRxFlag; |
Matt Briggs | 50:e89647e77fd5 | 72 | volatile bool ccIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 73 | volatile bool tamperIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 74 | volatile bool pairBtnIntFlag; |
Matt Briggs | 75:600cb3a9f126 | 75 | void uart1RxCallback () { |
Matt Briggs | 75:600cb3a9f126 | 76 | uartRxFlag = true; |
Matt Briggs | 75:600cb3a9f126 | 77 | } |
Matt Briggs | 50:e89647e77fd5 | 78 | void ccInIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 79 | ccIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 80 | } |
Matt Briggs | 50:e89647e77fd5 | 81 | void tamperIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 82 | tamperIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 83 | } |
Matt Briggs | 50:e89647e77fd5 | 84 | void pairBtnIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 85 | pairBtnIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 86 | } |
Matt Briggs | 50:e89647e77fd5 | 87 | |
Mike Fiore |
11:d2e31743433a | 88 | int main() { |
Matt Briggs | 53:a1563574a980 | 89 | CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute(); |
Matt Briggs | 53:a1563574a980 | 90 | BaseboardIO *bbio = new BaseboardIO(); |
Matt Briggs | 58:15aa7a785b9f | 91 | WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS); |
Matt Briggs | 77:176e3eb8f712 | 92 | SerialTermMgr serialTermMgr(bbio, flash, BridgeVersion); |
Matt Briggs | 58:15aa7a785b9f | 93 | |
Matt Briggs | 75:600cb3a9f126 | 94 | pc.baud(115200); |
Matt Briggs | 52:64a2c71c7c49 | 95 | CmdResult result; |
Matt Briggs | 75:600cb3a9f126 | 96 | uartRxFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 97 | ccIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 98 | tamperIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 99 | pairBtnIntFlag = false; |
Matt Briggs | 48:bab9f747d9ed | 100 | |
Matt Briggs | 31:9c535a708ae9 | 101 | RadioEvent events; // Custom event handler for automatically displaying RX data |
Matt Briggs | 69:eb391644b346 | 102 | #if RX_SEQ_LOG |
Matt Briggs | 69:eb391644b346 | 103 | SimpleRxSeqLog rxSeqLog(0x0000); |
Matt Briggs | 69:eb391644b346 | 104 | rxSeqLog.read(); |
Matt Briggs | 69:eb391644b346 | 105 | #endif |
Mike Fiore |
11:d2e31743433a | 106 | |
Matt Briggs | 27:6b68ff715ae1 | 107 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Matt Briggs | 66:bcaa6dbf538a | 108 | MyLog::setLogLevel(MyLog::DEBUG_LEVEL); |
Matt Briggs | 47:a68747642a7a | 109 | |
Mike Fiore |
11:d2e31743433a | 110 | dot = mDot::getInstance(); |
Mike Fiore |
11:d2e31743433a | 111 | |
Mike Fiore |
11:d2e31743433a | 112 | // make sure library logging is turned on |
Matt Briggs | 66:bcaa6dbf538a | 113 | dot->setLogLevel(mts::MTSLog::WARNING_LEVEL); |
Mike Fiore |
11:d2e31743433a | 114 | |
Matt Briggs | 63:e1efbe3402d9 | 115 | myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Matt Briggs | 63:e1efbe3402d9 | 116 | myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str()); |
Matt Briggs | 69:eb391644b346 | 117 | myLogInfo("Vortex Wireless Bridge SW Version %0.2f", BridgeVersion); |
Matt Briggs | 62:9751a8504c82 | 118 | |
Mike Fiore |
11:d2e31743433a | 119 | // attach the custom events handler |
Matt Briggs | 54:c04d7b6fa075 | 120 | dot->setEvents(&events); // Little bonus event debug information |
Matt Briggs | 53:a1563574a980 | 121 | |
Matt Briggs | 53:a1563574a980 | 122 | // Finish radio init |
Matt Briggs | 63:e1efbe3402d9 | 123 | myLogInfo("= Protocol Init Starting =\r\n"); |
Matt Briggs | 53:a1563574a980 | 124 | protocol->init(); |
Matt Briggs | 62:9751a8504c82 | 125 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 126 | myLogInfo("= Protocol Init Finished Successfully =\r\n"); |
Matt Briggs | 62:9751a8504c82 | 127 | } |
Matt Briggs | 62:9751a8504c82 | 128 | else { |
Matt Briggs | 63:e1efbe3402d9 | 129 | myLogInfo("= Protocol Init Finished with Error =\r\n"); |
Matt Briggs | 62:9751a8504c82 | 130 | } |
Matt Briggs | 63:e1efbe3402d9 | 131 | protocol->printDotConfig(); |
Matt Briggs | 53:a1563574a980 | 132 | |
Matt Briggs | 53:a1563574a980 | 133 | // save changes to configuration |
Matt Briggs | 53:a1563574a980 | 134 | // logInfo("saving configuration"); |
Matt Briggs | 53:a1563574a980 | 135 | // core_util_critical_section_enter(); |
Matt Briggs | 53:a1563574a980 | 136 | // bool dotConfigSaved = dot->saveConfig(); |
Matt Briggs | 53:a1563574a980 | 137 | // core_util_critical_section_exit(); |
Matt Briggs | 53:a1563574a980 | 138 | // if (!dotConfigSaved) { |
Matt Briggs | 53:a1563574a980 | 139 | // logError("failed to save configuration"); |
Matt Briggs | 53:a1563574a980 | 140 | // } |
Mike Fiore |
11:d2e31743433a | 141 | |
Matt Briggs | 31:9c535a708ae9 | 142 | // Setup programmable voltage detector |
Matt Briggs | 31:9c535a708ae9 | 143 | // PVD_LEVEL0 Falling 1.85 |
Matt Briggs | 31:9c535a708ae9 | 144 | // PVD_LEVEL1 Falling 2.04 |
Matt Briggs | 31:9c535a708ae9 | 145 | // PVD_LEVEL2 Falling 2.24 |
Matt Briggs | 31:9c535a708ae9 | 146 | // PVD_LEVEL3 Falling 2.44 |
Matt Briggs | 31:9c535a708ae9 | 147 | // PVD_LEVEL4 Falling 2.64 |
Matt Briggs | 31:9c535a708ae9 | 148 | // PVD_LEVEL5 Falling 2.84 |
Matt Briggs | 31:9c535a708ae9 | 149 | // PVD_LEVEL6 Falling 3.05 |
Matt Briggs | 50:e89647e77fd5 | 150 | // PWR_PVDTypeDef pvdConfig; |
Matt Briggs | 50:e89647e77fd5 | 151 | // pvdConfig.Mode = PWR_PVD_MODE_NORMAL; |
Matt Briggs | 50:e89647e77fd5 | 152 | // pvdConfig.PVDLevel = PWR_PVDLEVEL_5; |
Matt Briggs | 50:e89647e77fd5 | 153 | // |
Matt Briggs | 50:e89647e77fd5 | 154 | // HAL_PWR_ConfigPVD(&pvdConfig); |
Matt Briggs | 50:e89647e77fd5 | 155 | // HAL_PWR_EnablePVD(); |
Matt Briggs | 50:e89647e77fd5 | 156 | // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel); |
Matt Briggs | 50:e89647e77fd5 | 157 | // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation |
Mike Fiore |
11:d2e31743433a | 158 | |
Matt Briggs | 58:15aa7a785b9f | 159 | dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT); |
Matt Briggs | 75:600cb3a9f126 | 160 | dot->setWakePin(GPIO0); |
Matt Briggs | 75:600cb3a9f126 | 161 | // dot->setWakePin(UART_RX); |
Matt Briggs | 24:fdf87e4b72e5 | 162 | |
Matt Briggs | 62:9751a8504c82 | 163 | display_config(); // Print configuration for now |
Matt Briggs | 62:9751a8504c82 | 164 | |
Mike Fiore |
11:d2e31743433a | 165 | // display configuration |
Matt Briggs | 53:a1563574a980 | 166 | // display_config(); |
Matt Briggs | 52:64a2c71c7c49 | 167 | |
Matt Briggs | 63:e1efbe3402d9 | 168 | myLogInfo("= Baseboard Init Starting =\r\n"); |
Matt Briggs | 53:a1563574a980 | 169 | result = bbio->init(); |
Matt Briggs | 52:64a2c71c7c49 | 170 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 171 | myLogInfo("= Baseboard Init Finished Successfully =\r\n"); |
Matt Briggs | 52:64a2c71c7c49 | 172 | } |
Matt Briggs | 52:64a2c71c7c49 | 173 | else { |
Matt Briggs | 63:e1efbe3402d9 | 174 | myLogInfo("= Baseboard Init Finished with Error =\r\n"); |
Matt Briggs | 52:64a2c71c7c49 | 175 | } |
Matt Briggs | 64:46c8819c07cc | 176 | LedPatterns ledPatterns(bbio); |
Mike Fiore |
11:d2e31743433a | 177 | |
Matt Briggs | 76:d1b20a259d8f | 178 | uart1RxIntIn.rise(&uart1RxCallback); // Rising does appear to work better than falling edge |
Matt Briggs | 75:600cb3a9f126 | 179 | uart1RxIntIn.enable_irq(); |
Matt Briggs | 50:e89647e77fd5 | 180 | Callback<void()> ccInIntObj (&ccInIntCallback); |
Matt Briggs | 69:eb391644b346 | 181 | // Callback<void()> tamperIntObj (&tamperIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 182 | Callback<void()> pairBtnIntObj (&pairBtnIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 183 | |
Matt Briggs | 53:a1563574a980 | 184 | bbio->sampleUserSwitches(); |
Matt Briggs | 53:a1563574a980 | 185 | bbio->relayNormal(); // Always force relay in known state |
Matt Briggs | 58:15aa7a785b9f | 186 | bbio->regCCInInt(ccInIntObj); |
Matt Briggs | 69:eb391644b346 | 187 | // bbio->regTamperInt(tamperIntObj); |
Matt Briggs | 58:15aa7a785b9f | 188 | bbio->regPairBtnInt(pairBtnIntObj); |
Matt Briggs | 26:9411b26a5084 | 189 | |
Matt Briggs | 66:bcaa6dbf538a | 190 | if (bbio->isTx()) { |
Matt Briggs | 66:bcaa6dbf538a | 191 | protocol->setTx(true); |
Matt Briggs | 66:bcaa6dbf538a | 192 | } |
Matt Briggs | 66:bcaa6dbf538a | 193 | else { // RX |
Matt Briggs | 66:bcaa6dbf538a | 194 | protocol->setTx(false); |
Matt Briggs | 66:bcaa6dbf538a | 195 | } |
Matt Briggs | 66:bcaa6dbf538a | 196 | |
Matt Briggs | 58:15aa7a785b9f | 197 | // Start flash powered down |
Matt Briggs | 58:15aa7a785b9f | 198 | flash->powerDown(); |
Matt Briggs | 58:15aa7a785b9f | 199 | |
Matt Briggs | 69:eb391644b346 | 200 | unsigned int loopCnt = 0; // Just a quick temp variable to keep track of loopNums |
Matt Briggs | 58:15aa7a785b9f | 201 | bool prevCCNormallyOpen; |
Matt Briggs | 61:8d9efd33cac9 | 202 | PairBtnState pairBtnState; |
Matt Briggs | 67:2115a2f1b945 | 203 | uint32_t rxMsgCnt = 0; |
Matt Briggs | 69:eb391644b346 | 204 | uint32_t maxSeenMsgSeqNum = 0; |
Matt Briggs | 47:a68747642a7a | 205 | /** |
Matt Briggs | 47:a68747642a7a | 206 | * Main Loop |
Matt Briggs | 47:a68747642a7a | 207 | */ |
Mike Fiore |
11:d2e31743433a | 208 | while (true) { |
Matt Briggs | 69:eb391644b346 | 209 | // myLogInfo("Start of loop time %d", us_ticker_read()); |
Matt Briggs | 64:46c8819c07cc | 210 | ledPatterns.turnOff(); |
Matt Briggs | 47:a68747642a7a | 211 | |
Matt Briggs | 66:bcaa6dbf538a | 212 | // Pair logic and switch sampling |
Matt Briggs | 61:8d9efd33cac9 | 213 | if (pairBtnIntFlag) { |
Matt Briggs | 61:8d9efd33cac9 | 214 | pairBtnState = PairBtnInterp::read(bbio); |
Matt Briggs | 66:bcaa6dbf538a | 215 | |
Matt Briggs | 66:bcaa6dbf538a | 216 | // Sample IO and update any configuration |
Matt Briggs | 66:bcaa6dbf538a | 217 | prevCCNormallyOpen = bbio->isCCNO(); |
Matt Briggs | 66:bcaa6dbf538a | 218 | bbio->sampleUserSwitches(); |
Matt Briggs | 66:bcaa6dbf538a | 219 | if (prevCCNormallyOpen != bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed |
Matt Briggs | 66:bcaa6dbf538a | 220 | bbio->regCCInInt(ccInIntObj); |
Matt Briggs | 66:bcaa6dbf538a | 221 | bbio->relayNormal(); |
Matt Briggs | 66:bcaa6dbf538a | 222 | } |
Matt Briggs | 66:bcaa6dbf538a | 223 | if (bbio->isTx()) { |
Matt Briggs | 66:bcaa6dbf538a | 224 | protocol->setTx(true); |
Matt Briggs | 66:bcaa6dbf538a | 225 | } |
Matt Briggs | 66:bcaa6dbf538a | 226 | else { // RX |
Matt Briggs | 66:bcaa6dbf538a | 227 | protocol->setTx(false); |
Matt Briggs | 66:bcaa6dbf538a | 228 | } |
Matt Briggs | 61:8d9efd33cac9 | 229 | if (protocol->isTx()) { |
Matt Briggs | 61:8d9efd33cac9 | 230 | if (pairBtnState == pairBtnMediumPress) { |
Matt Briggs | 62:9751a8504c82 | 231 | protocol->configForPairingNetwork(); |
Matt Briggs | 61:8d9efd33cac9 | 232 | protocol->sendPairReq(); |
Matt Briggs | 63:e1efbe3402d9 | 233 | myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME); |
Matt Briggs | 61:8d9efd33cac9 | 234 | result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME); |
Matt Briggs | 61:8d9efd33cac9 | 235 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 236 | myLogInfo("Got accept"); |
Matt Briggs | 64:46c8819c07cc | 237 | ledPatterns.turnOff(); |
Matt Briggs | 64:46c8819c07cc | 238 | wait(0.5); |
Matt Briggs | 64:46c8819c07cc | 239 | ledPatterns.tripleBlink(); |
Matt Briggs | 61:8d9efd33cac9 | 240 | } |
Matt Briggs | 61:8d9efd33cac9 | 241 | else { |
Matt Briggs | 63:e1efbe3402d9 | 242 | myLogInfo("Did not receive accept"); |
Matt Briggs | 61:8d9efd33cac9 | 243 | } |
Matt Briggs | 62:9751a8504c82 | 244 | protocol->configForSavedNetwork(); |
Matt Briggs | 63:e1efbe3402d9 | 245 | protocol->printDotConfig(); |
Matt Briggs | 64:46c8819c07cc | 246 | protocol->resetCounters(); |
Matt Briggs | 71:2b57529df137 | 247 | |
Matt Briggs | 71:2b57529df137 | 248 | // Clear test log |
Matt Briggs | 71:2b57529df137 | 249 | rxSeqLog.clear(); |
Matt Briggs | 71:2b57529df137 | 250 | rxSeqLog.save(); |
Matt Briggs | 71:2b57529df137 | 251 | myLogInfo("NVM Cleared Seq Log"); |
Matt Briggs | 61:8d9efd33cac9 | 252 | } |
Matt Briggs | 61:8d9efd33cac9 | 253 | } |
Matt Briggs | 61:8d9efd33cac9 | 254 | if (protocol->isRx()) { |
Matt Briggs | 61:8d9efd33cac9 | 255 | if (pairBtnState == pairBtnMediumPress) { |
Matt Briggs | 62:9751a8504c82 | 256 | protocol->configForPairingNetwork(); |
Matt Briggs | 64:46c8819c07cc | 257 | ledPatterns.turnOn(); |
Matt Briggs | 63:e1efbe3402d9 | 258 | myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME); |
Matt Briggs | 61:8d9efd33cac9 | 259 | result = protocol->waitForPairing(RX_PAIR_WAIT_TIME); |
Matt Briggs | 64:46c8819c07cc | 260 | ledPatterns.turnOff(); |
Matt Briggs | 61:8d9efd33cac9 | 261 | if (result == cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 262 | myLogInfo("Got pair request and responded"); |
Matt Briggs | 64:46c8819c07cc | 263 | ledPatterns.tripleBlink(); |
Matt Briggs | 61:8d9efd33cac9 | 264 | } |
Matt Briggs | 61:8d9efd33cac9 | 265 | else if (result == cmdTimeout) { |
Matt Briggs | 63:e1efbe3402d9 | 266 | myLogInfo("Did not receive request"); |
Matt Briggs | 61:8d9efd33cac9 | 267 | } |
Matt Briggs | 61:8d9efd33cac9 | 268 | else { |
Matt Briggs | 63:e1efbe3402d9 | 269 | myLogInfo("Unknown pair error"); |
Matt Briggs | 61:8d9efd33cac9 | 270 | } |
Matt Briggs | 62:9751a8504c82 | 271 | protocol->configForSavedNetwork(); |
Matt Briggs | 63:e1efbe3402d9 | 272 | protocol->printDotConfig(); |
Matt Briggs | 64:46c8819c07cc | 273 | protocol->resetCounters(); |
Matt Briggs | 71:2b57529df137 | 274 | |
Matt Briggs | 71:2b57529df137 | 275 | // Clear test log |
Matt Briggs | 71:2b57529df137 | 276 | rxSeqLog.clear(); |
Matt Briggs | 71:2b57529df137 | 277 | rxSeqLog.save(); |
Matt Briggs | 71:2b57529df137 | 278 | myLogInfo("NVM Cleared Seq Log"); |
Matt Briggs | 61:8d9efd33cac9 | 279 | } |
Matt Briggs | 61:8d9efd33cac9 | 280 | else if (pairBtnState == pairBtnLongPress) { |
Matt Briggs | 63:e1efbe3402d9 | 281 | myLogInfo("Clearing pair values and generating new ones."); |
Matt Briggs | 61:8d9efd33cac9 | 282 | protocol->clearPair(); |
Matt Briggs | 63:e1efbe3402d9 | 283 | protocol->printDotConfig(); |
Matt Briggs | 64:46c8819c07cc | 284 | ledPatterns.tenBlinks(); |
Matt Briggs | 64:46c8819c07cc | 285 | protocol->resetCounters(); |
Matt Briggs | 61:8d9efd33cac9 | 286 | } |
Matt Briggs | 61:8d9efd33cac9 | 287 | } |
Matt Briggs | 61:8d9efd33cac9 | 288 | } |
Matt Briggs | 69:eb391644b346 | 289 | else { |
Matt Briggs | 69:eb391644b346 | 290 | pairBtnState = pairBtnNoPress; |
Matt Briggs | 69:eb391644b346 | 291 | } |
Matt Briggs | 69:eb391644b346 | 292 | pairBtnIntFlag = false; |
Matt Briggs | 47:a68747642a7a | 293 | |
Matt Briggs | 75:600cb3a9f126 | 294 | // Serial Terminal |
Matt Briggs | 75:600cb3a9f126 | 295 | if (uartRxFlag) { |
Matt Briggs | 75:600cb3a9f126 | 296 | uart1RxIntIn.disable_irq(); |
Matt Briggs | 75:600cb3a9f126 | 297 | // pc.printf("Got uart flag!!!\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 298 | uartRxFlag = false; |
Matt Briggs | 77:176e3eb8f712 | 299 | outPc = new BufferedSerial(UART1_TX, UART1_RX); |
Matt Briggs | 76:d1b20a259d8f | 300 | outPc->baud(TERM_BAUD); |
Matt Briggs | 75:600cb3a9f126 | 301 | serialTermMgr.regSerial(outPc); |
Matt Briggs | 75:600cb3a9f126 | 302 | char c; |
Matt Briggs | 75:600cb3a9f126 | 303 | if (outPc->readable()) { |
Matt Briggs | 75:600cb3a9f126 | 304 | c = outPc->getc(); // Throw away the first char |
Matt Briggs | 75:600cb3a9f126 | 305 | pc.printf("Got %d, %c\r\n", c, c); |
Matt Briggs | 75:600cb3a9f126 | 306 | } |
Matt Briggs | 75:600cb3a9f126 | 307 | |
Matt Briggs | 75:600cb3a9f126 | 308 | outPc->printf("Starting Terminal...\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 309 | serialTermMgr.printScreen(); |
Matt Briggs | 75:600cb3a9f126 | 310 | time_t termLastAction = time(NULL); |
Matt Briggs | 75:600cb3a9f126 | 311 | bool quit = false; |
Matt Briggs | 75:600cb3a9f126 | 312 | while(!quit && (time(NULL) < (termLastAction + TERM_TIMEOUT))) { |
Matt Briggs | 75:600cb3a9f126 | 313 | if (outPc->readable()) { |
Matt Briggs | 75:600cb3a9f126 | 314 | // pc.printf("Got %d\r\n", outPc->getc()); |
Matt Briggs | 77:176e3eb8f712 | 315 | quit = serialTermMgr.input(); |
Matt Briggs | 75:600cb3a9f126 | 316 | termLastAction = time(NULL); |
Matt Briggs | 75:600cb3a9f126 | 317 | } |
Matt Briggs | 75:600cb3a9f126 | 318 | } |
Matt Briggs | 75:600cb3a9f126 | 319 | if (quit) { |
Matt Briggs | 75:600cb3a9f126 | 320 | outPc->printf("Terminal quit resuming operation <press any key to reactivate>...\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 321 | } |
Matt Briggs | 75:600cb3a9f126 | 322 | else { |
Matt Briggs | 75:600cb3a9f126 | 323 | outPc->printf("Terminal timeout resuming operation <press any key to reactivate>...\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 324 | } |
Matt Briggs | 77:176e3eb8f712 | 325 | wait(0.01); // Wait for end message to be shifted out |
Matt Briggs | 75:600cb3a9f126 | 326 | serialTermMgr.regSerial(NULL); |
Matt Briggs | 75:600cb3a9f126 | 327 | delete outPc; |
Matt Briggs | 75:600cb3a9f126 | 328 | uart1RxIntIn.enable_irq(); |
Matt Briggs | 75:600cb3a9f126 | 329 | } |
Matt Briggs | 75:600cb3a9f126 | 330 | wait(0.1); |
Matt Briggs | 75:600cb3a9f126 | 331 | |
Matt Briggs | 69:eb391644b346 | 332 | myLogInfo("Loop #%d. isTX %d, CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnState %d", |
Matt Briggs | 69:eb391644b346 | 333 | loopCnt, protocol->isTx(), ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnState); |
Matt Briggs | 61:8d9efd33cac9 | 334 | // Alert code |
Matt Briggs | 53:a1563574a980 | 335 | if (protocol->isTx()) { |
Matt Briggs | 53:a1563574a980 | 336 | // TODO add tamper |
Matt Briggs | 50:e89647e77fd5 | 337 | if (ccIntFlag || // If contact closure in |
Matt Briggs | 57:bdac7dd17af2 | 338 | bbio->isCCInAlert() || // If closure remains in effect |
Matt Briggs | 69:eb391644b346 | 339 | pairBtnState == pairBtnShortPress) { |
Matt Briggs | 50:e89647e77fd5 | 340 | ccIntFlag = false; |
Matt Briggs | 64:46c8819c07cc | 341 | ledPatterns.turnOn(); |
Matt Briggs | 50:e89647e77fd5 | 342 | |
Matt Briggs | 66:bcaa6dbf538a | 343 | myLogInfo("Sending msg num: %d.", protocol->getSeqNum()); |
Matt Briggs | 65:d546060aa03d | 344 | protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper |
Matt Briggs | 64:46c8819c07cc | 345 | ledPatterns.turnOff(); |
Matt Briggs | 50:e89647e77fd5 | 346 | } |
Matt Briggs | 50:e89647e77fd5 | 347 | |
Matt Briggs | 64:46c8819c07cc | 348 | ledPatterns.turnOff(); |
Matt Briggs | 69:eb391644b346 | 349 | // tamperIntFlag = false; |
Matt Briggs | 69:eb391644b346 | 350 | if (pairBtnIntFlag) {// An Interrupt happened during transmission |
Matt Briggs | 69:eb391644b346 | 351 | continue; // Go straight to pair handling |
Matt Briggs | 69:eb391644b346 | 352 | } |
Matt Briggs | 58:15aa7a785b9f | 353 | bbio->prepareSleep(); |
Matt Briggs | 57:bdac7dd17af2 | 354 | if (bbio->isCCInAlert()) { // Still in alert mode |
Matt Briggs | 68:51c25f4f6d9a | 355 | // Sleep for 5 seconds to ensure that receiver does not miss a message sequence |
Matt Briggs | 75:600cb3a9f126 | 356 | dot->sleep(5, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 5 secs if CCInAlert is asserted |
Matt Briggs | 57:bdac7dd17af2 | 357 | } |
Matt Briggs | 57:bdac7dd17af2 | 358 | else { |
Matt Briggs | 75:600cb3a9f126 | 359 | dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until interrupt event |
Matt Briggs | 75:600cb3a9f126 | 360 | // wait(0.5); |
Matt Briggs | 57:bdac7dd17af2 | 361 | } |
Matt Briggs | 58:15aa7a785b9f | 362 | bbio->exitSleep(); |
Matt Briggs | 47:a68747642a7a | 363 | } |
Mike Fiore |
11:d2e31743433a | 364 | |
Matt Briggs | 53:a1563574a980 | 365 | if (protocol->isRx()) { |
Matt Briggs | 66:bcaa6dbf538a | 366 | ccIntFlag = false; |
Matt Briggs | 69:eb391644b346 | 367 | // tamperIntFlag = false; |
Matt Briggs | 69:eb391644b346 | 368 | // myLogInfo("Before listen time %d", us_ticker_read()); |
Matt Briggs | 47:a68747642a7a | 369 | bool msgPending; |
Matt Briggs | 53:a1563574a980 | 370 | protocol->listen(msgPending); |
Matt Briggs | 47:a68747642a7a | 371 | if (msgPending) { |
Matt Briggs | 65:d546060aa03d | 372 | std::vector<uint8_t> txEui; |
Matt Briggs | 65:d546060aa03d | 373 | txEui.reserve(8); |
Matt Briggs | 65:d546060aa03d | 374 | uint16_t data; |
Matt Briggs | 65:d546060aa03d | 375 | uint32_t msgSeqNum; |
Matt Briggs | 65:d546060aa03d | 376 | protocol->recvAlert(txEui, data, msgSeqNum); |
Matt Briggs | 69:eb391644b346 | 377 | if (msgSeqNum > maxSeenMsgSeqNum) |
Matt Briggs | 69:eb391644b346 | 378 | maxSeenMsgSeqNum = msgSeqNum; |
Matt Briggs | 69:eb391644b346 | 379 | rxMsgCnt++; // for message |
Matt Briggs | 69:eb391644b346 | 380 | myLogInfo("Got rxMsgCnt #%d, with Seqnum: %d", rxMsgCnt, msgSeqNum); |
Matt Briggs | 53:a1563574a980 | 381 | bbio->relayAlert(); |
Matt Briggs | 64:46c8819c07cc | 382 | ledPatterns.turnOn(); |
Matt Briggs | 66:bcaa6dbf538a | 383 | myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1())); |
Matt Briggs | 58:15aa7a785b9f | 384 | // Hold time for alert |
Matt Briggs | 58:15aa7a785b9f | 385 | // TODO maybe use sleep instead of wait |
Matt Briggs | 58:15aa7a785b9f | 386 | wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1())); |
Matt Briggs | 64:46c8819c07cc | 387 | ledPatterns.turnOff(); |
Matt Briggs | 58:15aa7a785b9f | 388 | bbio->relayNormal(); |
Matt Briggs | 47:a68747642a7a | 389 | } |
Matt Briggs | 69:eb391644b346 | 390 | if (pairBtnIntFlag) {// An Interrupt happened during reception |
Matt Briggs | 69:eb391644b346 | 391 | continue; // Go straight to pair handling |
Matt Briggs | 69:eb391644b346 | 392 | } |
Matt Briggs | 69:eb391644b346 | 393 | //// TEMP LOGGING |
Matt Briggs | 69:eb391644b346 | 394 | #if RX_SEQ_LOG |
Matt Briggs | 69:eb391644b346 | 395 | if ((loopCnt % 1000 == 0) && |
Matt Briggs | 69:eb391644b346 | 396 | rxMsgCnt > rxSeqLog.rxMsgCount()) { |
Matt Briggs | 69:eb391644b346 | 397 | rxSeqLog.setLoopCount(loopCnt); |
Matt Briggs | 69:eb391644b346 | 398 | rxSeqLog.setRxMsgCount(rxMsgCnt); |
Matt Briggs | 69:eb391644b346 | 399 | rxSeqLog.setMaxSeenMsgSeqNum(maxSeenMsgSeqNum); |
Matt Briggs | 69:eb391644b346 | 400 | rxSeqLog.save(); |
Matt Briggs | 69:eb391644b346 | 401 | myLogInfo("EEPROM Saved."); |
Matt Briggs | 69:eb391644b346 | 402 | } |
Matt Briggs | 69:eb391644b346 | 403 | myLogInfo("NVM Log: Loop Cnt: %d, RxMsgCnt %d, MaxSeenSeqNum %d", |
Matt Briggs | 69:eb391644b346 | 404 | rxSeqLog.loopCount(), rxSeqLog.rxMsgCount(), rxSeqLog.maxSeenMsgSeqNum()); |
Matt Briggs | 69:eb391644b346 | 405 | #endif |
Matt Briggs | 69:eb391644b346 | 406 | |
Matt Briggs | 63:e1efbe3402d9 | 407 | myLogInfo("Sleeping. Time %d", us_ticker_read()); |
Matt Briggs | 58:15aa7a785b9f | 408 | bbio->prepareSleep(); |
Matt Briggs | 47:a68747642a7a | 409 | dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 58:15aa7a785b9f | 410 | bbio->exitSleep(); |
Matt Briggs | 47:a68747642a7a | 411 | } |
Matt Briggs | 31:9c535a708ae9 | 412 | |
Matt Briggs | 64:46c8819c07cc | 413 | protocol->resetCounters(); |
Matt Briggs | 63:e1efbe3402d9 | 414 | myLogInfo("\r\n================================"); |
Matt Briggs | 58:15aa7a785b9f | 415 | loopCnt++; |
Mike Fiore |
11:d2e31743433a | 416 | } |
Matt Briggs | 47:a68747642a7a | 417 | |
Matt Briggs | 55:79ab0bbc5008 | 418 | delete protocol; |
Matt Briggs | 55:79ab0bbc5008 | 419 | delete bbio; |
Mike Fiore |
11:d2e31743433a | 420 | return 0; |
Mike Fiore |
11:d2e31743433a | 421 | } |
Matt Briggs | 47:a68747642a7a | 422 | #endif |
Mike Fiore |
11:d2e31743433a | 423 | |
Matt Briggs | 27:6b68ff715ae1 | 424 |