更新struct 存从文档中读到的数据(测试gpio)

源代码

测试gpio调用sdk库的三个函数

  1. GPIO端口初始化 要操作GPIO,应该首先初始化对应端口。
    其函数接口如下:
    error_t gpioInit(int pins, GPIO_MODE mode, bool_t value); -注:形参“pins”为模块外部表现的引脚序号,从1开始。
  2. 设置输出模式的GPIO端口高/低电平
    如果之前端口已正确配置为输出模式(GPIO_MODE_OUT_PP、GPIO_MODE_OUT_OD、GPIO_MODE_OUT_OD_PULLUP),可使用此接口配置其输出电平。其函数接口如下:
    error_t gpioSet(int pins, bool_t value);
  3. 获取输入模式的GPIO端口高/低电平值 如果之前端口已正确配置,可使用此接口获取其电平状态。
    其函数接口如下:bool_t gpioGet(int pins);

normal模式:测试循环timec次
special模式:测试循环time秒
loop模式:单个pins测试循环time秒,再测试下一个pins

执行代码./gpio gpio.txt result.txt total_pins
gpio.txt书写格式如下:

类型 time
pins mode values

// 在运行过程中动态分配内存
    gpio = (gpio_stu *) malloc(sizeof(gpio_stu) * (MAXSIZE)) ;
    Init[i] = gpioInit(pins , (mode) , 1);    //平台GPIO初始化
    Set[i] = gpioSet(pins , values);   //设置输出模式的GPIO端口高/低电平
    val[i] = gpioGet(pins);  //获取输入模式的GPIO端口高/低电平值

下面展示所有代码。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <ct_gpio.h>

int  MAXSIZE ;//测多少个gpio
time_t start , end ;
char type[10]; 
//int times , times , loop;
int times ,jump = 0;
/**********************************struct 函数*********************************************/
typedef struct gpio_stu
{
    int pin;
    int io_modes;
    int value;
}gpio_stu;

struct gpio_stu * gpio = NULL ;
/***********************************读文件*********************************************/
void read_buff(char *fopen_gpio_txt) 
{
    FILE * fp = NULL ;   // 定义文件指针:
    int i;  
	fp = fopen(fopen_gpio_txt, "r+") ;   // 打开文件
    if(NULL == fp)
	{
		perror("");  //输出错误原因
		return ;
    }	
	fscanf(fp , "%s" , type) ;
	printf("--------------------------\n");
	printf("读取到的type为:%s\n" , type) ;  //打印读到的type
	if ((0 != strcmp("normal", type )) && (0 != strcmp("special", type )) && (0 != strcmp("loop", type )))
	{
		printf("read_buff:type只能为'normal'or'speical'or'loop'\n");	
		jump ++ ;
		return ;
	} 
	//printf(" read_buff :trump here\n") ;
    fscanf(fp , "%d" , &times) ;
	printf("--------------------------\n");
	printf("读取到的times为:%d\n" , times) ;   //打印读到的type
    for(i = 0 ; i < MAXSIZE ; i++)
	{
	 	fscanf(fp,"%d %d %d",&gpio[i].pin,&gpio[i].io_modes,&gpio[i].value);
		printf("读取到的pin为:%d,读到的modes为:%d,读到的value为: %d,i:%d\n",gpio[i].pin,gpio[i].io_modes,gpio[i].value,i) ; 
		if(feof(fp))
		{
			break ;
        }
	}
    fclose(fp) ;  // 关闭文件
	return ;
}
/*******************************写文件**************************************************/
void compare_write(FILE * fp , int pins , int error ,int test)
{
	int i ;
	int error_ [] = {0 , -1 , -2 , -3 , -4 , -5 , -6 , -7 , -8 , -9 , -10 , -11 , -12 , -13 , -14 } ;
	char errors[][30] = {"无错误" , "错误" , "操作超时" , "资源满", "资源空" , "内存不足" , "系统未启动", "忙" , "IO错误 " , "参数列表太长 ", "参数无效 " , "权限被拒绝" , "魔数不匹配", "参数列表太短" , "取消" , "返回值未知"};
     for(i=0 ; i<15 ; i++)
    {
        if(error_[i] == error)
        {
            printf("error=%d , 第%d(i) 个显示:%s\n" , error , i , errors[i]) ;
        }
    }
    if( (error > 0) || (error < -14))
    {
    printf("pins=%d , 第15 个显示:%s\n" , error ,   errors[15]) ;
    }

	if (test == 0)
	{
		fprintf(fp ,"pins = %d , Init = %s " , pins , &errors) ;  // 写Init
		printf ("compare_write : pin = %d , Init = %s " , pins , errors);
	}
	else if(test == 1)
	{
        fprintf(fp ,"Set = %s/n " ,  &errors) ;  // 写Set
        printf("compare_write : Set = %s \n" ,  errors) ;
        fprintf(fp , "\n") ;
	}
	return ;
}
/**********************测量当前时间********************************************************/
int measure_time()
{
	int  cost ;
	//int timer = 50 * 1000 ; //延时50ms进行下一轮测试
	time(&end);
	cost = difftime(end,start);// double difftime(time_t time1, time_t time2) 返回 time1 和 time2 之间相差的秒数 (time1 - time2)
	int cost_time = (int) cost ; //cost设置成整形
	printf("measure_time times=%d*******************************\n",cost_time) ;
	return cost_time;
}
/**********************************测试 io***********************************************/
void test_buff(FILE * fp)
{
	int  i , modes , pins , values , cost_time ,  test , Init_w , Set_w ;
	int add_time = times ;
	int Init[MAXSIZE] , Set[MAXSIZE] , val[MAXSIZE] ;//, Init_w[MAXSIZE] , Set_w[MAXSIZE] ;
	for (i = 0 ; i < MAXSIZE ; i++)
	{
		pins = gpio[i].pin;
		values= gpio[i].value;
	    modes = gpio[i].io_modes;
    	printf("test_buff1 :pins = %d , values = %d , modes = %d\n",pins ,modes , values);
		GPIO_MODE mode = (GPIO_MODE) modes;	
		while(1)
		{
			Init[i] = gpioInit(pins , (mode) , 1);    //平台GPIO初始化
    		Set[i] = gpioSet(pins , values);   //设置输出模式的GPIO端口高/低电平
    		val[i] = gpioGet(pins);  //获取输入模式的GPIO端口高/低电平值	
			printf ("test_buff2 : pins = %d , Init = %d , Set = %d , val = %d\n", pins , Init[i] , Set[i] , val[i]) ;
			if((values != val[i]) || (Init[i] != 0) || (Set[i] != 0) )
			{   
				test = 0 ;
				printf("----------------------------------------------------\n");
				printf("1pins = %d , val = %d , values=%d\n" , pins , val[i] , values) ; 
				Init_w = Init[i] ;
		    	compare_write(fp , pins , Init_w , test) ;
				Set_w = Set[i] ;
				test++ ;
				compare_write(fp , pins , Set_w , test) ;
			}
				if((0 != strcmp("loop", type )))
				{
					break ;
				}
					printf(" test_buff_loopif :trump here\n") ;
					cost_time = measure_time() ;
					printf("if外  :add_time= %d\n\n", add_time) ;
					if (cost_time == add_time)//
				{
					printf ("cost_time运行到第%d秒^&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n",cost_time);	
					add_time = add_time + times;
					printf("if内  :add_time= %d\n\n", add_time) ;
					printf ("add_time=%d秒,times= %d ^&&&&&&&&&&&&&&&&&&&&&&&\n" , add_time, times);
			
					break ;
				}	

		}
	}
    return;
} 

/***********************************normal 模式**********************************/
void measure_mode_normal(FILE * fp)
{
	int i ;
	int timer = 50 * 1000 ; //延时50ms进行下一轮测试
	printf("measure_mode_normal times=%d*******************************\n",times) ;
	for ( i = 0; i < times; i++)
	{
		fprintf(fp , "times=%d\n",i+1) ;
		test_buff(fp);
		usleep(timer); //延时50ms
		fprintf(fp , "\n") ;
	}
	return ;
}
/***********************************special 模式**********************************/
void measure_mode_special(FILE * fp)
{
	int i , cost ;
	int timer = 50 * 1000 ; //延时50ms进行下一轮测试
	printf("measure_mode_special times=%d*******************************\n",times) ;
	for ( i = 0;; i++)
	{
		test_buff(fp);
		usleep(timer); //延时50ms
		fprintf(fp , "\n") ;
		time(&end);
		//printf("start=%d , end=%d\n", start ,end);
		cost = difftime(end,start);// double difftime(time_t time1, time_t time2) 返回 time1 和 time2 之间相差的秒数 (time1 - time2)
		int cost_time = (int) cost ; //cost设置成整形
		//printf("cost_time=%d , times=%d\n", cost_time ,times);
		if (cost_time == times)//
		{
			printf ("运行了%d秒\n",cost_time);
			break ;		
		}	
	}
	return ;
}
/************************************类型有误 跳出循环*********************************************/
void jump_error(FILE * fp)
{
	char errors[50] ;
	strcpy(errors , "type只能为'normal'or'speical'or'loop'类型\n")  ; 	
    fprintf(fp ," result :%s/n " ,  &errors) ;  // 报错
	return ;
}
/*****************************************************************************/

/*****************************main 函数**************************************/
int main(int argc, char *argv[])
{
	MAXSIZE = atoi(argv[3]);//测试pins总数
    FILE * fp = NULL ;
	char *fopen_gpio_txt ;
	char *fopen_result_txt ;
	time(&start);	
     // 在运行过程中动态分配内存
    gpio = (gpio_stu *) malloc(sizeof(gpio_stu) * (MAXSIZE)) ;

	fopen_result_txt = argv[2];
	fp = fopen(fopen_result_txt , "w+") ;  //打开文本
    if(NULL == fp)
	{
		printf("open fiil failed") ;
        return 0 ;
    }
	printf("--------------------------\n");
	fopen_gpio_txt = argv[1];
	printf("--------------------------\n");
	read_buff(fopen_gpio_txt);
	printf("--------------------------\n");
	if (jump == 1)
	{
		jump_error(fp) ;
		goto __RETURN;
	}
	if(0 == strcmp("normal", type ))
	{
		measure_mode_normal(fp) ; //normal模式 循环测试times次
	}
	else if (0 == strcmp("special", type))
	{
		measure_mode_special(fp)	; //special模式(/s) 循环测试times秒
	}
	else if (0 == strcmp("loop", type))
	{
		test_buff(fp);//special模式(/s)	 pins分开测试 每一个pins单个循环测试times秒
	}
	__RETURN: 	
	printf ("jump 到此main\n") ;
	fclose(fp) ;  //关闭文件 
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值