Files
Power-Pico/Power_Pico/BSP/KEY/key.h

104 lines
2.3 KiB
C
Raw Normal View History

2025-08-05 16:06:46 +08:00
#ifndef __KEY_H__
#define __KEY_H__
#ifdef __cplusplus
extern "C" {
#endif
#pragma once
2025-08-05 16:06:46 +08:00
#include "main.h"
#include <stdint.h>
#include <stdbool.h>
// 时间参数单位ms
2025-08-05 16:06:46 +08:00
#define KEY_LONG_THRESHOLD_MS 500 // 长按判定时间
#define KEY_REPEAT_INTERVAL_MS 150 // 连发间隔时间
#define KEY_DEBOUNCE_MS 3 // 消抖时间
/* 掩码工具(位索引即 key_id_t */
#define KEY_BIT(id) (1U << (uint8_t)(id))
#define KEY_ALL_MASK ((uint32_t)((KEY_ID_MAX >= 32) ? 0xFFFFFFFFu : ((1u << KEY_ID_MAX) - 1)))
2025-08-12 18:44:02 +08:00
//KEYB
#define KEYB_PORT GPIOA
#define KEYB_PIN GPIO_PIN_0
2025-11-25 19:53:23 +08:00
//KEYN
#define KEYN_PORT GPIOA
#define KEYN_PIN GPIO_PIN_4
2025-08-12 18:44:02 +08:00
//KEYL
#define KEYL_PORT GPIOA
2025-10-15 17:02:57 +08:00
#define KEYL_PIN GPIO_PIN_3
2025-08-12 18:44:02 +08:00
//KEYR
#define KEYR_PORT GPIOA
2025-10-15 17:02:57 +08:00
#define KEYR_PIN GPIO_PIN_2
2025-08-12 18:44:02 +08:00
2025-11-25 19:53:23 +08:00
//KEYY
#define KEYY_PORT GPIOA
#define KEYY_PIN GPIO_PIN_1
2025-08-05 16:06:46 +08:00
/**
* @brief ID
*/
typedef enum {
KEY_ID_B = 0, // KEYB
KEY_ID_Y, // KEYY
KEY_ID_L, // KEYL
KEY_ID_R, // KEYR
KEY_ID_N, // KEYN
KEY_ID_MAX
} key_id_t;
/**
* @brief
*/
typedef enum {
KEY_EVT_NONE = 0,
KEY_EVT_DOWN = 1, // 去抖后稳定按下
KEY_EVT_UP = 2, // 去抖后稳定释放
KEY_EVT_CLICK = 3, // 短按(一次按下->释放,且未触发长按)
KEY_EVT_LONG = 4, // 达到长按阈值触发一次
KEY_EVT_REPEAT = 5 // 长按连发周期事件
} key_evt_type_t;
/**
* @brief
*/
typedef struct {
key_id_t id;
key_evt_type_t type;
} key_event_t;
/**
2025-12-02 19:40:48 +08:00
* @brief "|"
*/
typedef enum {
KEY_MODE_REPORT_DOWN_UP = 0x01, // 上报 DOWN/UP
KEY_MODE_CLICK_ON_RELEASE = 0x02, // 释放时若未长按,上报 CLICK
KEY_MODE_LONG_ONCE = 0x04, // 达到阈值上报一次 LONG
KEY_MODE_REPEAT = 0x08 // 达到阈值后周期性上报 REPEAT
} key_mode_t;
/**
* @brief ms
*/
typedef struct {
uint16_t debounce_ms; // 消抖时间,建议 3~20
uint16_t long_ms; // 长按判定时间,建议 400~800
uint16_t repeat_ms; // 连发间隔,建议 80~200
} key_timing_t;
2025-08-05 16:06:46 +08:00
void Key_Init(void);
bool Key_Scan(key_event_t* out_evt);
2025-08-05 16:06:46 +08:00
#ifdef __cplusplus
}
#endif
#endif