/** * 按键输入与 ADC 读取模块 * 按键: PB12(上), PB13(下), PB14(确认), PB15(返回) - 上拉输入, 按下低电平 * ADC: 16通道 (ADC_IN0~15), 使用 ADC2 扫描 */ #ifndef INPUT_H #define INPUT_H #include "main.h" #include "adc.h" #include "config.h" /* 按键定义 */ #define KEY_UP 0 #define KEY_DOWN 1 #define KEY_OK 2 #define KEY_BACK 3 #define KEY_NONE 0xFF /* 按键状态 */ #define KEY_STATE_IDLE 0 #define KEY_STATE_PRESS 1 #define KEY_STATE_HOLD 2 /* 长按 */ #define KEY_STATE_RELEASE 3 /* 长按阈值 (ms) */ #define KEY_LONG_PRESS_MS 800 #define KEY_POWER_OFF_MS 2000 /* ADC 通道数 */ #define ADC_CHANNEL_COUNT 16 /* ADC 原始值范围 */ #define ADC_RAW_MIN 0 #define ADC_RAW_MAX 4095 /* 函数声明 */ void Input_Init(void); void Input_Scan(void); /* 每 10ms 调用一次 */ /* 按键查询 */ uint8_t Input_GetKey(void); /* 返回当前按下的键 (单次) */ uint8_t Input_GetKeyState(uint8_t key); /* 获取按键状态 */ uint8_t Input_IsKeyPressed(uint8_t key); /* 是否刚按下 */ uint8_t Input_IsKeyHeld(uint8_t key); /* 是否长按 (800ms) */ uint8_t Input_IsKeyHeldFor(uint8_t key, uint32_t ms); /* 是否按住指定时长 */ uint8_t Input_IsKeyReleased(uint8_t key); /* 是否刚释放 */ /* ADC 读取 */ uint16_t Input_GetADC(uint8_t ch); /* 读取指定通道原始值 */ uint16_t Input_GetADC_Percent(uint8_t ch); /* 读取百分比 0-1000 */ void Input_ReadAllADC(void); /* 读取所有 ADC 通道 */ /* ADC 值存储 (用于通道映射) */ extern uint16_t adc_values[ADC_CHANNEL_COUNT]; extern uint8_t key_values[4]; /* 按键数字值 (0/1) */ /* 引脚编码解码 */ GPIO_TypeDef* Config_GetPort(uint16_t pin_code); uint16_t Config_GetPin(uint16_t pin_code); #endif /* INPUT_H */