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