Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Committer:
Matt Briggs
Date:
Tue Mar 14 09:54:04 2017 -0600
Revision:
63:e1efbe3402d9
Parent:
62:9751a8504c82
Child:
64:46c8819c07cc
Quick refactor of logging

Who changed what in which revision?

UserRevisionLine numberNew 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"
mfiore 17:d4f82e16de5f 11
Matt Briggs 47:a68747642a7a 12 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 13 #include "CommProtocolPeerBrute.h"
Matt Briggs 31:9c535a708ae9 14
Matt Briggs 47:a68747642a7a 15 #ifndef __TEST__ // Exclude code for tests
Mike Fiore 11:d2e31743433a 16 Serial pc(USBTX, USBRX);
Mike Fiore 11:d2e31743433a 17
Matt Briggs 47:a68747642a7a 18 mDot* dot = NULL; // Used by dot-utils
Matt Briggs 39:64f79fa6e3cc 19
Matt Briggs 50:e89647e77fd5 20 volatile bool ccIntFlag;
Matt Briggs 50:e89647e77fd5 21 volatile bool tamperIntFlag;
Matt Briggs 50:e89647e77fd5 22 volatile bool pairBtnIntFlag;
Matt Briggs 50:e89647e77fd5 23 void ccInIntCallback () {
Matt Briggs 50:e89647e77fd5 24 ccIntFlag = true;
Matt Briggs 50:e89647e77fd5 25 }
Matt Briggs 50:e89647e77fd5 26 void tamperIntCallback () {
Matt Briggs 50:e89647e77fd5 27 tamperIntFlag = true;
Matt Briggs 50:e89647e77fd5 28 }
Matt Briggs 50:e89647e77fd5 29 void pairBtnIntCallback () {
Matt Briggs 50:e89647e77fd5 30 pairBtnIntFlag = true;
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 58:15aa7a785b9f 36 WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
Matt Briggs 58:15aa7a785b9f 37
Matt Briggs 52:64a2c71c7c49 38 CmdResult result;
Matt Briggs 50:e89647e77fd5 39 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 40 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 41 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 42 pc.baud(115200);
Matt Briggs 48:bab9f747d9ed 43
Matt Briggs 31:9c535a708ae9 44 RadioEvent events; // Custom event handler for automatically displaying RX data
Mike Fiore 11:d2e31743433a 45
Matt Briggs 27:6b68ff715ae1 46 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 63:e1efbe3402d9 47 MyLog::setLogLevel(MyLog::INFO_LEVEL);
Matt Briggs 47:a68747642a7a 48
Mike Fiore 11:d2e31743433a 49 dot = mDot::getInstance();
Mike Fiore 11:d2e31743433a 50
Mike Fiore 11:d2e31743433a 51 // make sure library logging is turned on
Matt Briggs 63:e1efbe3402d9 52 dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
Mike Fiore 11:d2e31743433a 53
Matt Briggs 63:e1efbe3402d9 54 myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 63:e1efbe3402d9 55 myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
Matt Briggs 62:9751a8504c82 56
Mike Fiore 11:d2e31743433a 57 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 58 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 59
Matt Briggs 53:a1563574a980 60 // Finish radio init
Matt Briggs 63:e1efbe3402d9 61 myLogInfo("= Protocol Init Starting =\r\n");
Matt Briggs 53:a1563574a980 62 protocol->init();
Matt Briggs 62:9751a8504c82 63 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 64 myLogInfo("= Protocol Init Finished Successfully =\r\n");
Matt Briggs 62:9751a8504c82 65 }
Matt Briggs 62:9751a8504c82 66 else {
Matt Briggs 63:e1efbe3402d9 67 myLogInfo("= Protocol Init Finished with Error =\r\n");
Matt Briggs 62:9751a8504c82 68 }
Matt Briggs 63:e1efbe3402d9 69 protocol->printDotConfig();
Matt Briggs 53:a1563574a980 70
Matt Briggs 53:a1563574a980 71 // save changes to configuration
Matt Briggs 53:a1563574a980 72 // logInfo("saving configuration");
Matt Briggs 53:a1563574a980 73 // core_util_critical_section_enter();
Matt Briggs 53:a1563574a980 74 // bool dotConfigSaved = dot->saveConfig();
Matt Briggs 53:a1563574a980 75 // core_util_critical_section_exit();
Matt Briggs 53:a1563574a980 76 // if (!dotConfigSaved) {
Matt Briggs 53:a1563574a980 77 // logError("failed to save configuration");
Matt Briggs 53:a1563574a980 78 // }
Mike Fiore 11:d2e31743433a 79
Matt Briggs 31:9c535a708ae9 80 // Setup programmable voltage detector
Matt Briggs 31:9c535a708ae9 81 // PVD_LEVEL0 Falling 1.85
Matt Briggs 31:9c535a708ae9 82 // PVD_LEVEL1 Falling 2.04
Matt Briggs 31:9c535a708ae9 83 // PVD_LEVEL2 Falling 2.24
Matt Briggs 31:9c535a708ae9 84 // PVD_LEVEL3 Falling 2.44
Matt Briggs 31:9c535a708ae9 85 // PVD_LEVEL4 Falling 2.64
Matt Briggs 31:9c535a708ae9 86 // PVD_LEVEL5 Falling 2.84
Matt Briggs 31:9c535a708ae9 87 // PVD_LEVEL6 Falling 3.05
Matt Briggs 50:e89647e77fd5 88 // PWR_PVDTypeDef pvdConfig;
Matt Briggs 50:e89647e77fd5 89 // pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
Matt Briggs 50:e89647e77fd5 90 // pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
Matt Briggs 50:e89647e77fd5 91 //
Matt Briggs 50:e89647e77fd5 92 // HAL_PWR_ConfigPVD(&pvdConfig);
Matt Briggs 50:e89647e77fd5 93 // HAL_PWR_EnablePVD();
Matt Briggs 50:e89647e77fd5 94 // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
Matt Briggs 50:e89647e77fd5 95 // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
Mike Fiore 11:d2e31743433a 96
Matt Briggs 58:15aa7a785b9f 97 dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
Matt Briggs 54:c04d7b6fa075 98 dot->setWakePin(UART1_RX);
Matt Briggs 24:fdf87e4b72e5 99
Matt Briggs 62:9751a8504c82 100 display_config(); // Print configuration for now
Matt Briggs 62:9751a8504c82 101
Mike Fiore 11:d2e31743433a 102 // display configuration
Matt Briggs 53:a1563574a980 103 // display_config();
Matt Briggs 52:64a2c71c7c49 104
Matt Briggs 63:e1efbe3402d9 105 myLogInfo("= Baseboard Init Starting =\r\n");
Matt Briggs 53:a1563574a980 106 result = bbio->init();
Matt Briggs 52:64a2c71c7c49 107 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 108 myLogInfo("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 52:64a2c71c7c49 109 }
Matt Briggs 52:64a2c71c7c49 110 else {
Matt Briggs 63:e1efbe3402d9 111 myLogInfo("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 52:64a2c71c7c49 112 }
Mike Fiore 11:d2e31743433a 113
Matt Briggs 57:bdac7dd17af2 114 uint16_t txSeqNum=0;
Matt Briggs 57:bdac7dd17af2 115 uint16_t rxSeqNum=0;
Matt Briggs 50:e89647e77fd5 116 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 50:e89647e77fd5 117 Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 50:e89647e77fd5 118 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 50:e89647e77fd5 119
Matt Briggs 53:a1563574a980 120 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 121 bbio->relayNormal(); // Always force relay in known state
Matt Briggs 58:15aa7a785b9f 122 bbio->regCCInInt(ccInIntObj);
Matt Briggs 58:15aa7a785b9f 123 bbio->regTamperInt(tamperIntObj);
Matt Briggs 58:15aa7a785b9f 124 bbio->regPairBtnInt(pairBtnIntObj);
Matt Briggs 26:9411b26a5084 125
Matt Briggs 58:15aa7a785b9f 126 // Start flash powered down
Matt Briggs 58:15aa7a785b9f 127 flash->powerDown();
Matt Briggs 58:15aa7a785b9f 128
Matt Briggs 58:15aa7a785b9f 129 unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
Matt Briggs 58:15aa7a785b9f 130 bool prevCCNormallyOpen;
Matt Briggs 61:8d9efd33cac9 131 PairBtnState pairBtnState;
Matt Briggs 47:a68747642a7a 132 /**
Matt Briggs 47:a68747642a7a 133 * Main Loop
Matt Briggs 47:a68747642a7a 134 */
Mike Fiore 11:d2e31743433a 135 while (true) {
Matt Briggs 25:fea776239709 136 std::vector<uint8_t> data;
Matt Briggs 53:a1563574a980 137 bbio->ledOff();
Matt Briggs 47:a68747642a7a 138
Matt Briggs 50:e89647e77fd5 139 // Sample IO and update any configuration
Matt Briggs 58:15aa7a785b9f 140 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 53:a1563574a980 141 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 142 if (prevCCNormallyOpen == bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 53:a1563574a980 143 bbio->regCCInInt(ccInIntObj);
Matt Briggs 53:a1563574a980 144 bbio->relayNormal();
Matt Briggs 50:e89647e77fd5 145 }
Matt Briggs 53:a1563574a980 146 if (bbio->isTx()) {
Matt Briggs 53:a1563574a980 147 protocol->setTx(true);
Matt Briggs 50:e89647e77fd5 148 }
Matt Briggs 50:e89647e77fd5 149 else { // RX
Matt Briggs 53:a1563574a980 150 protocol->setTx(false);
Matt Briggs 50:e89647e77fd5 151 }
Matt Briggs 50:e89647e77fd5 152 // End sample and update
Matt Briggs 61:8d9efd33cac9 153 // Pair logic
Matt Briggs 61:8d9efd33cac9 154 if (pairBtnIntFlag) {
Matt Briggs 61:8d9efd33cac9 155 pairBtnState = PairBtnInterp::read(bbio);
Matt Briggs 61:8d9efd33cac9 156 if (protocol->isTx()) {
Matt Briggs 61:8d9efd33cac9 157 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 61:8d9efd33cac9 158 protocol->sampleDLC();
Matt Briggs 62:9751a8504c82 159 protocol->configForPairingNetwork();
Matt Briggs 61:8d9efd33cac9 160 protocol->sendPairReq();
Matt Briggs 63:e1efbe3402d9 161 myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 162 result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 163 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 164 myLogInfo("Got accept");
Matt Briggs 61:8d9efd33cac9 165 }
Matt Briggs 61:8d9efd33cac9 166 else {
Matt Briggs 63:e1efbe3402d9 167 myLogInfo("Did not receive accept");
Matt Briggs 61:8d9efd33cac9 168 }
Matt Briggs 62:9751a8504c82 169 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 170 protocol->printDotConfig();
Matt Briggs 61:8d9efd33cac9 171 }
Matt Briggs 61:8d9efd33cac9 172 }
Matt Briggs 61:8d9efd33cac9 173 if (protocol->isRx()) {
Matt Briggs 61:8d9efd33cac9 174 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 61:8d9efd33cac9 175 protocol->sampleDLC();
Matt Briggs 62:9751a8504c82 176 protocol->configForPairingNetwork();
Matt Briggs 61:8d9efd33cac9 177 bbio->ledOn();
Matt Briggs 63:e1efbe3402d9 178 myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 179 result = protocol->waitForPairing(RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 180 bbio->ledOff();
Matt Briggs 61:8d9efd33cac9 181 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 182 myLogInfo("Got pair request and responded");
Matt Briggs 61:8d9efd33cac9 183 }
Matt Briggs 61:8d9efd33cac9 184 else if (result == cmdTimeout) {
Matt Briggs 63:e1efbe3402d9 185 myLogInfo("Did not receive request");
Matt Briggs 61:8d9efd33cac9 186 }
Matt Briggs 61:8d9efd33cac9 187 else {
Matt Briggs 63:e1efbe3402d9 188 myLogInfo("Unknown pair error");
Matt Briggs 61:8d9efd33cac9 189 }
Matt Briggs 62:9751a8504c82 190 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 191 protocol->printDotConfig();
Matt Briggs 61:8d9efd33cac9 192 }
Matt Briggs 61:8d9efd33cac9 193 else if (pairBtnState == pairBtnLongPress) {
Matt Briggs 63:e1efbe3402d9 194 myLogInfo("Clearing pair values and generating new ones.");
Matt Briggs 61:8d9efd33cac9 195 protocol->clearPair();
Matt Briggs 63:e1efbe3402d9 196 protocol->printDotConfig();
Matt Briggs 61:8d9efd33cac9 197 }
Matt Briggs 61:8d9efd33cac9 198 }
Matt Briggs 61:8d9efd33cac9 199 }
Matt Briggs 47:a68747642a7a 200
Matt Briggs 61:8d9efd33cac9 201 // Alert code
Matt Briggs 53:a1563574a980 202 if (protocol->isTx()) {
Matt Briggs 63:e1efbe3402d9 203 myLogInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
Matt Briggs 58:15aa7a785b9f 204 loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
Matt Briggs 53:a1563574a980 205 // TODO add tamper
Matt Briggs 50:e89647e77fd5 206 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 207 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 61:8d9efd33cac9 208 (pairBtnIntFlag && (pairBtnState == pairBtnShortPress))) {
Matt Briggs 50:e89647e77fd5 209 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 210 #if LED_FEEDBACK
Matt Briggs 53:a1563574a980 211 bbio->ledOn();
Matt Briggs 50:e89647e77fd5 212 #endif
Matt Briggs 50:e89647e77fd5 213
Matt Briggs 61:8d9efd33cac9 214 data.clear();
Matt Briggs 57:bdac7dd17af2 215 data.push_back((txSeqNum >> 8) & 0xFF);
Matt Briggs 57:bdac7dd17af2 216 data.push_back(txSeqNum & 0xFF);
Matt Briggs 57:bdac7dd17af2 217 std::string dataStr(data.begin(), data.end());
Matt Briggs 63:e1efbe3402d9 218 myLogInfo("Sent msg num: %d, payload: %s", txSeqNum, dataStr.c_str());
Matt Briggs 53:a1563574a980 219 protocol->send(data);
Matt Briggs 57:bdac7dd17af2 220 txSeqNum++;
Matt Briggs 50:e89647e77fd5 221 wait(0.5); // Leave the LED on so a person can see it.
Matt Briggs 50:e89647e77fd5 222 }
Matt Briggs 50:e89647e77fd5 223
Matt Briggs 53:a1563574a980 224 bbio->ledOff();
Matt Briggs 62:9751a8504c82 225 pairBtnIntFlag = false;
Matt Briggs 62:9751a8504c82 226 tamperIntFlag = false;
Matt Briggs 58:15aa7a785b9f 227 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 228 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 58:15aa7a785b9f 229 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 2 secs if CCInAlert is asserted
Matt Briggs 57:bdac7dd17af2 230 }
Matt Briggs 57:bdac7dd17af2 231 else {
Matt Briggs 57:bdac7dd17af2 232 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 57:bdac7dd17af2 233 }
Matt Briggs 58:15aa7a785b9f 234 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 235 }
Mike Fiore 11:d2e31743433a 236
Matt Briggs 53:a1563574a980 237 if (protocol->isRx()) {
Matt Briggs 47:a68747642a7a 238 bool msgPending;
Matt Briggs 53:a1563574a980 239 protocol->listen(msgPending);
Matt Briggs 63:e1efbe3402d9 240 myLogInfo("Loop Cnt %d. Listening.", loopCnt);
Matt Briggs 47:a68747642a7a 241 if (msgPending) {
Matt Briggs 62:9751a8504c82 242 data.clear();
Matt Briggs 53:a1563574a980 243 protocol->recv(data);
Matt Briggs 47:a68747642a7a 244 std::string dataStr(data.begin(), data.end());
Matt Briggs 63:e1efbe3402d9 245 myLogInfo("Got msg num: %d, payload: %s", rxSeqNum, dataStr.c_str());
Matt Briggs 53:a1563574a980 246 bbio->relayAlert();
Matt Briggs 57:bdac7dd17af2 247 rxSeqNum++;
Matt Briggs 50:e89647e77fd5 248 #if LED_FEEDBACK
Matt Briggs 53:a1563574a980 249 bbio->ledOn();
Matt Briggs 50:e89647e77fd5 250 #endif
Matt Briggs 58:15aa7a785b9f 251 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 252 // TODO maybe use sleep instead of wait
Matt Briggs 63:e1efbe3402d9 253 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 254 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 255 bbio->ledOff();
Matt Briggs 58:15aa7a785b9f 256 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 257 }
Matt Briggs 63:e1efbe3402d9 258 myLogInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 54:c04d7b6fa075 259 protocol->sampleDLC();
Matt Briggs 58:15aa7a785b9f 260 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 261 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 262 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 263 }
Matt Briggs 31:9c535a708ae9 264
Matt Briggs 63:e1efbe3402d9 265 myLogInfo("\r\n================================");
Matt Briggs 58:15aa7a785b9f 266 loopCnt++;
Mike Fiore 11:d2e31743433a 267 }
Matt Briggs 47:a68747642a7a 268
Matt Briggs 55:79ab0bbc5008 269 delete protocol;
Matt Briggs 55:79ab0bbc5008 270 delete bbio;
Mike Fiore 11:d2e31743433a 271 return 0;
Mike Fiore 11:d2e31743433a 272 }
Matt Briggs 47:a68747642a7a 273 #endif
Mike Fiore 11:d2e31743433a 274
Matt Briggs 27:6b68ff715ae1 275