#ifndef BUTTON_H #define BUTTON_H /** * button.h — 第二个 OOPC 组件,与 Led 协作 * * 展示了 OOPC 组件之间的解耦方式:通过回调函数通知, * 不直接访问对方结构体 */ #include /* ── 前向声明 ── */ typedef struct Button_S Button_S; /* ── 按键事件类型 ── */ typedef enum { BTN_EVT_NONE = 0, BTN_EVT_PRESSED = 1, /* 按下 */ BTN_EVT_RELEASED = 2, /* 释放 */ BTN_EVT_CLICK = 3, /* 单击 */ BTN_EVT_DBL_CLICK = 4 /* 双击 */ } Button_Event; /* ── 回调函数类型:按键事件发生时通知上层 ── */ typedef void (*ButtonCallback)(Button_Event evt, void* context); /* ── 创建参数集(避免构造函数参数膨胀) ── */ typedef struct { uint8_t gpio_port; /* 0=A, 1=B, 2=C ... */ uint16_t gpio_pin; /* 0-15 */ uint8_t active_low; /* 1=按下时低电平, 0=高电平 */ ButtonCallback callback; /* 事件回调 */ void* context; /* 回调上下文(任意指针) */ } ButtonConfig; /* ── 公开 API ── */ Button_S* Button_Create(const ButtonConfig* cfg); void Button_Poll(Button_S* me); uint8_t Button_IsPressed(const Button_S* me); void Button_Destroy(Button_S* me); #endif /* BUTTON_H */