Skip to content

Commit 4d5cb7b

Browse files
committed
[timer] Fix time_point cast for short versions
1 parent 8546bcb commit 4d5cb7b

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

src/modm/processing/timer/periodic_timer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class GenericPeriodicTimer : public GenericTimeout<Clock, Duration>
5454
size_t count{0};
5555
if (this->_interval.count())
5656
{
57-
const typename Clock::time_point now = Clock::now();
57+
const auto now = this->now();
5858
while(1)
5959
{
6060
this->_start += this->_interval;
@@ -64,7 +64,7 @@ class GenericPeriodicTimer : public GenericTimeout<Clock, Duration>
6464
}
6565
}
6666
else {
67-
this->_start = Clock::now();
67+
this->_start = this->now();
6868
count = 1;
6969
}
7070
this->_state = this->ARMED;

src/modm/processing/timer/timeout.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class GenericTimeout
123123
bool
124124
checkExpiration() const;
125125

126+
time_point
127+
now() const;
128+
126129
enum
127130
InternalState : uint8_t
128131
{

src/modm/processing/timer/timeout_impl.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ modm::GenericTimeout<Clock, Duration>::restart(std::chrono::duration<Rep, Period
3838
modm_assert_continue_fail_debug(interval.count() <= duration::max().count(), "tmr.size",
3939
"Timer interval must be smaller than the maximal duration!", interval.count());
4040

41-
_start = std::chrono::time_point_cast<duration>(Clock::now());
41+
_start = now();
4242
_interval = cast_interval;
4343
_state = ARMED;
4444
}
@@ -83,7 +83,7 @@ modm::GenericTimeout<Clock, Duration>::remaining() const
8383
return wide_signed_duration{0};
8484
return std::chrono::duration_cast<wide_signed_duration>(_interval) +
8585
std::chrono::time_point_cast<wide_signed_duration>(_start) -
86-
std::chrono::time_point_cast<wide_signed_duration>(Clock::now());
86+
std::chrono::time_point_cast<wide_signed_duration>(now());
8787
}
8888

8989
// ----------------------------------------------------------------------------
@@ -124,5 +124,12 @@ template< class Clock, class Duration >
124124
bool
125125
modm::GenericTimeout<Clock, Duration>::checkExpiration() const
126126
{
127-
return (_state & ARMED) and (Clock::now() - _start) >= _interval;
127+
return (_state & ARMED) and (now() - _start) >= _interval;
128+
}
129+
130+
template< class Clock, class Duration >
131+
typename modm::GenericTimeout<Clock, Duration>::time_point
132+
modm::GenericTimeout<Clock, Duration>::now() const
133+
{
134+
return std::chrono::time_point_cast<duration>(Clock::now());
128135
}

test/modm/processing/timer/timeout_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ TimeoutTest::testTimeOverflow()
236236
timeoutShort.restart(time+1ms);
237237
TEST_ASSERT_EQUALS(timeoutShort.remaining(), 0ms);
238238
TEST_ASSERT_TRUE(timeoutShort.execute());
239+
240+
test_clock::setTime(-1000);
241+
timeoutShort.restart(500ms);
242+
TEST_ASSERT_EQUALS(timeoutShort.remaining(), 500ms);
243+
TEST_ASSERT_FALSE(timeoutShort.execute());
239244
}
240245

241246
void

0 commit comments

Comments
 (0)