提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
面向对象之GPIO
提示:这里可以添加本文要记录的大概内容:
看了《UML+OOPC嵌入式C语言开发》后收到启发,原来面向对象编程和面向过程编程是两种截然不同的方式,一种是针对这个对象,把这个对象里的方法和属性封装在结构体中在书中又叫做类,而平常我们在面向过程编程就好比做房子,买材料,砌房子等都要一步步有自己来完成,只要哪个环节要进行修改,其他环节也要跟着改变,这不利于代码的一致,而面向对象编程就好比专人专做某件事,买材料就由采购者来实现,砌房子就由专门师傅实现。下面由LED来举例子来实现面向对象,改例子模仿卧室的灯,在床头边有一个控制按键,在门口旁边也有一个控制按键,皆控制卧室的同一盏灯
提示:以下是本篇文章正文内容,下面案例可供参考
一、main文件
#include<stdio.h>
#include"Lcd.h"
#include"switch.h"
extern struct LedOperation *LedNum[LEDNUM];
int main(void)
{
switch_led_t DoorSwitch,WallSwitch;
struct LedOperation* LpstrLedOper = NULL;
LedRegister();
/*下面两条注释打开,把14行屏蔽编译运行显示段错误,求解?*/
//LpstrLedOper = LedNum[0];
//SwitchInit(&DoorSwitch,LpstrLedOper);
SwitchInit(&DoorSwitch,LedNum[0]);
SwitchInit(&WallSwitch,LedNum[0]);
DoorSwitch.set_switch(&DoorSwitch);
WallSwitch.set_switch(&WallSwitch);
return 0;
}
二、Switch文件
1.Switch.h
代码如下(示例):
#ifndef __SWITCH__
#define __SWITCH__
#include"Lcd.h"
typedef struct switch_led
{
LedOperation *LedOper;
int status;
void (*init)(struct switch_led* LedSwitch,LedOperation *LedOper);
int (*get_status)(struct switch_led* LedSwitch);
void (*set_switch)(struct switch_led* LedSwitch);
}switch_led_t;
external void set_switch(struct switch_led* LedSwitch,int falg);
#endif /*__SWITCH__*/
2.Switch.c
代码如下(示例):
#include"switch.h"
#include<stdio.h>
int get_status(struct switch_led* LedSwitch);
void set_switch(struct switch_led* LedSwitch);
void SwitchInit(struct switch_led* LedSwitch,LedOperation *LedOper)
{
LedSwitch->get_status = get_status;
LedSwitch->LedOper = LedOper;
LedSwitch->status = 0;
LedSwitch->set_switch = set_switch;
}
int get_status(struct switch_led* LedSwitch)
{
return LedSwitch->status;
}
void set_switch(struct switch_led* LedSwitch)
{
int Ledstatus = 0;
//LedSwitch->status = falg;
Ledstatus = LedSwitch->LedOper->status;
LedSwitch->LedOper->status = !Ledstatus;
if(!Ledstatus)
{
LedSwitch->LedOper->write(LedSwitch->LedOper,1);
}
else
{
LedSwitch->LedOper->write(LedSwitch->LedOper,0);
}
}
三、Led文件
1、Led.c
#include"Lcd.h"
#include<stdio.h>
static void write(struct LedOperation* LedOper,int status);
static int read(struct LedOperation* LedOper);
struct LedOperation *LedNum[LEDNUM];
struct LedOperation Led1 =
{
.GPIO_PIN = 1,
.GPIO_NUM = 2,
.name = "Led_OK",
.status = 0,
.write = write,
.read = read,
};
struct LedOperation Led2 =
{
.GPIO_PIN = 2,
.GPIO_NUM = 3,
.name = "Led_NG",
.status = 0,
.write = write,
.read = read,
};
/* 结构体之间不能直接赋值,可以通过memcpy */
//struct LedOperation LedTotal[Led_NUM];
//LedTotal[0] = Led1;
//LedTotal[1] = Led2;
static void write(struct LedOperation* LedOper,int status)
{
LedOper->status = status;
/* 模拟使用库函数 */
//GPIO_Write(LedOper->GPIO_NUM,LedOper->GPIO_PIN,status);
if(!LedOper->status)
{
printf("%s is close\r\n",LedOper->name);
}
else
{
printf("%s is open\r\n",LedOper->name);
}
}
static int read(struct LedOperation* LedOper)
{
return LedOper->status;
}
int RegisterLed(struct LedOperation * LpstrLedOperation)
{
int i;
for(i=0;i<LEDNUM;i++)
{
if(!LedNum[i])
{
LedNum[i] = LpstrLedOperation;
printf("%s is register\n",LedNum[i]->name);
return i;
}
}
return -1;
}
void LedRegister(void)
{
/*注册LED*/
int i =RegisterLed(&Led1);
if(i<0)
{
printf("register error");
}
i =RegisterLed(&Led2);
if(i<0)
{
printf("register error");
}
}
2、Led.h
#ifndef __LCD_H__
#define __LCD_H__
#define LEDNUM 2
typedef enum
{
Led_OK = 0,
Led_NG,
Led_NUM,
}Led_Count;
typedef struct LedOperation
{
int GPIO_PIN;
int GPIO_NUM;
char *name;
int status;
void (*write)(struct LedOperation* LedOper,int status);
int (*read)(struct LedOperation* LedOper);
}LedOperation;
extern struct LedOperation *LedNum[LEDNUM];
extern void LedRegister(void);
extern struct LedOperation Led1;
#endif /* Lcd.h */