diff --git a/components/display/CMakeLists.txt b/components/display/CMakeLists.txt index ebf0aa8..32fa1a8 100644 --- a/components/display/CMakeLists.txt +++ b/components/display/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( - SRCS "display_driver.c" + SRCS "display.c" "display_driver.c" INCLUDE_DIRS "." - REQUIRES driver fatal + REQUIRES driver fatal esp_timer time ) diff --git a/components/display/display.c b/components/display/display.c new file mode 100644 index 0000000..22f8424 --- /dev/null +++ b/components/display/display.c @@ -0,0 +1,107 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#include "display.h" + +#include "display_driver.h" +#include "fatal.h" +#include "time.h" + +#include "esp_log.h" +#include "esp_timer.h" +#include + +#define TAG "Display" + +#define DRIVER_TASK_PERIOD_US 4000UL +#define TIME_UPDATE_PERIOD_US 1000000UL + +#define CLAMP(x, lim) ((x) > (lim) ? (lim) : (x)) + +const DisplayDigitState digit_encodings[10] = { + [0] = { true, true, true, true, true, true, false, false }, + [1] = { false, true, true, false, false, false, false, false }, + [2] = { true, true, false, true, true, false, true, false }, + [3] = { true, true, true, true, false, false, true, false }, + [4] = { false, true, true, false, false, true, true, false }, + [5] = { true, false, true, true, false, true, true, false }, + [6] = { true, false, true, true, true, true, true, false }, + [7] = { true, true, true, false, false, false, false, false }, + [8] = { true, true, true, true, true, true, true, false }, + [9] = { true, true, true, false, false, true, true, false }, +}; + +static void show_digit(DisplayDigit digit, unsigned value) +{ + memcpy( + &display_state[digit], digit_encodings[value], + sizeof(DisplayDigitState)); +} + +static void show_time(unsigned hour, unsigned minute) +{ + if (hour > 99 || minute > 99) + ESP_LOGW(TAG, "Un-displayable time: %02u:%02u", hour, minute); + + show_digit(DISPLAY_DIGIT_1, (hour / 10) % 10); + show_digit(DISPLAY_DIGIT_2, hour % 10); + + display_state[DISPLAY_DIGIT_2][DISPLAY_SEGMENT_DP] = true; + + show_digit(DISPLAY_DIGIT_3, (minute / 10) % 10); + show_digit(DISPLAY_DIGIT_4, minute % 10); +} + +static void update_time(void *arg) +{ + (void)arg; + const Time time = time_get(); + show_time(time.hour, time.minute); +} + +void display_init() +{ + esp_err_t error; + + display_driver_init(); + + esp_timer_handle_t driver_timer; + const esp_timer_create_args_t driver_timer_config = { + .callback = &display_driver_task, + .arg = NULL, + .name = "display driver task", + }; + error = esp_timer_create(&driver_timer_config, &driver_timer); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error creating timer for driver task: %04x", error); + FATAL(); + } + + error = esp_timer_start_periodic(driver_timer, DRIVER_TASK_PERIOD_US); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error starting timer for driver task: %04x", error); + FATAL(); + } + + esp_timer_handle_t update_timer; + const esp_timer_create_args_t update_timer_config = { + .callback = &update_time, + .arg = NULL, + .name = "display driver task", + }; + error = esp_timer_create(&update_timer_config, &update_timer); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error creating timer for driver task: %04x", error); + FATAL(); + } + + error = esp_timer_start_periodic(update_timer, TIME_UPDATE_PERIOD_US); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error starting timer for driver task: %04x", error); + FATAL(); + } + + update_time(NULL); +} diff --git a/components/display/display.h b/components/display/display.h new file mode 100644 index 0000000..3e9e5b8 --- /dev/null +++ b/components/display/display.h @@ -0,0 +1,17 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#ifndef DISPLAY_H +#define DISPLAY_H + +/** + * Initialize the display. + * + * This will configure the relevant GPIO pins and start a task that + * manages updates to the display. + */ +void display_init(void); + +#endif diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 86f52a4..f983e56 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "main.c" INCLUDE_DIRS "." - REQUIRES config + REQUIRES config display ) diff --git a/main/main.c b/main/main.c index e0cad45..c10eddf 100644 --- a/main/main.c +++ b/main/main.c @@ -4,8 +4,10 @@ */ #include "config.h" +#include "display.h" void app_main(void) { config_init(); + display_init(); }