C语言面向对象和分层设计方法的理解
已STM32的GPIO做LED灯为例#1.系统分层层级职责例子应用层业务逻辑如呼吸灯、闪烁暴击led_blink()设备驱动层实现具体的LED操作开、关、翻转led_on()/led_off硬件抽象层直接操作GPIO寄存器或标准库函数以及HAL函数HAL_GPIO_WritePin#2.面向对象的C实现##2.1定义LED对象的结构体/* led.h */ #ifndef __LED_H #define __LED_H #include stdint.h #include stdboot.h struct led_device; //LED操作函数指针类型 typedef void (*led_func_t)(struct led_device *led) //LED 对象结构体 类似class typedef struct led_device { bool state; //当前状态 uint16_t gpio_pin; //GPIO引脚 GPIO_TypeDef *gpio_port; //GPIO端口 //公共方法 void (*init) (struct led_device *led); void (*on) (struct led_device *led); void (*off) (struct led_device *led); }LedDevice; //构造函数 LedDevice* Led_Create(GPIO_TypeDef *gpio_port uint16_t gpio_pin)##2.2实现具体的LED对象封装多态/* led.c*/ #include led.h #include stm32f1xx_hal.h static void led_init_impl(LedDevice *led) { GPIO_InitTypeDef GPIO_InitStruct {0}; GPIO_InitStruct.pin led-gpio_pin; GPIO_InitStruct.Mode GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(led-gpio_port, GPIO_InitStruct); led-state false; led-off(led); } static void led_on_impl(LedDevice *led) { HAL_GPIO_WritePin(led-gpio_port, led-gpio_pin, GPIO_PIN_SET); led-state true; } static void led_off_impl(LedDevice *led) { HAL_GPIO_WritePin(led-gpio_port, led-gpio_pin, GPIO_PIN_RESET); led-state false; } static void led_toggle_impl(LedDevice *led) { if (led-state) { led-off(led); } else { led-on(led); } } LedDevice *led_Create(GPIO_TypeDef *port, uint16_t pin) { static LedDevice led_pool[4]; static int idx 0; if(idx 4) return NULL; LedDvice *led led_pool(idx) led-gpio_port port; led-gpio_pin pin; led-state false; led-init led_init_impl; led-on led_on_impl; led-toggle led_toggle_impl; led-init(led); }##2.3应用层使用LED对象分层示例#include led.h //全局对象 LedDevice *led_status; LedDevice *led_error; int main(void) { HAL_Init(); SystemClock_Config(); led_status led_create(GPIOC,GPIO_PIN_13); led_error led_create(GPIOA,GPIO_PIN_5); led_status-on(led_status); while(1) { HAL_Delay(500); led_error-toggle(led_error); } }#3.扩展多态;不同类型LEDPWM呼吸灯##3.1定义PWM LED的派生结构体继承/*pwm_led.h*/ typedef struct { LedDevice parent; TIM_HandleTypDef *htim; uint32_t channel; uint8_t brightness; }PwmLedDevice;##3.2实现PWM LED的操作函数/* pwm_led.c */ static void pwm_led_on_impl(LedDevice *led) { PwmLedDevice *pwm_led (PwmLedDevice*)led; // 类型转换 pwm_led-brightness 255; __HAL_TIM_SET_COMPARE(pwm_led-htim, pwm_led-channel, pwm_led-brightness); led-state true; } static void pwm_led_off_impl(LedDevice *led) { PwmLedDevice *pwm_led (PwmLedDevice*)led; pwm_led-brightness 0; __HAL_TIM_SET_COMPARE(pwm_led-htim, pwm_led-channel, 0); led-state false; } // 构造函数 PwmLedDevice* pwm_led_create(TIM_HandleTypeDef *htim, uint32_t channel) { static PwmLedDevice pool[2]; static int idx 0; PwmLedDevice *dev pool[idx]; dev-htim htim; dev-channel channel; dev-brightness 0; // 父类指针的方法指向 PWM 实现 dev-parent.on pwm_led_on_impl; dev-parent.off pwm_led_off_impl; dev-parent.toggle NULL; // 可以不支持 dev-parent.init pwm_led_init_impl; dev-parent.init((LedDevice*)dev); return dev; }这样应用层用完全相同的led-on().led-off控制普通的GPIO LED或PWM led 实现了多态。