IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
display_ui.c File Reference
#include "display_ui.h"
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "app_state.h"
#include "priv_i2c_u8g2.h"
#include "time_service.h"
+ Include dependency graph for display_ui.c:

Go to the source code of this file.

Macros

#define CONFIG_DISPLAY_UI_REFRESH_MS   1000
 

Functions

static void display_ui_draw_portal_screen (const app_state_snapshot_t *snapshot)
 
static void display_ui_draw_clock_screen (const app_state_snapshot_t *snapshot)
 
static void display_ui_task (void *arg)
 
esp_err_t display_ui_start (void)
 Initialize display backend and start the UI task.
 

Variables

static const char * TAG = "display_ui"
 
static i2c_u8g2_handle_t s_display_handle
 
static u8g2_t * s_u8g2
 

Macro Definition Documentation

◆ CONFIG_DISPLAY_UI_REFRESH_MS

#define CONFIG_DISPLAY_UI_REFRESH_MS   1000

Definition at line 21 of file display_ui.c.

Function Documentation

◆ display_ui_draw_clock_screen()

static void display_ui_draw_clock_screen ( const app_state_snapshot_t *  snapshot)
static

Definition at line 39 of file display_ui.c.

40{
41 struct tm local_time;
42 int64_t now_epoch = 0;
43 char time_line[16];
44 char status_line[20];
45 char alarm_indicator[10]; /* "HH:MM+" or "HH:MM", max 6 chars */
46 bool time_valid = time_service_get_local_time(&local_time, &now_epoch);
47 static uint32_t alarm_blink_counter = 0;
48
49 (void)now_epoch;
50
51 u8g2_ClearBuffer(s_u8g2);
52 u8g2_SetContrast(s_u8g2, snapshot->config.brightness);
53
54 if (snapshot->runtime.alarm_active) {
55 u8g2_SetFont(s_u8g2, u8g2_font_6x10_tf);
56 u8g2_DrawStr(s_u8g2, 20, 10, "ALARM ACTIVE!");
57
58 /* Blink the clock: show on even counter, hide on odd */
59 if ((alarm_blink_counter / 2) % 2 == 0) {
60 if (time_valid) {
61 snprintf(time_line, sizeof(time_line), "%02d:%02d:%02d",
62 local_time.tm_hour, local_time.tm_min, local_time.tm_sec);
63 u8g2_SetFont(s_u8g2, u8g2_font_logisoso20_tn);
64 u8g2_DrawStr(s_u8g2, 0, 38, time_line);
65 }
66 }
67
68 alarm_blink_counter++;
69 u8g2_SendBuffer(s_u8g2);
70 return;
71 }
72
73 alarm_blink_counter = 0;
74
75 /* ---- yellow zone (rows 0-15): status left, next alarm right ---- */
76
77 if (snapshot->runtime.portal_mode) {
78 snprintf(status_line, sizeof(status_line), "Setup AP");
79 } else if (snapshot->runtime.wifi_connected) {
80 snprintf(status_line, sizeof(status_line), "%s", snapshot->runtime.sta_ip);
81 } else {
82 snprintf(status_line, sizeof(status_line), "No WiFi");
83 }
84
85 alarm_indicator[0] = '\0';
86 {
87 int first_h = -1, first_m = -1;
88 int enabled_count = 0;
89 uint8_t i;
90
91 for (i = 0; i < snapshot->config.alarm_count && i < APP_ALARM_MAX_COUNT; i++) {
92 if (snapshot->config.alarms[i].enabled) {
93 if (first_h < 0) {
94 first_h = snapshot->config.alarms[i].hour;
95 first_m = snapshot->config.alarms[i].minute;
96 }
97 enabled_count++;
98 }
99 }
100
101 if (enabled_count == 1) {
102 snprintf(alarm_indicator, sizeof(alarm_indicator), "%02d:%02d", first_h, first_m);
103 } else if (enabled_count > 1) {
104 /* '+' suffix signals additional alarms beyond the first */
105 snprintf(alarm_indicator, sizeof(alarm_indicator), "%02d:%02d+", first_h, first_m);
106 }
107 }
108
109 u8g2_SetFont(s_u8g2, u8g2_font_6x10_tf);
110 u8g2_DrawStr(s_u8g2, 0, 10, status_line);
111 if (alarm_indicator[0] != '\0') {
112 u8g2_uint_t aw = u8g2_GetStrWidth(s_u8g2, alarm_indicator);
113 u8g2_DrawStr(s_u8g2, (u8g2_uint_t)(128u - aw), 10, alarm_indicator);
114 }
115
116 /* thin separator between the two physical colour zones */
117 u8g2_DrawHLine(s_u8g2, 0, 14, 128);
118
119 /* ---- blue zone (rows 17-63): large clock face ---- */
120 /* logisoso20 ascent ~20 px; baseline at 38 puts the top at ~18, well below the separator */
121 if (time_valid) {
122 snprintf(time_line, sizeof(time_line), "%02d:%02d:%02d",
123 local_time.tm_hour, local_time.tm_min, local_time.tm_sec);
124 u8g2_SetFont(s_u8g2, u8g2_font_logisoso24_tn);
125 u8g2_DrawStr(s_u8g2, 10, 48, time_line);
126 } else {
127 snprintf(time_line, sizeof(time_line), "TIME NOT SET");
128 u8g2_SetFont(s_u8g2, u8g2_font_6x10_tf);
129 u8g2_DrawStr(s_u8g2, 28, 30, time_line);
130 }
131
132 u8g2_SendBuffer(s_u8g2);
133}
#define APP_ALARM_MAX_COUNT
Maximum number of configurable alarms.
Definition app_state.h:26
static u8g2_t * s_u8g2
Definition display_ui.c:25
bool enabled
Definition app_state.h:34
uint8_t hour
Definition app_state.h:32
uint8_t minute
Definition app_state.h:33
uint8_t brightness
Definition app_state.h:50
alarm_entry_t alarms[APP_ALARM_MAX_COUNT]
Definition app_state.h:46
uint8_t alarm_count
Definition app_state.h:47
bool portal_mode
Definition app_state.h:59
bool alarm_active
Definition app_state.h:61
bool wifi_connected
Definition app_state.h:60
char sta_ip[APP_IP_ADDRESS_MAX_LEN]
Definition app_state.h:63
app_config_t config
Definition app_state.h:70
app_runtime_t runtime
Definition app_state.h:71
bool time_service_get_local_time(struct tm *out_tm, int64_t *out_epoch)
Get local time adjusted by the configured timezone offset.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ display_ui_draw_portal_screen()

static void display_ui_draw_portal_screen ( const app_state_snapshot_t *  snapshot)
static

Definition at line 27 of file display_ui.c.

28{
29 u8g2_ClearBuffer(s_u8g2);
30 u8g2_SetFont(s_u8g2, u8g2_font_6x10_tf);
31 u8g2_DrawStr(s_u8g2, 0, 10, "Provision WiFi");
32 u8g2_DrawStr(s_u8g2, 0, 22, snapshot->config.ap_ssid);
33 u8g2_DrawStr(s_u8g2, 0, 34, "Password:");
34 u8g2_DrawStr(s_u8g2, 0, 46, snapshot->config.ap_password);
35 u8g2_DrawStr(s_u8g2, 0, 58, "http://192.168.4.1");
36 u8g2_SendBuffer(s_u8g2);
37}
char ap_ssid[APP_AP_SSID_MAX_LEN+1]
Definition app_state.h:44
char ap_password[APP_AP_PASSWORD_MAX_LEN+1]
Definition app_state.h:45
+ Here is the caller graph for this function:

◆ display_ui_start()

esp_err_t display_ui_start ( void  )

Initialize display backend and start the UI task.

Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 154 of file display_ui.c.

155{
157 esp_err_t ret = i2c_u8g2_init(&s_display_handle, &config);
158 if (ret != ESP_OK) {
159 ESP_LOGE(TAG, "Display init failed (%s)", esp_err_to_name(ret));
160 return ret;
161 }
162
164 if (s_u8g2 == NULL) {
165 return ESP_FAIL;
166 }
167
168 BaseType_t task_ok = xTaskCreate(display_ui_task, "display_ui", 4096, NULL, 4, NULL);
169 return (task_ok == pdPASS) ? ESP_OK : ESP_FAIL;
170}
static void display_ui_task(void *arg)
Definition display_ui.c:135
static const char * TAG
Definition display_ui.c:18
static i2c_u8g2_handle_t s_display_handle
Definition display_ui.c:24
i2c_u8g2_config_t i2c_u8g2_config_default(void)
Fill a config with menuconfig-backed defaults.
u8g2_t * i2c_u8g2_get_u8g2(i2c_u8g2_handle_t *handle)
Get the U8G2 object for draw operations.
esp_err_t i2c_u8g2_init(i2c_u8g2_handle_t *handle, const i2c_u8g2_config_t *config)
Initialize I2C bus and U8G2 display.
Configuration for I2C-backed U8G2 display initialization.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ display_ui_task()

static void display_ui_task ( void *  arg)
static

Definition at line 135 of file display_ui.c.

136{
137 (void)arg;
138
139 while (true) {
140 app_state_snapshot_t snapshot;
141
142 app_state_get_snapshot(&snapshot);
143
144 if (snapshot.runtime.portal_mode) {
146 } else {
148 }
149
150 vTaskDelay(pdMS_TO_TICKS(CONFIG_DISPLAY_UI_REFRESH_MS));
151 }
152}
void app_state_get_snapshot(app_state_snapshot_t *snapshot)
Get a thread-safe snapshot of the full state.
Definition app_state.c:207
static void display_ui_draw_portal_screen(const app_state_snapshot_t *snapshot)
Definition display_ui.c:27
static void display_ui_draw_clock_screen(const app_state_snapshot_t *snapshot)
Definition display_ui.c:39
#define CONFIG_DISPLAY_UI_REFRESH_MS
Definition display_ui.c:21
Snapshot of full app state.
Definition app_state.h:69
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ s_display_handle

i2c_u8g2_handle_t s_display_handle
static

Definition at line 24 of file display_ui.c.

◆ s_u8g2

u8g2_t* s_u8g2
static

Definition at line 25 of file display_ui.c.

◆ TAG

const char* TAG = "display_ui"
static

Definition at line 18 of file display_ui.c.