I made this code work but non properly, HELP
# USO TASTO FLASH SU SCHE PER MENU SCROLL
# Define a button component for the input pin
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode:
input: true
pullup: true
name: "button"
id: "button"
internal: true # Hide from Home Assistant
i2c:
sda: GPIO12
scl: GPIO14
scan: false
frequency: 800kHz
display:
- platform: ssd1306_i2c
model: "SSD1306 128x64"
address: 0x3C
id: oled
update_interval: 1s
lambda: |-
// Define the menu structure
const char* main_menu[] = {"Main Menu 1 Meteo", "Main Menu 2 Consumi", "Main Menu 3 Luci"};
const char* sub_menu_1[] = {"Submenu 1", "Submenu 2", "RUN SOMETHING", "Submenu 3"};
const char* sub_menu_2[] = {"Submenu 4", "Submenu 5", "Submenu 6"};
const char* sub_menu_3[] = {"Submenu 7", "Submenu 8", "Submenu 9"};
// Define the number of items in each menu
const int main_menu_size = sizeof(main_menu) / sizeof(main_menu[0]);
const int sub_menu_1_size = sizeof(sub_menu_1) / sizeof(sub_menu_1[0]);
const int sub_menu_2_size = sizeof(sub_menu_2) / sizeof(sub_menu_2[0]);
const int sub_menu_3_size = sizeof(sub_menu_3) / sizeof(sub_menu_3[0]);
// Define the button debounce time
const int debounce_time = 50;
// Define font display
const int display_height = 128;
const int font_height = 8;
const int font_width = 2;
// Define some variables to store the current menu and item index
int current_menu = 0; // 0 for main menu, 1 for sub menu 1, etc.
int current_item = 0; // The index of the selected item in the current menu
// Define some variables to store the button state and the last press time
bool button_pressed = false;
unsigned long last_press_time = 0;
// Read the button state and debounce it
bool button_state = id(button).state;
if (button_state == false && millis() - last_press_time > debounce_time) {
button_pressed = true;
last_press_time = millis();
}
// Handle the button press logic depending on the current menu and item
if (button_pressed) {
switch (current_menu) {
case 0: // Main menu
current_item++; // Go to the next item in the main menu
if (current_item >= main_menu_size) { // Wrap around if the end is reached
current_item = 0;
}
break;
case 1: // Sub menu 1
if (current_item == 2) { // If the item is "RUN SOMETHING"
// Do something here, like calling a service or changing a state
ESP_LOGD("display", "Running something...");
} else { // Otherwise, go to the next item in the sub menu 1
current_item++;
if (current_item >= sub_menu_1_size) { // Wrap around if the end is reached
current_item = 0;
}
}
break;
case 2: // Sub menu 2
current_item++; // Go to the next item in the sub menu 2
if (current_item >= sub_menu_2_size) { // Wrap around if the end is reached
current_item = 0;
}
break;
case 3: // Sub menu 3
current_item++; // Go to the next item in the sub menu 3
if (current_item >= sub_menu_3_size) { // Wrap around if the end is reached
current_item = 0;
}
break;
}
button_pressed = false; // Reset the button state
}
// Handle the long press logic to enter or exit a sub menu
if (button_state == false && millis() - last_press_time > 1000) {
if (current_menu == 0) { // If in the main menu, enter the corresponding sub menu
switch (current_item) {
case 0:
current_menu = 1; // Enter sub menu 1
break;
case 1:
current_menu = 2; // Enter sub menu 2
break;
case 2:
current_menu = 3; // Enter sub menu 3
break;
}
} else { // If in a sub menu, go back to the main menu
current_menu = 0;
}
current_item = 0; // Reset the item index
last_press_time = millis(); // Update the press time
}
// Display the current menu and item on the OLED
switch (current_menu) {
case 0: // Main menu
it.printf(0, 0, id(font4), "Main Menu");
for (int i = 0; i < main_menu_size; i++) {
if (i == current_item) { // Highlight the selected item
it.filled_rectangle(0, 10 + i * 10, 128, 10);
it.printf(0, 10 + i * 10, id(font4), TextAlign::TOP_LEFT, "%s", main_menu[i]);
} else { // Display the other items normally
it.printf(0, 10 + i * 10, id(font4), "%s", main_menu[i]);
}
}
break;
case 1: // Sub menu 1
it.printf(0, 0, id(font4), "Sub Menu 1");
for (int i = 0; i < sub_menu_1_size; i++) {
if (i == current_item) { // Highlight the selected item
it.filled_rectangle(0, 10 + i * 10, 128, 10);
it.printf(0, 10 + i * 10, id(font4), TextAlign::TOP_LEFT, "%s", sub_menu_1[i]);
} else { // Display the other items normally
it.printf(0, 10 + i * 10, id(font4), "%s", sub_menu_1[i]);
}
}
break;
case 2: // Sub menu 2
it.printf(0, 0, id(font4), "Sub Menu 2");
for (int i = 0; i < sub_menu_2_size; i++) {
if (i == current_item) { // Highlight the selected item
it.filled_rectangle(0, 10 + i * 10, 128, 10);
it.printf(0, 10 + i * 10, id(font4), TextAlign::TOP_LEFT, "%s", sub_menu_2[i]);
} else { // Display the other items normally
it.printf(0, 10 + i * 10, id(font4), "%s", sub_menu_2[i]);
}
}
break;
case 3: // Sub menu 3
it.printf(0, 0, id(font4), "Sub Menu 3");
for (int i = 0; i < sub_menu_3_size; i++) {
if (i == current_item) { // Highlight the selected item
it.filled_rectangle(0, 10 + i * 10, 128, 10);
it.printf(0, 10 + i * 10, id(font4), TextAlign::TOP_LEFT, "%s", sub_menu_3[i]);
} else { // Display the other items normally
it.printf(0, 10 + i * 10, id(font4), "%s", sub_menu_3[i]);
}
}
break;
}
// Display a hint for long press to enter or exit a sub menu
if (current_menu == 0) {
it.print(64 - strlen("Long press to enter") / 2 * font_width,
display_height - font_height,
id(font4),
"Long press to enter");
} else {
it.print(64 - strlen("Long press to exit") / 2 * font_width,
display_height - font_height,
id(font4),
"Long press to exit");
}