![](/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/SerialTermMgr.cpp@98:3609f600c2f5, 2017-11-28 (annotated)
- Committer:
- mbriggs_vortex
- Date:
- Tue Nov 28 14:31:54 2017 -0700
- Revision:
- 98:3609f600c2f5
- Parent:
- 94:8028c07f71fa
- Child:
- 99:83b54c851187
Adding first cut of term settings.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 75:600cb3a9f126 | 1 | /* |
Matt Briggs | 75:600cb3a9f126 | 2 | * SerialTermMgr.cpp |
Matt Briggs | 75:600cb3a9f126 | 3 | * |
Matt Briggs | 75:600cb3a9f126 | 4 | * Created on: May 8, 2017 |
Matt Briggs | 75:600cb3a9f126 | 5 | * Author: mbriggs |
Matt Briggs | 75:600cb3a9f126 | 6 | */ |
Matt Briggs | 75:600cb3a9f126 | 7 | |
Matt Briggs | 75:600cb3a9f126 | 8 | #include "SerialTermMgr.h" |
Matt Briggs | 75:600cb3a9f126 | 9 | #include "UserInterface.h" |
Matt Briggs | 75:600cb3a9f126 | 10 | |
Matt Briggs | 76:d1b20a259d8f | 11 | extern Serial pc; |
Matt Briggs | 76:d1b20a259d8f | 12 | const char ACK = 0x06; |
Matt Briggs | 76:d1b20a259d8f | 13 | const char NAK = 0x15; |
Matt Briggs | 77:176e3eb8f712 | 14 | const char SOH = 0x01; |
Matt Briggs | 77:176e3eb8f712 | 15 | const char EOT = 0x04; |
Matt Briggs | 77:176e3eb8f712 | 16 | const char SUB = 0x1A; |
Matt Briggs | 76:d1b20a259d8f | 17 | |
Matt Briggs | 77:176e3eb8f712 | 18 | SerialTermMgr::SerialTermMgr(BaseboardIO *bbio, WinbondSPIFlash *flash, float fwVersion) |
Matt Briggs | 75:600cb3a9f126 | 19 | { |
Matt Briggs | 75:600cb3a9f126 | 20 | mPc = NULL; |
Matt Briggs | 75:600cb3a9f126 | 21 | mFwVersion = fwVersion; |
Matt Briggs | 75:600cb3a9f126 | 22 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 23 | mBbio = bbio; |
Matt Briggs | 77:176e3eb8f712 | 24 | mFlash = flash; |
Matt Briggs | 75:600cb3a9f126 | 25 | } |
Matt Briggs | 75:600cb3a9f126 | 26 | void SerialTermMgr::printScreen() |
Matt Briggs | 75:600cb3a9f126 | 27 | { |
Matt Briggs | 75:600cb3a9f126 | 28 | switch (mCurrScreen) { |
Matt Briggs | 75:600cb3a9f126 | 29 | case mainScreenId: |
Matt Briggs | 75:600cb3a9f126 | 30 | printMainScreen(); |
Matt Briggs | 75:600cb3a9f126 | 31 | break; |
Matt Briggs | 75:600cb3a9f126 | 32 | case genInfoScreenId: |
Matt Briggs | 75:600cb3a9f126 | 33 | printGenInfo(); |
Matt Briggs | 75:600cb3a9f126 | 34 | break; |
Matt Briggs | 75:600cb3a9f126 | 35 | case settingsScreenId: |
mbriggs_vortex | 98:3609f600c2f5 | 36 | printSettings(); |
mbriggs_vortex | 98:3609f600c2f5 | 37 | break; |
Matt Briggs | 75:600cb3a9f126 | 38 | case statsScreenId: |
Matt Briggs | 75:600cb3a9f126 | 39 | case errorLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 40 | case liveLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 41 | case enterSerialBridgeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 42 | printMainScreen(); |
Matt Briggs | 76:d1b20a259d8f | 43 | break; |
Matt Briggs | 75:600cb3a9f126 | 44 | case enterProgModeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 45 | printEnterProgMode(); |
Matt Briggs | 75:600cb3a9f126 | 46 | } |
Matt Briggs | 75:600cb3a9f126 | 47 | } |
Matt Briggs | 75:600cb3a9f126 | 48 | bool SerialTermMgr::input() |
Matt Briggs | 75:600cb3a9f126 | 49 | { |
Matt Briggs | 75:600cb3a9f126 | 50 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 51 | return true; |
Matt Briggs | 75:600cb3a9f126 | 52 | } |
Matt Briggs | 75:600cb3a9f126 | 53 | bool quit = false; |
Matt Briggs | 75:600cb3a9f126 | 54 | char c = 10; |
Matt Briggs | 75:600cb3a9f126 | 55 | if (mPc->readable()) { |
Matt Briggs | 75:600cb3a9f126 | 56 | c = mPc->getc(); |
Matt Briggs | 75:600cb3a9f126 | 57 | } |
Matt Briggs | 75:600cb3a9f126 | 58 | else { |
Matt Briggs | 75:600cb3a9f126 | 59 | return false; // Do nothing if there is no input |
Matt Briggs | 75:600cb3a9f126 | 60 | } |
Matt Briggs | 75:600cb3a9f126 | 61 | switch (mCurrScreen) { |
Matt Briggs | 75:600cb3a9f126 | 62 | case mainScreenId: |
Matt Briggs | 75:600cb3a9f126 | 63 | quit = inputMainPage(c); |
Matt Briggs | 75:600cb3a9f126 | 64 | break; |
Matt Briggs | 75:600cb3a9f126 | 65 | case genInfoScreenId: |
Matt Briggs | 75:600cb3a9f126 | 66 | inputGenInfo(c); |
Matt Briggs | 75:600cb3a9f126 | 67 | break; |
Matt Briggs | 75:600cb3a9f126 | 68 | case settingsScreenId: |
mbriggs_vortex | 98:3609f600c2f5 | 69 | inputSettings(c); |
mbriggs_vortex | 98:3609f600c2f5 | 70 | break; |
Matt Briggs | 75:600cb3a9f126 | 71 | case statsScreenId: |
Matt Briggs | 75:600cb3a9f126 | 72 | case errorLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 73 | case liveLogScreenId: |
Matt Briggs | 75:600cb3a9f126 | 74 | case enterSerialBridgeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 75 | inputMainPage(c); |
Matt Briggs | 76:d1b20a259d8f | 76 | break; |
Matt Briggs | 75:600cb3a9f126 | 77 | case enterProgModeScreenId: |
Matt Briggs | 76:d1b20a259d8f | 78 | inputEnterProgMode(c); |
Matt Briggs | 75:600cb3a9f126 | 79 | } |
Matt Briggs | 75:600cb3a9f126 | 80 | return quit; |
Matt Briggs | 75:600cb3a9f126 | 81 | } |
Matt Briggs | 75:600cb3a9f126 | 82 | |
Matt Briggs | 75:600cb3a9f126 | 83 | // private methods |
Matt Briggs | 75:600cb3a9f126 | 84 | bool SerialTermMgr::inputMainPage (char in) { |
Matt Briggs | 75:600cb3a9f126 | 85 | bool quit = false; |
Matt Briggs | 75:600cb3a9f126 | 86 | switch (in) { |
Matt Briggs | 75:600cb3a9f126 | 87 | case '1': |
Matt Briggs | 75:600cb3a9f126 | 88 | mCurrScreen = genInfoScreenId; |
Matt Briggs | 75:600cb3a9f126 | 89 | break; |
Matt Briggs | 75:600cb3a9f126 | 90 | case '2': |
Matt Briggs | 76:d1b20a259d8f | 91 | mCurrScreen = enterProgModeScreenId; |
Matt Briggs | 75:600cb3a9f126 | 92 | break; |
mbriggs_vortex | 92:1f86edb14cbe | 93 | case '3': |
mbriggs_vortex | 98:3609f600c2f5 | 94 | mCurrScreen = settingsScreenId; |
mbriggs_vortex | 92:1f86edb14cbe | 95 | break; |
mbriggs_vortex | 91:8196900df6fe | 96 | // Future |
mbriggs_vortex | 91:8196900df6fe | 97 | // case '4': |
mbriggs_vortex | 91:8196900df6fe | 98 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 99 | //// mCurrScreen = statsScreenId; |
mbriggs_vortex | 91:8196900df6fe | 100 | // break; |
mbriggs_vortex | 91:8196900df6fe | 101 | // case '5': |
mbriggs_vortex | 91:8196900df6fe | 102 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 103 | //// mCurrScreen = errorLogScreenId; |
mbriggs_vortex | 91:8196900df6fe | 104 | // break; |
mbriggs_vortex | 91:8196900df6fe | 105 | // case '6': |
mbriggs_vortex | 91:8196900df6fe | 106 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 107 | //// mCurrScreen = liveLogScreenId; |
mbriggs_vortex | 91:8196900df6fe | 108 | // break; |
mbriggs_vortex | 91:8196900df6fe | 109 | // case '7': |
mbriggs_vortex | 91:8196900df6fe | 110 | // mPc->printf("\r\nNot implemented yet.\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 111 | //// mCurrScreen = enterSerialBridgeScreenId; |
mbriggs_vortex | 91:8196900df6fe | 112 | // break; |
Matt Briggs | 75:600cb3a9f126 | 113 | case 0x11: // ctrl-q |
Matt Briggs | 75:600cb3a9f126 | 114 | quit=true; |
Matt Briggs | 75:600cb3a9f126 | 115 | break; |
Matt Briggs | 75:600cb3a9f126 | 116 | default: |
Matt Briggs | 75:600cb3a9f126 | 117 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 118 | } |
Matt Briggs | 75:600cb3a9f126 | 119 | if (!quit) { |
Matt Briggs | 75:600cb3a9f126 | 120 | printScreen(); |
Matt Briggs | 75:600cb3a9f126 | 121 | } |
Matt Briggs | 75:600cb3a9f126 | 122 | return quit; |
Matt Briggs | 75:600cb3a9f126 | 123 | } |
Matt Briggs | 75:600cb3a9f126 | 124 | |
Matt Briggs | 75:600cb3a9f126 | 125 | void SerialTermMgr::printMainScreen() |
Matt Briggs | 75:600cb3a9f126 | 126 | { |
Matt Briggs | 75:600cb3a9f126 | 127 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 128 | return; |
Matt Briggs | 75:600cb3a9f126 | 129 | } |
Matt Briggs | 75:600cb3a9f126 | 130 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 131 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 132 | mPc->printf("= Wireless Bridge Terminal (Firmware: v%0.2f) =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 133 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 134 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 135 | mPc->printf("= Selection Options =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 136 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 137 | mPc->printf("= 0: Refresh (Enter and space also work) =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 138 | mPc->printf("= 1: General information =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 139 | mPc->printf("= 2: Enter programming mode =\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 140 | mPc->printf("= 3: Settings =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 141 | // Future |
mbriggs_vortex | 91:8196900df6fe | 142 | // mPc->printf("= 4: Statistics =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 143 | // mPc->printf("= 5: Error log =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 144 | // mPc->printf("= 6: Live log =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 145 | // mPc->printf("= 7: Enter serial bridge mode =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 146 | mPc->printf("= <ctrl>-q: Exit terminal =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 147 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 148 | } |
Matt Briggs | 75:600cb3a9f126 | 149 | |
Matt Briggs | 75:600cb3a9f126 | 150 | void SerialTermMgr::inputGenInfo (char in) { |
Matt Briggs | 75:600cb3a9f126 | 151 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 152 | return; |
Matt Briggs | 75:600cb3a9f126 | 153 | } |
Matt Briggs | 75:600cb3a9f126 | 154 | switch (in) { |
mbriggs_vortex | 91:8196900df6fe | 155 | case 0x1B: // esc |
Matt Briggs | 75:600cb3a9f126 | 156 | mCurrScreen = mainScreenId; |
Matt Briggs | 75:600cb3a9f126 | 157 | break; |
Matt Briggs | 75:600cb3a9f126 | 158 | case 'r': |
Matt Briggs | 75:600cb3a9f126 | 159 | mBbio->sampleUserSwitches(); |
Matt Briggs | 75:600cb3a9f126 | 160 | mPc->printf("User switches sampled.\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 161 | break; |
Matt Briggs | 75:600cb3a9f126 | 162 | default: |
Matt Briggs | 75:600cb3a9f126 | 163 | mPc->printf("Invalid key.\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 164 | } |
Matt Briggs | 75:600cb3a9f126 | 165 | printScreen(); |
Matt Briggs | 75:600cb3a9f126 | 166 | } |
Matt Briggs | 75:600cb3a9f126 | 167 | |
Matt Briggs | 75:600cb3a9f126 | 168 | void SerialTermMgr::printGenInfo() |
Matt Briggs | 75:600cb3a9f126 | 169 | { |
Matt Briggs | 75:600cb3a9f126 | 170 | if (mPc == NULL) { |
Matt Briggs | 75:600cb3a9f126 | 171 | return; |
Matt Briggs | 75:600cb3a9f126 | 172 | } |
Matt Briggs | 75:600cb3a9f126 | 173 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 174 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 175 | mPc->printf("= General Info (ESC to return to main menu) =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 176 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 177 | mPc->printf("= Press r to sample user switch values =\r\n"); // FIXME |
Matt Briggs | 75:600cb3a9f126 | 178 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 179 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 180 | mPc->printf("= Firmware version: v%0.2f =\r\n", mFwVersion); |
Matt Briggs | 75:600cb3a9f126 | 181 | //TODO |
Matt Briggs | 75:600cb3a9f126 | 182 | // mPc->printf("= Radio EUI: %s =\r\n", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); |
Matt Briggs | 75:600cb3a9f126 | 183 | mPc->printf("= Contact closure: %s =\r\n", mBbio->isCCNO() ? "Normally open " : |
Matt Briggs | 75:600cb3a9f126 | 184 | "Normally closed"); |
Matt Briggs | 75:600cb3a9f126 | 185 | mPc->printf("= WB Mode: %s =\r\n", mBbio->isTx() ? "Transmitter" : |
Matt Briggs | 75:600cb3a9f126 | 186 | "Receiver "); |
mbriggs_vortex | 91:8196900df6fe | 187 | // mPc->printf("= Comm Mode: %s =\r\n", mBbio->isLoRaWANMode() ? "LoRaWAN " : |
mbriggs_vortex | 91:8196900df6fe | 188 | // "Peer-to-peer"); |
mbriggs_vortex | 91:8196900df6fe | 189 | // mPc->printf("= Serial Mode: %s =\r\n", mBbio->isSerialEnabled() ? "Enabled " : |
mbriggs_vortex | 91:8196900df6fe | 190 | // "Disabled"); |
Matt Briggs | 75:600cb3a9f126 | 191 | if (mBbio->isTx()) { |
Matt Briggs | 75:600cb3a9f126 | 192 | mPc->printf("= Currently no rotary switches apply for TX =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 193 | } |
Matt Briggs | 75:600cb3a9f126 | 194 | else { |
Matt Briggs | 75:600cb3a9f126 | 195 | mPc->printf("= Rotary 1 hold setting is %0.1f seconds =\r\n", HoldTimeSetting::rotVal2Sec(mBbio->rotarySwitch1())); |
Matt Briggs | 75:600cb3a9f126 | 196 | mPc->printf("= Rotary 2 currently does not apply for RX =\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 197 | } |
Matt Briggs | 75:600cb3a9f126 | 198 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 75:600cb3a9f126 | 199 | } |
Matt Briggs | 76:d1b20a259d8f | 200 | |
mbriggs_vortex | 98:3609f600c2f5 | 201 | void SerialTermMgr::inputSettings (char in) { |
mbriggs_vortex | 98:3609f600c2f5 | 202 | if (mPc == NULL) { |
mbriggs_vortex | 98:3609f600c2f5 | 203 | return; |
mbriggs_vortex | 98:3609f600c2f5 | 204 | } |
mbriggs_vortex | 98:3609f600c2f5 | 205 | switch (in) { |
mbriggs_vortex | 98:3609f600c2f5 | 206 | case 0x1B: // esc |
mbriggs_vortex | 98:3609f600c2f5 | 207 | mCurrScreen = mainScreenId; |
mbriggs_vortex | 98:3609f600c2f5 | 208 | break; |
mbriggs_vortex | 98:3609f600c2f5 | 209 | case 0x13: // ctrl-s |
mbriggs_vortex | 98:3609f600c2f5 | 210 | applySaveSettings(); |
mbriggs_vortex | 98:3609f600c2f5 | 211 | mPc->printf("Settings saved \r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 212 | mCurrScreen = mainScreenId; |
mbriggs_vortex | 98:3609f600c2f5 | 213 | break; |
mbriggs_vortex | 98:3609f600c2f5 | 214 | case 'r': |
mbriggs_vortex | 98:3609f600c2f5 | 215 | mBbio->sampleUserSwitches(); |
mbriggs_vortex | 98:3609f600c2f5 | 216 | mPc->printf("User switches sampled.\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 217 | break; |
mbriggs_vortex | 98:3609f600c2f5 | 218 | case '1': |
mbriggs_vortex | 98:3609f600c2f5 | 219 | mSaveIsCCNO = !mSaveIsCCNO; |
mbriggs_vortex | 98:3609f600c2f5 | 220 | case '2': |
mbriggs_vortex | 98:3609f600c2f5 | 221 | mSaveIsTx = !mSaveIsTx; |
mbriggs_vortex | 98:3609f600c2f5 | 222 | case '3': |
mbriggs_vortex | 98:3609f600c2f5 | 223 | mSaveRot1 = (++mSaveRot1) % 10; |
mbriggs_vortex | 98:3609f600c2f5 | 224 | default: |
mbriggs_vortex | 98:3609f600c2f5 | 225 | mPc->printf("Invalid key.\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 226 | } |
mbriggs_vortex | 98:3609f600c2f5 | 227 | printScreen(); |
mbriggs_vortex | 98:3609f600c2f5 | 228 | } |
mbriggs_vortex | 98:3609f600c2f5 | 229 | |
mbriggs_vortex | 98:3609f600c2f5 | 230 | void SerialTermMgr::printSettings() |
mbriggs_vortex | 98:3609f600c2f5 | 231 | { |
mbriggs_vortex | 98:3609f600c2f5 | 232 | if (mPc == NULL) { |
mbriggs_vortex | 98:3609f600c2f5 | 233 | return; |
mbriggs_vortex | 98:3609f600c2f5 | 234 | } |
mbriggs_vortex | 98:3609f600c2f5 | 235 | mPc->printf("\r\n\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 236 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 237 | mPc->printf("= Settings (ESC to return to main menu) =\r\n", mFwVersion); |
mbriggs_vortex | 98:3609f600c2f5 | 238 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 239 | mPc->printf("= Press r to sample user switch values =\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 240 | mPc->printf("= Press <ctrl>-s to save values =\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 241 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 242 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 243 | mPc->printf("= Setting: current value [Modify Key], save value =\r\n", mFwVersion); |
mbriggs_vortex | 98:3609f600c2f5 | 244 | mPc->printf("= Contact closure [1]: %s, %s =\r\n", |
mbriggs_vortex | 98:3609f600c2f5 | 245 | mBbio->isCCNO() ? "NO" : "NC", |
mbriggs_vortex | 98:3609f600c2f5 | 246 | mSaveIsCCNO ? "NO" : "NC"); |
mbriggs_vortex | 98:3609f600c2f5 | 247 | mPc->printf("= WB Mode [2]: %s, %s =\r\n", |
mbriggs_vortex | 98:3609f600c2f5 | 248 | mBbio->isTx() ? "Transmitter" : "Receiver ", |
mbriggs_vortex | 98:3609f600c2f5 | 249 | mSaveIsTx ? "Transmitter" : "Receiver "); |
mbriggs_vortex | 98:3609f600c2f5 | 250 | if (mSaveIsTx) { |
mbriggs_vortex | 98:3609f600c2f5 | 251 | mPc->printf("= Currently no rotary switches apply for TX =\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 252 | } |
mbriggs_vortex | 98:3609f600c2f5 | 253 | else { |
mbriggs_vortex | 98:3609f600c2f5 | 254 | mPc->printf("= Rotary 1 hold setting: %0.1f secs, %0.1f =\r\n", |
mbriggs_vortex | 98:3609f600c2f5 | 255 | HoldTimeSetting::rotVal2Sec(mBbio->rotarySwitch1()), |
mbriggs_vortex | 98:3609f600c2f5 | 256 | HoldTimeSetting::rotVal2Sec(mSaveRot1)); |
mbriggs_vortex | 98:3609f600c2f5 | 257 | mPc->printf("= Rotary 2 currently does not apply for RX =\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 258 | } |
mbriggs_vortex | 98:3609f600c2f5 | 259 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 98:3609f600c2f5 | 260 | } |
mbriggs_vortex | 98:3609f600c2f5 | 261 | |
mbriggs_vortex | 98:3609f600c2f5 | 262 | void SerialTermMgr::seedSaveSettings() |
mbriggs_vortex | 98:3609f600c2f5 | 263 | { |
mbriggs_vortex | 98:3609f600c2f5 | 264 | mSaveIsCCNO = mBbio->isCCNO(); |
mbriggs_vortex | 98:3609f600c2f5 | 265 | mSaveIsTx = mBbio->isTx(); |
mbriggs_vortex | 98:3609f600c2f5 | 266 | mSaveRot1 = mBbio->rotarySwitch1(); |
mbriggs_vortex | 98:3609f600c2f5 | 267 | } |
mbriggs_vortex | 98:3609f600c2f5 | 268 | |
mbriggs_vortex | 98:3609f600c2f5 | 269 | void SerialTermMgr::applySaveSettings() |
mbriggs_vortex | 98:3609f600c2f5 | 270 | { |
mbriggs_vortex | 98:3609f600c2f5 | 271 | mBbio->setIsCCNO(mSaveIsCCNO); |
mbriggs_vortex | 98:3609f600c2f5 | 272 | mBbio->setIsTx(mSaveIsTx); |
mbriggs_vortex | 98:3609f600c2f5 | 273 | mBbio->setRotarySwitch1(mSaveRot1); |
mbriggs_vortex | 98:3609f600c2f5 | 274 | } |
mbriggs_vortex | 98:3609f600c2f5 | 275 | |
Matt Briggs | 76:d1b20a259d8f | 276 | void SerialTermMgr::printEnterProgMode() |
Matt Briggs | 76:d1b20a259d8f | 277 | { |
Matt Briggs | 76:d1b20a259d8f | 278 | if (mPc == NULL) { |
Matt Briggs | 76:d1b20a259d8f | 279 | return; |
Matt Briggs | 76:d1b20a259d8f | 280 | } |
Matt Briggs | 76:d1b20a259d8f | 281 | mPc->printf("\r\n\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 282 | mPc->printf("===============================================\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 283 | mPc->printf("= Serial Firmware Prog Mode (Esc to return) =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 284 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 285 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 286 | mPc->printf("= To program firmware: \r\n"); |
Matt Briggs | 76:d1b20a259d8f | 287 | mPc->printf("= 1: Have bin file ready =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 288 | mPc->printf("= 2: Hit <ctrl>-d to enter XMODEM mode =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 289 | mPc->printf("= 3: In terminal program (such as teraterm) =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 290 | mPc->printf("= select transfer via XMODEM =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 291 | mPc->printf("= 4: Programming may take up to a minute =\r\n"); |
mbriggs_vortex | 91:8196900df6fe | 292 | mPc->printf("= 5: If successful terminal prints \"Success\" =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 293 | mPc->printf("= 6: Wireless Bridge will then automatically =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 294 | mPc->printf("= reboot with new firmware. =\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 295 | mPc->printf("===============================================\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 296 | } |
Matt Briggs | 76:d1b20a259d8f | 297 | |
Matt Briggs | 76:d1b20a259d8f | 298 | void SerialTermMgr::inputEnterProgMode (char in) { |
Matt Briggs | 76:d1b20a259d8f | 299 | if (mPc == NULL) { |
Matt Briggs | 76:d1b20a259d8f | 300 | return; |
Matt Briggs | 76:d1b20a259d8f | 301 | } |
Matt Briggs | 76:d1b20a259d8f | 302 | switch (in) { |
mbriggs_vortex | 91:8196900df6fe | 303 | case 0x1B: // esc: |
Matt Briggs | 76:d1b20a259d8f | 304 | mCurrScreen = mainScreenId; |
Matt Briggs | 76:d1b20a259d8f | 305 | break; |
Matt Briggs | 76:d1b20a259d8f | 306 | case 0x04: |
Matt Briggs | 76:d1b20a259d8f | 307 | xmodem2Flash(); |
Matt Briggs | 76:d1b20a259d8f | 308 | break; |
Matt Briggs | 76:d1b20a259d8f | 309 | default: |
Matt Briggs | 76:d1b20a259d8f | 310 | mPc->printf("Invalid key.\r\n"); |
Matt Briggs | 76:d1b20a259d8f | 311 | } |
Matt Briggs | 76:d1b20a259d8f | 312 | printScreen(); |
Matt Briggs | 76:d1b20a259d8f | 313 | } |
Matt Briggs | 76:d1b20a259d8f | 314 | |
Matt Briggs | 76:d1b20a259d8f | 315 | bool SerialTermMgr::xmodem2Flash () |
Matt Briggs | 76:d1b20a259d8f | 316 | { |
Matt Briggs | 77:176e3eb8f712 | 317 | mPc->printf("Ready to receive file\r\n"); |
Matt Briggs | 77:176e3eb8f712 | 318 | mFlash->releaseFromPowerDown(); |
Matt Briggs | 77:176e3eb8f712 | 319 | mFlash->chipErase(); |
Matt Briggs | 76:d1b20a259d8f | 320 | char packetBuf[132]; |
Matt Briggs | 77:176e3eb8f712 | 321 | uint16_t packetNum = 1; |
Matt Briggs | 77:176e3eb8f712 | 322 | uint8_t csum = 0; |
Matt Briggs | 76:d1b20a259d8f | 323 | unsigned char pktIdx = 0; |
Matt Briggs | 76:d1b20a259d8f | 324 | time_t lastValidInput = 0; |
mbriggs_vortex | 78:43f074baac34 | 325 | unsigned char nTimeouts = 0; |
Matt Briggs | 76:d1b20a259d8f | 326 | while (1) { |
Matt Briggs | 77:176e3eb8f712 | 327 | // Send a NAK if a packet is not received within timeout period |
Matt Briggs | 76:d1b20a259d8f | 328 | if ((time(NULL)-lastValidInput) > XMODEM_TIMEOUT){ |
Matt Briggs | 76:d1b20a259d8f | 329 | pc.printf("Total chars: %d\r\n", pktIdx); |
Matt Briggs | 76:d1b20a259d8f | 330 | pktIdx = 0; |
Matt Briggs | 77:176e3eb8f712 | 331 | mPc->printf("%c", NAK); // Send NAK |
Matt Briggs | 77:176e3eb8f712 | 332 | lastValidInput = time(NULL); |
mbriggs_vortex | 78:43f074baac34 | 333 | nTimeouts++; |
mbriggs_vortex | 78:43f074baac34 | 334 | if (nTimeouts >= MAX_TIMEOUTS) { |
mbriggs_vortex | 78:43f074baac34 | 335 | mPc->printf("Programming timed out.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 336 | pc.printf("Programming timed out.\r\n"); |
mbriggs_vortex | 78:43f074baac34 | 337 | break; |
mbriggs_vortex | 78:43f074baac34 | 338 | } |
Matt Briggs | 76:d1b20a259d8f | 339 | } |
Matt Briggs | 76:d1b20a259d8f | 340 | if (mPc->readable()) { |
Matt Briggs | 77:176e3eb8f712 | 341 | packetBuf[pktIdx++] = mPc->getc(); |
Matt Briggs | 77:176e3eb8f712 | 342 | if (pktIdx == 1 && packetBuf[0] == EOT) { |
Matt Briggs | 77:176e3eb8f712 | 343 | lastValidInput = time(NULL); |
Matt Briggs | 77:176e3eb8f712 | 344 | mPc->printf("%c", ACK); // Last ACK |
mbriggs_vortex | 92:1f86edb14cbe | 345 | |
mbriggs_vortex | 92:1f86edb14cbe | 346 | // Check for update string in last packet |
mbriggs_vortex | 92:1f86edb14cbe | 347 | bool foundKey; |
mbriggs_vortex | 94:8028c07f71fa | 348 | for (int packetIdx=3;packetIdx<sizeof(packetBuf)-sizeof(VORTEX_UPDATE_KEY);packetIdx++) { |
mbriggs_vortex | 92:1f86edb14cbe | 349 | foundKey = true; |
mbriggs_vortex | 93:f3adc409c54e | 350 | for (int i=0;i<sizeof(VORTEX_UPDATE_KEY)-1;i++) { |
mbriggs_vortex | 92:1f86edb14cbe | 351 | if (packetBuf[packetIdx+i] != VORTEX_UPDATE_KEY[i]) { |
mbriggs_vortex | 92:1f86edb14cbe | 352 | foundKey = false; |
mbriggs_vortex | 92:1f86edb14cbe | 353 | break; |
mbriggs_vortex | 92:1f86edb14cbe | 354 | } |
mbriggs_vortex | 92:1f86edb14cbe | 355 | } |
mbriggs_vortex | 92:1f86edb14cbe | 356 | if (foundKey) { |
mbriggs_vortex | 92:1f86edb14cbe | 357 | break; |
mbriggs_vortex | 92:1f86edb14cbe | 358 | } |
mbriggs_vortex | 92:1f86edb14cbe | 359 | } |
mbriggs_vortex | 92:1f86edb14cbe | 360 | if (foundKey) { |
mbriggs_vortex | 92:1f86edb14cbe | 361 | mPc->printf("Success.\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 362 | pc.printf("Success on xmodem. Reset in progress.\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 363 | writeBootloaderCtrlPage(packetNum*XMODEM_PACKET_SIZE); |
mbriggs_vortex | 92:1f86edb14cbe | 364 | } |
mbriggs_vortex | 92:1f86edb14cbe | 365 | else { |
mbriggs_vortex | 92:1f86edb14cbe | 366 | mPc->printf("Failed update key validation.\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 367 | pc.printf("Failed update key validation\r\n"); |
mbriggs_vortex | 92:1f86edb14cbe | 368 | } |
mbriggs_vortex | 78:43f074baac34 | 369 | |
Matt Briggs | 77:176e3eb8f712 | 370 | wait(0.01); |
mbriggs_vortex | 78:43f074baac34 | 371 | mFlash->powerDown(); |
mbriggs_vortex | 92:1f86edb14cbe | 372 | if (foundKey) { |
mbriggs_vortex | 92:1f86edb14cbe | 373 | NVIC_SystemReset(); |
mbriggs_vortex | 92:1f86edb14cbe | 374 | } |
Matt Briggs | 77:176e3eb8f712 | 375 | break; |
Matt Briggs | 76:d1b20a259d8f | 376 | } |
Matt Briggs | 77:176e3eb8f712 | 377 | else if (pktIdx >= 132) { |
Matt Briggs | 77:176e3eb8f712 | 378 | lastValidInput = time(NULL); |
mbriggs_vortex | 78:43f074baac34 | 379 | nTimeouts = 0; // Clear n timeouts with new packet |
Matt Briggs | 77:176e3eb8f712 | 380 | csum = 0; |
Matt Briggs | 77:176e3eb8f712 | 381 | for (int i=3; i<131; i++) { |
Matt Briggs | 77:176e3eb8f712 | 382 | csum += packetBuf[i]; |
Matt Briggs | 77:176e3eb8f712 | 383 | } |
Matt Briggs | 77:176e3eb8f712 | 384 | pktIdx = 0; |
Matt Briggs | 77:176e3eb8f712 | 385 | if ((csum == packetBuf[131]) && (packetBuf[0] == SOH) && |
mbriggs_vortex | 92:1f86edb14cbe | 386 | (packetBuf[1] == ((uint8_t)packetNum)) && (((uint8_t)~packetBuf[2]) == ((uint8_t)packetNum))) { |
mbriggs_vortex | 92:1f86edb14cbe | 387 | mFlash->writeStream(FLASH_BIN_OFFSET+(packetNum-1)*XMODEM_PACKET_SIZE, packetBuf+3, |
mbriggs_vortex | 92:1f86edb14cbe | 388 | XMODEM_PACKET_SIZE); |
Matt Briggs | 77:176e3eb8f712 | 389 | mPc->printf("%c", ACK); |
Matt Briggs | 77:176e3eb8f712 | 390 | packetNum++; |
Matt Briggs | 77:176e3eb8f712 | 391 | } |
Matt Briggs | 77:176e3eb8f712 | 392 | else { |
Matt Briggs | 77:176e3eb8f712 | 393 | mPc->printf("%c", NAK); |
Matt Briggs | 77:176e3eb8f712 | 394 | } |
Matt Briggs | 77:176e3eb8f712 | 395 | } |
Matt Briggs | 76:d1b20a259d8f | 396 | } |
Matt Briggs | 76:d1b20a259d8f | 397 | } |
Matt Briggs | 77:176e3eb8f712 | 398 | mCurrScreen = mainScreenId; // Just return to main screen for now |
Matt Briggs | 77:176e3eb8f712 | 399 | mFlash->powerDown(); |
Matt Briggs | 77:176e3eb8f712 | 400 | |
Matt Briggs | 77:176e3eb8f712 | 401 | return true; |
Matt Briggs | 76:d1b20a259d8f | 402 | } |
mbriggs_vortex | 78:43f074baac34 | 403 | |
mbriggs_vortex | 78:43f074baac34 | 404 | void SerialTermMgr::writeBootloaderCtrlPage(uint32_t nBytes) |
mbriggs_vortex | 78:43f074baac34 | 405 | { |
mbriggs_vortex | 78:43f074baac34 | 406 | // This should always be less than 256 bytes |
mbriggs_vortex | 91:8196900df6fe | 407 | |
mbriggs_vortex | 91:8196900df6fe | 408 | // Zero updates the bootloader version so that is cleared until the bootloader adds its back |
mbriggs_vortex | 91:8196900df6fe | 409 | const uint32_t zeroBootloaderVersion = 0; |
mbriggs_vortex | 91:8196900df6fe | 410 | const unsigned int ctrlSize = sizeof(NEW_CODE)+sizeof(nBytes)+sizeof(zeroBootloaderVersion); |
mbriggs_vortex | 78:43f074baac34 | 411 | char buf[ctrlSize]; |
mbriggs_vortex | 78:43f074baac34 | 412 | std::memcpy(buf, NEW_CODE, sizeof(NEW_CODE)); |
mbriggs_vortex | 78:43f074baac34 | 413 | std::memcpy(buf+sizeof(NEW_CODE), &nBytes, sizeof(nBytes)); |
mbriggs_vortex | 91:8196900df6fe | 414 | std::memcpy(buf+sizeof(NEW_CODE)+sizeof(nBytes), |
mbriggs_vortex | 91:8196900df6fe | 415 | &zeroBootloaderVersion, sizeof(zeroBootloaderVersion)); |
mbriggs_vortex | 91:8196900df6fe | 416 | // TODO add reprogram counter or other functions |
mbriggs_vortex | 78:43f074baac34 | 417 | |
mbriggs_vortex | 78:43f074baac34 | 418 | // Copy to first page of flash |
mbriggs_vortex | 78:43f074baac34 | 419 | mFlash->writeStream(0, buf, sizeof(buf)); |
mbriggs_vortex | 78:43f074baac34 | 420 | } |