From d411376f8efb34405e9f928e99fe996e2d11c390 Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Fri, 12 Mar 2021 20:01:47 +0100 Subject: [PATCH] add timer --- lib/TaskManager/Timer.cpp | 36 ++++++++++++++++++++++++++++++++++++ lib/TaskManager/Timer.h | 26 ++++++++++++++++++++++++++ lib/TimeLib/TimeLib.h | 9 +++++---- src/TaskAprsIs.cpp | 9 +++++---- src/TaskAprsIs.h | 3 ++- 5 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 lib/TaskManager/Timer.cpp create mode 100644 lib/TaskManager/Timer.h diff --git a/lib/TaskManager/Timer.cpp b/lib/TaskManager/Timer.cpp new file mode 100644 index 0000000..c89a105 --- /dev/null +++ b/lib/TaskManager/Timer.cpp @@ -0,0 +1,36 @@ +#include "Timer.h" + +Timer::Timer() + : _timeout_sec(0), _timeout(0) +{ +} + +void Timer::setTimeout(const time_t timeout_sec) +{ + _timeout_sec = timeout_sec; +} + +time_t Timer::getTriggerTime() const +{ + return _timeout; +} + +bool Timer::isActive() const +{ + return _timeout != 0; +} + +void Timer::reset() +{ + _timeout = 0; +} + +bool Timer::check() +{ + return now() > _timeout; +} + +void Timer::start() +{ + _timeout = now() + _timeout_sec; +} diff --git a/lib/TaskManager/Timer.h b/lib/TaskManager/Timer.h new file mode 100644 index 0000000..60f78e7 --- /dev/null +++ b/lib/TaskManager/Timer.h @@ -0,0 +1,26 @@ +#ifndef TIMER_H_ +#define TIMER_H_ + +#include + +class Timer +{ +public: + Timer(); + + void setTimeout(const time_t timeout_sec); + time_t getTriggerTime() const; + + bool isActive() const; + + void reset(); + + bool check(); + void start(); + +private: + time_t _timeout_sec; + time_t _timeout; +}; + +#endif diff --git a/lib/TimeLib/TimeLib.h b/lib/TimeLib/TimeLib.h index b624542..a5ffb24 100644 --- a/lib/TimeLib/TimeLib.h +++ b/lib/TimeLib/TimeLib.h @@ -11,6 +11,7 @@ #define _Time_h #include +#include typedef enum {timeNotSet, timeNeedsSync, timeSet } timeStatus_t ; @@ -70,10 +71,10 @@ typedef time_t(*getExternalTime)(); /* Useful Macros for converting elapsed time to a time_t */ -#define minutesToTime_t ((M)) ( (M) * SECS_PER_MIN) -#define hoursToTime_t ((H)) ( (H) * SECS_PER_HOUR) -#define daysToTime_t ((D)) ( (D) * SECS_PER_DAY) // fixed on Jul 22 2011 -#define weeksToTime_t ((W)) ( (W) * SECS_PER_WEEK) +#define minutesToTime_t(M) ((M) * SECS_PER_MIN) +#define hoursToTime_t(H) ((H) * SECS_PER_HOUR) +#define daysToTime_t(D) ((D) * SECS_PER_DAY) // fixed on Jul 22 2011 +#define weeksToTime_t(W) ((W) * SECS_PER_WEEK) /*============================================================================*/ /* time and date functions */ diff --git a/src/TaskAprsIs.cpp b/src/TaskAprsIs.cpp index 7d05e54..83be5ad 100644 --- a/src/TaskAprsIs.cpp +++ b/src/TaskAprsIs.cpp @@ -8,7 +8,7 @@ String create_lat_aprs(double lat); String create_long_aprs(double lng); AprsIsTask::AprsIsTask() - : Task(TASK_APRS_IS, TaskAprsIs), _beacon_next_time(0) + : Task(TASK_APRS_IS, TaskAprsIs) { } @@ -18,6 +18,7 @@ AprsIsTask::~AprsIsTask() bool AprsIsTask::setup(std::shared_ptr config, std::shared_ptr boardConfig) { + _beacon_timer.setTimeout(minutesToTime_t(config->beacon.timeout)); _aprs_is = std::shared_ptr(new APRS_IS(config->callsign, config->aprs_is.passcode , "ESP32-APRS-IS", "0.2")); _beaconMsg = std::shared_ptr(new APRSMessage()); @@ -53,15 +54,15 @@ bool AprsIsTask::loop(std::shared_ptr config) _aprs_is->sendMessage(msg); } - if(_beacon_next_time < now()) + if(_beacon_timer.check()) { logPrintD("[" + timeString() + "] "); logPrintlnD(_beaconMsg->encode()); _aprs_is->sendMessage(_beaconMsg); Display::instance().addFrame(std::shared_ptr(new TextFrame("BEACON", _beaconMsg->toString()))); - _beacon_next_time = now() + config->beacon.timeout * 60UL; + _beacon_timer.start(); } - time_t diff = _beacon_next_time - now(); + time_t diff = _beacon_timer.getTriggerTime() - now(); _stateInfo = "beacon " + String(minute(diff)) + ":" + String(second(diff)); _state = Okay; return true; diff --git a/src/TaskAprsIs.h b/src/TaskAprsIs.h index 4c99e0c..f26041f 100644 --- a/src/TaskAprsIs.h +++ b/src/TaskAprsIs.h @@ -4,6 +4,7 @@ #include #include #include +#include class AprsIsTask : public Task { @@ -19,7 +20,7 @@ public: private: std::shared_ptr _aprs_is; std::shared_ptr _beaconMsg; - time_t _beacon_next_time; + Timer _beacon_timer; bool connect(std::shared_ptr config); };