Compare commits

..
2 Commits
Author SHA1 Message Date
aZholtikov 3bfbcc7779 wip: 2026-10-27 13:51:04 +03:00
aZholtikov 1f319b33f8 wip: 2026-10-27 12:07:05 +03:00
10 changed files with 4607 additions and 0 deletions

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
.DS_Store
.vscode
build
managed_components
dependencies.lock
sdkconfig.old
Executable
+3
View File
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(zh_sim_race_button_box)
Executable
+73
View File
@@ -0,0 +1,73 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+1
View File
@@ -0,0 +1 @@
idf_component_register(SRCS "zh_sim_race_button_box.c" INCLUDE_DIRS "" REQUIRES zh_160x_i2c zh_encoder zh_ota_server zh_menu esp_wifi nvs_flash)
+16
View File
@@ -0,0 +1,16 @@
description: In progress
url: https://github.com/aZholtikov/zh_sim_race_button_box
version: "1.0.0"
license: "Apache-2.0"
maintainers:
- "Alexey Zholtikov <alexey.zholtikov@yandex.ru>"
dependencies:
idf: ">=5.0"
zh_160x_i2c:
git: "https://github.com/aZholtikov/zh_160x_i2c.git"
zh_ota_server:
git: "https://github.com/aZholtikov/zh_ota_server.git"
zh_encoder:
git: "https://github.com/aZholtikov/zh_encoder.git"
zh_menu:
git: "https://github.com/aZholtikov/zh_menu.git"
+681
View File
@@ -0,0 +1,681 @@
#include "zh_sim_race_button_box.h"
#define ENCODER_NUMBER 0x01
#define LCD_VIEWPORT_ROWS 3
#define LCD_COLS 16
#define LCD_TOTAL_ROWS 4
#define BOOT_SPLASH_MS 5000
#define MAIN_SCREEN_COUNT 5
#define LOAD_SCREEN_COUNT 5
#define NVS_NAMESPACE "sim_race_box" /* NEW */
#define NVS_KEY_SETTINGS "settings" /* NEW */
static const char *TAG = "sim_race_box";
/* ============================================================
* Данные main-экранов (idle)
* ============================================================ */
int16_t main_race_data[VIEW_CELLS] = {185, 7500, 4, 234, 0};
int16_t main_engine_data[VIEW_CELLS] = {78, 82, 12, 45, 0};
int16_t main_times_data[VIEW_CELLS] = {1234, 1201, 1250, -49, 0};
int16_t main_debug_data[VIEW_CELLS] = {45, -42, 152, 12, 0};
int16_t main_blank_data[VIEW_CELLS] = {0, 0, 0, 0, 0};
/* ---------- Дескрипторы экранов ---------- */
typedef struct
{
const char *name;
const char *const labels[VIEW_ROWS];
int16_t *data;
} main_screen_desc_t;
typedef struct
{
const char *name;
const char *const lines[VIEW_ROWS];
} load_screen_desc_t;
static const main_screen_desc_t main_screens[MAIN_SCREEN_COUNT] = {
{"Race", {"SPD", "RPM", "GER", "LAP"}, main_race_data},
{"Engine", {"OIL", "WTR", "BAT", "FUL"}, main_engine_data},
{"Times", {"LAP", "BST", "LST", "DLT"}, main_times_data},
{"Debug", {"HEP", "SIG", "UPT", "CPU"}, main_debug_data},
{"Blank", {"---", "---", "---", "---"}, main_blank_data},
};
static const load_screen_desc_t load_screens[LOAD_SCREEN_COUNT] = {
{"Boot", {"Booting...", "RAM: OK", "I2C: OK", "Starting..."}},
{"Version", {"FW v1.0", "Build 2025-10", "ESP-IDF v5.4", "zh_menu v1.2"}},
{"Diag", {"I2C: OK", "LCD: OK", "ENC: OK", "All: OK"}},
{"Ready", {"System ready", "Ready for race", "Let's go", ""}},
{"Blank", {"", "", "", ""}},
};
/* ---------- Пользовательский выбор ---------- */
static int _boot_screen = 0;
static int _main_screen = 0;
static bool _boot_done = false;
static bool _prev_selected = false; /* NEW: для детекции выхода из edit */
/* ---------- Динамические буферы меток ---------- */
char lbl_boot_screen[MENU_LABEL_LEN] = "B:Boot";
char lbl_main_screen[MENU_LABEL_LEN] = "M:Race";
char lbl_temp_on[MENU_LABEL_LEN] = "Temp ON 15C";
char lbl_temp_off[MENU_LABEL_LEN] = "Temp OFF 35C";
char lbl_pw_ch_1[MENU_LABEL_LEN] = "PW CH 1 100%";
char lbl_pw_ch_2[MENU_LABEL_LEN] = "PW CH 2 100%";
char lbl_pw_ch_3[MENU_LABEL_LEN] = "PW CH 3 100%";
char lbl_pw_ch_4[MENU_LABEL_LEN] = "PW CH 4 100%";
int16_t g_temp_on = 15;
int16_t g_temp_off = 35;
uint8_t g_pw_ch[4] = {100, 100, 100, 100};
/* ---------- Handle'ы ---------- */
zh_encoder_handle_t *encoder_handle = NULL;
zh_menu_handle_t *menu_handle = NULL;
zh_pcf8574_handle_t *pcf8574_handle = NULL;
zh_160x_i2c_handle_t *lcd_handle = NULL;
/* ---------- Состояние меню ---------- */
static zh_menu_node_t *_last_node = NULL;
static uint8_t _last_index = 0;
static zh_menu_node_t *_cached_node = NULL;
/* ---------- Кэш строк LCD ---------- */
static char _row_cache[LCD_TOTAL_ROWS][LCD_COLS];
static bool _row_valid[LCD_TOTAL_ROWS] = {false, false, false, false};
/* ---------- Мьютекс LCD ---------- */
static SemaphoreHandle_t _lcd_mutex = NULL;
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data);
/* ============================================================
* NEW: NVS blob — все сохраняемые поля в одной структуре
* ============================================================ */
typedef struct
{
int16_t temp_on;
int16_t temp_off;
uint8_t pw_ch[4];
uint8_t boot_screen;
uint8_t main_screen;
} settings_blob_t;
/* ============================================================
* NEW: загрузка настроек из NVS.
* Безопасно к отсутствию записи, к несовпадению размера blob
* и к устаревшим индексам экранов (выходящим за границы).
* ============================================================ */
static void _load_settings(void)
{
nvs_handle_t nvs;
esp_err_t err = nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs);
if (err != ESP_OK)
{
ESP_LOGW(TAG, "No saved settings (%s), using defaults", esp_err_to_name(err));
return;
}
settings_blob_t blob;
size_t size = sizeof(blob);
err = nvs_get_blob(nvs, NVS_KEY_SETTINGS, &blob, &size);
nvs_close(nvs);
if (err != ESP_OK || size != sizeof(blob))
{
ESP_LOGW(TAG, "Settings blob missing or size mismatch (%s)", esp_err_to_name(err));
return;
}
/* Защита от устаревших индексов после обновления прошивки */
if (blob.temp_on >= 0 && blob.temp_on <= 100)
g_temp_on = blob.temp_on;
if (blob.temp_off >= 0 && blob.temp_off <= 100)
g_temp_off = blob.temp_off;
memcpy(g_pw_ch, blob.pw_ch, sizeof(g_pw_ch));
if (blob.boot_screen < LOAD_SCREEN_COUNT)
_boot_screen = blob.boot_screen;
if (blob.main_screen < MAIN_SCREEN_COUNT)
_main_screen = blob.main_screen;
ESP_LOGI(TAG, "Settings loaded: temp_on=%d temp_off=%d pw=[%u,%u,%u,%u] boot=%d main=%d",
g_temp_on, g_temp_off, g_pw_ch[0], g_pw_ch[1], g_pw_ch[2], g_pw_ch[3],
_boot_screen, _main_screen);
}
/* ============================================================
* NEW: сохранение настроек в NVS (единый blob, атомарно).
* ============================================================ */
static void _save_settings(void)
{
nvs_handle_t nvs;
esp_err_t err = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "nvs_open RW failed: %s", esp_err_to_name(err));
return;
}
settings_blob_t blob = {
.temp_on = g_temp_on,
.temp_off = g_temp_off,
.boot_screen = (uint8_t)_boot_screen,
.main_screen = (uint8_t)_main_screen,
};
memcpy(blob.pw_ch, g_pw_ch, sizeof(g_pw_ch));
err = nvs_set_blob(nvs, NVS_KEY_SETTINGS, &blob, sizeof(blob));
if (err == ESP_OK)
err = nvs_commit(nvs);
nvs_close(nvs);
if (err != ESP_OK)
ESP_LOGE(TAG, "Failed to save settings: %s", esp_err_to_name(err));
else
ESP_LOGI(TAG, "Settings saved");
}
/* ---------- Утилиты ---------- */
static inline int16_t _clamp_i16(int16_t v, int16_t lo, int16_t hi)
{
return v < lo ? lo : (v > hi ? hi : v);
}
static inline uint8_t _clamp_u8(int16_t v, uint8_t lo, uint8_t hi)
{
return v < lo ? lo : (v > hi ? hi : v);
}
static void _invalidate_row_cache(void)
{
memset(_row_valid, 0, sizeof(_row_valid));
}
/* ---------- Форматирование ---------- */
static void _format_row(char *buf, const char *label, bool selected, bool edit,
bool arrow_up, bool arrow_down)
{
memset(buf, ' ', LCD_COLS);
buf[LCD_COLS] = '\0';
buf[0] = edit ? '*' : (selected ? '>' : ' ');
uint8_t text_cols = (arrow_up || arrow_down) ? (LCD_COLS - 2) : (LCD_COLS - 1);
size_t len = strlen(label);
if (len > text_cols)
len = text_cols;
memcpy(&buf[1], label, len);
if (arrow_up)
{
buf[LCD_COLS - 1] = '^';
}
else if (arrow_down)
{
buf[LCD_COLS - 1] = 'v';
}
}
static void _format_main_row(char *buf, const char *label, int16_t value)
{
memset(buf, ' ', LCD_COLS);
buf[LCD_COLS] = '\0';
snprintf(buf, LCD_COLS + 1, "%3s:%5d", label, (int)value);
}
static void _format_load_row(char *buf, const char *line)
{
memset(buf, ' ', LCD_COLS);
buf[LCD_COLS] = '\0';
size_t len = strlen(line);
if (len > LCD_COLS)
len = LCD_COLS;
memcpy(buf, line, len);
}
static void _lcd_put_row(uint8_t row, const char *text)
{
if (_row_valid[row] && memcmp(_row_cache[row], text, LCD_COLS) == 0)
return;
zh_160x_set_cursor(&lcd_handle, row, 0);
zh_160x_print_char(&lcd_handle, text);
memcpy(_row_cache[row], text, LCD_COLS);
_row_valid[row] = true;
}
/* ---------- Точечное обновление меток ---------- */
static void _refresh_dynamic_labels(zh_menu_node_t *node)
{
if (node == NULL || node->submenu == NULL)
return;
for (uint8_t i = 0; i < node->item_numbers; ++i)
{
const char *lbl = node->submenu[i].label;
if (lbl == lbl_temp_on)
{
snprintf(lbl_temp_on, sizeof(lbl_temp_on), "Temp ON %2dC", (int)g_temp_on);
}
else if (lbl == lbl_temp_off)
{
snprintf(lbl_temp_off, sizeof(lbl_temp_off), "Temp OFF %2dC", (int)g_temp_off);
}
else if (lbl == lbl_pw_ch_1)
{
snprintf(lbl_pw_ch_1, sizeof(lbl_pw_ch_1), "PW CH 1 %3d%%", (int)g_pw_ch[0]);
}
else if (lbl == lbl_pw_ch_2)
{
snprintf(lbl_pw_ch_2, sizeof(lbl_pw_ch_2), "PW CH 2 %3d%%", (int)g_pw_ch[1]);
}
else if (lbl == lbl_pw_ch_3)
{
snprintf(lbl_pw_ch_3, sizeof(lbl_pw_ch_3), "PW CH 3 %3d%%", (int)g_pw_ch[2]);
}
else if (lbl == lbl_pw_ch_4)
{
snprintf(lbl_pw_ch_4, sizeof(lbl_pw_ch_4), "PW CH 4 %3d%%", (int)g_pw_ch[3]);
}
else if (lbl == lbl_boot_screen)
{
snprintf(lbl_boot_screen, sizeof(lbl_boot_screen), "B:%s", load_screens[_boot_screen].name);
}
else if (lbl == lbl_main_screen)
{
snprintf(lbl_main_screen, sizeof(lbl_main_screen), "M:%s", main_screens[_main_screen].name);
}
}
}
/* ---------- Отрисовка экранов ---------- */
static void _draw_main_screen(void)
{
const main_screen_desc_t *s = &main_screens[_main_screen];
for (uint8_t i = 0; i < VIEW_ROWS; ++i)
{
char buf[LCD_COLS + 1];
_format_main_row(buf, s->labels[i], s->data[i]);
_lcd_put_row(i, buf);
}
}
static void _draw_load_screen(void)
{
const load_screen_desc_t *s = &load_screens[_boot_screen];
for (uint8_t i = 0; i < VIEW_ROWS; ++i)
{
char buf[LCD_COLS + 1];
_format_load_row(buf, s->lines[i]);
_lcd_put_row(i, buf);
}
}
/* ---------- Отрисовка меню ---------- */
static void _draw_menu(zh_menu_node_t *current_node, uint8_t current_index,
const zh_menu_status_t *status)
{
if (current_node != _cached_node)
{
_invalidate_row_cache();
_cached_node = current_node;
}
_refresh_dynamic_labels(current_node);
zh_menu_view_t view = {0};
zh_menu_get_view(&menu_handle, &view);
char header[LCD_COLS + 1];
memset(header, ' ', LCD_COLS);
header[LCD_COLS] = '\0';
size_t hlen = strlen(current_node->label);
if (hlen > LCD_COLS)
hlen = LCD_COLS;
memcpy(header, current_node->label, hlen);
_lcd_put_row(0, header);
for (uint8_t i = 0; i < LCD_VIEWPORT_ROWS; ++i)
{
char buf[LCD_COLS + 1];
if (i < view.visible_count)
{
uint8_t idx = view.first_visible + i;
bool is_cursor = (idx == current_index);
bool is_edit = is_cursor && status->is_selected &&
current_node->submenu[idx].selectable;
bool arrow_up = (i == 0) && (view.first_visible > 0);
bool arrow_down = (i == (view.visible_count - 1)) &&
((uint8_t)(view.first_visible + view.visible_count) < current_node->item_numbers);
_format_row(buf, current_node->submenu[idx].label,
is_cursor, is_edit, arrow_up, arrow_down);
}
else
{
memset(buf, ' ', LCD_COLS);
buf[LCD_COLS] = '\0';
}
_lcd_put_row((uint8_t)(i + 1), buf);
}
}
/* ---------- Boot: 5 сек splash → main ---------- */
static void _boot_task(void *pv)
{
(void)pv;
vTaskDelay(pdMS_TO_TICKS(BOOT_SPLASH_MS));
_boot_done = true;
zh_menu_status_t s = {0};
zh_menu_get(&menu_handle, &s);
if (!s.is_active)
{
xSemaphoreTake(_lcd_mutex, portMAX_DELAY);
zh_160x_lcd_clear(&lcd_handle); /* NEW: очистка перед первым main view */
_invalidate_row_cache();
_draw_main_screen();
xSemaphoreGive(_lcd_mutex);
}
vTaskDelete(NULL);
}
/* ---------- app_main ---------- */
void app_main(void)
{
esp_log_level_set("zh_menu", ESP_LOG_ERROR);
esp_log_level_set("zh_encoder", ESP_LOG_ERROR);
esp_log_level_set("zh_160x_i2c", ESP_LOG_ERROR);
esp_log_level_set("zh_pcf8574", ESP_LOG_ERROR);
esp_log_level_set("zh_vector", ESP_LOG_ERROR);
/* ---------- NEW: NVS init + загрузка настроек ---------- */
esp_err_t nvs_err = nvs_flash_init();
if (nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
nvs_err = nvs_flash_init();
}
ESP_ERROR_CHECK(nvs_err);
_load_settings(); /* до отрисовки splash и до чтения _boot_screen */
esp_event_loop_create_default();
esp_event_handler_instance_register(ZH_ENCODER, ESP_EVENT_ANY_ID,
&zh_encoder_event_handler, NULL, NULL);
/* ---------- Encoder ---------- */
zh_encoder_init_config_t encoder_config = ZH_ENCODER_INIT_CONFIG_DEFAULT();
encoder_config.task_priority = 5;
encoder_config.stack_size = configMINIMAL_STACK_SIZE;
encoder_config.queue_size = 5;
encoder_config.a_gpio_number = GPIO_NUM_4;
encoder_config.b_gpio_number = GPIO_NUM_16;
encoder_config.s_gpio_number = GPIO_NUM_15;
encoder_config.encoder_min_value = -1000000;
encoder_config.encoder_max_value = 1000000;
encoder_config.encoder_step = 1;
encoder_config.encoder_number = ENCODER_NUMBER;
zh_encoder_init(&encoder_config, &encoder_handle);
/* ---------- I2C + PCF8574 + LCD ---------- */
i2c_master_bus_config_t i2c_bus_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_NUM_MAX - 1,
.scl_io_num = GPIO_NUM_26,
.sda_io_num = GPIO_NUM_25,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t i2c_bus_handle = NULL;
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle));
zh_pcf8574_init_config_t pcf_config = ZH_PCF8574_INIT_CONFIG_DEFAULT();
pcf_config.i2c_handle = i2c_bus_handle;
pcf_config.i2c_address = 0x27;
ESP_ERROR_CHECK(zh_pcf8574_init(&pcf_config, &pcf8574_handle));
ESP_ERROR_CHECK(zh_160x_init(&pcf8574_handle, &lcd_handle, ZH_LCD_16X4));
zh_160x_lcd_clear(&lcd_handle);
zh_160x_off_cursor(&lcd_handle);
_lcd_mutex = xSemaphoreCreateMutex();
configASSERT(_lcd_mutex != NULL);
/* ---------- Menu ---------- */
zh_menu_init_config_t menu_config = ZH_MENU_INIT_CONFIG_DEFAULT();
menu_config.task_priority = 5;
menu_config.stack_size = 4096;
menu_config.queue_size = 5;
menu_config.root = &main_menu;
menu_config.loop = false;
menu_config.viewport_rows = LCD_VIEWPORT_ROWS;
menu_config.enter_cb = &zh_menu_enter_main_menu_callback;
menu_config.exit_cb = &zh_menu_exit_main_menu_callback;
menu_config.change_cb = &zh_menu_change_menu_callback;
zh_menu_init(&menu_config, &menu_handle);
/* ---------- Splash: сохранённый load-экран ---------- */
_boot_done = false;
_prev_selected = false;
_invalidate_row_cache();
_draw_load_screen();
xTaskCreate(_boot_task, "boot", 3072, NULL, 3, NULL);
}
/* ---------- Encoder event handler ---------- */
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
(void)arg;
zh_menu_status_t menu_status = {0};
zh_menu_get(&menu_handle, &menu_status);
switch (event_id)
{
case ZH_BUTTON_EVENT:
{
zh_encoder_button_event_on_isr_t *button_event = event_data;
if (button_event->button_status == true)
{
menu_status.is_active == false ? zh_menu_set(&menu_handle, ZH_MENU_ENTER, NULL, 0)
: menu_status.is_selected == false ? zh_menu_set(&menu_handle, ZH_MENU_SELECT, NULL, 0)
: zh_menu_set(&menu_handle, ZH_MENU_BACK, NULL, 0);
}
break;
}
case ZH_ENCODER_EVENT:
{
zh_encoder_event_on_isr_t *encoder_event = event_data;
if (menu_status.is_active == true)
{
if (menu_status.is_selected == true)
{
zh_menu_set(&menu_handle, ZH_MENU_SELECT, encoder_event,
sizeof(zh_encoder_event_on_isr_t));
}
else
{
encoder_event->encoder_status == true
? zh_menu_set(&menu_handle, ZH_MENU_UP, NULL, 0)
: zh_menu_set(&menu_handle, ZH_MENU_DOWN, NULL, 0);
}
}
break;
}
default:
break;
}
}
/* ---------- Menu callbacks ---------- */
void zh_menu_enter_main_menu_callback(void *arg)
{
(void)arg;
xSemaphoreTake(_lcd_mutex, portMAX_DELAY);
_cached_node = NULL;
_invalidate_row_cache();
xSemaphoreGive(_lcd_mutex);
}
void zh_menu_exit_main_menu_callback(void *arg)
{
(void)arg;
/* NEW: страховочное сохранение при выходе из меню */
_save_settings();
_prev_selected = false;
xSemaphoreTake(_lcd_mutex, portMAX_DELAY);
zh_160x_lcd_clear(&lcd_handle);
_invalidate_row_cache();
if (_boot_done)
_draw_main_screen();
else
_draw_load_screen();
_cached_node = NULL;
xSemaphoreGive(_lcd_mutex);
}
void zh_menu_change_menu_callback(zh_menu_node_t *current_node, uint8_t current_index)
{
_last_node = current_node;
_last_index = current_index;
zh_menu_status_t status = {0};
zh_menu_get(&menu_handle, &status);
/* NEW: сохраняем при выходе из режима редактирования (BACK) */
if (_prev_selected && !status.is_selected)
{
_save_settings();
}
_prev_selected = status.is_selected;
xSemaphoreTake(_lcd_mutex, portMAX_DELAY);
_draw_menu(current_node, current_index, &status);
xSemaphoreGive(_lcd_mutex);
}
/* ---------- Selectable: Temp / PW ---------- */
void zh_menu_selectable_item_callback(void *arg)
{
if (_last_node == NULL || _last_node->submenu == NULL)
return;
if (_last_index >= _last_node->item_numbers)
return;
zh_menu_node_t *item = &_last_node->submenu[_last_index];
if (arg == NULL)
{
zh_menu_set(&menu_handle, ZH_MENU_SHOW, NULL, 0);
return;
}
zh_encoder_event_on_isr_t *enc = arg;
int8_t dir = enc->encoder_status ? +1 : -1;
bool changed = false;
if (item->label == lbl_temp_on)
{
g_temp_on = _clamp_i16(g_temp_on + dir, 10, 50);
changed = true;
}
else if (item->label == lbl_temp_off)
{
g_temp_off = _clamp_i16(g_temp_off + dir, 10, 50);
changed = true;
}
else if (item->label == lbl_pw_ch_1)
{
g_pw_ch[0] = _clamp_u8(g_pw_ch[0] + dir * 5, 0, 100);
changed = true;
}
else if (item->label == lbl_pw_ch_2)
{
g_pw_ch[1] = _clamp_u8(g_pw_ch[1] + dir * 5, 0, 100);
changed = true;
}
else if (item->label == lbl_pw_ch_3)
{
g_pw_ch[2] = _clamp_u8(g_pw_ch[2] + dir * 5, 0, 100);
changed = true;
}
else if (item->label == lbl_pw_ch_4)
{
g_pw_ch[3] = _clamp_u8(g_pw_ch[3] + dir * 5, 0, 100);
changed = true;
}
if (changed)
{
zh_menu_set(&menu_handle, ZH_MENU_SHOW, NULL, 0);
}
}
/* ---------- Selectable: выбор load-экрана ---------- */
void zh_menu_boot_screen_callback(void *arg)
{
if (arg == NULL)
{
zh_menu_set(&menu_handle, ZH_MENU_SHOW, NULL, 0);
return;
}
zh_encoder_event_on_isr_t *enc = arg;
int8_t dir = enc->encoder_status ? +1 : -1;
int next = _boot_screen + dir;
if (next < 0)
next = LOAD_SCREEN_COUNT - 1;
if (next >= LOAD_SCREEN_COUNT)
next = 0;
if (next != _boot_screen)
{
_boot_screen = next;
zh_menu_set(&menu_handle, ZH_MENU_SHOW, NULL, 0);
}
}
/* ---------- Selectable: выбор main-экрана ---------- */
void zh_menu_main_screen_callback(void *arg)
{
if (arg == NULL)
{
zh_menu_set(&menu_handle, ZH_MENU_SHOW, NULL, 0);
return;
}
zh_encoder_event_on_isr_t *enc = arg;
int8_t dir = enc->encoder_status ? +1 : -1;
int next = _main_screen + dir;
if (next < 0)
next = MAIN_SCREEN_COUNT - 1;
if (next >= MAIN_SCREEN_COUNT)
next = 0;
if (next != _main_screen)
{
_main_screen = next;
zh_menu_set(&menu_handle, ZH_MENU_SHOW, NULL, 0);
}
}
void zh_menu_simple_item_callback(void *arg)
{
(void)arg;
ESP_LOGI(TAG, "Simple item callback performed");
}
void zh_menu_back_command_callback(void *arg)
{
(void)arg;
zh_menu_set(&menu_handle, ZH_MENU_BACK, NULL, 0);
}
+85
View File
@@ -0,0 +1,85 @@
#pragma once
#include "nvs_flash.h"
#include "esp_wifi.h"
#include "zh_menu.h"
#include "zh_160x_i2c.h"
#include "zh_encoder.h"
#include "zh_ota_server.h"
#define ENCODER_NUMBER 0x01
#define MENU_VIEWPORT_ROWS 3
#define MENU_LABEL_LEN 17
/* ---------- Размеры view ---------- */
#define VIEW_CELLS 5 /* ячеек в массиве данных (main) */
#define VIEW_ROWS 4 /* строк отрисовывается на LCD */
/* ---------- Пул основных экранов (idle) — 5 массивов ---------- */
extern int16_t main_race_data[VIEW_CELLS];
extern int16_t main_engine_data[VIEW_CELLS];
extern int16_t main_times_data[VIEW_CELLS];
extern int16_t main_debug_data[VIEW_CELLS];
extern int16_t main_blank_data[VIEW_CELLS];
/* ---------- Динамические буферы меток ---------- */
extern char lbl_temp_on[MENU_LABEL_LEN];
extern char lbl_temp_off[MENU_LABEL_LEN];
extern char lbl_pw_ch_1[MENU_LABEL_LEN];
extern char lbl_pw_ch_2[MENU_LABEL_LEN];
extern char lbl_pw_ch_3[MENU_LABEL_LEN];
extern char lbl_pw_ch_4[MENU_LABEL_LEN];
extern char lbl_boot_screen[MENU_LABEL_LEN]; /* "B: <имя load-экрана>" */
extern char lbl_main_screen[MENU_LABEL_LEN]; /* "M: <имя main-экрана>" */
/* ---------- Переменные значений ---------- */
extern int16_t g_temp_on;
extern int16_t g_temp_off;
extern uint8_t g_pw_ch[4];
/* ---------- Callbacks ---------- */
void zh_menu_enter_main_menu_callback(void *arg);
void zh_menu_exit_main_menu_callback(void *arg);
void zh_menu_change_menu_callback(zh_menu_node_t *current_node, uint8_t current_index);
void zh_menu_selectable_item_callback(void *arg);
void zh_menu_simple_item_callback(void *args);
void zh_menu_back_command_callback(void *arg);
void zh_menu_boot_screen_callback(void *arg);
void zh_menu_main_screen_callback(void *arg);
#ifdef __cplusplus
extern "C"
{
#endif
zh_menu_node_t main_menu = {
.label = "Settings",
.submenu = (zh_menu_node_t[]){
{.label = "Screens", .submenu = (zh_menu_node_t[]){
{.label = lbl_boot_screen, .item_cb = &zh_menu_boot_screen_callback, .selectable = true},
{.label = lbl_main_screen, .item_cb = &zh_menu_main_screen_callback, .selectable = true},
{.label = "Back", .item_cb = &zh_menu_back_command_callback},
},
.item_numbers = 3},
{.label = "Air condition", .submenu = (zh_menu_node_t[]){
{.label = lbl_temp_on, .item_cb = &zh_menu_selectable_item_callback, .selectable = true},
{.label = lbl_temp_off, .item_cb = &zh_menu_selectable_item_callback, .selectable = true},
{.label = "Back", .item_cb = &zh_menu_back_command_callback},
},
.item_numbers = 3},
{.label = "Lights", .submenu = (zh_menu_node_t[]){
{.label = lbl_pw_ch_1, .item_cb = &zh_menu_selectable_item_callback, .selectable = true},
{.label = lbl_pw_ch_2, .item_cb = &zh_menu_selectable_item_callback, .selectable = true},
{.label = lbl_pw_ch_3, .item_cb = &zh_menu_selectable_item_callback, .selectable = true},
{.label = lbl_pw_ch_4, .item_cb = &zh_menu_selectable_item_callback, .selectable = true},
{.label = "Back", .item_cb = &zh_menu_back_command_callback},
},
.item_numbers = 5},
{.label = "Exit", .item_cb = &zh_menu_back_command_callback},
},
.item_numbers = 4,
};
#ifdef __cplusplus
}
#endif
Executable
+8
View File
@@ -0,0 +1,8 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x6000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 3500K,
ota_1, app, ota_1, , 3500K,
storage, data,fat , , 1M,
1 # Name, Type, SubType, Offset, Size, Flags
2 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 nvs, data, nvs, , 0x6000,
4 otadata, data, ota, , 0x2000,
5 phy_init, data, phy, , 0x1000,
6 ota_0, app, ota_0, , 3500K,
7 ota_1, app, ota_1, , 3500K,
8 storage, data,fat , , 1M,
+3733
View File
File diff suppressed because it is too large. Load diff
Executable
+1
View File
@@ -0,0 +1 @@
1.0.0