|
| 1 | +#include "freertos/FreeRTOS.h" |
| 2 | +#include "freertos/task.h" |
| 3 | +#include "esp_system.h" |
| 4 | +#include "esp_log.h" |
| 5 | +#include "lvgl.h" |
| 6 | +#include "bsp/esp-bsp.h" |
| 7 | +#include "esp_timer.h" |
| 8 | +#include "esp_ota_ops.h" |
| 9 | + |
| 10 | +#define TAG "Calculator" |
| 11 | + |
| 12 | +static lv_obj_t *display_label; |
| 13 | +static char current_input[64] = ""; |
| 14 | +static double stored_value = 0; |
| 15 | +static char current_operator = 0; |
| 16 | +static bool clear_next = false; |
| 17 | +static int64_t last_press_time = 0; |
| 18 | + |
| 19 | +static void update_display() { |
| 20 | + bsp_display_lock(0); |
| 21 | + lv_label_set_text(display_label, current_input); |
| 22 | + bsp_display_unlock(); |
| 23 | +} |
| 24 | + |
| 25 | +static void clear_input() { |
| 26 | + current_input[0] = '\0'; |
| 27 | + update_display(); |
| 28 | +} |
| 29 | + |
| 30 | +static void append_input(const char *text) { |
| 31 | + if (clear_next) { |
| 32 | + clear_input(); |
| 33 | + clear_next = false; |
| 34 | + } |
| 35 | + if (strlen(current_input) < sizeof(current_input) - 1) { |
| 36 | + strcat(current_input, text); |
| 37 | + } |
| 38 | + update_display(); |
| 39 | +} |
| 40 | + |
| 41 | +static void handle_operator(char operator) { |
| 42 | + if (current_operator != 0 && !clear_next) { |
| 43 | + double current_value = atof(current_input); |
| 44 | + switch (current_operator) { |
| 45 | + case '+': stored_value += current_value; break; |
| 46 | + case '-': stored_value -= current_value; break; |
| 47 | + case '*': stored_value *= current_value; break; |
| 48 | + case '/': if (current_value != 0) stored_value /= current_value; break; |
| 49 | + } |
| 50 | + snprintf(current_input, sizeof(current_input), "%g", stored_value); |
| 51 | + update_display(); |
| 52 | + } else { |
| 53 | + stored_value = atof(current_input); |
| 54 | + } |
| 55 | + current_operator = operator; |
| 56 | + clear_next = true; |
| 57 | +} |
| 58 | + |
| 59 | +static void btn_event_cb(lv_event_t *e) { |
| 60 | + int64_t now = esp_timer_get_time(); |
| 61 | + if (now - last_press_time < 500000) { // 500 ms debounce time |
| 62 | + return; |
| 63 | + } |
| 64 | + last_press_time = now; |
| 65 | + |
| 66 | + lv_obj_t *btn = lv_event_get_target(e); |
| 67 | + const char *txt = lv_btnmatrix_get_btn_text(btn, lv_btnmatrix_get_selected_btn(btn)); |
| 68 | + |
| 69 | + if (strcmp(txt, "C") == 0) { |
| 70 | + clear_input(); |
| 71 | + stored_value = 0; |
| 72 | + current_operator = 0; |
| 73 | + } else if (strcmp(txt, "=") == 0) { |
| 74 | + handle_operator(0); |
| 75 | + current_operator = 0; |
| 76 | + clear_next = true; |
| 77 | + } else if (strchr("+-*/", txt[0]) != NULL) { |
| 78 | + handle_operator(txt[0]); |
| 79 | + } else { |
| 80 | + append_input(txt); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void reset_to_factory_app() { |
| 85 | + // Get the partition structure for the factory partition |
| 86 | + const esp_partition_t *factory_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL); |
| 87 | + if (factory_partition != NULL) { |
| 88 | + if (esp_ota_set_boot_partition(factory_partition) == ESP_OK) { |
| 89 | + printf("Set boot partition to factory, restarting now.\n"); |
| 90 | + } else { |
| 91 | + printf("Failed to set boot partition to factory.\n"); |
| 92 | + } |
| 93 | + } else { |
| 94 | + printf("Factory partition not found.\n"); |
| 95 | + } |
| 96 | + |
| 97 | + fflush(stdout); |
| 98 | +} |
| 99 | + |
| 100 | +void app_main(void) { |
| 101 | + // Reset to factory app for the next boot. |
| 102 | + // It should return to graphical bootloader. |
| 103 | + reset_to_factory_app(); |
| 104 | + |
| 105 | + // Initialize the BSP |
| 106 | + bsp_i2c_init(); |
| 107 | + bsp_display_start(); |
| 108 | + lv_init(); |
| 109 | + |
| 110 | + // Create a label for the display |
| 111 | + display_label = lv_label_create(lv_scr_act()); |
| 112 | + lv_obj_set_size(display_label, 300, 30); |
| 113 | + lv_obj_align(display_label, LV_ALIGN_TOP_MID, 0, 10); |
| 114 | + lv_label_set_text(display_label, "0"); |
| 115 | + |
| 116 | + // Create a button matrix for the calculator |
| 117 | + static const char *btn_map[] = { |
| 118 | + "7", "8", "9", "C", "\n", |
| 119 | + "4", "5", "6", "*", "\n", |
| 120 | + "1", "2", "3", "-", "\n", |
| 121 | + "0", ".", "/", "+", "\n", |
| 122 | + "=", "" |
| 123 | + }; |
| 124 | + |
| 125 | + lv_obj_t *btnm = lv_btnmatrix_create(lv_scr_act()); |
| 126 | + lv_btnmatrix_set_map(btnm, btn_map); |
| 127 | + lv_obj_set_size(btnm, 320, 180); |
| 128 | + lv_obj_align(btnm, LV_ALIGN_CENTER, 0, 30); |
| 129 | + lv_obj_add_event_cb(btnm, btn_event_cb, LV_EVENT_VALUE_CHANGED, NULL); |
| 130 | + |
| 131 | + bsp_display_backlight_on(); |
| 132 | +} |
0 commit comments