一个设备接口实例(迭代)

[复制链接]
25145|18
 楼主| keer_zu 发表于 2021-12-24 07:37 | 显示全部楼层 |阅读模式
一个设备接口实例
这是之前给的一个接口的实例。
后续进行了迭代开发,陆续在楼下更新@qin552011373
 楼主| keer_zu 发表于 2021-12-24 07:43 | 显示全部楼层


接口的定义:

simple.h

  1. #ifndef __SAMPLE_H__
  2. #define __SAMPLE_H__

  3. #include "k_list.h"

  4. #ifndef NULL
  5. #define NULL ((void *)0)
  6. #endif


  7. typedef struct {
  8.         int (*on_info_recv)(void *);
  9.         int (*sample)(void *);
  10. }sample_methods_t;


  11. typedef struct {
  12.         char *name;
  13.         struct list_head dev_node;
  14.         sample_methods_t* methods;
  15.         void *pdev;
  16. }sample_interface_t;


  17. int register_sampler(sample_interface_t *devi);
  18. int run_sample(void);



  19. #endif


 楼主| keer_zu 发表于 2021-12-24 07:44 | 显示全部楼层
注册和运行:
simple.c
  1. #include <stdio.h>
  2. #include "sample.h"
  3. #include "os_depend.h"

  4. struct list_head g_samplers;

  5. int register_sampler(sample_interface_t *dev)
  6. {
  7.         if(dev == NULL) {
  8.                 printf("%d: devi is NULL!\n",__LINE__ + 1);
  9.                 return -__LINE__;
  10.         }
  11.        
  12.         if(dev->name == NULL || dev->methods == NULL) {
  13.                 printf("%d: name or methods or method is NULL!\n",__LINE__ + 1);
  14.                 return -__LINE__;
  15.         }

  16.        
  17.         ENTER_CRITICAL();
  18.         list_add(&dev->dev_node,&g_samplers);
  19.         EXIT_CRITICAL();
  20.        
  21.         return 0;
  22. }

  23. int run_sample(void)
  24. {
  25.         sample_interface_t *pos;
  26.         list_for_each_entry(pos,&g_samplers,dev_node) {
  27.                 if(pos->methods->sample != NULL) {
  28.                         pos->methods->sample(pos->pdev);
  29.                 }
  30.         }
  31.        
  32.         return 0;
  33. }


 楼主| keer_zu 发表于 2021-12-24 07:45 | 显示全部楼层
接口的实现之485:头文件:simple_485.h

  1. #ifndef __SAMPLE_485_H__
  2. #define __SAMPLE_485_H__

  3. #include "k_list.h"

  4. typedef struct {
  5.         char *name;
  6.         struct list_head node;
  7. }sample_485_t;


  8. int sample_485_init(void);



  9. #endif


 楼主| keer_zu 发表于 2021-12-24 07:46 | 显示全部楼层
接口的实现之485:simple_485.c


  1. #include <stdio.h>
  2. #include "sample.h"
  3. #include "sample_485.h"
  4. #include "os_depend.h"

  5. static int sample_485(void *param)
  6. {
  7.         sample_485_t *dev485;
  8.         if(param ==NULL){
  9.                 printf("%d: param NULL!\n",__LINE__ + 1);
  10.                 return -__LINE__;
  11.         }

  12.         list_for_each_entry(dev485,(struct list_head *)param,node) {

  13.         }
  14.        
  15.         return 0;
  16. }

  17. static int on_485_recv(void *param)
  18. {
  19.         if(param ==NULL){
  20.                 printf("%d: param NULL!\n",__LINE__ + 1);
  21.                 return -__LINE__;
  22.         }
  23.        
  24.         return 0;
  25. }


  26. sample_methods_t sample_485_methords = {
  27.         .sample = sample_485,
  28.         .on_info_recv = on_485_recv
  29. };

  30. sample_485_t g_sampler_485;

  31. static struct list_head dev_485_head;

  32. sample_interface_t dev_485 = {
  33.         .name = "485",
  34.         .pdev = &dev_485_head,
  35.         .methods = &sample_485_methords
  36. };


  37. int sample_485_init(void)
  38. {
  39.         INIT_LIST_HEAD(&dev_485_head);

  40.         int ret = register_sampler(&dev_485);
  41.        
  42.         return ret;
  43. }

  44. int add_485_sample_dev(struct list_head *node)
  45. {
  46.         ENTER_CRITICAL();
  47.         list_add(node,&dev_485_head);
  48.         EXIT_CRITICAL();
  49.         return 0;
  50. }


 楼主| keer_zu 发表于 2021-12-24 07:48 | 显示全部楼层
接口的实现之can:头文件:simple_can.h


  1. #ifndef __SAMPLE_CAN_H__
  2. #define __SAMPLE_CAN_H__

  3. #include "k_list.h"

  4. typedef unsigned char (*CAN_MODE_INIT_FUN)(unsigned char,unsigned char,unsigned char,unsigned short,unsigned char);
  5. typedef int (*CAN_RECV_MSG)(unsigned char *,unsigned int *,unsigned char *);
  6.        

  7. typedef struct {
  8.         char *name;
  9.         unsigned char tsjw;
  10.         unsigned char tbs2;
  11.         unsigned char tbs1;
  12.         unsigned short brp;
  13.         unsigned char mode;
  14.         CAN_MODE_INIT_FUN init;
  15.         CAN_RECV_MSG recv_msg;
  16.         struct list_head node;
  17. }sample_can_t;


  18. int sample_can_init(unsigned char);


  19. #endif


 楼主| keer_zu 发表于 2021-12-24 07:49 | 显示全部楼层
接口的实现之can:simple_can.c


  1. #include <stdio.h>
  2. #include "sample.h"
  3. #include "sample_can.h"
  4. #include "Device_info.h"
  5. #include "os_depend.h"

  6. extern ConfigCan_Info_t        CAN_Info;

  7. static int sample_can(void *param)
  8. {
  9.         sample_can_t *can;
  10.         if(param ==NULL){
  11.                 printf("%d: param NULL!\n",__LINE__ + 1);
  12.                 return -__LINE__;
  13.         }

  14.         list_for_each_entry(can,(struct list_head *)param,node) {
  15.                 if(can->recv_msg!= NULL) {
  16.                         //printf("+++++++++ name:%s\n",can->name);
  17.                         //pos->init(pos->tsjw,pos->tbs2,pos->tbs1,pos->brp,pos->mode);
  18.                 }
  19.         }
  20.        
  21.         return 0;
  22. }

  23. static int on_can_recv(void *param)
  24. {
  25.         if(param ==NULL){
  26.                 printf("%d: param NULL!\n",__LINE__ + 1);
  27.                 return -__LINE__;
  28.         }
  29.        
  30.         return 0;
  31. }


  32. sample_methods_t sample_can_methords = {
  33.         .sample = sample_can,
  34.         .on_info_recv = on_can_recv
  35. };

  36. static struct list_head dev_can_head;



  37. int add_can_sample_dev(struct list_head *node)
  38. {
  39.         ENTER_CRITICAL();
  40.         list_add(node,&dev_can_head);
  41.         EXIT_CRITICAL();
  42.         return 0;
  43. }


  44. sample_can_t g_sampler_can1 = {
  45.         .name = "can1",
  46.         .tsjw = CAN_SJW_1tq,
  47.         .tbs2 = CAN_BS2_3tq,
  48.         .tbs1 = CAN_BS1_10tq,
  49.         .brp  = 12,
  50.         .mode = CAN_Mode_Normal,
  51.         .init = CAN1_Mode_Init,
  52.         .recv_msg = CAN1_Receive_Msg_Ex
  53. };
  54. sample_can_t g_sampler_can2 = {
  55.         .name = "can2",
  56.         .tsjw = CAN_SJW_1tq,
  57.         .tbs2 = CAN_BS2_3tq,
  58.         .tbs1 = CAN_BS1_10tq,
  59.         .brp  = 12,            //CAN1初始化成功 波特率为 250K
  60.         .mode = CAN_Mode_Normal,
  61.         .init = CAN2_Mode_Init,
  62.         .recv_msg = CAN2_Receive_Msg_Ex
  63. };


  64. static int sample_can_start(unsigned char channel)
  65. {
  66.         switch(channel) {
  67.                 case 0x00: //默认不开启can       
  68.                         printf("\r\nCAN不初始化\r\n");
  69.                         break;
  70.                 case 0x01: //开启can1
  71.                         add_can_sample_dev(&g_sampler_can1.node);
  72.                         break;
  73.                 case 0x02: //开启can2
  74.                         add_can_sample_dev(&g_sampler_can2.node);
  75.                         break;
  76.                 case 0x03: //开启can3
  77.                         add_can_sample_dev(&g_sampler_can1.node);
  78.                         add_can_sample_dev(&g_sampler_can2.node);
  79.                         break;
  80.                 default:
  81.                         break;
  82.         }

  83.         return 0;
  84. }

  85. static sample_interface_t dev_can = {
  86.                 .name = "can",
  87.                 .pdev = &dev_can_head,
  88.                 .methods = &sample_can_methords
  89.         };


  90. int sample_can_init(unsigned char channel)
  91. {

  92.         INIT_LIST_HEAD(&dev_can_head);
  93.                
  94.        
  95.         sample_can_t *pos;

  96.         sample_can_start(channel);

  97.         list_for_each_entry(pos,&dev_can_head,node) {
  98.                 if(pos->init != NULL) {
  99.                         printf("+++++++ can init:init:0x%x  pos->tsjw:%d\n",pos->init,pos->tsjw);
  100.                         pos->init(pos->tsjw,pos->tbs2,pos->tbs1,pos->brp,pos->mode);
  101.                 }
  102.         }

  103.         int ret = register_sampler(&dev_can);
  104.        
  105.         return ret;
  106. }



评分

参与人数 1威望 +10 收起 理由
qin552011373 + 10 很给力!

查看全部评分

 楼主| keer_zu 发表于 2021-12-24 07:51 | 显示全部楼层
更新完毕,欢迎关注,@qin552011373

@21ic小管家 @21小跑堂 请给个原创和编辑推荐,以上内容包括代码都是原创
qin552011373 发表于 2021-12-24 12:51 | 显示全部楼层
代码这样子写的话,太清晰了,搞起来好舒服
 楼主| keer_zu 发表于 2021-12-24 13:58 | 显示全部楼层
qin552011373 发表于 2021-12-24 12:51
代码这样子写的话,太清晰了,搞起来好舒服

多谢鼓励
qin552011373 发表于 2021-12-25 18:19 | 显示全部楼层

可以考虑有时间就来分享给我学习下
 楼主| keer_zu 发表于 2021-12-27 08:14 | 显示全部楼层
qin552011373 发表于 2021-12-25 18:19
可以考虑有时间就来分享给我学习下

想要分享什么?
qin552011373 发表于 2021-12-27 15:29 | 显示全部楼层

这些接口就很值得学习啊,其他没有炫出来就不清楚了

评论

有啥学习,就加了些函数指针,加了层回调,等你忙的时候,有BUG的时候,恨不得直接调用那函数看结果, 这些花样是有时间,有闲功夫慢慢折腾看的  发表于 2021-12-29 13:44
li880wert 发表于 2021-12-29 13:49 | 显示全部楼层
好多次我写项目涉及到LCD 时候都想 做前后台,前台画界面,注册事件(按键,信号,IO),然后后台处理回调,但是写着写着,就开始骂自己SB了
单片机这玩意 本来就是简单易用为主,搞些这应用 ,把本来可以写简单的程序 ,多占用了几KB ROM.
就像ST写的USB库样,本来 就是 几十行代码搞定的,经过ST的层层封装 整个几千行代码

评论

编程,简单易用是每个人的追求,但是不能把简陋当简单,哈哈  发表于 2021-12-30 13:13
 楼主| keer_zu 发表于 2021-12-30 13:05 | 显示全部楼层
li880wert 发表于 2021-12-29 13:49
好多次我写项目涉及到LCD 时候都想 做前后台,前台画界面,注册事件(按键,信号,IO),然后后台处理回调, ...

有多少单片机系统刚开始就为了”简单易用”的目的,后来越来越不简单易用了,到后来自己都看不下去了,软件开发是需要花心思的活,今天花的心思是为了明天少花心思。
现在的单片机系统越来越不简单了,特别是平台不如其他系统友好
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12949

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部