|
| 1 | +#include <math.h> |
| 2 | +#include "lvgl.h" |
| 3 | +#include "esp_ota_ops.h" |
| 4 | +#include "esp_system.h" |
| 5 | + |
| 6 | +typedef struct { |
| 7 | + lv_obj_t *scr; |
| 8 | + int count_val; |
| 9 | +} my_timer_context_t; |
| 10 | + |
| 11 | +static lv_obj_t *img_text; |
| 12 | +static lv_obj_t *label_value; // Label to display the value |
| 13 | + |
| 14 | +static void ota_button_event_handler(lv_event_t *e) { |
| 15 | + lv_obj_t *btn = lv_event_get_target(e); |
| 16 | + const uint32_t btn_id = lv_obj_get_child_id(btn); |
| 17 | + |
| 18 | + printf("Button %ld clicked\n", btn_id + 1); |
| 19 | + |
| 20 | + // Initially assume the first OTA partition, which is typically 'ota_0' |
| 21 | + const esp_partition_t *next_partition = esp_ota_get_next_update_partition(NULL); |
| 22 | + |
| 23 | + // Iterate to find the correct OTA partition only if button ID is greater than 1 |
| 24 | + if (btn_id > 0 && btn_id <= 5) { |
| 25 | + for (int i = 0; i < btn_id; i++) { |
| 26 | + next_partition = esp_ota_get_next_update_partition(next_partition); |
| 27 | + if (!next_partition) break; // If no next partition, break from the loop |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // For button 1, next_partition will not change, thus pointing to 'ota_0' |
| 32 | + if (next_partition && esp_ota_set_boot_partition(next_partition) == ESP_OK) { |
| 33 | + printf("Setting boot partition to %s\n", next_partition->label); |
| 34 | + esp_restart(); // Restart to boot from the new partition |
| 35 | + } else { |
| 36 | + printf("Failed to set boot partition\n"); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +static void add_number_buttons(lv_obj_t *scr, lv_obj_t *ref_obj) { |
| 42 | + int button_width = 50; |
| 43 | + int button_height = 60; |
| 44 | + |
| 45 | + lv_obj_t *flex_cont = lv_obj_create(scr); |
| 46 | + lv_obj_set_size(flex_cont, 5 * button_width + 4 * 10, button_height); |
| 47 | + lv_obj_align_to(flex_cont, ref_obj, LV_ALIGN_OUT_TOP_MID, 0, -10); |
| 48 | + lv_obj_set_flex_flow(flex_cont, LV_FLEX_FLOW_ROW); |
| 49 | + lv_obj_set_flex_align(flex_cont, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); |
| 50 | + lv_obj_set_scrollbar_mode(flex_cont, LV_SCROLLBAR_MODE_OFF); |
| 51 | + |
| 52 | + for (int i = 0; i < 5; i++) { |
| 53 | + lv_obj_t *btn = lv_btn_create(flex_cont); |
| 54 | + lv_obj_set_size(btn, button_width, button_height); |
| 55 | + lv_obj_t *label = lv_label_create(btn); |
| 56 | + lv_label_set_text_fmt(label, "%d", i + 1); |
| 57 | + |
| 58 | + lv_color_t text_color; |
| 59 | + switch (i) { |
| 60 | + case 0: text_color = lv_color_make(0, 255, 0); break; |
| 61 | + case 1: text_color = lv_color_make(255, 255, 0); break; |
| 62 | + case 2: text_color = lv_color_make(255, 165, 0); break; |
| 63 | + case 3: text_color = lv_color_make(255, 69, 0); break; |
| 64 | + case 4: text_color = lv_color_make(255, 0, 0); break; |
| 65 | + default: text_color = lv_color_make(0, 0, 0); break; |
| 66 | + } |
| 67 | + lv_obj_set_style_text_color(label, text_color, 0); |
| 68 | + |
| 69 | + // Register the event handler for the button |
| 70 | + lv_obj_add_event_cb(btn, ota_button_event_handler, LV_EVENT_CLICKED, NULL); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +static void add_icon_buttons(lv_obj_t *scr, lv_obj_t *ref_obj) { |
| 75 | + int button_width = 60; // Button width |
| 76 | + int button_height = 60; // Button height |
| 77 | + |
| 78 | + // Create a flex container for the icon buttons |
| 79 | + lv_obj_t *icon_flex_cont = lv_obj_create(scr); |
| 80 | + lv_obj_set_size(icon_flex_cont, 4 * button_width + 3 * 10, button_height); |
| 81 | + lv_obj_align_to(icon_flex_cont, ref_obj, LV_ALIGN_OUT_TOP_MID, 0, -button_height - 20); |
| 82 | + lv_obj_set_flex_flow(icon_flex_cont, LV_FLEX_FLOW_ROW); |
| 83 | + lv_obj_set_flex_align(icon_flex_cont, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); |
| 84 | + lv_obj_set_scrollbar_mode(icon_flex_cont, LV_SCROLLBAR_MODE_OFF); |
| 85 | + |
| 86 | + // Array of raw images for icons |
| 87 | + const lv_img_dsc_t *icons[] = {&cloud_raw, &sun_raw, &snowflake_raw, &wind_raw}; |
| 88 | + |
| 89 | + // Create and position the buttons within the icon flex container |
| 90 | + for (int i = 0; i < 4; i++) { |
| 91 | + lv_obj_t *btn = lv_btn_create(icon_flex_cont); |
| 92 | + lv_obj_set_size(btn, button_width, button_height); |
| 93 | + |
| 94 | + // Create an image for the button with the icon |
| 95 | + lv_obj_t *img = lv_img_create(btn); |
| 96 | + lv_img_set_src(img, icons[i]); |
| 97 | + lv_obj_center(img); // Center the image within the button |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +void bootloader_ui(lv_obj_t *scr) { |
| 103 | + // Create new image and make it transparent |
| 104 | + img_text = lv_img_create(scr); |
| 105 | + lv_img_set_src(img_text, &esp_text); |
| 106 | + lv_obj_set_style_img_opa(img_text, 0, 0); |
| 107 | + |
| 108 | + // Create a label for displaying the slider value |
| 109 | + label_value = lv_label_create(scr); |
| 110 | + lv_label_set_text(label_value, "0°C"); // Initialize with a default value |
| 111 | + |
| 112 | + // Create a slider |
| 113 | + slider = lv_slider_create(scr); |
| 114 | + lv_obj_set_width(slider, 200); // Set the slider's width |
| 115 | + |
| 116 | + // Position the slider at the bottom of the screen |
| 117 | + lv_obj_align(slider, LV_ALIGN_BOTTOM_MID, 0, -10); // -10 pixels from the bottom for some padding |
| 118 | + |
| 119 | + // Position the label above the slider |
| 120 | + lv_obj_align_to(label_value, slider, LV_ALIGN_OUT_TOP_MID, 0, -10); // Align above slider with some padding |
| 121 | + |
| 122 | + // Set the slider's range |
| 123 | + lv_slider_set_range(slider, -20, 40); |
| 124 | + |
| 125 | + // Add event callback to the slider |
| 126 | + lv_obj_add_event_cb(slider, slider_event_handler, LV_EVENT_VALUE_CHANGED, NULL); |
| 127 | + |
| 128 | + add_number_buttons(scr, label_value); |
| 129 | + add_icon_buttons(scr, label_value); |
| 130 | +} |
0 commit comments