/** ****************************************************************************** * @file io_monitor.h * @brief IO状态监控模块头文件 * @author Application Layer * @version 1.1 ****************************************************************************** * @attention * 本模块实现四路数字输入(DI1-DI4)的状态监控 * 采用定时扫描+软件去抖方式检测IO状态变化 * 状态变化时通过回调函数上报,支持多端口路由 * * 修订历史: * v1.1 - 增加事件回调机制,支持多端口路由 ****************************************************************************** */ #ifndef __IO_MONITOR_H #define __IO_MONITOR_H #ifdef __cplusplus extern "C" { #endif #include #include #define IO_CHANNEL_COUNT 4 #define IO_SCAN_PERIOD_MS 10 #define IO_DEBOUNCE_COUNT 3 /** * @brief IO事件回调函数类型 * @note 当IO状态变化时调用,用于事件路由 * @param channel: 通道号(0-3对应DI1-DI4) * @param state: 当前状态(0=LOW, 1=HIGH) * @param event_msg: 事件消息字符串 */ typedef void (*io_event_callback_t)(uint8_t channel, uint8_t state, const char *event_msg); /** * @brief 初始化IO监控模块 * @note 初始化各通道状态,读取初始IO电平 * @param 无 * @retval 无 */ void IO_Monitor_Init(void); /** * @brief IO监控任务处理函数 * @note 在主循环中调用,每10ms扫描一次IO状态 * 检测到状态变化时自动上报 * @param 无 * @retval 无 */ void IO_Monitor_Task(void); /** * @brief 获取指定通道的IO状态 * @note 返回经过去抖处理后的稳定状态 * @param channel: 通道号(0-3对应DI1-DI4) * @retval 0: LOW, 1: HIGH */ uint8_t IO_Monitor_GetState(uint8_t channel); /** * @brief 获取所有IO通道状态 * @note 返回4位状态值,每位对应一个通道 * @param 无 * @retval 状态值 (bit0=DI1, bit1=DI2, bit2=DI3, bit3=DI4) */ uint8_t IO_Monitor_GetAllStates(void); /** * @brief 启用/禁用IO状态变化上报 * @note 禁用后状态变化不会触发上报,但状态仍会更新 * @param enable: true=启用, false=禁用 * @retval 无 */ void IO_Monitor_EnableReport(bool enable); /** * @brief 获取IO状态变化次数统计 * @note 用于调试和诊断 * @param channel: 通道号(0-3) * @retval 该通道的状态变化次数 */ uint32_t IO_Monitor_GetChangeCount(uint8_t channel); /** * @brief 设置IO事件回调函数 * @note 设置后,IO状态变化将通过回调函数上报 * @param callback: 回调函数指针,NULL则使用默认UART2输出 * @retval 无 */ void IO_Monitor_SetEventCallback(io_event_callback_t callback); #ifdef __cplusplus } #endif #endif