mirror of
https://github.com/lora-aprs/LoRa_APRS_iGate.git
synced 2026-02-27 18:14:16 +01:00
add timer
This commit is contained in:
parent
0b01014e50
commit
d411376f8e
36
lib/TaskManager/Timer.cpp
Normal file
36
lib/TaskManager/Timer.cpp
Normal file
|
|
@ -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;
|
||||
}
|
||||
26
lib/TaskManager/Timer.h
Normal file
26
lib/TaskManager/Timer.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef TIMER_H_
|
||||
#define TIMER_H_
|
||||
|
||||
#include <TimeLib.h>
|
||||
|
||||
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
|
||||
|
|
@ -11,6 +11,7 @@
|
|||
#define _Time_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
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 */
|
||||
|
|
|
|||
|
|
@ -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<Configuration> config, std::shared_ptr<BoardConfig> boardConfig)
|
||||
{
|
||||
_beacon_timer.setTimeout(minutesToTime_t(config->beacon.timeout));
|
||||
_aprs_is = std::shared_ptr<APRS_IS>(new APRS_IS(config->callsign, config->aprs_is.passcode , "ESP32-APRS-IS", "0.2"));
|
||||
|
||||
_beaconMsg = std::shared_ptr<APRSMessage>(new APRSMessage());
|
||||
|
|
@ -53,15 +54,15 @@ bool AprsIsTask::loop(std::shared_ptr<Configuration> 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<DisplayFrame>(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;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <TaskManager.h>
|
||||
#include <APRS-IS.h>
|
||||
#include <APRSMessage.h>
|
||||
#include <Timer.h>
|
||||
|
||||
class AprsIsTask : public Task
|
||||
{
|
||||
|
|
@ -19,7 +20,7 @@ public:
|
|||
private:
|
||||
std::shared_ptr<APRS_IS> _aprs_is;
|
||||
std::shared_ptr<APRSMessage> _beaconMsg;
|
||||
time_t _beacon_next_time;
|
||||
Timer _beacon_timer;
|
||||
|
||||
bool connect(std::shared_ptr<Configuration> config);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue