Skip to content

Commit

Permalink
leds: add update_timeout to refresh output when idle
Browse files Browse the repository at this point in the history
  • Loading branch information
SpComb committed Oct 21, 2023
1 parent 307b647 commit 3b78c90
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions main/leds_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ int leds_cmd_stats(int argc, char **argv, void *ctx)
print_stats_timer("task", "update", &stats->update);
printf("\n");
print_stats_counter("artnet", "timeout", &stats->artnet_timeout);
print_stats_counter("update", "timeout", &stats->update_timeout);
printf("\n");
}

Expand Down
2 changes: 2 additions & 0 deletions main/leds_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ struct leds_config {
unsigned gpio_count;
#endif

uint16_t update_timeout;

bool test_enabled;
int test_mode;
bool test_auto;
Expand Down
5 changes: 5 additions & 0 deletions main/leds_configtab.i
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ const struct configtab LEDS_CONFIGTAB[] = {
},
#endif

{ CONFIG_TYPE_UINT16, "update_timeout",
.description = "Update LED outputs after given milliseconds without any updates. Default 0 -> hold output idle.",
.uint16_type = { .value = &LEDS_CONFIG.update_timeout },
},

{ CONFIG_TYPE_BOOL, "test_enabled",
.description = "Enable test pattern output, active when TEST button pressed",
.bool_type = { .value = &LEDS_CONFIG.test_enabled, .default_value = true },
Expand Down
1 change: 1 addition & 0 deletions main/leds_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct leds_state {
const struct leds_config *config;

struct leds *leds;
TickType_t update_tick;

xTaskHandle task;
EventGroupHandle_t event_group;
Expand Down
1 change: 1 addition & 0 deletions main/leds_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ void init_leds_stats()
stats_timer_init(&stats->update);

stats_counter_init(&stats->artnet_timeout);
stats_counter_init(&stats->update_timeout);
}
}
1 change: 1 addition & 0 deletions main/leds_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct leds_stats {
struct stats_timer sequence;

struct stats_timer update;
struct stats_counter update_timeout;
};

extern struct leds_sequence_stats leds_sequence_stats;
Expand Down
42 changes: 42 additions & 0 deletions main/leds_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,45 @@

#include <logging.h>

static TickType_t leds_update_wait(struct leds_state *state)
{
if (state->config->update_timeout && state->update_tick) {
// refresh valid date
return state->update_tick + state->config->update_timeout / portTICK_PERIOD_MS;
}

return 0;
}


static bool leds_update_active(struct leds_state *state)
{
// not configured
if (!state->config->update_timeout) {
return false;
}

// not initialized
if (!state->update_tick) {
return false;
}

return xTaskGetTickCount() >= state->update_tick + state->config->update_timeout / portTICK_PERIOD_MS;
}

static EventBits_t leds_task_wait(struct leds_state *state)
{
TickType_t tick;

// first tick to wait until
TickType_t wait_tick = portMAX_DELAY;

if ((tick = leds_update_wait(state))) {
if (tick < wait_tick) {
wait_tick = tick;
}
}

if (state->test && (tick = leds_test_wait(state))) {
if (tick < wait_tick) {
wait_tick = tick;
Expand Down Expand Up @@ -97,7 +129,17 @@ static void leds_main(void *ctx)
}
}

if (leds_update_active(state)) {
LOG_DEBUG("update timeout");

stats_counter_increment(&stats->update_timeout);

update = true;
}

if (update) {
state->update_tick = xTaskGetTickCount();

WITH_STATS_TIMER(&stats->update) {
if (update_leds(state)) {
LOG_WARN("leds%d: update_leds", state->index + 1);
Expand Down

0 comments on commit 3b78c90

Please sign in to comment.