-
Notifications
You must be signed in to change notification settings - Fork 4
Example Timer
ANYKS edited this page Sep 1, 2025
·
8 revisions
#include <chrono>
#include <awh/core/timer.hpp>
using namespace awh;
using namespace placeholders;
class Executor {
private:
chrono::time_point <chrono::system_clock> _ts;
chrono::time_point <chrono::system_clock> _is;
private:
uint16_t _count;
private:
log_t * _log;
public:
void interval(const uint16_t tid, awh::timer_t * timer){
auto shift = chrono::system_clock::now();
this->_log->print("Interval: %u seconds", log_t::flag_t::INFO, chrono::duration_cast <chrono::seconds> (shift - this->_is).count());
this->_is = shift;
if((this->_count++) >= 10){
timer->clear(tid);
timer->stop();
}
}
void timeout([[maybe_unused]] const uint16_t id){
this->_log->print("Timeout: %u seconds", log_t::flag_t::INFO, chrono::duration_cast <chrono::seconds> (chrono::system_clock::now() - this->_ts).count());
}
void launched(const awh::core_t::status_t status, awh::timer_t * timer){
switch(static_cast <uint8_t> (status)){
case static_cast <uint8_t> (awh::core_t::status_t::START): {
this->_ts = chrono::system_clock::now();
this->_is = this->_ts;
this->_log->print("%s", log_t::flag_t::INFO, "Start timer");
uint16_t tid = timer->timeout(10000);
timer->on(tid, &Executor::timeout, this, tid);
tid = timer->interval(5000);
timer->on(tid, &Executor::interval, this, tid, timer);
} break;
case static_cast <uint8_t> (awh::core_t::status_t::STOP):
this->_log->print("%s", log_t::flag_t::INFO, "Stop timer");
break;
}
}
public:
Executor(log_t * log) : _ts(chrono::system_clock::now()), _is(chrono::system_clock::now()), _count(0), _log(log) {}
};
int32_t main(int32_t argc, char * argv[]){
fmk_t fmk;
log_t log(&fmk);
Executor executor(&log);
awh::timer_t timer(&fmk, &log);
log.name("Timer");
log.format("%H:%M:%S %d.%m.%Y");
dynamic_cast <awh::core_t &> (timer).on <void (const awh::core_t::status_t)> ("status", &Executor::launched, &executor, _1, &timer);
timer.start();
return EXIT_SUCCESS;
}copyright © ANYKS