|
| 1 | +#include "menu_power_information.h" |
| 2 | +#include <string.h> |
| 3 | +#include "bsp/input.h" |
| 4 | +#include "bsp/power.h" |
| 5 | +#include "common/display.h" |
| 6 | +#include "common/theme.h" |
| 7 | +#include "driver/temperature_sensor.h" |
| 8 | +#include "gui_style.h" |
| 9 | +#include "icons.h" |
| 10 | +#include "menu/message_dialog.h" |
| 11 | +#include "pax_gfx.h" |
| 12 | +#include "pax_matrix.h" |
| 13 | +#include "pax_text.h" |
| 14 | +#include "pax_types.h" |
| 15 | + |
| 16 | +#if defined(CONFIG_BSP_TARGET_TANMATSU) || defined(CONFIG_BSP_TARGET_KONSOOL) || \ |
| 17 | + defined(CONFIG_BSP_TARGET_HACKERHOTEL_2026) |
| 18 | +#define FOOTER_LEFT ((gui_element_icontext_t[]){{get_icon(ICON_ESC), "/"}, {get_icon(ICON_F1), "Back"}}), 2 |
| 19 | +#define FOOTER_RIGHT NULL, 0 |
| 20 | +#define TEXT_FONT pax_font_sky_mono |
| 21 | +#define TEXT_SIZE 18 |
| 22 | +#elif defined(CONFIG_BSP_TARGET_MCH2022) |
| 23 | +#define FOOTER_LEFT ((gui_element_icontext_t[]){{NULL, "🅱 Back"}}), 1 |
| 24 | +#define FOOTER_RIGHT NULL, 0 |
| 25 | +#define TEXT_FONT pax_font_sky_mono |
| 26 | +#define TEXT_SIZE 9 |
| 27 | +#else |
| 28 | +#define FOOTER_LEFT NULL, 0 |
| 29 | +#define FOOTER_RIGHT NULL, 0 |
| 30 | +#define TEXT_FONT pax_font_sky_mono |
| 31 | +#define TEXT_SIZE 9 |
| 32 | +#endif |
| 33 | + |
| 34 | +static temperature_sensor_handle_t temp_handle = NULL; |
| 35 | + |
| 36 | +static void render(void) { |
| 37 | + pax_buf_t* buffer = display_get_buffer(); |
| 38 | + gui_theme_t* theme = get_theme(); |
| 39 | + |
| 40 | + int header_height = theme->header.height + (theme->header.vertical_margin * 2); |
| 41 | + int footer_height = theme->footer.height + (theme->footer.vertical_margin * 2); |
| 42 | + pax_vec2_t position = { |
| 43 | + .x0 = theme->menu.horizontal_margin + theme->menu.horizontal_padding, |
| 44 | + .y0 = header_height + theme->menu.vertical_margin + theme->menu.vertical_padding, |
| 45 | + .x1 = pax_buf_get_width(buffer) - theme->menu.horizontal_margin - theme->menu.horizontal_padding, |
| 46 | + .y1 = pax_buf_get_height(buffer) - footer_height - theme->menu.vertical_margin - theme->menu.vertical_padding, |
| 47 | + }; |
| 48 | + |
| 49 | + render_base_screen_statusbar(buffer, theme, true, true, true, |
| 50 | + ((gui_element_icontext_t[]){{get_icon(ICON_DEVICE_INFO), "Power information"}}), 1, |
| 51 | + FOOTER_LEFT, FOOTER_RIGHT); |
| 52 | + char text_buffer[256]; |
| 53 | + int line = 0; |
| 54 | + |
| 55 | + bsp_power_battery_information_t information = {0}; |
| 56 | + |
| 57 | + esp_err_t res = bsp_power_get_battery_information(&information); |
| 58 | + |
| 59 | + if (res != ESP_OK) { |
| 60 | + snprintf(text_buffer, sizeof(text_buffer), "Unavailable (%s)", esp_err_to_name(res)); |
| 61 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 62 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 63 | + display_blit_buffer(buffer); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (information.battery_available) { |
| 68 | + snprintf(text_buffer, sizeof(text_buffer), "Battery type: %s", |
| 69 | + information.type ? information.type : "Unknown"); |
| 70 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 71 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 72 | + snprintf(text_buffer, sizeof(text_buffer), "Charging disabled: %s", |
| 73 | + information.charging_disabled ? "Yes" : "No"); |
| 74 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 75 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 76 | + snprintf(text_buffer, sizeof(text_buffer), "Battery charging: %s", |
| 77 | + information.battery_charging ? "Yes" : "No"); |
| 78 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 79 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 80 | + snprintf(text_buffer, sizeof(text_buffer), "Max charging current: %" PRIu16 " mA", |
| 81 | + information.maximum_charging_current); |
| 82 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 83 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 84 | + snprintf(text_buffer, sizeof(text_buffer), "Current charging current: %" PRIu16 " mA", |
| 85 | + information.current_charging_current); |
| 86 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 87 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 88 | + snprintf(text_buffer, sizeof(text_buffer), "Battery voltage: %" PRIu16 " mV", information.voltage); |
| 89 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 90 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 91 | + snprintf(text_buffer, sizeof(text_buffer), "Charging target voltage: %" PRIu16 " mV", |
| 92 | + information.charging_target_voltage); |
| 93 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 94 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 95 | + snprintf(text_buffer, sizeof(text_buffer), "Remaining charge: %.2f%%", |
| 96 | + information.remaining_percentage); |
| 97 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 98 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 99 | + } else { |
| 100 | + snprintf(text_buffer, sizeof(text_buffer), "No battery detected"); |
| 101 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 102 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 103 | + } |
| 104 | + |
| 105 | + line++; |
| 106 | + snprintf(text_buffer, sizeof(text_buffer), "Power supply detected: %s", |
| 107 | + information.power_supply_available ? "Yes" : "No"); |
| 108 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 109 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 110 | + |
| 111 | + uint16_t system_voltage = 0; |
| 112 | + esp_err_t res_sys = bsp_power_get_system_voltage(&system_voltage); |
| 113 | + if (res_sys == ESP_OK) { |
| 114 | + snprintf(text_buffer, sizeof(text_buffer), "System voltage: %" PRIu16 " mV", system_voltage); |
| 115 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 116 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 117 | + } |
| 118 | + |
| 119 | + uint16_t input_voltage = 0; |
| 120 | + esp_err_t res_input = bsp_power_get_input_voltage(&input_voltage); |
| 121 | + if (res_input == ESP_OK) { |
| 122 | + snprintf(text_buffer, sizeof(text_buffer), "Input voltage: %" PRIu16 " mV", input_voltage); |
| 123 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 124 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 125 | + } |
| 126 | + |
| 127 | +#if defined(CONFIG_IDF_TARGET_ESP32P4) |
| 128 | + res = temperature_sensor_enable(temp_handle); |
| 129 | + if (res == ESP_OK) { |
| 130 | + float tsens_out; |
| 131 | + res = temperature_sensor_get_celsius(temp_handle, &tsens_out); |
| 132 | + if (res == ESP_OK) { |
| 133 | + line++; |
| 134 | + snprintf(text_buffer, sizeof(text_buffer), "SoC temperature: ~%.2f *C", tsens_out); |
| 135 | + pax_draw_text(buffer, theme->palette.color_foreground, TEXT_FONT, TEXT_SIZE, position.x0, |
| 136 | + position.y0 + (TEXT_SIZE + 2) * (line++), text_buffer); |
| 137 | + }; |
| 138 | + temperature_sensor_disable(temp_handle); |
| 139 | + } |
| 140 | +#endif |
| 141 | + |
| 142 | + display_blit_buffer(buffer); |
| 143 | +} |
| 144 | + |
| 145 | +void menu_power_information(void) { |
| 146 | + QueueHandle_t input_event_queue = NULL; |
| 147 | + ESP_ERROR_CHECK(bsp_input_get_queue(&input_event_queue)); |
| 148 | + |
| 149 | +#if defined(CONFIG_IDF_TARGET_ESP32P4) |
| 150 | + if (temp_handle == NULL) { |
| 151 | + temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(20, 50); |
| 152 | + ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_handle)); |
| 153 | + } |
| 154 | +#endif |
| 155 | + |
| 156 | + render(); |
| 157 | + while (1) { |
| 158 | + bsp_input_event_t event; |
| 159 | + if (xQueueReceive(input_event_queue, &event, pdMS_TO_TICKS(1000)) == pdTRUE) { |
| 160 | + switch (event.type) { |
| 161 | + case INPUT_EVENT_TYPE_NAVIGATION: { |
| 162 | + if (event.args_navigation.state) { |
| 163 | + switch (event.args_navigation.key) { |
| 164 | + case BSP_INPUT_NAVIGATION_KEY_ESC: |
| 165 | + case BSP_INPUT_NAVIGATION_KEY_F1: |
| 166 | + case BSP_INPUT_NAVIGATION_KEY_GAMEPAD_B: |
| 167 | + return; |
| 168 | + default: |
| 169 | + break; |
| 170 | + } |
| 171 | + } |
| 172 | + break; |
| 173 | + } |
| 174 | + default: |
| 175 | + break; |
| 176 | + } |
| 177 | + } else { |
| 178 | + render(); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments