stateScript for Nucleo-F401RE board

Dependencies:   SOMO_II mbed

Committer:
alustig3
Date:
Mon Feb 08 18:56:09 2016 +0000
Revision:
0:ecf80f0172d0
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alustig3 0:ecf80f0172d0 1 #include "hardwareInterface.h"
alustig3 0:ecf80f0172d0 2 //#include <ostream>
alustig3 0:ecf80f0172d0 3 #include <sstream>
alustig3 0:ecf80f0172d0 4
alustig3 0:ecf80f0172d0 5 using namespace std;
alustig3 0:ecf80f0172d0 6
alustig3 0:ecf80f0172d0 7 //In debug mode, debug messages output to the screen
alustig3 0:ecf80f0172d0 8 #ifdef DEBUGOUTPUT
alustig3 0:ecf80f0172d0 9 bool debugOut = true;
alustig3 0:ecf80f0172d0 10 #else
alustig3 0:ecf80f0172d0 11 bool debugOut = false;
alustig3 0:ecf80f0172d0 12 #endif
alustig3 0:ecf80f0172d0 13
alustig3 0:ecf80f0172d0 14 uint32_t timeKeeper; //the main clock (updated every ms)
alustig3 0:ecf80f0172d0 15 bool resetTimer;
alustig3 0:ecf80f0172d0 16 bool clockSlave;
alustig3 0:ecf80f0172d0 17 bool changeToSlave;
alustig3 0:ecf80f0172d0 18 bool changeToStandAlone;
alustig3 0:ecf80f0172d0 19
alustig3 0:ecf80f0172d0 20
alustig3 0:ecf80f0172d0 21 #ifdef MBEDHARDWARE
alustig3 0:ecf80f0172d0 22
alustig3 0:ecf80f0172d0 23 //On the MBED, this needs to be put on a defined bank of memory, or else we run out of memory
alustig3 0:ecf80f0172d0 24 __attribute((section("AHBSRAM0"),aligned)) outputStream textDisplay(512);
alustig3 0:ecf80f0172d0 25
alustig3 0:ecf80f0172d0 26 #else
alustig3 0:ecf80f0172d0 27 outputStream textDisplay(256);
alustig3 0:ecf80f0172d0 28
alustig3 0:ecf80f0172d0 29 #endif
alustig3 0:ecf80f0172d0 30
alustig3 0:ecf80f0172d0 31
alustig3 0:ecf80f0172d0 32 //---------------------------------------------------------
alustig3 0:ecf80f0172d0 33
alustig3 0:ecf80f0172d0 34 sSystem::sSystem() {
alustig3 0:ecf80f0172d0 35 for (int i=0;i<32;i++) {
alustig3 0:ecf80f0172d0 36 ignorePortUpdates[i] = false;
alustig3 0:ecf80f0172d0 37 }
alustig3 0:ecf80f0172d0 38 }
alustig3 0:ecf80f0172d0 39
alustig3 0:ecf80f0172d0 40 //The user can toggle whether a particular port triggers a state update report every time it changes
alustig3 0:ecf80f0172d0 41 //This is useful for inputs that toggle continuously.
alustig3 0:ecf80f0172d0 42 void sSystem::setPortUpdatesOff(int portNum) {
alustig3 0:ecf80f0172d0 43 ignorePortUpdates[portNum] = true;
alustig3 0:ecf80f0172d0 44 }
alustig3 0:ecf80f0172d0 45
alustig3 0:ecf80f0172d0 46 void sSystem::setPortUpdatesOn(int portNum) {
alustig3 0:ecf80f0172d0 47 ignorePortUpdates[portNum] = false;
alustig3 0:ecf80f0172d0 48 }
alustig3 0:ecf80f0172d0 49
alustig3 0:ecf80f0172d0 50 bool* sSystem::getIgnoreUpdates() {
alustig3 0:ecf80f0172d0 51 return ignorePortUpdates;
alustig3 0:ecf80f0172d0 52 }
alustig3 0:ecf80f0172d0 53
alustig3 0:ecf80f0172d0 54 void sSystem::immediateClockReset() {
alustig3 0:ecf80f0172d0 55 //For external clock reset
alustig3 0:ecf80f0172d0 56 timeKeeper = 0;
alustig3 0:ecf80f0172d0 57 textDisplay << timeKeeper << " Clock reset\r\n";
alustig3 0:ecf80f0172d0 58 }
alustig3 0:ecf80f0172d0 59
alustig3 0:ecf80f0172d0 60 void sSystem::mainLoopToDo() {
alustig3 0:ecf80f0172d0 61
alustig3 0:ecf80f0172d0 62 }
alustig3 0:ecf80f0172d0 63
alustig3 0:ecf80f0172d0 64 void sSystem::pauseInterrupts() {
alustig3 0:ecf80f0172d0 65
alustig3 0:ecf80f0172d0 66 }
alustig3 0:ecf80f0172d0 67
alustig3 0:ecf80f0172d0 68 void sSystem::resumeInterrupts() {
alustig3 0:ecf80f0172d0 69
alustig3 0:ecf80f0172d0 70 }
alustig3 0:ecf80f0172d0 71
alustig3 0:ecf80f0172d0 72 int sSystem::getPendingFunctionTriggers(uint16_t *bufferPtr) {
alustig3 0:ecf80f0172d0 73 return 0;
alustig3 0:ecf80f0172d0 74 }
alustig3 0:ecf80f0172d0 75
alustig3 0:ecf80f0172d0 76 uint32_t sSystem::getDigitalOutputChangeFlags() {
alustig3 0:ecf80f0172d0 77
alustig3 0:ecf80f0172d0 78 }
alustig3 0:ecf80f0172d0 79
alustig3 0:ecf80f0172d0 80 uint32_t sSystem::getDigitalInputChangeFlags() {
alustig3 0:ecf80f0172d0 81
alustig3 0:ecf80f0172d0 82 }
alustig3 0:ecf80f0172d0 83
alustig3 0:ecf80f0172d0 84 //------------------------------------------------------
alustig3 0:ecf80f0172d0 85 sDigitalOut::sDigitalOut() {
alustig3 0:ecf80f0172d0 86
alustig3 0:ecf80f0172d0 87 }
alustig3 0:ecf80f0172d0 88
alustig3 0:ecf80f0172d0 89 //----------------------------------------------------
alustig3 0:ecf80f0172d0 90 sDigitalIn::sDigitalIn() {
alustig3 0:ecf80f0172d0 91 lastDownEvent.triggered = false;
alustig3 0:ecf80f0172d0 92 lastUpEvent.triggered = false;
alustig3 0:ecf80f0172d0 93 bufferedDownEvent.triggered = false;
alustig3 0:ecf80f0172d0 94 bufferedUpEvent.triggered = false;
alustig3 0:ecf80f0172d0 95 updating = false;
alustig3 0:ecf80f0172d0 96 }
alustig3 0:ecf80f0172d0 97
alustig3 0:ecf80f0172d0 98 void sDigitalIn::addStateChange(int newState, uint32_t timeStamp) {
alustig3 0:ecf80f0172d0 99
alustig3 0:ecf80f0172d0 100 //With levers and beam breaks, there will be flutter when triggers happen.
alustig3 0:ecf80f0172d0 101 //The goal is to capture the initial event time, so we ignore extra triggers
alustig3 0:ecf80f0172d0 102 //until it has been processed
alustig3 0:ecf80f0172d0 103 if (!updating) {
alustig3 0:ecf80f0172d0 104 if ((newState == 0) && (!lastDownEvent.triggered)){
alustig3 0:ecf80f0172d0 105 lastDownEvent.timeStamp = timeStamp;
alustig3 0:ecf80f0172d0 106 lastDownEvent.triggered = true;
alustig3 0:ecf80f0172d0 107 } else if ((newState == 1) && (!lastUpEvent.triggered)) {
alustig3 0:ecf80f0172d0 108 lastUpEvent.timeStamp = timeStamp;
alustig3 0:ecf80f0172d0 109 lastUpEvent.triggered = true;
alustig3 0:ecf80f0172d0 110 }
alustig3 0:ecf80f0172d0 111 } else {
alustig3 0:ecf80f0172d0 112 //If we are currently checking this input, then we buffer the trigger and deal with it after
alustig3 0:ecf80f0172d0 113 if (newState == 0){
alustig3 0:ecf80f0172d0 114 bufferedDownEvent.timeStamp = timeStamp;
alustig3 0:ecf80f0172d0 115 bufferedDownEvent.triggered = true;
alustig3 0:ecf80f0172d0 116 } else if (newState == 1) {
alustig3 0:ecf80f0172d0 117 bufferedUpEvent.timeStamp = timeStamp;
alustig3 0:ecf80f0172d0 118 bufferedUpEvent.triggered = true;
alustig3 0:ecf80f0172d0 119 }
alustig3 0:ecf80f0172d0 120 }
alustig3 0:ecf80f0172d0 121 /*
alustig3 0:ecf80f0172d0 122 if ((newState == 0) && (!lastDownEvent.triggered)){
alustig3 0:ecf80f0172d0 123 lastDownEvent.timeStamp = timeStamp;
alustig3 0:ecf80f0172d0 124 lastDownEvent.triggered = true;
alustig3 0:ecf80f0172d0 125 } else if ((newState == 1) && (!lastUpEvent.triggered)) {
alustig3 0:ecf80f0172d0 126 lastUpEvent.timeStamp = timeStamp;
alustig3 0:ecf80f0172d0 127 lastUpEvent.triggered = true;
alustig3 0:ecf80f0172d0 128 }*/
alustig3 0:ecf80f0172d0 129 }
alustig3 0:ecf80f0172d0 130
alustig3 0:ecf80f0172d0 131 void sDigitalIn::setUpdate(bool state) {
alustig3 0:ecf80f0172d0 132 updating = state; //If true, then we buffer any trigger events until the update check is done.
alustig3 0:ecf80f0172d0 133 if (!updating) {
alustig3 0:ecf80f0172d0 134 if (bufferedUpEvent.triggered) {
alustig3 0:ecf80f0172d0 135 lastUpEvent = bufferedUpEvent;
alustig3 0:ecf80f0172d0 136 }
alustig3 0:ecf80f0172d0 137 if (bufferedDownEvent.triggered) {
alustig3 0:ecf80f0172d0 138 lastDownEvent = bufferedDownEvent;
alustig3 0:ecf80f0172d0 139 }
alustig3 0:ecf80f0172d0 140 bufferedDownEvent.triggered = false;
alustig3 0:ecf80f0172d0 141 bufferedUpEvent.triggered = false;
alustig3 0:ecf80f0172d0 142 }
alustig3 0:ecf80f0172d0 143 }
alustig3 0:ecf80f0172d0 144
alustig3 0:ecf80f0172d0 145 //-----------------------------------------------------
alustig3 0:ecf80f0172d0 146 sSerialPort::sSerialPort() {
alustig3 0:ecf80f0172d0 147
alustig3 0:ecf80f0172d0 148 }
alustig3 0:ecf80f0172d0 149
alustig3 0:ecf80f0172d0 150 //------------------------------------------------------
alustig3 0:ecf80f0172d0 151
alustig3 0:ecf80f0172d0 152 sSound::sSound(void):
alustig3 0:ecf80f0172d0 153 fileNameExists(false),
alustig3 0:ecf80f0172d0 154 fileNum(0),
alustig3 0:ecf80f0172d0 155 volumePtr(NULL),
alustig3 0:ecf80f0172d0 156 volume(-1),
alustig3 0:ecf80f0172d0 157 play(true),
alustig3 0:ecf80f0172d0 158 reset(false) {
alustig3 0:ecf80f0172d0 159 }
alustig3 0:ecf80f0172d0 160
alustig3 0:ecf80f0172d0 161 void sSound::setFile(string fileNameIn) {
alustig3 0:ecf80f0172d0 162 for (int i = 0; i < 20; i++) {
alustig3 0:ecf80f0172d0 163 fileName[i] = NULL;
alustig3 0:ecf80f0172d0 164 }
alustig3 0:ecf80f0172d0 165 size_t length = fileNameIn.size();
alustig3 0:ecf80f0172d0 166 if (length <= 20) {
alustig3 0:ecf80f0172d0 167 fileNameIn.copy(fileName, length, 0);
alustig3 0:ecf80f0172d0 168 fileNameExists = true;
alustig3 0:ecf80f0172d0 169 }
alustig3 0:ecf80f0172d0 170 }
alustig3 0:ecf80f0172d0 171
alustig3 0:ecf80f0172d0 172 void sSound::setFile(unsigned char filenNumIn) { //overload setFile to work with SOMO module
alustig3 0:ecf80f0172d0 173 fileNum = filenNumIn;
alustig3 0:ecf80f0172d0 174 }
alustig3 0:ecf80f0172d0 175
alustig3 0:ecf80f0172d0 176 void sSound::setVolume(int volumeIn) {
alustig3 0:ecf80f0172d0 177
alustig3 0:ecf80f0172d0 178 if ((volumeIn >= 0) && (volumeIn < 256)) {
alustig3 0:ecf80f0172d0 179 volume = volumeIn;
alustig3 0:ecf80f0172d0 180 volumePtr = NULL;
alustig3 0:ecf80f0172d0 181 }
alustig3 0:ecf80f0172d0 182 }
alustig3 0:ecf80f0172d0 183
alustig3 0:ecf80f0172d0 184 void sSound::setVolume(int* volumeIn) {
alustig3 0:ecf80f0172d0 185
alustig3 0:ecf80f0172d0 186 volume = -1;
alustig3 0:ecf80f0172d0 187 volumePtr = volumeIn;
alustig3 0:ecf80f0172d0 188
alustig3 0:ecf80f0172d0 189 }
alustig3 0:ecf80f0172d0 190
alustig3 0:ecf80f0172d0 191 void sSound::setPlayback(bool playIn) {
alustig3 0:ecf80f0172d0 192 play = playIn;
alustig3 0:ecf80f0172d0 193 }
alustig3 0:ecf80f0172d0 194
alustig3 0:ecf80f0172d0 195 void sSound::setReset() {
alustig3 0:ecf80f0172d0 196 reset = true;
alustig3 0:ecf80f0172d0 197 }
alustig3 0:ecf80f0172d0 198
alustig3 0:ecf80f0172d0 199
alustig3 0:ecf80f0172d0 200 //-----------------------------------------------------
alustig3 0:ecf80f0172d0 201 outputStream::outputStream(int bufferSizeIn):
alustig3 0:ecf80f0172d0 202 readHead(0),
alustig3 0:ecf80f0172d0 203 writeHead(0),
alustig3 0:ecf80f0172d0 204 totalWriteHead(0),
alustig3 0:ecf80f0172d0 205 totalReadHead(0),
alustig3 0:ecf80f0172d0 206 bufferSize(bufferSizeIn),
alustig3 0:ecf80f0172d0 207 unsentData(false),
alustig3 0:ecf80f0172d0 208 serialPtr(NULL) {
alustig3 0:ecf80f0172d0 209
alustig3 0:ecf80f0172d0 210 outputBuffer = new char[bufferSize];
alustig3 0:ecf80f0172d0 211
alustig3 0:ecf80f0172d0 212 }
alustig3 0:ecf80f0172d0 213
alustig3 0:ecf80f0172d0 214 outputStream::~outputStream() {
alustig3 0:ecf80f0172d0 215 delete[] outputBuffer;
alustig3 0:ecf80f0172d0 216 }
alustig3 0:ecf80f0172d0 217
alustig3 0:ecf80f0172d0 218 void outputStream::setSerial(sSerialPort *s) {
alustig3 0:ecf80f0172d0 219 serialPtr = s;
alustig3 0:ecf80f0172d0 220 }
alustig3 0:ecf80f0172d0 221
alustig3 0:ecf80f0172d0 222 //used to immediately write to serial port
alustig3 0:ecf80f0172d0 223 void outputStream::flush() {
alustig3 0:ecf80f0172d0 224 if (serialPtr != NULL) {
alustig3 0:ecf80f0172d0 225 while(unsentData) {
alustig3 0:ecf80f0172d0 226 serialPtr->writeChar(getNextChar());
alustig3 0:ecf80f0172d0 227 }
alustig3 0:ecf80f0172d0 228 }
alustig3 0:ecf80f0172d0 229 }
alustig3 0:ecf80f0172d0 230
alustig3 0:ecf80f0172d0 231 //adds text to the buffer
alustig3 0:ecf80f0172d0 232 void outputStream::send(const string &outputString) {
alustig3 0:ecf80f0172d0 233 int strLen = outputString.size();
alustig3 0:ecf80f0172d0 234
alustig3 0:ecf80f0172d0 235 int total = 0;
alustig3 0:ecf80f0172d0 236 int chunk = 0;
alustig3 0:ecf80f0172d0 237 if (totalWriteHead+strLen > (totalReadHead + bufferSize)) {
alustig3 0:ecf80f0172d0 238 //We don't have enough space in the buffer, so flush it
alustig3 0:ecf80f0172d0 239 flush();
alustig3 0:ecf80f0172d0 240 }
alustig3 0:ecf80f0172d0 241 if (!(totalWriteHead+strLen > (totalReadHead + bufferSize))) {
alustig3 0:ecf80f0172d0 242 while (strLen - total > 0) {
alustig3 0:ecf80f0172d0 243 chunk = min((bufferSize - writeHead), strLen - total);
alustig3 0:ecf80f0172d0 244 outputString.copy(outputBuffer + writeHead, chunk, total);
alustig3 0:ecf80f0172d0 245 writeHead = (writeHead + chunk) % bufferSize;
alustig3 0:ecf80f0172d0 246 totalWriteHead += chunk;
alustig3 0:ecf80f0172d0 247 total += chunk;
alustig3 0:ecf80f0172d0 248 }
alustig3 0:ecf80f0172d0 249 if (total > 0) {
alustig3 0:ecf80f0172d0 250 unsentData = true;
alustig3 0:ecf80f0172d0 251 }
alustig3 0:ecf80f0172d0 252 }
alustig3 0:ecf80f0172d0 253 }
alustig3 0:ecf80f0172d0 254
alustig3 0:ecf80f0172d0 255 //adds text to the buffer
alustig3 0:ecf80f0172d0 256 void outputStream::send(const char *s) {
alustig3 0:ecf80f0172d0 257 int strLen = strlen(s);
alustig3 0:ecf80f0172d0 258
alustig3 0:ecf80f0172d0 259 int total = 0;
alustig3 0:ecf80f0172d0 260 //int chunk = 0;
alustig3 0:ecf80f0172d0 261 if (totalWriteHead+strLen > (totalReadHead + bufferSize)) {
alustig3 0:ecf80f0172d0 262 //We don't have enough space in the buffer, so flush it
alustig3 0:ecf80f0172d0 263 flush();
alustig3 0:ecf80f0172d0 264 }
alustig3 0:ecf80f0172d0 265 if (!(totalWriteHead+strLen > (totalReadHead + bufferSize))) {
alustig3 0:ecf80f0172d0 266 while (strLen - total > 0) {
alustig3 0:ecf80f0172d0 267 strncpy(outputBuffer + writeHead, s+total,1);
alustig3 0:ecf80f0172d0 268 total++;
alustig3 0:ecf80f0172d0 269 writeHead = (writeHead + 1) % bufferSize;
alustig3 0:ecf80f0172d0 270 totalWriteHead += 1;
alustig3 0:ecf80f0172d0 271
alustig3 0:ecf80f0172d0 272 /*
alustig3 0:ecf80f0172d0 273 chunk = min((bufferSize - writeHead), strLen - total);
alustig3 0:ecf80f0172d0 274 strncpy(outputBuffer + writeHead,);
alustig3 0:ecf80f0172d0 275 outputString.copy(outputBuffer + writeHead, chunk, total);
alustig3 0:ecf80f0172d0 276 writeHead = (writeHead + chunk) % bufferSize;
alustig3 0:ecf80f0172d0 277 totalWriteHead += chunk;
alustig3 0:ecf80f0172d0 278 total += chunk;
alustig3 0:ecf80f0172d0 279 */
alustig3 0:ecf80f0172d0 280 }
alustig3 0:ecf80f0172d0 281 if (total > 0) {
alustig3 0:ecf80f0172d0 282 unsentData = true;
alustig3 0:ecf80f0172d0 283 }
alustig3 0:ecf80f0172d0 284 }
alustig3 0:ecf80f0172d0 285 }
alustig3 0:ecf80f0172d0 286
alustig3 0:ecf80f0172d0 287 void outputStream::debug(const char *s) {
alustig3 0:ecf80f0172d0 288 //send to serial immediately, but only if debugOut is true
alustig3 0:ecf80f0172d0 289 if (debugOut) {
alustig3 0:ecf80f0172d0 290 string tmpString = string(s);
alustig3 0:ecf80f0172d0 291 send(tmpString);
alustig3 0:ecf80f0172d0 292 flush();
alustig3 0:ecf80f0172d0 293 }
alustig3 0:ecf80f0172d0 294 }
alustig3 0:ecf80f0172d0 295
alustig3 0:ecf80f0172d0 296 //Overloaded << operator to for debugging output. This eliminates the
alustig3 0:ecf80f0172d0 297 //need for printf statements
alustig3 0:ecf80f0172d0 298 outputStream& outputStream::operator<<(const string &outputString) {
alustig3 0:ecf80f0172d0 299 send(outputString);
alustig3 0:ecf80f0172d0 300
alustig3 0:ecf80f0172d0 301 return *this;
alustig3 0:ecf80f0172d0 302 }
alustig3 0:ecf80f0172d0 303
alustig3 0:ecf80f0172d0 304 outputStream& outputStream::operator<<(const char* s) {
alustig3 0:ecf80f0172d0 305 //string tmpString = string(s);
alustig3 0:ecf80f0172d0 306 //send(tmpString);
alustig3 0:ecf80f0172d0 307 send(s);
alustig3 0:ecf80f0172d0 308 return *this;
alustig3 0:ecf80f0172d0 309 }
alustig3 0:ecf80f0172d0 310
alustig3 0:ecf80f0172d0 311 outputStream& outputStream::operator<<(int outputNum) {
alustig3 0:ecf80f0172d0 312 ostringstream varConvert;
alustig3 0:ecf80f0172d0 313 varConvert << outputNum;
alustig3 0:ecf80f0172d0 314 send(varConvert.str());
alustig3 0:ecf80f0172d0 315 return *this;
alustig3 0:ecf80f0172d0 316 }
alustig3 0:ecf80f0172d0 317
alustig3 0:ecf80f0172d0 318 outputStream& outputStream::operator<<(uint32_t outputNum) {
alustig3 0:ecf80f0172d0 319 ostringstream varConvert;
alustig3 0:ecf80f0172d0 320 varConvert << outputNum;
alustig3 0:ecf80f0172d0 321 send(varConvert.str());
alustig3 0:ecf80f0172d0 322 return *this;
alustig3 0:ecf80f0172d0 323 }
alustig3 0:ecf80f0172d0 324 //the main loop gets one character per loop and write it to the serial port
alustig3 0:ecf80f0172d0 325 char outputStream::getNextChar() {
alustig3 0:ecf80f0172d0 326
alustig3 0:ecf80f0172d0 327
alustig3 0:ecf80f0172d0 328 if (totalReadHead < totalWriteHead) {
alustig3 0:ecf80f0172d0 329 tmpOut = *(outputBuffer+readHead);
alustig3 0:ecf80f0172d0 330 readHead = (readHead+1) % bufferSize;
alustig3 0:ecf80f0172d0 331 totalReadHead++;
alustig3 0:ecf80f0172d0 332 if (totalReadHead >= totalWriteHead) {
alustig3 0:ecf80f0172d0 333 unsentData = false;
alustig3 0:ecf80f0172d0 334 }
alustig3 0:ecf80f0172d0 335 }
alustig3 0:ecf80f0172d0 336 return tmpOut;
alustig3 0:ecf80f0172d0 337
alustig3 0:ecf80f0172d0 338 }
alustig3 0:ecf80f0172d0 339
alustig3 0:ecf80f0172d0 340