Microcontroller firmware that uses a simple, yet powerful scripting language to control the timing of input and output events with high temporal resolution. Written by Mattias Karlsson

Dependencies:   SMARTWAV mbed

hardwareInterface.cpp

Committer:
mkarlsso
Date:
2015-06-03
Revision:
1:3a050d26d4f6
Parent:
0:8dbd6bd9167f

File content as of revision 1:3a050d26d4f6:

#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(4096);

#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;
    bufferedDownEvent.triggered = false;
    bufferedUpEvent.triggered = false;
    updating = 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 (!updating) {
        if ((newState == 0) && (!lastDownEvent.triggered)){
            lastDownEvent.timeStamp = timeStamp;
            lastDownEvent.triggered = true;
        } else if ((newState == 1) && (!lastUpEvent.triggered)) {
            lastUpEvent.timeStamp = timeStamp;
            lastUpEvent.triggered = true;
        }
    } else {
        //If we are currently checking this input, then we buffer the trigger and deal with it after
        if (newState == 0){
            bufferedDownEvent.timeStamp = timeStamp;
            bufferedDownEvent.triggered = true;
        } else if (newState == 1) {
            bufferedUpEvent.timeStamp = timeStamp;
            bufferedUpEvent.triggered = true;
        }
    }
}

void sDigitalIn::setUpdate(bool state) {
    updating = state; //If true, then we buffer any trigger events until the update check is done.
    if (!updating) {
        if (bufferedUpEvent.triggered) {
            lastUpEvent = bufferedUpEvent;
        }
        if (bufferedDownEvent.triggered) {
            lastDownEvent = bufferedDownEvent;
        }
        bufferedDownEvent.triggered = false;
        bufferedUpEvent.triggered = false;
    }
}

//-----------------------------------------------------
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;

}