Fork to see if I can get working

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

Fork of xDotBridge_update_test20180823 by Matt Briggs

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers testBaseboardIO.cpp Source File

testBaseboardIO.cpp

00001 #include "mbed.h"
00002 #include <string>
00003 #include "../../config.h"
00004 #include "BaseboardIO.h"
00005 //#include "MTSLog.h"
00006 #include "dot_util.h"
00007 #include "MyLog.h"
00008 
00009 #ifdef __TEST_BBIO__
00010 mDot* dot = NULL; // Used by dot-utils
00011 Serial pc(USBTX, USBRX); // Externally defined
00012 
00013 const int VERSION = 7;
00014 
00015 char* bool2Str(bool in) {
00016     if (in) {
00017         return "Asserted\    "; // Extra space for alignment
00018     }
00019     else {
00020         return "Not Asserted";
00021     }
00022 }
00023 
00024 volatile uint8_t ccInIntCnt;
00025 volatile uint8_t tamperIntCnt;
00026 volatile uint8_t pairBtnIntCnt;
00027 volatile uint8_t pvdIntCnt;
00028 
00029 void ccInIntCallback () {
00030     ccInIntCnt++;
00031 }
00032 void tamperIntCallback () {
00033     tamperIntCnt++;
00034 }
00035 void pairBtnIntCallback () {
00036     pairBtnIntCnt++;
00037 }
00038 
00039 class MenuManager
00040 {
00041 private:
00042     uint8_t mCurrSel; // Current selection
00043     BaseboardIO *mBbio;
00044     bool validInput(uint8_t in) {
00045         if (in > 0 && in <= 8)
00046             return true;
00047         else {
00048             return false;
00049         }
00050     }
00051 public:
00052     MenuManager() {
00053         mCurrSel = 0;
00054         mBbio = NULL;
00055     }
00056     void regBBIO (BaseboardIO *bbio) {
00057         mBbio = bbio;
00058     }
00059     uint8_t getCurrentSel() {
00060         return mCurrSel;
00061     }
00062     void applyInput(uint8_t in) {
00063         if (validInput(in)) {
00064 //            mCurrSel = in;
00065             if (in == 1) {
00066                 mBbio->ledOn();
00067             }
00068             else if (in == 2) {
00069                 mBbio->ledOff();
00070             }
00071             else if (in == 3) {
00072                 mBbio->relayNormal();
00073             }
00074             else if (in == 4) {
00075                 mBbio->relayAlert();
00076             }
00077             else if (in == 5) {
00078                 mBbio->serialRx(true);
00079             }
00080             else if (in == 6) {
00081                 mBbio->serialRx(false);
00082             }
00083             else if (in == 7) {
00084                 mBbio->serialTx(true);
00085             }
00086             else if (in == 8) {
00087                 mBbio->serialTx(false);
00088             }
00089         }
00090     }
00091     void printMenu() {
00092         mBbio->sampleUserSwitches();
00093         pc.printf("\r\n\r\n");
00094         pc.printf("===============================================\r\n");
00095         pc.printf("= Baseboard I/O Tester v%02d                   =\r\n", VERSION);
00096         pc.printf("===============================================\r\n");
00097         pc.printf("===============================================\r\n");
00098         pc.printf("= Selection Options                           =\r\n");
00099         pc.printf("===============================================\r\n");
00100         pc.printf("= 0: Just refresh                             =\r\n");
00101         pc.printf("= 1: Turn on LED                              =\r\n");
00102         pc.printf("= 2: Turn off LED                             =\r\n");
00103         pc.printf("= 3: Toggle Relay Normal                      =\r\n");
00104         pc.printf("= 4: Toggle Relay Alert                       =\r\n");
00105         pc.printf("= 5: Turn on 232 RX                           =\r\n");
00106         pc.printf("= 6: Turn off 232 RX                          =\r\n");
00107         pc.printf("= 7: Turn on 232 TX (Note RX on as well)      =\r\n");
00108         pc.printf("= 8: Turn off 232 TX                          =\r\n");
00109         pc.printf("===============================================\r\n");
00110         pc.printf("= Status and Counters                         =\r\n");
00111         pc.printf("===============================================\r\n");
00112         pc.printf("= Pair btn. State: %s  IntCnt: %02d   =\r\n",
00113                 bool2Str(mBbio->isPairBtn()), pairBtnIntCnt);
00114         pc.printf("= Tamper. State: N/A             IntCnt: %04d =\r\n",
00115                 tamperIntCnt);
00116         pc.printf("= CCIN. State: %s      IntCnt: %02d   =\r\n",
00117                 bool2Str(mBbio->isCCInAlert()), ccInIntCnt);
00118         pc.printf("= PVD (BROKEN). State: N/A       IntCnt: %02d   =\r\n",
00119                 pvdIntCnt);
00120         pc.printf("= CC Normally Closed. State: %s     =\r\n", bool2Str(mBbio->isCCNC()));
00121         pc.printf("= Is TX. State: %s                  =\r\n", bool2Str(mBbio->isTx()));
00122         pc.printf("= Is LoraWAN. State: %s             =\r\n", bool2Str(mBbio->isLoRaWANMode()));
00123         pc.printf("= Is Serial En. State: %s           =\r\n", bool2Str(mBbio->isSerialEnabled()));
00124         pc.printf("= Rotary Switch 1.  Value: %02d                 =\r\n", mBbio->rotarySwitch1());
00125         pc.printf("= Rotary Switch 2.  Value: %02d                 =\r\n", mBbio->rotarySwitch2());
00126         pc.printf("===============================================\r\n");
00127     }
00128 };
00129 
00130 char WAIT_CHARS [] = {'-', '\\', '|', '/'};
00131 /**
00132  * Checks that in idle state all the IOs are pulled up.
00133  */
00134 int main ()
00135 {
00136     MenuManager menuMgr;
00137     CmdResult result;
00138     ccInIntCnt = 0;
00139     tamperIntCnt = 0;
00140     pairBtnIntCnt = 0;
00141     pvdIntCnt = 0;
00142 
00143     pc.baud(115200);
00144     mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
00145     MyLog::setLogLevel(MyLog::DEBUG_LEVEL);
00146 
00147     wait(1.0);
00148 
00149     pc.printf("===============================================\r\n");
00150     pc.printf("= Baseboard Constructor Starting              =\r\n");
00151     BaseboardIO bbio;
00152     pc.printf("= Baseboard Constructor Finished              =\r\n");
00153 
00154     pc.printf("= Baseboard Init Starting                     =\r\n");
00155     result = bbio.init(true); // Overwrite current NVM
00156 //    result = bbio.init(false);
00157     if (result == cmdSuccess) {
00158         pc.printf("= Baseboard Init Finished Successfully    =\r\n");
00159     }
00160     else {
00161         pc.printf("= Baseboard Init Finished with Error      =\r\n");
00162     }
00163 
00164     Callback<void()> ccInIntObj (&ccInIntCallback);
00165     Callback<void()> tamperIntObj (&tamperIntCallback);
00166     Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
00167     bbio.regCCInInt(ccInIntObj);
00168     bbio.regTamperInt(tamperIntObj);
00169     bbio.regPairBtnInt(pairBtnIntObj);
00170 
00171     menuMgr.regBBIO(&bbio);
00172     menuMgr.printMenu();
00173 
00174     uint8_t waitCharIdx = 0;
00175     while (true) {
00176 
00177         if (pc.readable()) {
00178             char menuInput = pc.getc();
00179             menuInput -= '0'; // Convert to raw interger value
00180             menuMgr.applyInput(menuInput);
00181             menuMgr.printMenu();
00182         }
00183         else {
00184             pc.printf("\b%c", WAIT_CHARS[waitCharIdx]);
00185             if (waitCharIdx >= sizeof(WAIT_CHARS)) {
00186                 waitCharIdx = 0;
00187             }
00188             else {
00189                 waitCharIdx++;
00190             }
00191         }
00192         wait(0.1);
00193     }
00194     return 0;
00195 }
00196 
00197 #endif