#include #include "fastclock.h" void Fastclock::setClockSpeed(unsigned int _msPerModelSecond) { msPerModelSecond = _msPerModelSecond; char speedString[10]; snprintf(speedString, 6, "1:%1.1f", 1000.0 / msPerModelSecond); display->setClockSpeed(speedString); } void Fastclock::incrementClockByMilliseconds(int amount) { millisecond += amount; if (millisecond >= 1000) { unsigned int carryover = millisecond / 1000; millisecond = millisecond % 1000; second += carryover; if (second >= 60) { carryover = second / 60; second = second % 60; minute += carryover; if (minute >= 60) { carryover = minute / 60; minute = minute % 60; hour += carryover; if (hour >= 24) { carryover = hour / 24; hour = hour % 24; weekday += carryover; if (weekday >= 7) { weekday = weekday % 7; } } } } } /* char timeString[51]; snprintf(timeString, 50, "%02d:%02d:%02d.%03d day %d, incBy_ms=%d", hour, minute, second, millisecond, weekday, amount); Serial.print("*** new clock: "); Serial.println(timeString); */ } static const char *weekdayString[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; void Fastclock::loop(void) { long newTimeTick = millis(); uint8_t oldMinute, oldDay; if (msPerModelSecond == 0) { Serial.println("Model speed invalid, msPerModelSecond=0"); msPerModelSecond = 500; } if (newTimeTick - lastTimeTickUsed < MIN_TIME_ms_BETWEEN_CLOCK_UPDATES) return; if (halted) { lastTimeTickUsed = newTimeTick; return; } int fastclockTimeAdvance = (newTimeTick - lastTimeTickUsed) * 1000 / msPerModelSecond; oldMinute = minute; oldDay = weekday; incrementClockByMilliseconds(fastclockTimeAdvance); lastTimeTickUsed = newTimeTick; if (oldMinute != minute) { Serial.print("*** minute change, tick adv="); Serial.println(fastclockTimeAdvance); display->setTime(hour, minute); if (oldDay != weekday) display->setClockWeekday(weekdayString[weekday]); // time has changed, send an update via rf // @TODO implement sending radio message Serial.println("Would send new time"); } } void Fastclock::begin(void) { lastTimeTickUsed = millis(); Serial.print("*** Setting up Fastclock, init lastSentTimeTick="); Serial.println(lastTimeTickUsed); // following setters do not change anything, but they update the display as well setClockSpeed(msPerModelSecond); display->setClockHalted(halted); display->setTime(hour, minute); }