108 lines
2.8 KiB
C
108 lines
2.8 KiB
C
/*
|
|
* 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_manager.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 = get_time();
|
|
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);
|
|
}
|