From 7ba44d1c0505325f49eed35cb2937129d497fa4d Mon Sep 17 00:00:00 2001 From: midilab Date: Mon, 4 Dec 2017 08:47:46 -0200 Subject: [PATCH] Added elapsed time support for seconds, minutes, hours and days. // elapsed time support uint8_t getNumberOfSeconds(uint32_t time); uint8_t getNumberOfMinutes(uint32_t time); uint8_t getNumberOfHours(uint32_t time); uint8_t getNumberOfDays(uint32_t time); uint32_t uClockClass::getNowTimer(); --- src/uClock.cpp | 32 ++++++++++++++++++++++++++++++++ src/uClock.h | 14 ++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/uClock.cpp b/src/uClock.cpp index 2773f9f..7b0ca8f 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -87,6 +87,8 @@ static uint32_t phase_mult(uint32_t val) void uClockClass::start() { + start_timer = millis(); + if (mode == INTERNAL_CLOCK) { state = STARTED; mod6_counter = 0; @@ -286,18 +288,48 @@ void uClockClass::handleTimerInt() } } + +// elapsed time support +uint8_t uClockClass::getNumberOfSeconds(uint32_t time) +{ + return ((millis() - time) / 1000) % SECS_PER_MIN; +} + +uint8_t uClockClass::getNumberOfMinutes(uint32_t time) +{ + return (((millis() - time) / 1000) / SECS_PER_MIN) % SECS_PER_MIN; +} + +uint8_t uClockClass::getNumberOfHours(uint32_t time) +{ + return (((millis() - time) / 1000) % SECS_PER_DAY) / SECS_PER_HOUR; +} + +uint8_t uClockClass::getNumberOfDays(uint32_t time) +{ + return ((millis() - time) / 1000) / SECS_PER_DAY; +} + +uint32_t uClockClass::getNowTimer() +{ + return _timer; +} } } // end namespace umodular::clock umodular::clock::uClockClass uClock; volatile uint16_t _clock = 0; +volatile uint32_t _timer = 0; // // TIMER1 HANDLER INTERRUPT // ISR(TIMER1_OVF_vect) { + // global timer counter + _timer = millis(); + if (uClock.state == uClock.STARTED) { _clock++; uClock.handleTimerInt(); diff --git a/src/uClock.h b/src/uClock.h index 220f78f..ab89487 100755 --- a/src/uClock.h +++ b/src/uClock.h @@ -33,6 +33,10 @@ #include #include +#define SECS_PER_MIN (60UL) +#define SECS_PER_HOUR (3600UL) +#define SECS_PER_DAY (SECS_PER_HOUR * 24L) + namespace umodular { namespace clock { class uClockClass { @@ -57,6 +61,7 @@ class uClockClass { uint32_t indiv96th_counter; uint16_t pll_x; uint16_t tempo; + uint32_t start_timer; enum { PAUSED = 0, @@ -108,6 +113,14 @@ class uClockClass { void shuffle(); void tap(); + + // elapsed time support + uint8_t getNumberOfSeconds(uint32_t time); + uint8_t getNumberOfMinutes(uint32_t time); + uint8_t getNumberOfHours(uint32_t time); + uint8_t getNumberOfDays(uint32_t time); + uint32_t uClockClass::getNowTimer(); + }; } } // end namespace umodular::clock @@ -116,6 +129,7 @@ extern umodular::clock::uClockClass uClock; extern "C" { extern volatile uint16_t _clock; +extern volatile uint32_t _timer; } #endif /* __U_CLOCK_H__ */