Timer* addTimer(int timerCode, unsigned long timeDiviser, interruptTimerCallbackFunc* callback, char* timerName, int* object) { if (timerList.maxSize == 0) { writeError(TIMERS_LIST_NOT_INITIALIZED); return NULL; } unsigned char size = timerList.size; if (size < timerList.maxSize) { Timer* result = getTimerByIndex(size); if (result == NULL) { writeError(TIMER_NULL); return NULL; } result->time = 0; result->markTime = 0; result->timerCode = timerCode; result->timeDiviser = timeDiviser; result->callback = callback; result->enabled = false; result->lock = false; result->working = false; result->name = timerName; result->object = object; timerList.size++; return result; } else { writeError(TOO_MUCH_TIMERS); return NULL; } }
void stopTimerList() { int i; for (i = 0; i < timerList.size; i++) { Timer* timer = getTimerByIndex(i); if (timer == NULL) { writeError(TIMER_NULL); return; } stopTimer(timer); } }
Timer* getTimerByCode(int timerCode) { int i; for (i = 0; i < timerList.size; i++) { Timer* timer = getTimerByIndex(i); if (timer == NULL) { writeError(TIMER_NULL); return NULL; } if (timer->timerCode == timerCode) { return timer; } } return NULL; }
void startTimerList(bool enabledAll) { _initTimers(); timerList.started = true; if (enabledAll) { int i; for (i = 0; i < timerList.size; i++) { Timer* timer = getTimerByIndex(i); if (timer == NULL) { writeError(TIMER_NULL); return; } startTimer(timer); } } }
/** * @private */ void _internalUpdateTimerListValues(int incrementSinceLastCall) { timerList.working = true; if (timerList.size > 0) { int i = 0; for (i = 0; i < timerList.size; i++) { Timer* currentTimer = getTimerByIndex(i); if (currentTimer == NULL) { writeError(TIMER_NULL); timerList.working = false; return; } bool enabled = currentTimer->enabled; if (!enabled) { continue; } // increments the counter and test if it is > to the timeDiviser currentTimer->timeInternalCounter += incrementSinceLastCall; if (currentTimer->timeInternalCounter >= currentTimer->timeDiviser) { // block the timer if we wait for bool lock = currentTimer->lock; if (lock) { continue; } // we only subtract and not clear to 0, so that, if the timer is locked, we will not forget // any firing currentTimer->timeInternalCounter -= currentTimer->timeDiviser; currentTimer->time++; // lock the timer to avoid concurrence problem currentTimer->working = true; if (currentTimer->callback) { currentTimer->callback(currentTimer); } // indicates the timer is not working currentTimer->working = false; } } } timerList.working = false; }
void deviceTimerHandleRawData(char commandHeader, InputStream* inputStream, OutputStream* outputStream) { if (commandHeader == COMMAND_TIMER_LIST) { printTimerList(getInfoOutputStreamLogger(), getTimerList()); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_LIST); } else if (commandHeader == COMMAND_TIMER_COUNT) { ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_COUNT); unsigned timerCount = getTimerCount(); appendHex2(outputStream, timerCount); } else if (commandHeader == COMMAND_TIMER_READ) { ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_READ); unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); appendHex2(outputStream, timerIndex); appendSeparator(outputStream); appendHex2(outputStream, timer->timerCode); appendSeparator(outputStream); appendHex4(outputStream, timer->timeDiviser); appendSeparator(outputStream); appendHex4(outputStream, timer->timeInternalCounter); appendSeparator(outputStream); appendHex6(outputStream, timer->time); appendSeparator(outputStream); appendHex6(outputStream, timer->markTime); appendSeparator(outputStream); appendBool(outputStream, timer->enabled); } // Enable / Tisable else if (commandHeader == COMMAND_TIMER_ENABLE_DISABLE) { unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); checkIsSeparator(inputStream); unsigned enableAsChar = readHex(inputStream); bool enabled = enableAsChar != 0; timer->enabled = enabled; ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_ENABLE_DISABLE); } // Mark else if (commandHeader == COMMAND_TIMER_MARK) { unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); unsigned long time = markTimer(timer); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_MARK); appendHex6(outputStream, time); } else if (commandHeader == COMMAND_TIMER_TIME_SINCE_LAST_MARK) { unsigned char timerIndex = readHex2(inputStream); Timer* timer = getTimerByIndex(timerIndex); unsigned long value = getTimeSinceLastMark(timer); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_TIME_SINCE_LAST_MARK); appendHex6(outputStream, value); } else if (commandHeader == COMMAND_TIMER_TIMEOUT) { unsigned char timerIndex = readHex2(inputStream); checkIsSeparator(inputStream); unsigned long timeToCheck = (unsigned long) readHex6(inputStream); Timer* timer = getTimerByIndex(timerIndex); bool value = timeout(timer, timeToCheck); ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_TIMEOUT); appendHex2(outputStream, timerIndex); appendSeparator(outputStream); appendBool(outputStream, value); } // Demo else if (commandHeader == COMMAND_TIMER_DEMO) { Timer* timer = getTimerByCode(DEMO_TIMER_INDEX); if (timer == NULL) { timer = addTimerDemo(); } // Timer could be null when adding the timerDemo because of limit, we don't want any crash ! if (timer != NULL) { unsigned enableAsChar = readHex(inputStream); bool enabled = enableAsChar != 0; timer->enabled = enabled; } ackCommand(outputStream, TIMER_DEVICE_HEADER, COMMAND_TIMER_DEMO); } }