Added functionality to align display refreshes

This commit is contained in:
2026-06-30 16:40:10 +03:00
parent 9e00821802
commit 30922ca93c
5 changed files with 102 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
#include "TRMNL.h"
#include "helpers.h"
#include <time.h>
using std::list;
using std::map;
using std::optional;
@@ -11,11 +13,13 @@ using nlohmann::json;
TRMNL::TRMNL(const string& id,
const string& api_key,
const string& friendly_id,
int refresh_rate)
int refresh_rate,
bool is_refresh_aligned)
: m_id(id),
m_api_key(api_key),
m_friendly_id(friendly_id),
m_refresh_rate(refresh_rate),
m_is_refresh_aligned(is_refresh_aligned),
m_update_handler()
{}
@@ -52,6 +56,65 @@ int TRMNL::refresh_rate() const
return m_refresh_rate;
}
bool TRMNL::is_refresh_aligned() const
{
return m_is_refresh_aligned;
}
int TRMNL::get_next_refresh(int min_time, int offset) const
{
if (!m_is_refresh_aligned)
{
return m_refresh_rate;
}
if (min_time > m_refresh_rate)
{
min_time = m_refresh_rate;
}
int result = 0;
time_t curr = time(nullptr);
struct tm* now = localtime(&curr);
int tmp_refresh = m_refresh_rate;
bool align_minutes = false;
bool align_hours = false;
if (0 == m_refresh_rate % 60)
{
align_minutes = true;
tmp_refresh /= 60;
if (0 == tmp_refresh % 60)
{
align_hours = true;
tmp_refresh /= 60;
}
}
if (align_minutes)
{
now->tm_sec = 0;
now->tm_min += 1;
}
if (align_hours)
{
now->tm_min = 0;
now->tm_hour += 1;
}
time_t next = mktime(now);
result = next - curr + offset;
while (result < min_time)
{
result += m_refresh_rate;
}
return result;
}
void TRMNL::id(const std::string& id)
{
m_id = id;
@@ -88,6 +151,15 @@ void TRMNL::refresh_rate(int refresh_rate)
}
}
void TRMNL::is_refresh_aligned(bool to_align)
{
m_is_refresh_aligned = to_align;
if (m_update_handler)
{
m_update_handler(*this);
}
}
void TRMNL::set_update_handler(std::function<void (const TRMNL& trmnl)> handler)
{
m_update_handler = handler;