工作中常常会写若干个模块,以便于提高代码的维护性阅读性。
将某个单独的功能代码用模块的方式写成独立的C文件不仅能XXX,而且还XXXX,反正XXX;
以下是一般代码中 Linux C中常用的模块级别的代码模版。如果需要建立一个模块。
复制,粘贴,搜索,替换......你懂得..........
/*
* @Author: hongzhunzhun
* @Date: 2023-08-17 17:01:45
* @email: hongzhunzhun@qq.com
* @LastEditors: hongzhunzhun
* @LastEditTime: 2023-10-16 15:28:23
* @FilePath: /monitorEvse1.0/src/Template/Template.c
* @Description:
* Copyright (C) 2023 by hongzhunzhun , All Rights Reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <pthread.h>
#include "hardware.h"
#include "midware.h"
#define TEMPLATE_FUNC_1 0x01
#define CAN_TEMPLATE_INDEX 1
/*状态机 分类*/
enum TEMPLATE_COM_STA {
TEMPLATE_COM_STA1,
TEMPLATE_COM_STA2,
TEMPLATE_COM_STA3
};
/*接收指令 分类*/
enum TEMPLATE_COM_RECV_CMD {
TEMPLATE_COM_RECV_CMD_1,
TEMPLATE_COM_RECV_CMD_2,
TEMPLATE_COM_RECV_CMD_3
};
/*上下文数据结构体*/
struct TEMPLATECONTENT {
int canfd;
pthread_t TemplatePthread;
int fsm;
int ReadStatus1;
int ReadStatus2;
};
struct TEMPLATECONTENT sTemplateContent;
static void MidWare_TemplateSendProcess(void)
{
switch(sTemplateContent.fsm)
{
case TEMPLATE_COM_STA1:
break;
case TEMPLATE_COM_STA2:
break;
case TEMPLATE_COM_STA3:
break;
default:
break;
}
}
static void MidWare_TemplateParserPack(struct can_frame *frame)
{
int32 CommandType, CommandData;
CommandType = frame->can_id;
CommandData = frame->data[0];
switch(CommandType)
{
case TEMPLATE_COM_RECV_CMD_1:
sTemplateContent.ReadStatus1 = CommandData;
break;
case TEMPLATE_COM_RECV_CMD_2:
sTemplateContent.ReadStatus2 = CommandData;
break;
default:
break;
}
}
static void MidWare_TemplateRecvProcess(void)
{
int ret = 0;
struct can_frame tCan_frame;
memset(&tCan_frame, 0, sizeof(tCan_frame));
ret = HalCanRecv(sTemplateContent.canfd, &tCan_frame);
if(ret > 0)
{
MidWare_TemplateParserPack(&tCan_frame);
}
}
/*TEMPLATE 主任务*/
static void *MidWare_TemplateProcess(void* arg)
{
while(1)
{
/*线程1*/
MidWare_TemplateSendProcess();
/*线程2*/
MidWare_TemplateRecvProcess();
usleep(30*1000);
}
}
/*TEMPLATE 模块初始化*/
void MidWare_TemplateInit()
{
int ret ;
memset(&sTemplateContent, 0, sizeof(sTemplateContent));
sTemplateContent.canfd = HalCanOpen(CAN_TEMPLATE_INDEX, 250000);
ret = pthread_create(&(sTemplateContent.TemplatePthread) , NULL, MidWare_TemplateProcess, NULL);
if (ret != 0)
{
printf("Template pthread error!");
}
}
void MidWare_TemplateFinaliz()
{
pthread_cancel(sTemplateContent.TemplatePthread);
HalCanClose(sTemplateContent.canfd);
}
stm32 裸机功能模版待补充;
freeRTOS 功能模版待补充