参考

【编程之美】常用于单片机的接口适配器模式C语言实现.pdf

  • 基于以上内容稍作修改
  • 结合RT-Thread操作系统
  • 设计了面向对象的LED驱动接口
  • 可移植性大大提高,移植文件时无任何全局变量
  • 将对象的要素作为指针传递,无需考虑参数过大导致出入栈耗时问题
  • 函数作为方法封装

源文件

  1. /*
  2. * Copyright (c) 2020 - ~, HIT_HERO Team
  3. *
  4. * LED MODULE SOUCE FILE
  5. * Used in RT-Thread Operate System
  6. *
  7. * Change Logs:
  8. * Date Author Notes QQ
  9. * 2020-06-02 WangXi first version 1098547591
  10. */
  11. #include "Module_LED.h"
  12. /* User Code Begin*/
  13. void LEDTest(int argc,char **argv)
  14. {
  15. rt_err_t res = RT_EOK;
  16. MODULE_LED dev_led_1 = {GET_PIN(C, 3),MODULE_LED_ON};
  17. res = Module_Led_Config(&dev_led_1);
  18. if(res != RT_EOK){
  19. rt_kprintf("Error: Some wrong happened while led config\n");
  20. return;
  21. }
  22. if (argc < 2){
  23. rt_kprintf("Error: Command missing arguments\n");
  24. return;
  25. }
  26. if(!rt_strcmp(argv[1], "-on")){
  27. dev_led_1.Enable(&dev_led_1);
  28. }
  29. else if(!rt_strcmp(argv[1], "-off")){
  30. dev_led_1.Disable(&dev_led_1);
  31. }
  32. else{
  33. rt_kprintf("Error: Command not found\n");
  34. }
  35. }
  36. MSH_CMD_EXPORT(LEDTest , LEDTest(PC3) <-on|-off>);
  37. /* User Code End */
  38. /* Static Method */
  39. static void Module_LedInit(MODULE_LED *module);
  40. static void Module_LedToggle(MODULE_LED *module);
  41. static void Module_LedEnable(MODULE_LED *module);
  42. static void Module_LedDisable(MODULE_LED *module);
  43. /* Global Method */
  44. rt_err_t Module_Led_Config(MODULE_LED *Dev_LED){
  45. if(Dev_LED->Init==NULL &&
  46. Dev_LED->Toggle==NULL &&
  47. Dev_LED->Enable==NULL &&
  48. Dev_LED->Disable == NULL
  49. ){
  50. /* Link the Method */
  51. Dev_LED->Init = Module_LedInit;
  52. Dev_LED->Toggle = Module_LedToggle;
  53. Dev_LED->Enable = Module_LedEnable;
  54. Dev_LED->Disable = Module_LedDisable;
  55. }
  56. else{
  57. rt_kprintf("Warning: Module Led is Configed twice\n");
  58. return RT_ERROR;
  59. }
  60. /* Device Init */
  61. Dev_LED->Init(Dev_LED);
  62. return RT_EOK;
  63. }
  64. /* Static Method */
  65. static void Module_LedInit(MODULE_LED *module){
  66. rt_pin_mode(module->pin, PIN_MODE_OUTPUT);
  67. rt_pin_write(module->pin, module->status);
  68. }
  69. static void Module_LedToggle(MODULE_LED *module){
  70. rt_pin_write(module->pin, !rt_pin_read(module->pin));
  71. module->status = rt_pin_read(module->pin);
  72. }
  73. static void Module_LedEnable(MODULE_LED *module){
  74. rt_pin_write(module->pin, MODULE_LED_ON);
  75. module->status = rt_pin_read(module->pin);
  76. }
  77. static void Module_LedDisable(MODULE_LED *module){
  78. rt_pin_write(module->pin, MODULE_LED_OFF);
  79. module->status = rt_pin_read(module->pin);
  80. }
  81. /************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/

头文件

  1. /*
  2. * Copyright (c) 2020 - ~, HIT_HERO Team
  3. *
  4. * LED MODULE HEAD FILE
  5. * Used in RT-Thread Operate System
  6. *
  7. * Change Logs:
  8. * Date Author Notes QQ
  9. * 2020-06-02 WangXi first version 1098547591
  10. */
  11. #ifndef _MODULE_LED_H_
  12. #define _MODULE_LED_H_
  13. #define _LED_POSITIVE_POLE //RESET Enable LED
  14. //#define _LED_NEGETIVE_POLE //SET Enable LED
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include <board.h>
  18. enum LED_STATION
  19. {
  20. #ifdef _LED_POSITIVE_POLE
  21. MODULE_LED_ON = 0,
  22. MODULE_LED_OFF
  23. #endif
  24. #ifdef _LED_NEGETIVE_POLE
  25. MODULE_LED_OFF = 0,
  26. MODULE_LED_ON
  27. #endif
  28. };
  29. struct _MODULE_LED
  30. {
  31. /* Property */
  32. rt_base_t pin; /*!< Specifies the LED module pins to be configured.
  33. This parameter is defined by function @ref GET_PIN(GPIOPORT, GPIO_PIN_NUM) */
  34. rt_bool_t status; /*!< Specifies the LED init status.
  35. This parameter is defined by @ref LED_STATION */
  36. /* Method */
  37. void (*Init)(struct _MODULE_LED *module);
  38. void (*Toggle)(struct _MODULE_LED *module);
  39. void (*Enable)(struct _MODULE_LED *module);
  40. void (*Disable)(struct _MODULE_LED *module);
  41. };
  42. typedef struct _MODULE_LED MODULE_LED;
  43. /* Glodal Method */
  44. rt_err_t Module_Led_Config(MODULE_LED *Dev_LED);
  45. #endif
  46. /************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/

使用

  1. MODULE_LED dev_SystemLed = {GET_PIN(C, 2),MODULE_LED_ON};
  2. Module_Led_Config(&dev_SystemLed);
  3. while (1){
  4. rt_thread_mdelay(500);
  5. dev_SystemLed.Toggle(&dev_SystemLed);
  6. }