![](/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@57:bdac7dd17af2, 2017-02-28 (annotated)
- Committer:
- Matt Briggs
- Date:
- Tue Feb 28 09:26:27 2017 -0700
- Revision:
- 57:bdac7dd17af2
- Parent:
- 55:79ab0bbc5008
- Child:
- 58:15aa7a785b9f
Got static networking with static configuration working. Also added disabling test mode for DS2408.
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 | 41:9ef4c4d77711 | 7 | //#include <xdot_low_power.h> |
mfiore | 17:d4f82e16de5f | 8 | |
Matt Briggs | 47:a68747642a7a | 9 | #include "BaseboardIO.h" |
Matt Briggs | 41:9ef4c4d77711 | 10 | #include "CommProtocolPeerBrute.h" |
Matt Briggs | 31:9c535a708ae9 | 11 | |
Matt Briggs | 47:a68747642a7a | 12 | #ifndef __TEST__ // Exclude code for tests |
Mike Fiore |
11:d2e31743433a | 13 | Serial pc(USBTX, USBRX); |
Mike Fiore |
11:d2e31743433a | 14 | |
Matt Briggs | 47:a68747642a7a | 15 | mDot* dot = NULL; // Used by dot-utils |
Matt Briggs | 39:64f79fa6e3cc | 16 | |
Matt Briggs | 52:64a2c71c7c49 | 17 | //DigitalOut gpio3(GPIO3, 1); // Flash ~hold signal |
Matt Briggs | 48:bab9f747d9ed | 18 | |
Matt Briggs | 50:e89647e77fd5 | 19 | volatile bool ccIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 20 | volatile bool tamperIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 21 | volatile bool pairBtnIntFlag; |
Matt Briggs | 50:e89647e77fd5 | 22 | void ccInIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 23 | ccIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 24 | } |
Matt Briggs | 50:e89647e77fd5 | 25 | void tamperIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 26 | tamperIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 27 | } |
Matt Briggs | 50:e89647e77fd5 | 28 | void pairBtnIntCallback () { |
Matt Briggs | 50:e89647e77fd5 | 29 | pairBtnIntFlag = true; |
Matt Briggs | 50:e89647e77fd5 | 30 | } |
Matt Briggs | 50:e89647e77fd5 | 31 | |
Matt Briggs | 50:e89647e77fd5 | 32 | |
Mike Fiore |
11:d2e31743433a | 33 | int main() { |
Matt Briggs | 53:a1563574a980 | 34 | CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute(); |
Matt Briggs | 53:a1563574a980 | 35 | BaseboardIO *bbio = new BaseboardIO(); |
Matt Briggs | 52:64a2c71c7c49 | 36 | CmdResult result; |
Matt Briggs | 50:e89647e77fd5 | 37 | ccIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 38 | tamperIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 39 | pairBtnIntFlag = false; |
Matt Briggs | 48:bab9f747d9ed | 40 | pc.baud(115200); |
Matt Briggs | 48:bab9f747d9ed | 41 | |
Matt Briggs | 31:9c535a708ae9 | 42 | RadioEvent events; // Custom event handler for automatically displaying RX data |
Mike Fiore |
11:d2e31743433a | 43 | |
Matt Briggs | 27:6b68ff715ae1 | 44 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Matt Briggs | 47:a68747642a7a | 45 | |
Mike Fiore |
11:d2e31743433a | 46 | dot = mDot::getInstance(); |
Mike Fiore |
11:d2e31743433a | 47 | |
Mike Fiore |
16:a3832552dfe1 | 48 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Matt Briggs | 55:79ab0bbc5008 | 49 | logInfo(" libxDot-mbed5 library ID: %s", dot->getId().c_str()); |
Mike Fiore |
16:a3832552dfe1 | 50 | |
Mike Fiore |
12:ec9768677cea | 51 | // start from a well-known state |
Matt Briggs | 55:79ab0bbc5008 | 52 | // logInfo("defaulting Dot configuration"); |
Matt Briggs | 55:79ab0bbc5008 | 53 | // dot->resetConfig(); |
Mike Fiore |
12:ec9768677cea | 54 | |
Mike Fiore |
11:d2e31743433a | 55 | // make sure library logging is turned on |
Matt Briggs | 27:6b68ff715ae1 | 56 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Mike Fiore |
11:d2e31743433a | 57 | |
Mike Fiore |
11:d2e31743433a | 58 | // attach the custom events handler |
Matt Briggs | 54:c04d7b6fa075 | 59 | dot->setEvents(&events); // Little bonus event debug information |
Matt Briggs | 53:a1563574a980 | 60 | |
Matt Briggs | 53:a1563574a980 | 61 | // Finish radio init |
Matt Briggs | 53:a1563574a980 | 62 | protocol->init(); |
Matt Briggs | 53:a1563574a980 | 63 | |
Matt Briggs | 53:a1563574a980 | 64 | // save changes to configuration |
Matt Briggs | 53:a1563574a980 | 65 | // logInfo("saving configuration"); |
Matt Briggs | 53:a1563574a980 | 66 | // core_util_critical_section_enter(); |
Matt Briggs | 53:a1563574a980 | 67 | // bool dotConfigSaved = dot->saveConfig(); |
Matt Briggs | 53:a1563574a980 | 68 | // core_util_critical_section_exit(); |
Matt Briggs | 53:a1563574a980 | 69 | // if (!dotConfigSaved) { |
Matt Briggs | 53:a1563574a980 | 70 | // logError("failed to save configuration"); |
Matt Briggs | 53:a1563574a980 | 71 | // } |
Mike Fiore |
11:d2e31743433a | 72 | |
Matt Briggs | 31:9c535a708ae9 | 73 | // Setup programmable voltage detector |
Matt Briggs | 31:9c535a708ae9 | 74 | // PVD_LEVEL0 Falling 1.85 |
Matt Briggs | 31:9c535a708ae9 | 75 | // PVD_LEVEL1 Falling 2.04 |
Matt Briggs | 31:9c535a708ae9 | 76 | // PVD_LEVEL2 Falling 2.24 |
Matt Briggs | 31:9c535a708ae9 | 77 | // PVD_LEVEL3 Falling 2.44 |
Matt Briggs | 31:9c535a708ae9 | 78 | // PVD_LEVEL4 Falling 2.64 |
Matt Briggs | 31:9c535a708ae9 | 79 | // PVD_LEVEL5 Falling 2.84 |
Matt Briggs | 31:9c535a708ae9 | 80 | // PVD_LEVEL6 Falling 3.05 |
Matt Briggs | 50:e89647e77fd5 | 81 | // PWR_PVDTypeDef pvdConfig; |
Matt Briggs | 50:e89647e77fd5 | 82 | // pvdConfig.Mode = PWR_PVD_MODE_NORMAL; |
Matt Briggs | 50:e89647e77fd5 | 83 | // pvdConfig.PVDLevel = PWR_PVDLEVEL_5; |
Matt Briggs | 50:e89647e77fd5 | 84 | // |
Matt Briggs | 50:e89647e77fd5 | 85 | // HAL_PWR_ConfigPVD(&pvdConfig); |
Matt Briggs | 50:e89647e77fd5 | 86 | // HAL_PWR_EnablePVD(); |
Matt Briggs | 50:e89647e77fd5 | 87 | // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel); |
Matt Briggs | 50:e89647e77fd5 | 88 | // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation |
Mike Fiore |
11:d2e31743433a | 89 | |
Matt Briggs | 54:c04d7b6fa075 | 90 | dot->setWakePin(UART1_RX); |
Matt Briggs | 24:fdf87e4b72e5 | 91 | |
Mike Fiore |
11:d2e31743433a | 92 | |
Mike Fiore |
11:d2e31743433a | 93 | // display configuration |
Matt Briggs | 53:a1563574a980 | 94 | // display_config(); |
Matt Briggs | 52:64a2c71c7c49 | 95 | |
Matt Briggs | 53:a1563574a980 | 96 | pc.printf("= Baseboard Init Starting =\r\n"); |
Matt Briggs | 53:a1563574a980 | 97 | result = bbio->init(); |
Matt Briggs | 52:64a2c71c7c49 | 98 | if (result == cmdSuccess) { |
Matt Briggs | 52:64a2c71c7c49 | 99 | pc.printf("= Baseboard Init Finished Successfully =\r\n"); |
Matt Briggs | 52:64a2c71c7c49 | 100 | } |
Matt Briggs | 52:64a2c71c7c49 | 101 | else { |
Matt Briggs | 52:64a2c71c7c49 | 102 | pc.printf("= Baseboard Init Finished with Error =\r\n"); |
Matt Briggs | 52:64a2c71c7c49 | 103 | } |
Mike Fiore |
11:d2e31743433a | 104 | |
Matt Briggs | 57:bdac7dd17af2 | 105 | uint16_t txSeqNum=0; |
Matt Briggs | 57:bdac7dd17af2 | 106 | uint16_t rxSeqNum=0; |
Matt Briggs | 50:e89647e77fd5 | 107 | Callback<void()> ccInIntObj (&ccInIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 108 | Callback<void()> tamperIntObj (&tamperIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 109 | Callback<void()> pairBtnIntObj (&pairBtnIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 110 | |
Matt Briggs | 53:a1563574a980 | 111 | bbio->sampleUserSwitches(); |
Matt Briggs | 53:a1563574a980 | 112 | bbio->relayNormal(); // Always force relay in known state |
Matt Briggs | 26:9411b26a5084 | 113 | |
Matt Briggs | 53:a1563574a980 | 114 | unsigned int intCnt = 0; // Just a quick temp varaible to keep track of ints |
Matt Briggs | 47:a68747642a7a | 115 | /** |
Matt Briggs | 47:a68747642a7a | 116 | * Main Loop |
Matt Briggs | 47:a68747642a7a | 117 | */ |
Mike Fiore |
11:d2e31743433a | 118 | while (true) { |
Matt Briggs | 25:fea776239709 | 119 | std::vector<uint8_t> data; |
Matt Briggs | 53:a1563574a980 | 120 | bbio->ledOff(); |
Matt Briggs | 47:a68747642a7a | 121 | |
Matt Briggs | 50:e89647e77fd5 | 122 | // Sample IO and update any configuration |
Matt Briggs | 53:a1563574a980 | 123 | bool prevCCNormallyOpen = bbio->isCCNO(); |
Matt Briggs | 53:a1563574a980 | 124 | bbio->sampleUserSwitches(); |
Matt Briggs | 53:a1563574a980 | 125 | if (prevCCNormallyOpen == bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed |
Matt Briggs | 53:a1563574a980 | 126 | bbio->regCCInInt(ccInIntObj); |
Matt Briggs | 53:a1563574a980 | 127 | bbio->regTamperInt(tamperIntObj); |
Matt Briggs | 53:a1563574a980 | 128 | bbio->regPairBtnInt(pairBtnIntObj); |
Matt Briggs | 53:a1563574a980 | 129 | bbio->relayNormal(); |
Matt Briggs | 50:e89647e77fd5 | 130 | } |
Matt Briggs | 53:a1563574a980 | 131 | if (bbio->isTx()) { |
Matt Briggs | 53:a1563574a980 | 132 | protocol->setTx(true); |
Matt Briggs | 50:e89647e77fd5 | 133 | } |
Matt Briggs | 50:e89647e77fd5 | 134 | else { // RX |
Matt Briggs | 53:a1563574a980 | 135 | protocol->setTx(false); |
Matt Briggs | 50:e89647e77fd5 | 136 | } |
Matt Briggs | 50:e89647e77fd5 | 137 | // End sample and update |
Matt Briggs | 47:a68747642a7a | 138 | |
Matt Briggs | 53:a1563574a980 | 139 | if (protocol->isTx()) { |
Matt Briggs | 57:bdac7dd17af2 | 140 | logInfo("Status #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d", |
Matt Briggs | 57:bdac7dd17af2 | 141 | intCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag); |
Matt Briggs | 53:a1563574a980 | 142 | // TODO add tamper |
Matt Briggs | 53:a1563574a980 | 143 | if (pairBtnIntFlag) { // Wait up to 1 second for short button hit |
Matt Briggs | 53:a1563574a980 | 144 | for (int i=0; i<10;i++) { |
Matt Briggs | 53:a1563574a980 | 145 | if (bbio->isPairBtn() == false){ // Button released |
Matt Briggs | 53:a1563574a980 | 146 | break; |
Matt Briggs | 53:a1563574a980 | 147 | } |
Matt Briggs | 53:a1563574a980 | 148 | wait(0.1); |
Matt Briggs | 53:a1563574a980 | 149 | } |
Matt Briggs | 53:a1563574a980 | 150 | } |
Matt Briggs | 50:e89647e77fd5 | 151 | if (ccIntFlag || // If contact closure in |
Matt Briggs | 57:bdac7dd17af2 | 152 | bbio->isCCInAlert() || // If closure remains in effect |
Matt Briggs | 53:a1563574a980 | 153 | (pairBtnIntFlag && (bbio->isPairBtn() == false))) { // If short pair btn hit |
Matt Briggs | 47:a68747642a7a | 154 | |
Matt Briggs | 50:e89647e77fd5 | 155 | ccIntFlag = false; |
Matt Briggs | 57:bdac7dd17af2 | 156 | pairBtnIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 157 | tamperIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 158 | #if LED_FEEDBACK |
Matt Briggs | 53:a1563574a980 | 159 | bbio->ledOn(); |
Matt Briggs | 50:e89647e77fd5 | 160 | #endif |
Matt Briggs | 50:e89647e77fd5 | 161 | |
Matt Briggs | 57:bdac7dd17af2 | 162 | data.push_back((txSeqNum >> 8) & 0xFF); |
Matt Briggs | 57:bdac7dd17af2 | 163 | data.push_back(txSeqNum & 0xFF); |
Matt Briggs | 57:bdac7dd17af2 | 164 | std::string dataStr(data.begin(), data.end()); |
Matt Briggs | 57:bdac7dd17af2 | 165 | logInfo("Sent msg num: %d, payload: %s", txSeqNum, dataStr.c_str()); |
Matt Briggs | 53:a1563574a980 | 166 | protocol->send(data); |
Matt Briggs | 57:bdac7dd17af2 | 167 | txSeqNum++; |
Matt Briggs | 50:e89647e77fd5 | 168 | wait(0.5); // Leave the LED on so a person can see it. |
Matt Briggs | 50:e89647e77fd5 | 169 | } |
Matt Briggs | 50:e89647e77fd5 | 170 | |
Matt Briggs | 53:a1563574a980 | 171 | bbio->ledOff(); |
Matt Briggs | 52:64a2c71c7c49 | 172 | // sleep_save_io(); |
Matt Briggs | 52:64a2c71c7c49 | 173 | // sleep_configure_io(); |
Matt Briggs | 57:bdac7dd17af2 | 174 | if (bbio->isCCInAlert()) { // Still in alert mode |
Matt Briggs | 57:bdac7dd17af2 | 175 | dot->sleep(10, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 2 secs if CCInAlert is asserted |
Matt Briggs | 57:bdac7dd17af2 | 176 | } |
Matt Briggs | 57:bdac7dd17af2 | 177 | else { |
Matt Briggs | 57:bdac7dd17af2 | 178 | dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 57:bdac7dd17af2 | 179 | } |
Matt Briggs | 53:a1563574a980 | 180 | // sleep_restore_io(); |
Matt Briggs | 47:a68747642a7a | 181 | } |
Mike Fiore |
11:d2e31743433a | 182 | |
Matt Briggs | 53:a1563574a980 | 183 | if (protocol->isRx()) { |
Matt Briggs | 47:a68747642a7a | 184 | bool msgPending; |
Matt Briggs | 53:a1563574a980 | 185 | protocol->listen(msgPending); |
Matt Briggs | 53:a1563574a980 | 186 | logInfo("Listening."); |
Matt Briggs | 47:a68747642a7a | 187 | if (msgPending) { |
Matt Briggs | 53:a1563574a980 | 188 | protocol->recv(data); |
Matt Briggs | 47:a68747642a7a | 189 | std::string dataStr(data.begin(), data.end()); |
Matt Briggs | 57:bdac7dd17af2 | 190 | logInfo("Got msg num: %d, payload: %s", rxSeqNum, dataStr.c_str()); |
Matt Briggs | 53:a1563574a980 | 191 | bbio->relayAlert(); |
Matt Briggs | 57:bdac7dd17af2 | 192 | rxSeqNum++; |
Matt Briggs | 50:e89647e77fd5 | 193 | #if LED_FEEDBACK |
Matt Briggs | 53:a1563574a980 | 194 | bbio->ledOn(); |
Matt Briggs | 50:e89647e77fd5 | 195 | #endif |
Matt Briggs | 54:c04d7b6fa075 | 196 | wait(5.0); // TODO this should be configurable |
Matt Briggs | 47:a68747642a7a | 197 | } |
Matt Briggs | 53:a1563574a980 | 198 | bbio->ledOff(); |
Matt Briggs | 53:a1563574a980 | 199 | bbio->relayNormal(); |
Matt Briggs | 47:a68747642a7a | 200 | logInfo("Sleeping. Time %d", us_ticker_read()); |
Matt Briggs | 52:64a2c71c7c49 | 201 | // sleep_save_io(); |
Matt Briggs | 52:64a2c71c7c49 | 202 | // sleep_configure_io(); |
Matt Briggs | 47:a68747642a7a | 203 | // TODO maybe add if statement here to prevent double hits by sleeping for a longer time |
Matt Briggs | 54:c04d7b6fa075 | 204 | protocol->sampleDLC(); |
Matt Briggs | 47:a68747642a7a | 205 | dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 52:64a2c71c7c49 | 206 | // sleep_restore_io(); |
Matt Briggs | 47:a68747642a7a | 207 | } |
Matt Briggs | 31:9c535a708ae9 | 208 | |
Matt Briggs | 50:e89647e77fd5 | 209 | // TODO maybe a good place to put pairing logic |
Matt Briggs | 29:e05e35976cfe | 210 | |
Matt Briggs | 44:ece6330e9b57 | 211 | logInfo("================================"); |
Matt Briggs | 57:bdac7dd17af2 | 212 | wait(1.0); // May want to remove |
Matt Briggs | 47:a68747642a7a | 213 | // Need to re-implement some of these sleep functions |
Matt Briggs | 41:9ef4c4d77711 | 214 | // sleep_save_io(); |
Matt Briggs | 41:9ef4c4d77711 | 215 | // sleep_configure_io(); |
Matt Briggs | 53:a1563574a980 | 216 | intCnt++; |
Mike Fiore |
11:d2e31743433a | 217 | } |
Matt Briggs | 47:a68747642a7a | 218 | |
Matt Briggs | 55:79ab0bbc5008 | 219 | delete protocol; |
Matt Briggs | 55:79ab0bbc5008 | 220 | delete bbio; |
Mike Fiore |
11:d2e31743433a | 221 | return 0; |
Mike Fiore |
11:d2e31743433a | 222 | } |
Matt Briggs | 47:a68747642a7a | 223 | #endif |
Mike Fiore |
11:d2e31743433a | 224 | |
Matt Briggs | 27:6b68ff715ae1 | 225 |