Skip to content

Commit 6a6802a

Browse files
committed
[timers] Add wait method for fibers
1 parent d779cd8 commit 6a6802a

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/modm/processing/timer/periodic_timer.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ class GenericPeriodicTimer : public GenericTimeout<Clock, Duration>
7373
}
7474
return 0;
7575
}
76+
77+
/// Wait until the periodic timer expired.
78+
/// @warning This is a blocking call! Inside a fiber, this function yields.
79+
/// @return the number of missed periods
80+
size_t
81+
wait()
82+
{
83+
size_t count{};
84+
modm::this_fiber::poll([&]{ return (count = execute()); });
85+
return count;
86+
}
7687
};
7788

7889
/**

src/modm/processing/timer/timeout.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <modm/math/utils/arithmetic_traits.hpp>
2020
#include <modm/architecture/interface/clock.hpp>
2121
#include <modm/architecture/interface/assert.hpp>
22+
#include <modm/processing/fiber.hpp>
2223

2324
namespace modm
2425
{
@@ -108,6 +109,11 @@ class GenericTimeout
108109
bool
109110
execute();
110111

112+
/// Wait until the timeout expired.
113+
/// @warning This is a blocking call! Inside a fiber, this function yields.
114+
void
115+
wait();
116+
111117
protected:
112118
bool
113119
checkExpiration() const;

src/modm/processing/timer/timeout_impl.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ modm::GenericTimeout<Clock, Duration>::isExpired() const
126126
return state() == TimerState::Expired;
127127
}
128128

129+
template< class Clock, class Duration >
130+
void
131+
modm::GenericTimeout<Clock, Duration>::wait()
132+
{
133+
modm::this_fiber::poll([this]{ return not execute(); });
134+
}
135+
129136
// ----------------------------------------------------------------------------
130137
template< class Clock, class Duration >
131138
bool

0 commit comments

Comments
 (0)