hardware button support

This commit is contained in:
Adolfo Reyna
2026-01-29 15:23:03 -05:00
parent 1684eb0198
commit 8d0bca691a
7 changed files with 204 additions and 40 deletions

View File

@@ -17,6 +17,7 @@
#include "display/low_level_render.h"
#include "display/low_level_gui.h"
#include "display/low_level_display.h"
#include "display/low_level_display_epaper.h"
#include "display/low_level_touch.h"
@@ -30,6 +31,12 @@ volatile bool touch_interrupt_flag = false;
volatile bool touch_event_down = false;
LowLevelTouch* touch = nullptr;
// Button interrupt handling
#ifdef BUTTON_KEY0_PIN
volatile bool button_key0_pressed = false;
volatile bool button_key1_pressed = false;
#endif
/**
* @brief Touch interrupt callback handler
*
@@ -86,6 +93,35 @@ void touch_interrupt_handler(uint gpio, uint32_t events) {
}
}
#ifdef BUTTON_KEY0_PIN
/**
* @brief Button interrupt callback handler
*
* Called automatically by hardware when button pins change state.
* Buttons are active LOW (pressed = 0, released = 1) with pull-ups.
*
* This runs in interrupt context, so keep it fast - just set flags.
*
* @param gpio GPIO pin number that triggered the interrupt
* @param events Event mask (GPIO_IRQ_EDGE_FALL and/or GPIO_IRQ_EDGE_RISE)
*/
void button_interrupt_handler(uint gpio, uint32_t events) {
// Only respond to falling edge (button press)
if (events & GPIO_IRQ_EDGE_FALL) {
if (gpio == BUTTON_KEY0_PIN) {
button_key0_pressed = true;
printf("Button KEY0 pressed\n");
}
#ifdef BUTTON_KEY1_PIN
else if (gpio == BUTTON_KEY1_PIN) {
button_key1_pressed = true;
printf("Button KEY1 pressed\n");
}
#endif
}
}
#endif
// Screen dimensions and configuration from board_config.h
const int V_WIDTH = DISPLAY_WIDTH;
const int V_HEIGHT = DISPLAY_HEIGHT;
@@ -139,6 +175,20 @@ int main()
return -1;
}
// Do a full refresh with white screen first (removes ghosting on e-paper)
printf("Performing initial full refresh to white...\n");
display->clear(true); // Clear to white
// For e-paper, do a full refresh to ensure clean display
if (display->get_type() == DISPLAY_TYPE_EPAPER) {
LowLevelDisplayEPaper* epaper = static_cast<LowLevelDisplayEPaper*>(display);
epaper->full_refresh(); // Full refresh removes ghosting
printf("Full refresh complete\n");
} else {
refresh_screen(bit_buffer, display); // For TFT, just refresh normally
}
// Now clear to black for drawing
display->clear(false); // Clear to black
LowLevelRenderer renderer(bit_buffer, V_WIDTH, V_HEIGHT);
@@ -173,6 +223,36 @@ int main()
printf("Touch initialization failed or not configured\n");
}
#ifdef BUTTON_KEY0_PIN
// Initialize hardware buttons (e-ink board only)
printf("\nInitializing hardware buttons...\n");
// Initialize KEY0 button
gpio_init(BUTTON_KEY0_PIN);
gpio_set_dir(BUTTON_KEY0_PIN, GPIO_IN);
gpio_pull_up(BUTTON_KEY0_PIN); // Active LOW with pull-up
printf(" KEY0 initialized on GP%d (active LOW)\n", BUTTON_KEY0_PIN);
#ifdef BUTTON_KEY1_PIN
// Initialize KEY1 button
gpio_init(BUTTON_KEY1_PIN);
gpio_set_dir(BUTTON_KEY1_PIN, GPIO_IN);
gpio_pull_up(BUTTON_KEY1_PIN); // Active LOW with pull-up
printf(" KEY1 initialized on GP%d (active LOW)\n", BUTTON_KEY1_PIN);
#endif
// Enable interrupts on falling edge (button press)
gpio_set_irq_enabled_with_callback(BUTTON_KEY0_PIN,
GPIO_IRQ_EDGE_FALL,
true,
&button_interrupt_handler);
#ifdef BUTTON_KEY1_PIN
gpio_set_irq_enabled(BUTTON_KEY1_PIN, GPIO_IRQ_EDGE_FALL, true);
#endif
printf("Button interrupts enabled (falling edge = press)\n");
#endif
// Test SD card and FatFS
// if (sd_card_init_with_board_config()) {
// sd_card_test_fatfs();
@@ -203,6 +283,27 @@ int main()
// Te(); // Wait For Event - CPU sleeps until interrupt or evenurs
__wfi(); // Wait For Interrupt - CPU sleeps until any interrupt
#ifdef BUTTON_KEY0_PIN
// Handle button presses with debouncing
if (button_key0_pressed) {
button_key0_pressed = false;
sleep_ms(50); // Debounce delay
if (gpio_get(BUTTON_KEY0_PIN) == 0) { // Verify button still pressed
printf("Button KEY0 action triggered\n");
// TODO: Add your KEY0 action here (e.g., focus next widget)
}
}
if (button_key1_pressed) {
button_key1_pressed = false;
sleep_ms(50); // Debounce delay
if (gpio_get(BUTTON_KEY1_PIN) == 0) { // Verify button still pressed
printf("Button KEY1 action triggered\n");
// TODO: Add your KEY1 action here (e.g., activate focused widget)
}
}
#endif
// Check if our touch interrupt flag was set
if (!touch_interrupt_flag) {
continue; // Woken by different interrupt, go back to sleep