按钮事物需要重写

This commit is contained in:
2026-07-31 21:28:28 +08:00
parent d9cc6e1f3a
commit 952ff88b6e
7 changed files with 119 additions and 71 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
idf_component_register(SRCS "hello_world_main.c" "iic.c" "oled.c" "state.c" "tca9535.c" "button.c" "moonshineOS_core/cli.c"
idf_component_register(SRCS "power.c" "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")
+8 -66
View File
@@ -1,9 +1,7 @@
#include "button.h"
#include "esp_timer.h"
struct button button_ems;
struct button button_power;
struct button button_beep;
void button_init(void)
{
@@ -12,94 +10,38 @@ 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;
}
}
}
+2
View File
@@ -6,6 +6,7 @@
#define BUTTON_PASS_TIMES 50
#define BUTTON_LONGPASS_TIMES 1000
#define BUTTON_POWER_LONGPASS_TIMES 2000
struct button {
uint8_t lock;
@@ -15,6 +16,7 @@ struct button {
uint8_t pass_;
uint8_t longpassread;
uint32_t time;
uint32_t longpass_time;
};
+4 -4
View File
@@ -17,6 +17,7 @@
#include "oled.h"
#include "state.h"
#include "tca9535.h"
#include "power.h"
#include "cli.h"
void app_main(void)
@@ -51,10 +52,9 @@ void app_main(void)
state_init();
iic_init();
tca9535_init();
//tca9535 P12是POWER_EN,这个io为1时维持供电,需要设为输出模式,需要先设为0再delay一段时间然后再将这个io设为1
//tca9535_set_pin(12, 0);
//delay(1000);
//tca9535_set_pin(12, 1);
power_init();
oled_init();
cli_init();
+72
View File
@@ -0,0 +1,72 @@
#include "power.h"
#include "button.h"
#include "tca9535.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
static const char *TAG = "POWER";
#define POWER_EN_LATCH_DELAY_MS 100
#define POWER_TASK_POLL_MS 10
#define POWER_TASK_STACK 2048
#define POWER_TASK_PRIO 5
static bool s_latched = false;
static void power_latch_on(void)
{
tca9535_digital_write(POWER_EN_PIN, 0);
vTaskDelay(POWER_EN_LATCH_DELAY_MS / portTICK_PERIOD_MS);
tca9535_digital_write(POWER_EN_PIN, 1);
s_latched = true;
ESP_LOGI(TAG, "电源自锁, POWER_EN=1");
}
static void power_off(void)
{
ESP_LOGI(TAG, "关机, POWER_EN=0");
tca9535_digital_write(POWER_EN_PIN, 0);
}
static void power_task(void *arg)
{
uint8_t level = 1;
while (1) {
if (tca9535_digital_read(POWER_KEY_PIN, &level) != ESP_OK) {
vTaskDelay(POWER_TASK_POLL_MS / portTICK_PERIOD_MS);
continue;
}
button_updata(&button_power, level);
if (button_longpass(&button_power)) {
if (s_latched) {
power_off();
} else {
power_latch_on();
}
}
vTaskDelay(POWER_TASK_POLL_MS / portTICK_PERIOD_MS);
}
}
esp_err_t power_init(void)
{
esp_err_t ret = tca9535_pin_mode(POWER_EN_PIN, TCA9535_OUTPUT);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "POWER_EN引脚配置失败");
return ret;
}
ret = tca9535_digital_write(POWER_EN_PIN, 0);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "POWER_EN复位失败");
return ret;
}
vTaskDelay(2000 / portTICK_PERIOD_MS);
// 背光测试: P1.0 先设输出再置高
tca9535_pin_mode(TCA9535_PIN_P10, TCA9535_OUTPUT);
tca9535_digital_write(TCA9535_PIN_P10, 1);
ESP_LOGI(TAG, "电源管理初始化成功");
return ESP_OK;
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef POWER_H_
#define POWER_H_
#include "esp_err.h"
#define POWER_EN_PIN TCA9535_PIN_P12
#define POWER_KEY_PIN TCA9535_PIN_P13
esp_err_t power_init(void);
#endif
+20
View File
@@ -20,6 +20,26 @@
#define TCA9535_PIN_COUNT 16
// P0 口 (0-7)
#define TCA9535_PIN_P00 0 // P0.0
#define TCA9535_PIN_P01 1 // P0.1
#define TCA9535_PIN_P02 2 // P0.2
#define TCA9535_PIN_P03 3 // P0.3
#define TCA9535_PIN_P04 4 // P0.4
#define TCA9535_PIN_P05 5 // P0.5
#define TCA9535_PIN_P06 6 // P0.6
#define TCA9535_PIN_P07 7 // P0.7
// P1 口 (8-15)
#define TCA9535_PIN_P10 8 // P1.0
#define TCA9535_PIN_P11 9 // P1.1
#define TCA9535_PIN_P12 10 // P1.2
#define TCA9535_PIN_P13 11 // P1.3
#define TCA9535_PIN_P14 12 // P1.4
#define TCA9535_PIN_P15 13 // P1.5
#define TCA9535_PIN_P16 14 // P1.6
#define TCA9535_PIN_P17 15 // P1.7
esp_err_t tca9535_init(void);
esp_err_t tca9535_read_reg(uint8_t reg, uint8_t *val);