hid_display.c (7749B)
1 // Copyright 2023 zzeneg (@zzeneg) 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 4 #include "hid_display.h" 5 #include "display.h" 6 #include "raw_hid.h" 7 #include "lvgl_helpers.h" 8 9 uint16_t home_screen_timer = 0; 10 11 /* screens */ 12 static lv_obj_t *screen_home; 13 static lv_obj_t *screen_volume; 14 static lv_obj_t *screen_media; 15 16 /* home screen content */ 17 static lv_obj_t *label_time; 18 static lv_obj_t *label_volume_home; 19 static lv_obj_t *label_shift; 20 static lv_obj_t *label_ctrl; 21 static lv_obj_t *label_alt; 22 static lv_obj_t *label_gui; 23 static lv_obj_t *label_layer; 24 static lv_obj_t *label_caps; 25 #ifdef CAPS_WORD_ENABLE 26 static lv_obj_t *label_caps_word; 27 #endif 28 static lv_obj_t *label_layout; 29 30 /* volume screen content */ 31 static lv_obj_t *arc_volume; 32 static lv_obj_t *label_volume_arc; 33 34 /* media screen content */ 35 static lv_obj_t *label_media_artist; 36 static lv_obj_t *label_media_title; 37 38 enum layout { _EN = 0, _RU }; 39 void set_layout_label(uint8_t layout) { 40 switch (layout) { 41 case _EN: 42 lv_label_set_text(label_layout, "EN"); 43 break; 44 45 case _RU: 46 lv_label_set_text(label_layout, "RU"); 47 break; 48 } 49 } 50 51 void read_string(uint8_t *data, char *string_data) { 52 uint8_t data_length = data[1]; 53 memcpy(string_data, data + 2, data_length); 54 string_data[data_length] = '\0'; 55 } 56 57 void start_home_screen_timer(void) { 58 dprint("start_home_screen_timer\n"); 59 home_screen_timer = timer_read(); 60 } 61 62 void init_screen_home_custom(void) { 63 screen_home = lv_scr_act(); 64 lv_obj_add_style(screen_home, &style_screen, 0); 65 use_flex_column(screen_home); 66 67 label_volume_home = lv_label_create(screen_home); 68 lv_label_set_text(label_volume_home, ""); 69 70 lv_obj_t *mods = lv_obj_create(screen_home); 71 lv_obj_add_style(mods, &style_container, 0); 72 use_flex_row(mods); 73 74 label_gui = create_button(mods, "GUI", &style_button, &style_button_active); 75 label_alt = create_button(mods, "ALT", &style_button, &style_button_active); 76 label_ctrl = create_button(mods, "CTL", &style_button, &style_button_active); 77 label_shift = create_button(mods, "SFT", &style_button, &style_button_active); 78 79 label_time = lv_label_create(screen_home); 80 lv_label_set_text(label_time, "00:00"); 81 lv_obj_set_style_text_font(label_time, &lv_font_montserrat_48, LV_PART_MAIN); 82 83 lv_obj_t *caps = lv_obj_create(screen_home); 84 lv_obj_add_style(caps, &style_container, 0); 85 use_flex_row(caps); 86 87 label_caps = create_button(caps, "CAPS", &style_button, &style_button_active); 88 #ifdef CAPS_WORD_ENABLE 89 label_caps_word = create_button(caps, "CAPS WORD", &style_button, &style_button_active); 90 #endif 91 92 lv_obj_t *bottom_row = lv_obj_create(screen_home); 93 lv_obj_add_style(bottom_row, &style_container, 0); 94 95 label_layer = lv_label_create(bottom_row); 96 lv_label_set_text(label_layer, ""); 97 lv_obj_align(label_layer, LV_ALIGN_LEFT_MID, 10, 0); 98 display_process_layer_state(0); 99 100 label_layout = lv_label_create(bottom_row); 101 lv_label_set_text(label_layout, ""); 102 lv_obj_align(label_layout, LV_ALIGN_RIGHT_MID, -10, 0); 103 set_layout_label(0); 104 } 105 106 void init_screen_volume(void) { 107 screen_volume = lv_obj_create(NULL); 108 lv_obj_add_style(screen_volume, &style_screen, 0); 109 110 arc_volume = lv_arc_create(screen_volume); 111 lv_obj_set_size(arc_volume, 200, 200); 112 lv_obj_center(arc_volume); 113 114 label_volume_arc = lv_label_create(screen_volume); 115 lv_label_set_text(label_volume_arc, "00"); 116 lv_obj_set_style_text_font(label_volume_arc, &lv_font_montserrat_48, LV_PART_MAIN); 117 lv_obj_center(label_volume_arc); 118 119 lv_obj_t *volume_text_label = lv_label_create(screen_volume); 120 lv_label_set_text(volume_text_label, "Volume"); 121 lv_obj_align(volume_text_label, LV_ALIGN_BOTTOM_MID, 0, -10); 122 } 123 124 void init_screen_media(void) { 125 screen_media = lv_obj_create(NULL); 126 lv_obj_add_style(screen_media, &style_screen, 0); 127 use_flex_column(screen_media); 128 lv_obj_set_flex_align(screen_media, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); 129 130 label_media_artist = lv_label_create(screen_media); 131 lv_label_set_text(label_media_artist, "N/A"); 132 lv_label_set_long_mode(label_media_artist, LV_LABEL_LONG_WRAP); 133 lv_obj_set_width(label_media_artist, lv_pct(90)); 134 lv_obj_set_style_text_align(label_media_artist, LV_TEXT_ALIGN_CENTER, 0); 135 136 label_media_title = lv_label_create(screen_media); 137 lv_label_set_text(label_media_title, "N/A"); 138 lv_label_set_long_mode(label_media_title, LV_LABEL_LONG_WRAP); 139 lv_obj_set_width(label_media_title, lv_pct(90)); 140 lv_obj_set_style_text_align(label_media_title, LV_TEXT_ALIGN_CENTER, 0); 141 } 142 143 bool display_init_user(void) { 144 init_screen_home_custom(); 145 init_screen_volume(); 146 init_screen_media(); 147 148 return false; 149 } 150 151 void display_process_raw_hid_data(uint8_t *data, uint8_t length) { 152 uint8_t data_type = data[0]; 153 char string_data[length - 2]; 154 dprintf("display_process_raw_hid_data - received data_type %u \n", data_type); 155 switch (data_type) { 156 case _TIME: 157 dprintf("time %02d:%02d\n", data[1], data[2]); 158 lv_label_set_text_fmt(label_time, "%02d:%02d", data[1], data[2]); 159 break; 160 161 case _VOLUME: 162 dprintf("volume %d\n", data[1]); 163 lv_label_set_text_fmt(label_volume_home, "Volume: %02d%%", data[1]); 164 lv_label_set_text_fmt(label_volume_arc, "%02d", data[1]); 165 lv_arc_set_value(arc_volume, data[1]); 166 lv_scr_load(screen_volume); 167 start_home_screen_timer(); 168 break; 169 170 case _LAYOUT: 171 dprintf("layout %d\n", data[1]); 172 set_layout_label(data[1]); 173 break; 174 175 case _MEDIA_ARTIST: 176 read_string(data, string_data); 177 dprintf("media artist %s\n", string_data); 178 lv_label_set_text(label_media_artist, string_data); 179 lv_scr_load(screen_media); 180 start_home_screen_timer(); 181 break; 182 183 case _MEDIA_TITLE: 184 read_string(data, string_data); 185 dprintf("media title %s\n", string_data); 186 lv_label_set_text(label_media_title, string_data); 187 lv_scr_load(screen_media); 188 start_home_screen_timer(); 189 break; 190 } 191 } 192 193 void display_process_layer_state(uint8_t layer) { 194 switch (layer) { 195 case _QWERTY: 196 lv_label_set_text(label_layer, "QWERTY"); 197 break; 198 case _GAME: 199 lv_label_set_text(label_layer, "GAME"); 200 break; 201 case _NAV: 202 lv_label_set_text(label_layer, "NAV"); 203 break; 204 case _NUMBER: 205 lv_label_set_text(label_layer, "NUMBER"); 206 break; 207 case _SYMBOL: 208 lv_label_set_text(label_layer, "SYMBOL"); 209 break; 210 case _FUNC: 211 lv_label_set_text(label_layer, "FUNC"); 212 break; 213 case _SYS: 214 lv_label_set_text(label_layer, "SYSTEM"); 215 break; 216 } 217 } 218 219 void display_housekeeping_task(void) { 220 if (home_screen_timer && timer_elapsed(home_screen_timer) > 5000) { 221 home_screen_timer = 0; 222 lv_scr_load(screen_home); 223 } 224 225 toggle_state(label_shift, LV_STATE_PRESSED, MODS_SHIFT); 226 toggle_state(label_ctrl, LV_STATE_PRESSED, MODS_CTRL); 227 toggle_state(label_alt, LV_STATE_PRESSED, MODS_ALT); 228 toggle_state(label_gui, LV_STATE_PRESSED, MODS_GUI); 229 } 230 231 void display_process_caps(bool active) { 232 toggle_state(label_caps, LV_STATE_PRESSED, active); 233 } 234 235 #ifdef CAPS_WORD_ENABLE 236 void display_process_caps_word(bool active) { 237 dprint("display_process_caps_word\n"); 238 toggle_state(label_caps_word, LV_STATE_PRESSED, active); 239 } 240 #endif