Add button counter test on GPIO22 with SoftSPI touch and LVGL.
Each press increments on-screen count; touch Reset clears. Flashable firmware/cyd package. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
55
src/button.cpp
Normal file
55
src/button.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "button.h"
|
||||
#include "pins.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
static uint32_t g_count = 0;
|
||||
static bool g_stable_down = false;
|
||||
static bool g_last_raw = false;
|
||||
static uint32_t g_last_change_ms = 0;
|
||||
static constexpr uint32_t DEBOUNCE_MS = 40;
|
||||
|
||||
void button_init() {
|
||||
pinMode(BTN_PIN, INPUT_PULLUP);
|
||||
g_count = 0;
|
||||
g_stable_down = false;
|
||||
g_last_raw = (digitalRead(BTN_PIN) == LOW);
|
||||
g_last_change_ms = millis();
|
||||
Serial.printf("Button GPIO%d INPUT_PULLUP (press=LOW)\n", BTN_PIN);
|
||||
}
|
||||
|
||||
bool button_poll_press() {
|
||||
const bool raw_down = (digitalRead(BTN_PIN) == LOW);
|
||||
const uint32_t now = millis();
|
||||
|
||||
if (raw_down != g_last_raw) {
|
||||
g_last_raw = raw_down;
|
||||
g_last_change_ms = now;
|
||||
}
|
||||
|
||||
if ((now - g_last_change_ms) < DEBOUNCE_MS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (raw_down == g_stable_down) {
|
||||
return false;
|
||||
}
|
||||
|
||||
g_stable_down = raw_down;
|
||||
if (!g_stable_down) {
|
||||
return false; // 只计按下边沿
|
||||
}
|
||||
|
||||
g_count++;
|
||||
Serial.printf("Button press count=%lu\n", (unsigned long)g_count);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t button_count() { return g_count; }
|
||||
|
||||
void button_reset_count() {
|
||||
g_count = 0;
|
||||
Serial.println("Button count reset");
|
||||
}
|
||||
|
||||
bool button_is_down() { return g_stable_down; }
|
||||
Reference in New Issue
Block a user