example/heartbeat: Refactor
This cleans up the clock and time source declarations by defining the
ClockId and Timer types up front.
Tested:
Built the example and it runs as expected.
Change-Id: I67326f5804f8eea54624150f6aa5d735bbde05fd
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/example/heartbeat.cpp b/example/heartbeat.cpp
index 1840f60..c4adaac 100644
--- a/example/heartbeat.cpp
+++ b/example/heartbeat.cpp
@@ -6,10 +6,12 @@
#include <string>
#include <utility>
-using sdeventplus::Clock;
-using sdeventplus::ClockId;
using sdeventplus::Event;
-using sdeventplus::source::Time;
+using sdeventplus::source::Enabled;
+
+constexpr auto clockId = sdeventplus::ClockId::RealTime;
+using Clock = sdeventplus::Clock<clockId>;
+using Time = sdeventplus::source::Time<clockId>;
int main(int argc, char* argv[])
{
@@ -23,13 +25,16 @@
fprintf(stderr, "Beating every %u seconds\n", interval);
auto event = Event::get_default();
- auto hbFunc = [interval](Time<ClockId::RealTime>& source,
- Time<ClockId::RealTime>::TimePoint time) {
+ auto hbFunc = [interval](Time& source, Time::TimePoint time) {
printf("Beat\n");
+
+ // Time sources are oneshot and are based on an absolute time
+ // we need to reconfigure the time source to go off again after the
+ // configured interval and re-enable it.
source.set_time(time + std::chrono::seconds{interval});
+ source.set_enabled(Enabled::OneShot);
};
- Time<ClockId::RealTime> time(event, Clock<ClockId::RealTime>(event).now(),
- std::chrono::seconds{1}, std::move(hbFunc));
- time.set_enabled(sdeventplus::source::Enabled::On);
+ Time time(event, Clock(event).now(), std::chrono::seconds{1},
+ std::move(hbFunc));
return event.loop();
}