Andy Lustig / Mbed 2 deprecated stateScript_v2_karpova

Dependencies:   SMARTWAV mbed

Fork of stateScript_v2 by Mattias Karlsson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hardwareInterface.cpp Source File

hardwareInterface.cpp

00001 #include "hardwareInterface.h"
00002 //#include <ostream>
00003 #include <sstream>
00004 
00005 using namespace std;
00006 
00007 //In debug mode, debug messages output to the screen
00008 #ifdef DEBUGOUTPUT
00009 bool debugOut = true;
00010 #else
00011 bool debugOut = false;
00012 #endif
00013 
00014 uint32_t timeKeeper; //the main clock (updated every ms)
00015 bool resetTimer;
00016 bool clockSlave;
00017 bool changeToSlave;
00018 bool changeToStandAlone;
00019 
00020 
00021 #ifdef MBEDHARDWARE
00022 
00023 //On the MBED, this needs to be put on a defined bank of memory, or else we run out of memory
00024 __attribute((section("AHBSRAM0"),aligned)) outputStream textDisplay(512);
00025 
00026 #else
00027 outputStream textDisplay(256);
00028 
00029 #endif
00030 
00031 
00032 //---------------------------------------------------------
00033 
00034 sSystem::sSystem() {
00035     for (int i=0;i<32;i++) {
00036         ignorePortUpdates[i] = false;
00037     }
00038 }
00039 
00040 //The user can toggle whether a particular port triggers a state update report every time it changes
00041 //This is useful for inputs that toggle continuously.
00042 void sSystem::setPortUpdatesOff(int portNum) {
00043     ignorePortUpdates[portNum] = true;
00044 }
00045 
00046 void sSystem::setPortUpdatesOn(int portNum) {
00047     ignorePortUpdates[portNum] = false;
00048 }
00049 
00050 bool* sSystem::getIgnoreUpdates() {
00051     return ignorePortUpdates;
00052 }
00053 
00054 void sSystem::immediateClockReset() {
00055     //For external clock reset
00056     timeKeeper = 0;
00057     textDisplay << timeKeeper << " Clock reset\r\n";
00058 }
00059 
00060 void sSystem::mainLoopToDo() {
00061 
00062 }
00063 
00064 void sSystem::pauseInterrupts() {
00065 
00066 }
00067 
00068 void sSystem::resumeInterrupts() {
00069 
00070 }
00071 
00072 int sSystem::getPendingFunctionTriggers(uint16_t *bufferPtr) {
00073     return 0;
00074 }
00075 
00076 uint32_t sSystem::getDigitalOutputChangeFlags() {
00077 
00078 }
00079 
00080 uint32_t sSystem::getDigitalInputChangeFlags() {
00081 
00082 }
00083 
00084 //------------------------------------------------------
00085 sDigitalOut::sDigitalOut() {
00086 
00087 }
00088 
00089 //----------------------------------------------------
00090 sDigitalIn::sDigitalIn() {
00091     lastDownEvent.triggered = false;
00092     lastUpEvent.triggered = false;
00093     bufferedDownEvent.triggered = false;
00094     bufferedUpEvent.triggered = false;
00095     updating = false;
00096 }
00097 
00098 void sDigitalIn::addStateChange(int newState, uint32_t timeStamp) {
00099 
00100     //With levers and beam breaks, there will be flutter when triggers happen.
00101     //The goal is to capture the initial event time, so we ignore extra triggers
00102     //until it has been processed
00103     if (!updating) {
00104         if ((newState == 0) && (!lastDownEvent.triggered)){
00105             lastDownEvent.timeStamp = timeStamp;
00106             lastDownEvent.triggered = true;
00107         } else if ((newState == 1) && (!lastUpEvent.triggered)) {
00108             lastUpEvent.timeStamp = timeStamp;
00109             lastUpEvent.triggered = true;
00110         }
00111     } else {
00112         //If we are currently checking this input, then we buffer the trigger and deal with it after
00113         if (newState == 0){
00114             bufferedDownEvent.timeStamp = timeStamp;
00115             bufferedDownEvent.triggered = true;
00116         } else if (newState == 1) {
00117             bufferedUpEvent.timeStamp = timeStamp;
00118             bufferedUpEvent.triggered = true;
00119         }
00120     }
00121     /*
00122     if ((newState == 0) && (!lastDownEvent.triggered)){
00123         lastDownEvent.timeStamp = timeStamp;
00124         lastDownEvent.triggered = true;
00125     } else if ((newState == 1) && (!lastUpEvent.triggered)) {
00126         lastUpEvent.timeStamp = timeStamp;
00127         lastUpEvent.triggered = true;
00128     }*/
00129 }
00130 
00131 void sDigitalIn::setUpdate(bool state) {
00132     updating = state; //If true, then we buffer any trigger events until the update check is done.
00133     if (!updating) {
00134         if (bufferedUpEvent.triggered) {
00135             lastUpEvent = bufferedUpEvent;
00136         }
00137         if (bufferedDownEvent.triggered) {
00138             lastDownEvent = bufferedDownEvent;
00139         }
00140         bufferedDownEvent.triggered = false;
00141         bufferedUpEvent.triggered = false;
00142     }
00143 }
00144 
00145 //-----------------------------------------------------
00146 sSerialPort::sSerialPort() {
00147 
00148 }
00149 
00150 //------------------------------------------------------
00151 
00152 sSound::sSound(void):
00153     fileNameExists(false),
00154     volumePtr(NULL),
00155     volume(-1),
00156     play(true),
00157     reset(false) {
00158 }
00159 
00160 void sSound::setFile(string fileNameIn) {
00161     for (int i = 0; i < 20; i++) {
00162         fileName[i] = NULL;
00163     }
00164     size_t length = fileNameIn.size();
00165     if (length <= 20) {
00166         fileNameIn.copy(fileName, length, 0);
00167         fileNameExists = true;
00168     }
00169 }
00170 void sSound::setVolume(int volumeIn) {
00171 
00172     if ((volumeIn >= 0) && (volumeIn < 256)) {
00173         volume = volumeIn;
00174         volumePtr = NULL;
00175     }
00176 }
00177 
00178 void sSound::setVolume(int* volumeIn) {
00179 
00180     volume = -1;
00181     volumePtr = volumeIn;
00182 
00183 }
00184 
00185 void sSound::setPlayback(bool playIn) {
00186     play = playIn;
00187 }
00188 
00189 void sSound::setReset() {
00190     reset = true;
00191 }
00192 
00193 
00194 //-----------------------------------------------------
00195 outputStream::outputStream(int bufferSizeIn):
00196     readHead(0),
00197     writeHead(0),
00198     totalWriteHead(0),
00199     totalReadHead(0),
00200     bufferSize(bufferSizeIn),
00201     unsentData(false),
00202     serialPtr(NULL) {
00203 
00204     outputBuffer = new char[bufferSize];
00205 
00206 }
00207 
00208 outputStream::~outputStream() {
00209     delete[] outputBuffer;
00210 }
00211 
00212 void outputStream::setSerial(sSerialPort *s) {
00213     serialPtr = s;
00214 }
00215 
00216 //used to immediately write to serial port
00217 void outputStream::flush() {
00218     if (serialPtr != NULL) {
00219         while(unsentData) {
00220             serialPtr->writeChar(getNextChar());
00221         }
00222     }
00223 }
00224 
00225 //adds text to the buffer
00226 void outputStream::send(const string &outputString) {
00227     int strLen = outputString.size();
00228 
00229     int total = 0;
00230     int chunk = 0;
00231     if (totalWriteHead+strLen > (totalReadHead + bufferSize)) {
00232         //We don't have enough space in the buffer, so flush it
00233         flush();
00234     }
00235     if (!(totalWriteHead+strLen > (totalReadHead + bufferSize))) {
00236         while (strLen - total > 0) {
00237             chunk = min((bufferSize - writeHead), strLen - total);
00238             outputString.copy(outputBuffer + writeHead, chunk, total);
00239             writeHead = (writeHead + chunk) % bufferSize;
00240             totalWriteHead += chunk;
00241             total += chunk;
00242         }
00243         if (total > 0) {
00244             unsentData = true;
00245         }
00246     }
00247 }
00248 
00249 //adds text to the buffer
00250 void outputStream::send(const char *s) {
00251     int strLen = strlen(s);
00252 
00253     int total = 0;
00254     //int chunk = 0;
00255     if (totalWriteHead+strLen > (totalReadHead + bufferSize)) {
00256         //We don't have enough space in the buffer, so flush it
00257         flush();
00258     }
00259     if (!(totalWriteHead+strLen > (totalReadHead + bufferSize))) {
00260         while (strLen - total > 0) {
00261             strncpy(outputBuffer + writeHead, s+total,1);
00262             total++;
00263             writeHead = (writeHead + 1) % bufferSize;
00264             totalWriteHead += 1;
00265 
00266             /*
00267             chunk = min((bufferSize - writeHead), strLen - total);
00268             strncpy(outputBuffer + writeHead,);
00269             outputString.copy(outputBuffer + writeHead, chunk, total);
00270             writeHead = (writeHead + chunk) % bufferSize;
00271             totalWriteHead += chunk;
00272             total += chunk;
00273             */
00274         }
00275         if (total > 0) {
00276             unsentData = true;
00277         }
00278     }
00279 }
00280 
00281 void outputStream::debug(const char *s) {
00282     //send to serial immediately, but only if debugOut is true
00283     if (debugOut) {
00284         string tmpString = string(s);
00285         send(tmpString);
00286         flush();
00287     }
00288 }
00289 
00290 //Overloaded << operator to for debugging output.  This eliminates the
00291 //need for printf statements
00292 outputStream& outputStream::operator<<(const string &outputString) {
00293     send(outputString);
00294 
00295     return *this;
00296 }
00297 
00298 outputStream& outputStream::operator<<(const char* s) {
00299     //string tmpString = string(s);
00300     //send(tmpString);
00301     send(s);
00302     return *this;
00303 }
00304 
00305 outputStream& outputStream::operator<<(int outputNum) {
00306     ostringstream varConvert;
00307     varConvert << outputNum;
00308     send(varConvert.str());
00309     return *this;
00310 }
00311 
00312 outputStream& outputStream::operator<<(uint32_t outputNum) {
00313     ostringstream varConvert;
00314     varConvert << outputNum;
00315     send(varConvert.str());
00316     return *this;
00317 }
00318 //the main loop gets one character per loop and write it to the serial port
00319 char outputStream::getNextChar() {
00320 
00321 
00322     if (totalReadHead < totalWriteHead) {
00323         tmpOut = *(outputBuffer+readHead);
00324         readHead = (readHead+1) % bufferSize;
00325         totalReadHead++;
00326         if (totalReadHead >= totalWriteHead) {
00327             unsentData = false;
00328         }
00329     }
00330     return tmpOut;
00331 
00332 }
00333 
00334