From ca8685e62d6e2f091190e9b9d8a43ff9d9737e69 Mon Sep 17 00:00:00 2001 From: Advaith Menon Date: Tue, 18 Nov 2025 17:10:45 -0500 Subject: [PATCH] Add clock AM/PM display code, fix ghost arms and phase offset * Fix the problem of ghost arms/arms not being cleared out * Fix the issue wherein the angles are offset pi/2 from where they should be * Add digital time indicator which partially works (says 00 instead of 12) * Make the font "SMART WATCH OS" bold * Reduce sampling period to 500 ms to allow regular 1s updates with no skips * Add slower_baud to allow reducing baud rate back to 9600 * Allow lcd_clock_draw_hand to accept color --- main/task_lcd.c | 75 +++++++++++++++++++++++++++++++++++++++++-------- main/task_lcd.h | 2 ++ 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/main/task_lcd.c b/main/task_lcd.c index 496cbf7..933bf8f 100644 --- a/main/task_lcd.c +++ b/main/task_lcd.c @@ -15,7 +15,8 @@ const uart_port_t uart_num = UART_NUM_1; void lcd_clock_draw_face(gl_display_t *); -void lcd_clock_draw_hand(gl_display_t *, uint16_t, uint16_t, uint8_t); +void lcd_clock_draw_hand(gl_display_t *, uint16_t, uint16_t, uint8_t, gl_word_t); +gl_word_t txt_strw(gl_display_t *, char *); int write_tx(const void *data, size_t sz) { @@ -55,6 +56,22 @@ void faster_baud() { ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); } + +void slower_baud() { + uart_config_t uart_config = { + .baud_rate = 9600, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS, + .rx_flow_ctrl_thresh = 10, + }; + // Configure UART parameters + ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); +} + +/* ================================================================= */ + void lcd_task(void) { // Setup UART buffered IO with event queue const int uart_buffer_size = UART_HW_FIFO_LEN(uart_num) + 4; @@ -130,11 +147,14 @@ void lcd_boot_flash(gl_display_t *disp) { gl_txt_FGcolour(disp, BLACK); gl_txt_BGcolour(disp, WHITE); gl_txt_MoveCursor(disp, 6, 10); + gl_txt_Bold(disp, 1); gl_putstr(disp, (uint8_t *) "SMART"); gl_txt_MoveCursor(disp, 7, 10); + gl_txt_Bold(disp, 1); gl_putstr(disp, (uint8_t *) "WATCH"); gl_txt_MoveCursor(disp, 8, 10); - gl_putstr(disp, (uint8_t *) "OS (v1)"); + gl_txt_Bold(disp, 1); + gl_putstr(disp, (uint8_t *) "OS v0.1"); gl_txt_MoveCursor(disp, 10, 10); gl_putstr(disp, (uint8_t *) "Anish &"); gl_txt_MoveCursor(disp, 11, 10); @@ -144,20 +164,39 @@ void lcd_boot_flash(gl_display_t *disp) { void lcd_clock_loop(gl_display_t *disp) { - gl_gfx_Cls(disp); gl_gfx_BGcolour(disp, BLACK); + gl_gfx_Cls(disp); lcd_clock_draw_face(disp); + gl_txt_FGcolour(disp, DARKGREEN); + gl_txt_BGcolour(disp, ALICEBLUE); while (1) { time_t tm = time(NULL); - char txt[10]; - snprintf(txt, 10, "%lld", (long long) tm); + char txt[12]; + // snprintf(txt, 10, "%lld", (long long) tm); + snprintf(txt, 12, "%02lld:%02lld %s", + tm / (60 * 60) % 12, + tm / 60 % 60, + (tm / (60 * 60) % 24 > 12) ? "PM" : "AM"); + gl_txt_Italic(disp, 1); + uint16_t chw = txt_strw(disp, txt); + gl_gfx_MoveTo(disp, (LCD_WIDTH / 2) - (chw / 2), (LCD_HEIGHT / 2) + + (CLOCKSCR_CIRCRAD / 2)); + gl_putstr(disp, (unsigned char *) txt); // gl_txt_MoveCursor(disp, 1, 0); // gl_putstr(disp, (uint8_t *) txt); - lcd_clock_draw_hand(disp, tm % 60, 60, CLOCKSCR_TIMEHDSE); - lcd_clock_draw_hand(disp, tm % (60 * 60), (60 * 60), CLOCKSCR_TIMEHDMN); + lcd_clock_draw_hand(disp, (tm + 60 - 1) % 60, + 60, CLOCKSCR_TIMEHDSE, ALICEBLUE); + lcd_clock_draw_hand(disp, (tm + (60 * 60) - 1) % (60 * 60), + (60 * 60), CLOCKSCR_TIMEHDMN, + ALICEBLUE); + lcd_clock_draw_hand(disp, (tm + (60 * 60 * 12) - 1) % (60 * 60 * 12), + (60 * 60 * 12), CLOCKSCR_TIMEHDHR, ALICEBLUE); + lcd_clock_draw_hand(disp, tm % 60, 60, CLOCKSCR_TIMEHDSE, RED); + lcd_clock_draw_hand(disp, tm % (60 * 60), (60 * 60), CLOCKSCR_TIMEHDMN, + BLACK); lcd_clock_draw_hand(disp, tm % (60 * 60 * 12), - (60 * 60 * 12), CLOCKSCR_TIMEHDHR); - vTaskDelay(pdMS_TO_TICKS(1000)); + (60 * 60 * 12), CLOCKSCR_TIMEHDHR, BLACK); + vTaskDelay(pdMS_TO_TICKS(500)); } } @@ -216,10 +255,12 @@ void lcd_clock_draw_face(gl_display_t *disp) { } -void lcd_clock_draw_hand(gl_display_t *disp, uint16_t x, uint16_t n, uint8_t l) { +void lcd_clock_draw_hand(gl_display_t *disp, uint16_t x, uint16_t n, uint8_t l, + gl_word_t color) { /* x = current (minute/second etc., n = total, l = length of hand */ - double angle = (double) x * M_PI * 2 / n; + /* offset to center of clock */ + double angle = ((double) x * M_PI * 2 / n) - M_PI_2; /* full 'x' tick end length */ int8_t fxe = cos(angle) * l; @@ -230,5 +271,15 @@ void lcd_clock_draw_hand(gl_display_t *disp, uint16_t x, uint16_t n, uint8_t l) 0 + (LCD_WIDTH / 2), 0 + (LCD_HEIGHT / 2), fxe + (LCD_WIDTH / 2), - fye + (LCD_HEIGHT / 2), BLACK); + fye + (LCD_HEIGHT / 2), color); +} + + +gl_word_t txt_strw(gl_display_t *disp, char *str) { + gl_word_t pxlw = 0; + while (*str) { + pxlw += gl_charwidth(disp, (unsigned char) *str); + str++; + } + return pxlw; } diff --git a/main/task_lcd.h b/main/task_lcd.h index cebf466..a6cde84 100644 --- a/main/task_lcd.h +++ b/main/task_lcd.h @@ -46,6 +46,8 @@ /* time hand (sec) length */ #define CLOCKSCR_TIMEHDSE 45 +/* digital clock spacing */ + typedef struct { uint8_t width; uint8_t height; -- 2.47.3