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 08:32:55 2017 -0600
Revision:
62:9751a8504c82
Parent:
61:8d9efd33cac9
Child:
63:e1efbe3402d9
First cut at pair code

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