example/heartbeat: Show a time example
diff --git a/.gitignore b/.gitignore
index 5a7271a..11b0674 100644
--- a/.gitignore
+++ b/.gitignore
@@ -42,6 +42,7 @@
# Output binaries
/example/follow
+/example/heartbeat
/test/clock
/test/event
/test/exception
diff --git a/example/Makefile.am b/example/Makefile.am
index fa40acb..7d4be73 100644
--- a/example/Makefile.am
+++ b/example/Makefile.am
@@ -6,4 +6,8 @@
follow_SOURCES = follow.cpp
follow_LDADD = $(SDEVENTPLUS_LIBS)
+noinst_PROGRAMS += heartbeat
+heartbeat_SOURCES = heartbeat.cpp
+heartbeat_LDADD = $(SDEVENTPLUS_LIBS)
+
endif
diff --git a/example/heartbeat.cpp b/example/heartbeat.cpp
new file mode 100644
index 0000000..e45ae89
--- /dev/null
+++ b/example/heartbeat.cpp
@@ -0,0 +1,35 @@
+#include <chrono>
+#include <cstdio>
+#include <sdeventplus/clock.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/time.hpp>
+#include <string>
+#include <utility>
+
+using sdeventplus::Clock;
+using sdeventplus::ClockId;
+using sdeventplus::Event;
+using sdeventplus::source::Time;
+
+int main(int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
+ return 1;
+ }
+
+ unsigned interval = std::stoul(argv[1]);
+ 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) {
+ printf("Beat\n");
+ source.set_time(time + std::chrono::seconds{interval});
+ };
+ Time<ClockId::RealTime> time(event, Clock<ClockId::RealTime>(event).now(),
+ std::chrono::seconds{1}, std::move(hbFunc));
+ time.set_enabled(SD_EVENT_ON);
+ return event.loop();
+}