blob: c4adaaca8914d3259a5a042138ff853960a3ca6e [file] [log] [blame]
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07001#include <chrono>
2#include <cstdio>
3#include <sdeventplus/clock.hpp>
4#include <sdeventplus/event.hpp>
5#include <sdeventplus/source/time.hpp>
6#include <string>
7#include <utility>
8
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07009using sdeventplus::Event;
William A. Kennington III406b86e2018-09-25 15:23:00 -070010using sdeventplus::source::Enabled;
11
12constexpr auto clockId = sdeventplus::ClockId::RealTime;
13using Clock = sdeventplus::Clock<clockId>;
14using Time = sdeventplus::source::Time<clockId>;
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070015
16int main(int argc, char* argv[])
17{
18 if (argc != 2)
19 {
20 fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
21 return 1;
22 }
23
24 unsigned interval = std::stoul(argv[1]);
25 fprintf(stderr, "Beating every %u seconds\n", interval);
26
27 auto event = Event::get_default();
William A. Kennington III406b86e2018-09-25 15:23:00 -070028 auto hbFunc = [interval](Time& source, Time::TimePoint time) {
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070029 printf("Beat\n");
William A. Kennington III406b86e2018-09-25 15:23:00 -070030
31 // Time sources are oneshot and are based on an absolute time
32 // we need to reconfigure the time source to go off again after the
33 // configured interval and re-enable it.
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070034 source.set_time(time + std::chrono::seconds{interval});
William A. Kennington III406b86e2018-09-25 15:23:00 -070035 source.set_enabled(Enabled::OneShot);
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070036 };
William A. Kennington III406b86e2018-09-25 15:23:00 -070037 Time time(event, Clock(event).now(), std::chrono::seconds{1},
38 std::move(hbFunc));
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070039 return event.loop();
40}