display.c (4295B)
1 // Copyright 2023 zzeneg (@zzeneg) 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 4 #include "display.h" 5 #include "qp.h" 6 #include "lvgl_helpers.h" 7 8 /* shared styles */ 9 lv_style_t style_screen; 10 lv_style_t style_container; 11 lv_style_t style_button; 12 lv_style_t style_button_active; 13 14 /* screens */ 15 static lv_obj_t *screen_home; 16 17 /* home screen content */ 18 static lv_obj_t *label_shift; 19 static lv_obj_t *label_ctrl; 20 static lv_obj_t *label_alt; 21 static lv_obj_t *label_gui; 22 static lv_obj_t *label_caps; 23 24 void init_styles(void) { 25 lv_style_init(&style_screen); 26 lv_style_set_bg_color(&style_screen, lv_color_black()); 27 28 lv_style_init(&style_container); 29 lv_style_set_pad_top(&style_container, 0); 30 lv_style_set_pad_bottom(&style_container, 0); 31 lv_style_set_pad_left(&style_container, 0); 32 lv_style_set_pad_right(&style_container, 0); 33 lv_style_set_bg_opa(&style_container, 0); 34 lv_style_set_border_width(&style_container, 0); 35 lv_style_set_width(&style_container, lv_pct(100)); 36 lv_style_set_height(&style_container, LV_SIZE_CONTENT); 37 38 lv_style_init(&style_button); 39 lv_style_set_pad_top(&style_button, 4); 40 lv_style_set_pad_bottom(&style_button, 4); 41 lv_style_set_pad_left(&style_button, 4); 42 lv_style_set_pad_right(&style_button, 4); 43 lv_style_set_radius(&style_button, 6); 44 lv_style_set_text_color(&style_button, lv_palette_main(LV_PALETTE_AMBER)); 45 46 lv_style_init(&style_button_active); 47 lv_style_set_bg_color(&style_button_active, lv_palette_main(LV_PALETTE_AMBER)); 48 lv_style_set_bg_opa(&style_button_active, LV_OPA_100); 49 lv_style_set_text_color(&style_button_active, lv_color_black()); 50 } 51 52 void init_screen_home(void) { 53 screen_home = lv_scr_act(); 54 55 lv_obj_add_style(screen_home, &style_screen, 0); 56 use_flex_column(screen_home); 57 58 lv_obj_t *mods = lv_obj_create(screen_home); 59 lv_obj_add_style(mods, &style_container, 0); 60 use_flex_column(mods); 61 62 lv_obj_t *mods_row1 = lv_obj_create(mods); 63 lv_obj_add_style(mods_row1, &style_container, 0); 64 use_flex_row(mods_row1); 65 label_gui = create_button(mods_row1, "GUI", &style_button, &style_button_active); 66 label_alt = create_button(mods_row1, "ALT", &style_button, &style_button_active); 67 68 lv_obj_t *mods_row2 = lv_obj_create(mods); 69 lv_obj_add_style(mods_row2, &style_container, 0); 70 use_flex_row(mods_row2); 71 label_ctrl = create_button(mods_row2, "CTL", &style_button, &style_button_active); 72 label_shift = create_button(mods_row2, "SFT", &style_button, &style_button_active); 73 74 lv_obj_t *label_stront = lv_label_create(screen_home); 75 lv_label_set_text(label_stront, "stront"); 76 #if LV_FONT_MONTSERRAT_48 77 lv_obj_set_style_text_font(label_stront, &lv_font_montserrat_48, LV_PART_MAIN); 78 #endif 79 80 label_caps = create_button(screen_home, "CAPS", &style_button, &style_button_active); 81 } 82 83 bool display_init_kb(void) { 84 dprint("display_init_kb - start\n"); 85 86 backlight_enable(); 87 88 painter_device_t display = qp_st7789_make_spi_device(240, 300, LCD_CS_PIN, LCD_DC_PIN, LCD_RST_PIN, 16, 3); 89 qp_set_viewport_offsets(display, 0, 20); 90 91 if (!qp_init(display, QP_ROTATION_180) || !qp_power(display, true) || !qp_lvgl_attach(display)) return false; 92 93 dprint("display_init_kb - initialised\n"); 94 95 lv_disp_t *lv_display = lv_disp_get_default(); 96 lv_theme_t *lv_theme = lv_theme_default_init(lv_display, lv_palette_main(LV_PALETTE_AMBER), lv_palette_main(LV_PALETTE_BLUE), true, LV_FONT_DEFAULT); 97 lv_disp_set_theme(lv_display, lv_theme); 98 init_styles(); 99 100 bool res = display_init_user(); 101 if (res) { 102 dprint("display_init_kb - adding default home screen\n"); 103 init_screen_home(); 104 } 105 106 return true; 107 } 108 109 __attribute__((weak)) bool display_init_user(void) { 110 return true; 111 } 112 113 __attribute__((weak)) void display_housekeeping_task(void) { 114 dprint("display_housekeeping_task_kb\n"); 115 116 toggle_state(label_shift, LV_STATE_PRESSED, MODS_SHIFT); 117 toggle_state(label_ctrl, LV_STATE_PRESSED, MODS_CTRL); 118 toggle_state(label_alt, LV_STATE_PRESSED, MODS_ALT); 119 toggle_state(label_gui, LV_STATE_PRESSED, MODS_GUI); 120 } 121 122 __attribute__((weak)) void display_process_caps(bool active) { 123 dprint("display_process_caps\n"); 124 toggle_state(label_caps, LV_STATE_PRESSED, active); 125 }