![](/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@50:e89647e77fd5, 2017-02-17 (annotated)
- Committer:
- Matt Briggs
- Date:
- Fri Feb 17 11:58:40 2017 -0700
- Revision:
- 50:e89647e77fd5
- Parent:
- 48:bab9f747d9ed
- Child:
- 52:64a2c71c7c49
Added retries in ds2408 lib and started updating main to make static wireless bridge
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 | 48:bab9f747d9ed | 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 | 50:e89647e77fd5 | 34 | ccIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 35 | tamperIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 36 | pairBtnIntFlag = false; |
Matt Briggs | 48:bab9f747d9ed | 37 | pc.baud(115200); |
Matt Briggs | 48:bab9f747d9ed | 38 | |
Matt Briggs | 47:a68747642a7a | 39 | CommProtocolPeerBrute protocol; |
Matt Briggs | 44:ece6330e9b57 | 40 | BaseboardIO bbio; |
Matt Briggs | 31:9c535a708ae9 | 41 | RadioEvent events; // Custom event handler for automatically displaying RX data |
Mike Fiore |
11:d2e31743433a | 42 | |
Matt Briggs | 27:6b68ff715ae1 | 43 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Matt Briggs | 47:a68747642a7a | 44 | |
Mike Fiore |
11:d2e31743433a | 45 | dot = mDot::getInstance(); |
Mike Fiore |
11:d2e31743433a | 46 | |
Mike Fiore |
16:a3832552dfe1 | 47 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Mike Fiore |
16:a3832552dfe1 | 48 | |
Mike Fiore |
12:ec9768677cea | 49 | // start from a well-known state |
Mike Fiore |
12:ec9768677cea | 50 | logInfo("defaulting Dot configuration"); |
Mike Fiore |
12:ec9768677cea | 51 | dot->resetConfig(); |
Mike Fiore |
12:ec9768677cea | 52 | |
Mike Fiore |
11:d2e31743433a | 53 | // make sure library logging is turned on |
Matt Briggs | 27:6b68ff715ae1 | 54 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Mike Fiore |
11:d2e31743433a | 55 | |
Mike Fiore |
11:d2e31743433a | 56 | // attach the custom events handler |
Matt Briggs | 44:ece6330e9b57 | 57 | dot->setEvents(&events); // Little bonus event debug information |
Mike Fiore |
11:d2e31743433a | 58 | |
Matt Briggs | 31:9c535a708ae9 | 59 | // Setup programmable voltage detector |
Matt Briggs | 31:9c535a708ae9 | 60 | // PVD_LEVEL0 Falling 1.85 |
Matt Briggs | 31:9c535a708ae9 | 61 | // PVD_LEVEL1 Falling 2.04 |
Matt Briggs | 31:9c535a708ae9 | 62 | // PVD_LEVEL2 Falling 2.24 |
Matt Briggs | 31:9c535a708ae9 | 63 | // PVD_LEVEL3 Falling 2.44 |
Matt Briggs | 31:9c535a708ae9 | 64 | // PVD_LEVEL4 Falling 2.64 |
Matt Briggs | 31:9c535a708ae9 | 65 | // PVD_LEVEL5 Falling 2.84 |
Matt Briggs | 31:9c535a708ae9 | 66 | // PVD_LEVEL6 Falling 3.05 |
Matt Briggs | 50:e89647e77fd5 | 67 | // PWR_PVDTypeDef pvdConfig; |
Matt Briggs | 50:e89647e77fd5 | 68 | // pvdConfig.Mode = PWR_PVD_MODE_NORMAL; |
Matt Briggs | 50:e89647e77fd5 | 69 | // pvdConfig.PVDLevel = PWR_PVDLEVEL_5; |
Matt Briggs | 50:e89647e77fd5 | 70 | // |
Matt Briggs | 50:e89647e77fd5 | 71 | // HAL_PWR_ConfigPVD(&pvdConfig); |
Matt Briggs | 50:e89647e77fd5 | 72 | // HAL_PWR_EnablePVD(); |
Matt Briggs | 50:e89647e77fd5 | 73 | // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel); |
Matt Briggs | 50:e89647e77fd5 | 74 | // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation |
Mike Fiore |
11:d2e31743433a | 75 | |
Matt Briggs | 41:9ef4c4d77711 | 76 | dot->setWakePin(WAKE); |
Matt Briggs | 24:fdf87e4b72e5 | 77 | |
Mike Fiore |
11:d2e31743433a | 78 | // save changes to configuration |
Mike Fiore |
11:d2e31743433a | 79 | logInfo("saving configuration"); |
Mike Fiore |
11:d2e31743433a | 80 | if (!dot->saveConfig()) { |
Mike Fiore |
11:d2e31743433a | 81 | logError("failed to save configuration"); |
Mike Fiore |
11:d2e31743433a | 82 | } |
Mike Fiore |
11:d2e31743433a | 83 | |
Mike Fiore |
11:d2e31743433a | 84 | // display configuration |
Mike Fiore |
11:d2e31743433a | 85 | display_config(); |
Mike Fiore |
11:d2e31743433a | 86 | |
Matt Briggs | 41:9ef4c4d77711 | 87 | uint16_t seqNum=0; |
Matt Briggs | 50:e89647e77fd5 | 88 | Callback<void()> ccInIntObj (&ccInIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 89 | Callback<void()> tamperIntObj (&tamperIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 90 | Callback<void()> pairBtnIntObj (&pairBtnIntCallback); |
Matt Briggs | 50:e89647e77fd5 | 91 | bbio.regCCInInt(ccInIntObj); |
Matt Briggs | 50:e89647e77fd5 | 92 | bbio.regTamperInt(tamperIntObj); |
Matt Briggs | 50:e89647e77fd5 | 93 | bbio.regPairBtnInt(pairBtnIntObj); |
Matt Briggs | 50:e89647e77fd5 | 94 | |
Matt Briggs | 50:e89647e77fd5 | 95 | bbio.sampleUserSwitches(); |
Matt Briggs | 50:e89647e77fd5 | 96 | bbio.relayNormal(); // Always force relay in known state |
Matt Briggs | 26:9411b26a5084 | 97 | |
Matt Briggs | 47:a68747642a7a | 98 | /** |
Matt Briggs | 47:a68747642a7a | 99 | * Main Loop |
Matt Briggs | 47:a68747642a7a | 100 | */ |
Mike Fiore |
11:d2e31743433a | 101 | while (true) { |
Matt Briggs | 25:fea776239709 | 102 | std::vector<uint8_t> data; |
Matt Briggs | 47:a68747642a7a | 103 | bbio.ledOff(); |
Matt Briggs | 47:a68747642a7a | 104 | |
Matt Briggs | 50:e89647e77fd5 | 105 | // Sample IO and update any configuration |
Matt Briggs | 50:e89647e77fd5 | 106 | bool prevCCNormallyOpen = bbio.isCCNO(); |
Matt Briggs | 50:e89647e77fd5 | 107 | bbio.sampleUserSwitches(); |
Matt Briggs | 50:e89647e77fd5 | 108 | if (prevCCNormallyOpen == bbio.isCCNO()) { // Only activate the coil if the DIP SW has changed |
Matt Briggs | 50:e89647e77fd5 | 109 | bbio.relayNormal(); |
Matt Briggs | 50:e89647e77fd5 | 110 | } |
Matt Briggs | 50:e89647e77fd5 | 111 | if (bbio.isTx()) { |
Matt Briggs | 50:e89647e77fd5 | 112 | protocol.setTx(true); |
Matt Briggs | 50:e89647e77fd5 | 113 | } |
Matt Briggs | 50:e89647e77fd5 | 114 | else { // RX |
Matt Briggs | 50:e89647e77fd5 | 115 | protocol.setTx(false); |
Matt Briggs | 50:e89647e77fd5 | 116 | } |
Matt Briggs | 50:e89647e77fd5 | 117 | // End sample and update |
Matt Briggs | 47:a68747642a7a | 118 | |
Matt Briggs | 50:e89647e77fd5 | 119 | if (protocol.isTx()) { |
Matt Briggs | 50:e89647e77fd5 | 120 | if (ccIntFlag || // If contact closure in |
Matt Briggs | 50:e89647e77fd5 | 121 | (tamperIntFlag && (bbio.isPairBtn() == false))) { // If short pair btn hit |
Matt Briggs | 47:a68747642a7a | 122 | |
Matt Briggs | 50:e89647e77fd5 | 123 | ccIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 124 | tamperIntFlag = false; |
Matt Briggs | 50:e89647e77fd5 | 125 | #if LED_FEEDBACK |
Matt Briggs | 50:e89647e77fd5 | 126 | bbio.ledOn(); |
Matt Briggs | 50:e89647e77fd5 | 127 | #endif |
Matt Briggs | 50:e89647e77fd5 | 128 | |
Matt Briggs | 50:e89647e77fd5 | 129 | data.push_back((seqNum >> 8) & 0xFF); |
Matt Briggs | 50:e89647e77fd5 | 130 | data.push_back(seqNum & 0xFF); |
Matt Briggs | 50:e89647e77fd5 | 131 | protocol.send(data); |
Matt Briggs | 50:e89647e77fd5 | 132 | seqNum++; |
Matt Briggs | 50:e89647e77fd5 | 133 | wait(0.5); // Leave the LED on so a person can see it. |
Matt Briggs | 50:e89647e77fd5 | 134 | } |
Matt Briggs | 50:e89647e77fd5 | 135 | |
Matt Briggs | 47:a68747642a7a | 136 | bbio.ledOff(); |
Matt Briggs | 47:a68747642a7a | 137 | sleep_save_io(); |
Matt Briggs | 47:a68747642a7a | 138 | sleep_configure_io(); |
Matt Briggs | 47:a68747642a7a | 139 | dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 47:a68747642a7a | 140 | sleep_restore_io(); |
Matt Briggs | 47:a68747642a7a | 141 | } |
Mike Fiore |
11:d2e31743433a | 142 | |
Matt Briggs | 47:a68747642a7a | 143 | if (protocol.isRx()) { |
Matt Briggs | 47:a68747642a7a | 144 | bool msgPending; |
Matt Briggs | 47:a68747642a7a | 145 | protocol.listen(msgPending); |
Matt Briggs | 47:a68747642a7a | 146 | if (msgPending) { |
Matt Briggs | 47:a68747642a7a | 147 | protocol.recv(data); |
Matt Briggs | 47:a68747642a7a | 148 | std::string dataStr(data.begin(), data.end()); |
Matt Briggs | 47:a68747642a7a | 149 | logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str()); |
Matt Briggs | 50:e89647e77fd5 | 150 | bbio.relayAlert(); |
Matt Briggs | 47:a68747642a7a | 151 | seqNum++; |
Matt Briggs | 50:e89647e77fd5 | 152 | #if LED_FEEDBACK |
Matt Briggs | 47:a68747642a7a | 153 | bbio.ledOn(); |
Matt Briggs | 50:e89647e77fd5 | 154 | #endif |
Matt Briggs | 50:e89647e77fd5 | 155 | wait(0.5); // TODO this should be configurable |
Matt Briggs | 47:a68747642a7a | 156 | } |
Matt Briggs | 47:a68747642a7a | 157 | bbio.ledOff(); |
Matt Briggs | 50:e89647e77fd5 | 158 | bbio.relayNormal(); |
Matt Briggs | 47:a68747642a7a | 159 | logInfo("Sleeping. Time %d", us_ticker_read()); |
Matt Briggs | 47:a68747642a7a | 160 | sleep_save_io(); |
Matt Briggs | 47:a68747642a7a | 161 | sleep_configure_io(); |
Matt Briggs | 47:a68747642a7a | 162 | // TODO maybe add if statement here to prevent double hits by sleeping for a longer time |
Matt Briggs | 47:a68747642a7a | 163 | dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button |
Matt Briggs | 47:a68747642a7a | 164 | sleep_restore_io(); |
Matt Briggs | 47:a68747642a7a | 165 | } |
Matt Briggs | 31:9c535a708ae9 | 166 | |
Matt Briggs | 50:e89647e77fd5 | 167 | // TODO maybe a good place to put pairing logic |
Matt Briggs | 29:e05e35976cfe | 168 | |
Matt Briggs | 44:ece6330e9b57 | 169 | logInfo("================================"); |
Matt Briggs | 44:ece6330e9b57 | 170 | wait(1.0); |
Matt Briggs | 47:a68747642a7a | 171 | // Need to re-implement some of these sleep functions |
Matt Briggs | 41:9ef4c4d77711 | 172 | // sleep_save_io(); |
Matt Briggs | 41:9ef4c4d77711 | 173 | // sleep_configure_io(); |
Matt Briggs | 34:5618603e5fc3 | 174 | |
Matt Briggs | 47:a68747642a7a | 175 | // dot->sleep(2, mDot::INTERRUPT, false); // Go to sleep until wake button |
Mike Fiore |
11:d2e31743433a | 176 | } |
Matt Briggs | 47:a68747642a7a | 177 | |
Mike Fiore |
11:d2e31743433a | 178 | return 0; |
Mike Fiore |
11:d2e31743433a | 179 | } |
Matt Briggs | 47:a68747642a7a | 180 | #endif |
Mike Fiore |
11:d2e31743433a | 181 | |
Matt Briggs | 27:6b68ff715ae1 | 182 |