From d9cc6e1f3a1fe47f4d366cc185b8a8d59918912a Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 30 Jul 2026 21:01:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=89=E9=92=AE=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MSOS_ESP/main/CMakeLists.txt | 4 +- MSOS_ESP/main/button.c | 105 +++++++++++++++++++++++++++++++++++ MSOS_ESP/main/button.h | 30 ++++++++++ 3 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 MSOS_ESP/main/button.c create mode 100644 MSOS_ESP/main/button.h diff --git a/MSOS_ESP/main/CMakeLists.txt b/MSOS_ESP/main/CMakeLists.txt index 2c4d7ab..2fd89f6 100644 --- a/MSOS_ESP/main/CMakeLists.txt +++ b/MSOS_ESP/main/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "hello_world_main.c" "iic.c" "oled.c" "state.c" "tca9535.c" "moonshineOS_core/cli.c" - PRIV_REQUIRES spi_flash esp_driver_i2c esp_driver_uart log +idf_component_register(SRCS "hello_world_main.c" "iic.c" "oled.c" "state.c" "tca9535.c" "button.c" "moonshineOS_core/cli.c" + PRIV_REQUIRES spi_flash esp_driver_i2c esp_driver_uart log esp_timer INCLUDE_DIRS "." "moonshineOS_core") diff --git a/MSOS_ESP/main/button.c b/MSOS_ESP/main/button.c new file mode 100644 index 0000000..89bc46a --- /dev/null +++ b/MSOS_ESP/main/button.c @@ -0,0 +1,105 @@ +#include "button.h" +#include "esp_timer.h" + +struct button button_ems; +struct button button_power; +struct button button_beep; + +void button_init(void) +{ + +} + +void button_set_pass(struct button *b) +{ + b->pass = 1; +} + +void button_set_longpass(struct button *b) +{ + b->longpass = 1; +} + +char button_pass(struct button *b) +{ + if (b->pass == 1) { + b->passread = 0; + b->pass = 0; + return 1; + } + return 0; +} + +char button_pass_(struct button *b) +{ + if (b->pass_ == 1) { + b->passread = 0; + b->pass_ = 0; + return 1; + } + return 0; +} + +char button_longpass(struct button *b) +{ + if (b->longpass == 1) { + b->longpassread = 0; + b->longpass = 0; + b->passread = 0; + b->pass_ = 0; + return 1; + } + return 0; +} + +void button_reset(struct button *b) +{ + b->lock = 0; + b->pass = 0; + b->longpass = 0; + b->passread = 0; + b->pass_ = 0; + b->longpassread = 0; +} + +void button_updata(struct button *b, char a) +{ + uint32_t now = (uint32_t)(esp_timer_get_time() / 1000); + + if (b->lock == 0) { + if (a == 0) { + b->time = now; + b->lock = 1; + b->passread = 1; + b->longpassread = 1; + } + } else { + if (a == 0) { + if (now > b->time + BUTTON_PASS_TIMES && b->passread == 1) { + if (b->pass == 0) { + + } + b->pass = 1; + } + if (now > b->time + BUTTON_LONGPASS_TIMES && b->longpassread == 1) { + if (b->longpass == 0) { + + } + b->longpass = 1; + b->passread = 0; + b->pass = 0; + b->pass_ = 0; + } + } else { + if (now > b->time + BUTTON_PASS_TIMES) { + if (b->passread == 1) { + if (b->pass_ == 0) { + + } + b->pass_ = 1; + } + } + b->lock = 0; + } + } +} diff --git a/MSOS_ESP/main/button.h b/MSOS_ESP/main/button.h new file mode 100644 index 0000000..52c7b4d --- /dev/null +++ b/MSOS_ESP/main/button.h @@ -0,0 +1,30 @@ +#ifndef BUTTON_H_ +#define BUTTON_H_ + +#include +#include "state.h" + +#define BUTTON_PASS_TIMES 50 +#define BUTTON_LONGPASS_TIMES 1000 + +struct button { + uint8_t lock; + uint8_t pass; + uint8_t longpass; + uint8_t passread; + uint8_t pass_; + uint8_t longpassread; + uint32_t time; +}; + + +void button_init(void); +void button_set_pass(struct button *b); +void button_set_longpass(struct button *b); +char button_pass(struct button *b); +char button_pass_(struct button *b); +char button_longpass(struct button *b); +void button_reset(struct button *b); +void button_updata(struct button *b, char a); + +#endif