Create display module top level

This commit is contained in:
Camden Dixie O'Brien 2023-05-15 16:57:41 +01:00
parent 81f5c6aaa0
commit be666164f4
5 changed files with 129 additions and 3 deletions

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "display_driver.c" SRCS "display.c" "display_driver.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES driver fatal REQUIRES driver fatal esp_timer time
) )

View File

@ -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 <string.h>
#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);
}

View File

@ -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

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "main.c" SRCS "main.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES config REQUIRES config display
) )

View File

@ -4,8 +4,10 @@
*/ */
#include "config.h" #include "config.h"
#include "display.h"
void app_main(void) void app_main(void)
{ {
config_init(); config_init();
display_init();
} }