Component Test's Software to work with "Universal Controller Box" - Software is an interpreter or "compiler" for programs to be done with a .txt file and read off of the SD Card

Dependencies:   BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed

Committer:
mehatfie
Date:
Fri Oct 03 21:33:46 2014 +0000
Revision:
16:2482d226cf4d
Parent:
14:953820302fb7
- Updated Errors; - Initializes SD Card to work every time; - Cycle timer starts immediately on first cycle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mehatfie 4:86d0d04cc055 1 #include "PinIN.hpp"
mehatfie 4:86d0d04cc055 2 //#include "mbed.h"
mehatfie 4:86d0d04cc055 3 //#include "LocalPinNames.h"
mehatfie 4:86d0d04cc055 4 //#include "BridgeDriver.h"
mehatfie 4:86d0d04cc055 5
mehatfie 4:86d0d04cc055 6 //Constructor
mehatfie 5:e36e0538a903 7 PinIN::PinIN(LineData lineData){
mehatfie 14:953820302fb7 8 this->errorFlag = 0;
mehatfie 4:86d0d04cc055 9
mehatfie 9:5a0c4c6e39c7 10 //Order of Line: Command, Local_Name, PIN_IN, pinName, pinMode
mehatfie 9:5a0c4c6e39c7 11 if (lineData.numWords != 5)
mehatfie 9:5a0c4c6e39c7 12 this->errorFlag = 1;
mehatfie 9:5a0c4c6e39c7 13
mehatfie 5:e36e0538a903 14 string _pinName = lineData.word[3]; // Local Pin
mehatfie 5:e36e0538a903 15 string _pinMode = lineData.word[4]; // Pin Mode to Select
mehatfie 4:86d0d04cc055 16
mehatfie 4:86d0d04cc055 17 if(_pinName.compare("DIO0") == 0)
mehatfie 4:86d0d04cc055 18 this->pinName = DIO0;
mehatfie 4:86d0d04cc055 19 else if(_pinName.compare("DIO1") == 0)
mehatfie 4:86d0d04cc055 20 this->pinName = DIO1;
mehatfie 4:86d0d04cc055 21 else if(_pinName.compare("DIO2") == 0)
mehatfie 4:86d0d04cc055 22 this->pinName = DIO2;
mehatfie 4:86d0d04cc055 23 else if(_pinName.compare("DIO3") == 0)
mehatfie 4:86d0d04cc055 24 this->pinName = DIO3;
mehatfie 4:86d0d04cc055 25 else if(_pinName.compare("DIO4") == 0)
mehatfie 4:86d0d04cc055 26 this->pinName = DIO4;
mehatfie 4:86d0d04cc055 27 else if(_pinName.compare("DIO5") == 0)
mehatfie 4:86d0d04cc055 28 this->pinName = DIO5;
mehatfie 4:86d0d04cc055 29 else if(_pinName.compare("DIO6") == 0)
mehatfie 4:86d0d04cc055 30 this->pinName = DIO6;
mehatfie 4:86d0d04cc055 31 else if(_pinName.compare("DIO7") == 0)
mehatfie 4:86d0d04cc055 32 this->pinName = DIO7;
mehatfie 4:86d0d04cc055 33 else if(_pinName.compare("DIO8") == 0)
mehatfie 4:86d0d04cc055 34 this->pinName = DIO8;
mehatfie 4:86d0d04cc055 35 else if(_pinName.compare("DIO9") == 0)
mehatfie 4:86d0d04cc055 36 this->pinName = DIO9;
mehatfie 4:86d0d04cc055 37 else if(_pinName.compare("DIO10") == 0)
mehatfie 4:86d0d04cc055 38 this->pinName = DIO10;
mehatfie 4:86d0d04cc055 39 else if(_pinName.compare("DIO11") == 0)
mehatfie 4:86d0d04cc055 40 this->pinName = DIO11;
mehatfie 4:86d0d04cc055 41
mehatfie 4:86d0d04cc055 42 else if(_pinName.compare("AI0") == 0)
mehatfie 4:86d0d04cc055 43 this->pinName = AI0;
mehatfie 4:86d0d04cc055 44 else if(_pinName.compare("AI1") == 0)
mehatfie 4:86d0d04cc055 45 this->pinName = AI1;
mehatfie 4:86d0d04cc055 46 else if(_pinName.compare("AI2") == 0)
mehatfie 4:86d0d04cc055 47 this->pinName = AI2;
mehatfie 4:86d0d04cc055 48 else if(_pinName.compare("AI3") == 0)
mehatfie 4:86d0d04cc055 49 this->pinName = AI3;
mehatfie 4:86d0d04cc055 50 else if(_pinName.compare("AI4") == 0)
mehatfie 4:86d0d04cc055 51 this->pinName = AI4;
mehatfie 4:86d0d04cc055 52 else if(_pinName.compare("AI5") == 0)
mehatfie 4:86d0d04cc055 53 this->pinName = AI5;
mehatfie 9:5a0c4c6e39c7 54
mehatfie 9:5a0c4c6e39c7 55 //Pin Name not recognized
mehatfie 9:5a0c4c6e39c7 56 else
mehatfie 9:5a0c4c6e39c7 57 this->errorFlag = 1;
mehatfie 5:e36e0538a903 58
mehatfie 5:e36e0538a903 59 if(_pinMode.compare("PU") == 0)
mehatfie 5:e36e0538a903 60 this->pinMode = PullUp;
mehatfie 5:e36e0538a903 61 else if(_pinMode.compare("PD") == 0)
mehatfie 5:e36e0538a903 62 this->pinMode = PullDown;
mehatfie 5:e36e0538a903 63 else if(_pinMode.compare("PN") == 0)
mehatfie 5:e36e0538a903 64 this->pinMode = PullNone;
mehatfie 5:e36e0538a903 65 else if(_pinMode.compare("OD") == 0)
mehatfie 5:e36e0538a903 66 this->pinMode = OpenDrain;
mehatfie 4:86d0d04cc055 67
mehatfie 9:5a0c4c6e39c7 68 //Pin Mode not recognized
mehatfie 9:5a0c4c6e39c7 69 else
mehatfie 9:5a0c4c6e39c7 70 this->errorFlag = 1;
mehatfie 4:86d0d04cc055 71 }
mehatfie 4:86d0d04cc055 72
mehatfie 4:86d0d04cc055 73
mehatfie 4:86d0d04cc055 74 //A line consists of [ __(Local_Name)__ __(function)__ __(parameter1)__ __(parameter2)__ __(parameter3)__ ... and so on]
mehatfie 5:e36e0538a903 75 int PinIN::interpret(LineData &lineData){
mehatfie 9:5a0c4c6e39c7 76
mehatfie 9:5a0c4c6e39c7 77 //Initialize the Pin with appropriate values
mehatfie 5:e36e0538a903 78 DigitalIn signal(pinName, pinMode);
mehatfie 4:86d0d04cc055 79
mehatfie 4:86d0d04cc055 80 //Order of Line: Local_Name, Function_Name, Param1, Param2, Param3,.......
mehatfie 4:86d0d04cc055 81 string func = lineData.word[1];
mehatfie 4:86d0d04cc055 82
mehatfie 4:86d0d04cc055 83 /******************************************************************************/
mehatfie 4:86d0d04cc055 84 /*** <Func: wait> ***/
mehatfie 4:86d0d04cc055 85 /******************************************************************************/
mehatfie 4:86d0d04cc055 86 if (func.compare("wait") == 0){
mehatfie 4:86d0d04cc055 87
mehatfie 4:86d0d04cc055 88 if (lineData.numWords != 3){
mehatfie 9:5a0c4c6e39c7 89 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 90 return -1;
mehatfie 4:86d0d04cc055 91 }
mehatfie 4:86d0d04cc055 92
mehatfie 4:86d0d04cc055 93 //Initialize and Convert Parameters
mehatfie 4:86d0d04cc055 94 string pinValue = lineData.word[2];
mehatfie 4:86d0d04cc055 95 int pinValue_Value = 0;
mehatfie 9:5a0c4c6e39c7 96
mehatfie 9:5a0c4c6e39c7 97 int numValuesFound = sscanf(pinValue.c_str(), "%d", &pinValue_Value);
mehatfie 9:5a0c4c6e39c7 98 if (numValuesFound < 1){
mehatfie 9:5a0c4c6e39c7 99 ErrorOut("Parameter Unknown, pin value can't be converted", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 100 return -1;
mehatfie 9:5a0c4c6e39c7 101 }
mehatfie 4:86d0d04cc055 102
mehatfie 9:5a0c4c6e39c7 103 if(pinValue_Value < 0 && pinValue_Value > 1){
mehatfie 9:5a0c4c6e39c7 104 ErrorOut("Pin Value must be either 0 or 1", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 105 return -1;
mehatfie 9:5a0c4c6e39c7 106 }
mehatfie 9:5a0c4c6e39c7 107
mehatfie 10:e8db892fbc52 108 //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality
mehatfie 10:e8db892fbc52 109 if (DummyMode)
mehatfie 10:e8db892fbc52 110 return 0; //Function operated successfully but doesn't return a value
mehatfie 10:e8db892fbc52 111
mehatfie 9:5a0c4c6e39c7 112 //Wait on the signal to turn to the specified value
mehatfie 4:86d0d04cc055 113 while(signal.read() != pinValue_Value);
mehatfie 4:86d0d04cc055 114 }
mehatfie 4:86d0d04cc055 115
mehatfie 5:e36e0538a903 116
mehatfie 5:e36e0538a903 117 /******************************************************************************/
mehatfie 5:e36e0538a903 118 /*** <Func: val> ***/
mehatfie 5:e36e0538a903 119 /******************************************************************************/
mehatfie 5:e36e0538a903 120 // returns 1 if the check value is equal to the current check value of the Pin
mehatfie 9:5a0c4c6e39c7 121 else if (func.compare("val") == 0){
mehatfie 5:e36e0538a903 122
mehatfie 5:e36e0538a903 123 if (lineData.numWords != 3){
mehatfie 9:5a0c4c6e39c7 124 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 125 return -1;
mehatfie 5:e36e0538a903 126 }
mehatfie 5:e36e0538a903 127
mehatfie 5:e36e0538a903 128 //Initialize and Convert Parameters
mehatfie 5:e36e0538a903 129 string checkValue = lineData.word[2];
mehatfie 5:e36e0538a903 130 int checkValue_Value = 0;
mehatfie 9:5a0c4c6e39c7 131 int numValuesFound = sscanf(checkValue.c_str(), "%d", &checkValue_Value);
mehatfie 9:5a0c4c6e39c7 132 if (numValuesFound < 1){
mehatfie 9:5a0c4c6e39c7 133 ErrorOut("Parameter Unknown, pin value can't be converted", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 134 return -1;
mehatfie 9:5a0c4c6e39c7 135 }
mehatfie 5:e36e0538a903 136
mehatfie 9:5a0c4c6e39c7 137 if(checkValue_Value < 0 && checkValue_Value > 1){
mehatfie 9:5a0c4c6e39c7 138 ErrorOut("Pin Value must be either 0 or 1", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 139 return -1;
mehatfie 9:5a0c4c6e39c7 140 }
mehatfie 9:5a0c4c6e39c7 141
mehatfie 10:e8db892fbc52 142 //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to perform functionality
mehatfie 10:e8db892fbc52 143 if (DummyMode)
mehatfie 10:e8db892fbc52 144 return 0; //Function operated successfully but doesn't return a value
mehatfie 10:e8db892fbc52 145
mehatfie 9:5a0c4c6e39c7 146 //Return one if value meets the wanted pin value, return 0 if it doesn't
mehatfie 11:bc9cd2869f95 147 if(signal.read() == checkValue_Value){
mehatfie 11:bc9cd2869f95 148 //Debounce check to ensure the value is still correct, if incorrect after debounce time, return 0, since it's a false truth
mehatfie 11:bc9cd2869f95 149 if (signal.read() == checkValue_Value){
mehatfie 11:bc9cd2869f95 150 Timer debounceTimer;
mehatfie 11:bc9cd2869f95 151 debounceTimer.reset();
mehatfie 11:bc9cd2869f95 152 debounceTimer.start();
mehatfie 11:bc9cd2869f95 153 while (debounceTimer.read_ms() < 40);
mehatfie 11:bc9cd2869f95 154 if (signal.read() == checkValue_Value)
mehatfie 11:bc9cd2869f95 155 return 1;
mehatfie 11:bc9cd2869f95 156 else
mehatfie 11:bc9cd2869f95 157 return 0;
mehatfie 11:bc9cd2869f95 158 }
mehatfie 11:bc9cd2869f95 159 }
mehatfie 5:e36e0538a903 160 else
mehatfie 5:e36e0538a903 161 return 0;
mehatfie 5:e36e0538a903 162 }
mehatfie 5:e36e0538a903 163
mehatfie 4:86d0d04cc055 164 else {
mehatfie 9:5a0c4c6e39c7 165 ErrorOut("Unknown Command for PinIn Class", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 166 return -1;
mehatfie 4:86d0d04cc055 167 }
mehatfie 9:5a0c4c6e39c7 168
mehatfie 9:5a0c4c6e39c7 169 return 0; //Return success as 0 since no condition had to be met
mehatfie 4:86d0d04cc055 170 }