Andy Lustig
/
stateScript_v2
fork of StateScript
Fork of stateScript_v2 by
Diff: hardwareInterface.cpp
- Revision:
- 0:8dbd6bd9167f
- Child:
- 1:3a050d26d4f6
- Child:
- 2:35266b266eaa
diff -r 000000000000 -r 8dbd6bd9167f hardwareInterface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hardwareInterface.cpp Tue May 19 15:45:42 2015 +0000 @@ -0,0 +1,248 @@ +#include "hardwareInterface.h" +//#include <ostream> +#include <sstream> + +using namespace std; + +//In debug mode, debug messages output to the screen +#ifdef DEBUGOUTPUT +bool debugOut = true; +#else +bool debugOut = false; +#endif + +uint32_t timeKeeper; //the main clock (updated every ms) +bool resetTimer; +bool clockSlave; +bool changeToSlave; +bool changeToStandAlone; + + +#ifdef MBEDHARDWARE + +//On the MBED, this needs to be put on a defined bank of memory, or else we run out of memory +__attribute((section("AHBSRAM0"),aligned)) outputStream textDisplay(512); + +#else +outputStream textDisplay(256); + +#endif + + +//--------------------------------------------------------- + +sSystem::sSystem() { + +} + +void sSystem::immediateClockReset() { + //For external clock reset + timeKeeper = 0; + textDisplay << timeKeeper << " Clock reset\r\n"; +} + +void sSystem::mainLoopToDo() { + +} + +void sSystem::pauseInterrupts() { + +} + +void sSystem::resumeInterrupts() { + +} + +int sSystem::getPendingFunctionTriggers(uint16_t *bufferPtr) { + return 0; +} + +uint32_t sSystem::getDigitalOutputChangeFlags() { + +} + +uint32_t sSystem::getDigitalInputChangeFlags() { + +} + +//------------------------------------------------------ +sDigitalOut::sDigitalOut() { + +} + +//---------------------------------------------------- +sDigitalIn::sDigitalIn() { + lastDownEvent.triggered = false; + lastUpEvent.triggered = false; +} + +void sDigitalIn::addStateChange(int newState, uint32_t timeStamp) { + + //With levers and beam breaks, there will be flutter when triggers happen. + //The goal is to capture the initial event time, so we ignore extra triggers + //until it has been processed + if ((newState == 0) && (!lastDownEvent.triggered)){ + lastDownEvent.timeStamp = timeStamp; + lastDownEvent.triggered = true; + } else if ((newState == 1) && (!lastUpEvent.triggered)) { + lastUpEvent.timeStamp = timeStamp; + lastUpEvent.triggered = true; + } +} + +//----------------------------------------------------- +sSerialPort::sSerialPort() { + +} + +//------------------------------------------------------ + +sSound::sSound(void): + fileNameExists(false), + volumePtr(NULL), + volume(-1), + play(true), + reset(false) { +} + +void sSound::setFile(string fileNameIn) { + for (int i = 0; i < 20; i++) { + fileName[i] = NULL; + } + size_t length = fileNameIn.size(); + if (length <= 20) { + fileNameIn.copy(fileName, length, 0); + fileNameExists = true; + } +} +void sSound::setVolume(int volumeIn) { + + if ((volumeIn >= 0) && (volumeIn < 256)) { + volume = volumeIn; + volumePtr = NULL; + } +} + +void sSound::setVolume(int* volumeIn) { + + volume = -1; + volumePtr = volumeIn; + +} + +void sSound::setPlayback(bool playIn) { + play = playIn; +} + +void sSound::setReset() { + reset = true; +} + + +//----------------------------------------------------- +outputStream::outputStream(int bufferSizeIn): + readHead(0), + writeHead(0), + totalWriteHead(0), + totalReadHead(0), + bufferSize(bufferSizeIn), + unsentData(false), + serialPtr(NULL) { + + outputBuffer = new char[bufferSize]; + +} + +outputStream::~outputStream() { + delete[] outputBuffer; +} + +void outputStream::setSerial(sSerialPort *s) { + serialPtr = s; +} + +//used to immediately write to serial port +void outputStream::flush() { + if (serialPtr != NULL) { + while(unsentData) { + serialPtr->writeChar(getNextChar()); + } + } +} + +//adds text to the buffer +void outputStream::send(string outputString) { + int strLen = outputString.size(); + + int total = 0; + int chunk = 0; + if (totalWriteHead+strLen > (totalReadHead + bufferSize)) { + //We don't have enough space in the buffer, so flush it + flush(); + } + if (!(totalWriteHead+strLen > (totalReadHead + bufferSize))) { + while (strLen - total > 0) { + chunk = min((bufferSize - writeHead), strLen - total); + outputString.copy(outputBuffer + writeHead, chunk, total); + writeHead = (writeHead + chunk) % bufferSize; + totalWriteHead += chunk; + total += chunk; + } + if (total > 0) { + unsentData = true; + } + } +} + +void outputStream::debug(const char *s) { + //send to serial immediately, but only if debugOut is true + if (debugOut) { + string tmpString = string(s); + send(tmpString); + flush(); + } +} + +//Overloaded << operator to for debugging output. This eliminates the +//need for printf statements +outputStream& outputStream::operator<<(string outputString) { + send(outputString); + return *this; +} + +outputStream& outputStream::operator<<(const char* s) { + string tmpString = string(s); + send(tmpString); + return *this; +} + +outputStream& outputStream::operator<<(int outputNum) { + ostringstream varConvert; + varConvert << outputNum; + send(varConvert.str()); + return *this; +} + +outputStream& outputStream::operator<<(uint32_t outputNum) { + ostringstream varConvert; + varConvert << outputNum; + send(varConvert.str()); + return *this; +} +//the main loop gets one character per loop and write it to the serial port +char outputStream::getNextChar() { + + + if (totalReadHead < totalWriteHead) { + tmpOut = *(outputBuffer+readHead); + readHead = (readHead+1) % bufferSize; + totalReadHead++; + if (totalReadHead >= totalWriteHead) { + unsentData = false; + } + } + return tmpOut; + +} + +