所学均来自百问网
目录
1. 流程
2. 配置文件
# name can_be_touch command
led 1 led.sh
speaker 1
record 0
key1 0
key2 0
ap3216c 0
icm20608 0
RS485toCAN 0
CANtoRS485 0
4G 0
usb 0
otg_device 0
otg_host 0
serial 0
wifi 0
net0 0
net1 0
burn 0
ALL 0 test_sleep_key.sh
3. 数据结构的抽象
#ifndef _CONFIG_H
#define _CONFIG_H
#include <common.h>
#include <string.h>
#define ITEMCFG_MAX_NUM 30
#define CFG_FILE "/etc/test_gui/gui.conf"
// 描述每一行
typedef struct ItemCfg{
int index;//哪一项
char name[100];//名称
int bCanBeTouched;//是否可以被触摸
char command[100];//命令
}ItemCfg,*PItemCfg;
// 获取项的数量
int GetItemCfgCount(void);
// 获取某一个配置项
PItemCfg GetItemCfgByIndex(int index); // 根据项
PItemCfg GetItemCfgByName(char *name); // 根据名称
int ParseConfigFile(void);// 解析配置文件
#endif
4. 配置文件的处理
#include <config.h>
#include <stdio.h>
#include <string.h>
// 数组保存配置文件的项
static ItemCfg g_tItemCfgs[ITEMCFG_MAX_NUM];
static int g_iItemCfgCount = 0; // 保存总的项数
// 解析配置文件
int ParseConfigFile(void)
{
FILE *fd;
char buf[100];
char *p = buf;
// 1.打开文件
fd = fopen(CFG_FILE,"r");
if(!fd)
{
printf("can not open cfg file %s\n",CFG_FILE);
return -1;
}
while(fgets(buf,100,fd))
{
// 2.1 读出每一行
buf[99] = '\0';
// 2.2 除去开头的空格或TAB
p = buf;
while(*p == ' ' || *p == '\t')
p++;
// 2.3 忽略注释
if(*p == '#')
continue;
// 2.4 处理
g_tItemCfgs[g_iItemCfgCount].command[0] = '\0';
g_tItemCfgs[g_iItemCfgCount].index = g_iItemCfgCount;
sscanf(p,"%s %d %s",g_tItemCfgs[g_iItemCfgCount].name,&g_tItemCfgs[g_iItemCfgCount].bCanBeTouched,g_tItemCfgs[g_iItemCfgCount].command);
g_iItemCfgCount++;
}
return 0;
}
// 获取项的数量
int GetItemCfgCount(void)
{
return g_iItemCfgCount;
}
// 获取某一个配置项
PItemCfg GetItemCfgByIndex(int index)
{
if(index < g_iItemCfgCount)
return &g_tItemCfgs[index];
else
return NULL;
}
PItemCfg GetItemCfgByName(char *name)
{
int i;
for(i = 0; i < g_iItemCfgCount;i++)
{
if(strcmp(name, g_tItemCfgs[i].name) == 0)
return &g_tItemCfgs[i];
}
return NULL;
}
5. 测试
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <disp_manager.h>
#include <stdlib.h>
#include <font_manager.h>
#include <input_manager.h>
#include <page_manager.h>
int main(int argc, char **argv)
{
int error;
if(argc != 2)
{
printf("Usage: %s <font_file>\n",argv[0]);
return -1;
}
// 初始化显示系统
DisplayInit();
// 选择默认显示设备
SelectDefaultDisplay("fb");
// 初始化默认显示设备
InitDefaultDisplay();
// 初始化输入系统
InputInit();
// 初始化输入设备
InputDeviceInit();
// 初始化文字系统
FontsRegister();
error = SetectAndInitFont("freetype",argv[1]);
if(error)
{
printf("SetectAndInitFont err\n");
return -1;
}
// 初始化页面系统
PagesRegister();
// 运行业务系统的主页面
Page("main")->Run(NULL);
return 0;
}