首先声明了一个如下类型的结构体类型_m_usmart_dev
- struct _m_usmart_dev
- {
- struct _m_usmart_nametab *funs; //函数名指针
- void (*init)(u8); //初始化
- u8 (*cmd_rec)(u8*str); //识别函数名及参数
- void (*exe)(void); //执行
- void (*scan)(void); //扫描
- u8 fnum; //函数数量
- u8 pnum; //参数数量
- u8 id; //函数id
- u8 sptype; //参数显示类型(非字符串参数):0,10进制;1,16进制;
- u16 parmtype; //参数的类型
- u8 plentbl[MAX_PARM]; //每个参数的长度暂存表
- u8 parm[PARM_LEN]; //函数的参数
- u8 runtimeflag; //0,不统计函数执行时间;1,统计函数执行时间,注意:此功能必须在USMART_ENTIMX_SCAN使能的时候,才有用
- u32 runtime; //运行时间,单位:0.1ms,最大延时时间为定时器CNT值的2倍*0.1ms
- };
定义了类型为_m_usmart_dev的结构体usmart_dev并将其初始化。即usmart_dev.fun=usmart_nametab //该成员为结构体数组指针变 量,将结构体数组usmart_nametab的首地址
赋给了他
usmart_dev.Init=usmart_init //该成员为无返回值输入参数为u8类型的函数指针变 量,将函数usmart_init地址赋给它
usmart_dev.cmd_rec=usmart_cmd_rec //该成员为返回值和输入参数都为u8类型 的函数指针变量,将函 数usmart_cmd_rec地址赋给它
其他成员就不一一说啦
- struct _m_usmart_dev usmart_dev=
- {
- usmart_nametab,
- usmart_init,
- usmart_cmd_rec,
- usmart_exe,
- usmart_scan,
- sizeof(usmart_nametab)/sizeof(struct _m_usmart_nametab),//函数数量
- 0, //参数数量
- 0, //函数ID
- 1, //参数显示类型,0,10进制;1,16进制
- 0, //参数类型.bitx:,0,数字;1,字符串
- 0, //每个参数的长度暂存表,需要MAX_PARM个0初始化
- 0, //函数的参数,需要PARM_LEN个0初始化
- };
usmart_nametab 是结构体数组其成员是类型为_m_usmart_nametab的结构体(该结构体成员func为函数指针变量,成员name为指向u8类型的指针变量)
- struct _m_usmart_nametab //声明结构体类型
- {
- void* func; //函数指针
- const u8* name; //函数名(查找串)
- };
- struct _m_usmart_nametab usmart_nametab[]=
- {
-
- #if USMART_USE_WRFUNS==1 //如果使能了读写操作
-
- (void*)read_addr,"u32 read_addr(u32 addr)",
-
- (void*)write_addr,"void write_addr(u32 addr,u32 val)",
-
- #endif
-
- (void*)delay_ms,"void delay_ms(u16 nms)",
-
- (void*)delay_us,"void delay_us(u32 nus)",
-
- (void*)LCD_Clear,"void LCD_Clear(u16 Color)",
-
- (void*)LCD_Fill,"void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)",
-
- (void*)LCD_DrawLine,"void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)",
-
- (void*)LCD_DrawRectangle,"void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)",
-
- (void*)LCD_Draw_Circle,"void Draw_Circle(u16 x0,u16 y0,u8 r)",
-
- (void*)LCD_ShowNum,"void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)",
-
- (void*)LCD_ShowString,"void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)",
-
- (void*)led_set,"void led_set(u8 sta)",
-
- (void*)test_fun,"void test_fun(void(*ledset)(u8),u8 sta)",
-
- (void*)LCD_ReadPoint,"u16 LCD_ReadPoint(u16 x,u16 y)",
-
- };
上面的代码是在定义并初始化usmart_nametab,也就是先定义结构体数组usmart_nametab再赋初值。
其中
usmart_nametab[0]={(void*)read_addr,"u32 read_addr(u32 addr)"}
usmart_nametab[1]={(void*)write_addr,"void write_addr(u32 addr,u32 val)"}
usmart_nametab[2]={(void*)delay_ms,"void delay_ms(u16 nms)"}
...............
与下面的代码是一个意思
- struct student //声明结构体
- {
- int num
- char name
- }
-
- struct student student1[]= //定义结构体数组并初始化
- {
- 1,"Li Ming",
- 2,"Li Ergou",
- 3,"Li Yifeng"
- }