Skip to content

Commit

Permalink
2023.9.1-122: Change timestamp to milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjswan committed Nov 2, 2023
1 parent e4ba41d commit eacd9cb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 73 deletions.
127 changes: 67 additions & 60 deletions components/ehmtxv2/EHMTX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ namespace esphome
}
}

float get_tick()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000.0 + tv.tv_usec;
}

std::string get_icon_name(std::string iconname, char delim = '|')
{
std::stringstream stream(iconname);
Expand Down Expand Up @@ -203,8 +210,8 @@ namespace esphome

screen->text = "";
screen->mode = MODE_BITMAP_SCREEN;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process("bitmap", (uint8_t)screen->mode);
Expand All @@ -229,8 +236,8 @@ namespace esphome
screen->text_color = Color(r, g, b);
screen->mode = MODE_BITMAP_SMALL;
screen->default_font = default_font;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);

if (screen->sbitmap == NULL)
{
Expand Down Expand Up @@ -290,8 +297,8 @@ namespace esphome
screen->icon_name = id;
screen->mode = MODE_RAINBOW_BITMAP_SMALL;
screen->default_font = default_font;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);

if (screen->sbitmap == NULL)
{
Expand Down Expand Up @@ -647,7 +654,7 @@ namespace esphome
ESP_LOGD(TAG, "force_screen: found position: %d", i);
this->queue[i]->last_time = 0;
this->queue[i]->endtime += this->queue[i]->screen_time_;
this->next_action_time = this->clock->now().timestamp;
this->next_action_time = get_tick();
ESP_LOGW(TAG, "force_screen: icon %s in mode %d", icon_name.c_str(), mode);
}
}
Expand All @@ -657,7 +664,7 @@ namespace esphome
uint8_t EHMTX::find_oldest_queue_element()
{
uint8_t hit = MAXQUEUE;
time_t last_time = this->clock->now().timestamp;
float last_time = get_tick();
for (size_t i = 0; i < MAXQUEUE; i++)
{
if (this->night_mode)
Expand Down Expand Up @@ -695,7 +702,7 @@ namespace esphome
uint8_t hit = MAXQUEUE;
if (EHMTXv2_CLOCK_INTERVALL > 0)
{
time_t ts = this->clock->now().timestamp;
float ts = get_tick();
for (size_t i = 0; i < MAXQUEUE; i++)
{
if (this->night_mode)
Expand All @@ -716,7 +723,7 @@ namespace esphome

if ((this->queue[i]->mode == MODE_CLOCK) || (this->queue[i]->mode == MODE_RAINBOW_CLOCK) || (this->queue[i]->mode == MODE_ICON_CLOCK))
{
if (ts > (this->queue[i]->last_time + EHMTXv2_CLOCK_INTERVALL))
if (ts > (this->queue[i]->last_time + EHMTXv2_CLOCK_INTERVALL * 1000.0))
{
hit = i;
}
Expand All @@ -736,7 +743,7 @@ namespace esphome
if (this->clock->now().is_valid())
{
std::string infotext;
time_t ts = this->clock->now().timestamp;
float ts = get_tick();

for (size_t i = 0; i < MAXQUEUE; i++)
{
Expand Down Expand Up @@ -810,7 +817,7 @@ namespace esphome

uint8_t EHMTX::queue_count()
{
time_t ts = this->clock->now().timestamp;
float ts = get_tick();
uint8_t c = 0;
for (size_t i = 0; i < MAXQUEUE; i++)
{
Expand All @@ -836,7 +843,7 @@ namespace esphome

if (this->is_running && this->clock->now().is_valid())
{
time_t ts = this->clock->now().timestamp;
float ts = get_tick();

if (millis() - this->last_scroll_time >= EHMTXv2_SCROLL_INTERVALL)
{
Expand Down Expand Up @@ -919,12 +926,12 @@ namespace esphome

void EHMTX::skip_screen()
{
this->next_action_time = this->clock->now().timestamp - 1;
this->next_action_time = get_tick() - 1000.0;
}

void EHMTX::hold_screen(int time)
{
this->next_action_time = this->clock->now().timestamp + time;
this->next_action_time = get_tick() + time * 1000.0;
}

void EHMTX::get_status()
Expand Down Expand Up @@ -1056,15 +1063,15 @@ namespace esphome
ESP_LOGW(TAG, "del_screen: slot %d deleted", i);
this->queue[i]->mode = MODE_EMPTY;
this->queue[i]->endtime = 0;
this->queue[i]->last_time = this->clock->now().timestamp;
this->queue[i]->last_time = get_tick();
if (this->queue[i]->sbitmap != NULL)
{
delete [] this->queue[i]->sbitmap;
this->queue[i]->sbitmap = nullptr;
}
if (i == this->screen_pointer)
{
this->next_action_time = this->clock->now().timestamp;
this->next_action_time = get_tick();
}
}
}
Expand Down Expand Up @@ -1093,9 +1100,9 @@ namespace esphome
screen->mode = MODE_ALERT_SCREEN;
screen->icon_name = iconname;
screen->icon = icon;
screen->calc_scroll_time(text, screen_time);
screen->calc_scroll_time(text, screen_time * 1000);
// time needed for scrolling
screen->endtime = this->clock->now().timestamp + screen->screen_time_;
screen->endtime = get_tick() + screen->screen_time_;
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand Down Expand Up @@ -1130,8 +1137,8 @@ namespace esphome
screen->mode = MODE_ICON_SCREEN;
screen->icon_name = id;
screen->icon = icon;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand Down Expand Up @@ -1165,8 +1172,8 @@ namespace esphome
screen->icon_name = id;
screen->icon = icon;
screen->progress = (progress > 100) ? 100 : (progress < -100) ? -100 : progress;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand Down Expand Up @@ -1209,8 +1216,8 @@ namespace esphome
screen->mode = MODE_ICON_CLOCK;
screen->icon_name = id;
screen->icon = icon;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand Down Expand Up @@ -1242,8 +1249,8 @@ namespace esphome
screen->mode = MODE_ICON_DATE;
screen->icon_name = id;
screen->icon = icon;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand Down Expand Up @@ -1276,8 +1283,8 @@ namespace esphome
screen->mode = MODE_RAINBOW_ICON;
screen->icon_name = id;
screen->icon = icon;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand All @@ -1293,15 +1300,15 @@ namespace esphome
ESP_LOGD(TAG, "rainbow_clock_screen lifetime: %d screen_time: %d", lifetime, screen_time);
screen->mode = MODE_RAINBOW_CLOCK;
screen->default_font = default_font;
if (EHMTXv2_CLOCK_INTERVALL == 0 || (EHMTXv2_CLOCK_INTERVALL > screen_time))
if (EHMTXv2_CLOCK_INTERVALL == 0 || (EHMTXv2_CLOCK_INTERVALL * 1000.0 > screen_time * 1000.0))
{
screen->screen_time_ = screen_time;
screen->screen_time_ = screen_time * 1000.0;
}
else
{
screen->screen_time_ = EHMTXv2_CLOCK_INTERVALL - 2;
screen->screen_time_ = EHMTXv2_CLOCK_INTERVALL * 1000.0 - 2000.0;
}
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
screen->status();
}

Expand All @@ -1312,17 +1319,17 @@ namespace esphome

screen->mode = MODE_RAINBOW_DATE;
screen->default_font = default_font;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
screen->status();
}

void EHMTX::blank_screen(int lifetime, int showtime)
{
EHMTX_queue *screen = this->find_free_queue_element();
screen->mode = MODE_BLANK;
screen->screen_time_ = showtime;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = showtime * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process("blank",(uint8_t)screen->mode);
Expand All @@ -1334,9 +1341,9 @@ namespace esphome
{
EHMTX_queue *screen = this->find_free_queue_element();
screen->mode = MODE_COLOR;
screen->screen_time_ = showtime;
screen->screen_time_ = showtime * 1000.0;
screen->text_color = Color(r, g, b);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process("color",(uint8_t)screen->mode);
Expand All @@ -1352,8 +1359,8 @@ namespace esphome
screen->default_font = default_font;
screen->text_color = Color(r, g, b);
screen->mode = MODE_TEXT_SCREEN;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
screen->status();
}

Expand All @@ -1363,8 +1370,8 @@ namespace esphome
screen->text = text;
screen->default_font = default_font;
screen->mode = MODE_RAINBOW_TEXT;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
screen->status();
}

Expand All @@ -1373,8 +1380,8 @@ namespace esphome
EHMTX_queue *screen = this->find_mode_queue_element(MODE_FIRE);
screen->mode = MODE_FIRE;
screen->icon = 0;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process("Fire", (uint8_t)screen->mode);
Expand All @@ -1401,8 +1408,8 @@ namespace esphome
screen->mode = MODE_FULL_SCREEN;
screen->icon = icon;
screen->icon_name = iconname;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand All @@ -1418,8 +1425,8 @@ namespace esphome
ESP_LOGD(TAG, "clock_screen_color lifetime: %d screen_time: %d red: %d green: %d blue: %d", lifetime, screen_time, r, g, b);
screen->mode = MODE_CLOCK;
screen->default_font = default_font;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
screen->status();
}

Expand All @@ -1432,8 +1439,8 @@ namespace esphome

screen->mode = MODE_DATE;
screen->default_font = default_font;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
screen->status();
}

Expand Down Expand Up @@ -1461,8 +1468,8 @@ namespace esphome
screen->mode = MODE_ICON_TEXT_SCREEN;
screen->icon_name = id;
screen->icon = icon;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand Down Expand Up @@ -1494,8 +1501,8 @@ namespace esphome
screen->mode = MODE_RAINBOW_ICON_TEXT_SCREEN;
screen->icon_name = id;
screen->icon = icon;
screen->calc_scroll_time(text, screen_time);
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->calc_scroll_time(text, screen_time * 1000);
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);
for (auto *t : on_add_screen_triggers_)
{
t->process(screen->icon_name, (uint8_t)screen->mode);
Expand All @@ -1522,7 +1529,7 @@ namespace esphome

EHMTX_queue *EHMTX::find_free_queue_element()
{
time_t ts = this->clock->now().timestamp;
float ts = get_tick();
for (size_t i = 0; i < MAXQUEUE; i++)
{
if (this->queue[i]->endtime < ts)
Expand Down Expand Up @@ -1616,8 +1623,8 @@ namespace esphome

screen->mode = MODE_GRAPH_SCREEN;
screen->icon = MAXICONS;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);

this->graph->set_height(8);
this->graph->set_width(32);
Expand Down Expand Up @@ -1649,8 +1656,8 @@ namespace esphome
screen->mode = MODE_GRAPH_SCREEN;
screen->icon = icon;
screen->icon_name = iconname;
screen->screen_time_ = screen_time;
screen->endtime = this->clock->now().timestamp + (lifetime > 0 ? lifetime * 60 : screen->screen_time_);
screen->screen_time_ = screen_time * 1000.0;
screen->endtime = get_tick() + (lifetime > 0 ? lifetime * 60000.0 : screen->screen_time_);

this->graph->set_height(8);
this->graph->set_width(24);
Expand Down
Loading

0 comments on commit eacd9cb

Please sign in to comment.