To get parameters

From ThaiEasyElecWiki
Jump to: navigation, search

You can get numbers, characters, words or a string following those console command by using these functions from console.c.

int get_para_1(unsigned int *cons_i,unsigned char *p); //return 0 if parameter is valid
get_para_1 is used to get a character. If '1' is received, you will get 0x31, not 1.
int get_para_2(unsigned int *cons_i,unsigned char *p1,unsigned char *p2); //return 0 if parameter is valid
get_para_2 is used to get 2 attached characters.
int get_para_end(unsigned int *cons_i,char *str); //return len of string
get_para_end is used to get the whole string including all spaces until enter is found.
int get_para_word(unsigned int *cons_i,char *str); //return 0 if parameter is valid
get_para_word is used to get a word (string cut by a space).
int get_para_num(unsigned int *cons_i,int *p); //return 0 if parameter is valid
get_para_num is used to get a numeric value between -2147483648 and 2147483647.

Most of these functions return 0 when valid parameter is received, and return -1 when invalid parameter found. get_para_end is the only exception. Since the function receives any incoming bytes until either 0x0A or 0x0D is found, it returns length of the string received.

*cons_i, a pointer, points to the index of console RX buffer. It's passed from every console command, and when a parameter is checked, it is shifted further. For example, a line "showbmp 1.bmp<enter>" is sent by a terminal software to target board. Assume that the current index of RX buffer is 0. SHOWBMP command is recognized as an entered command so it's called.

void CmdShowBmp(unsigned int *cons_i)
	bmp_header_t bmp_header;
	unsigned short ho,vo;
	char str[128];

	if (get_para_end(cons_i,str))
		if (sd_save_bmp_to_mem((volatile unsigned char *)img_buf_on_sdram,str,&bmp_header))

At the beginning of the function, the value of cons_i is 7, that's where a space after the first 'p' locates. This value is passed (by reference, not by value) to get_para_end inside CmdShowBmp function. get_para_end uses this value to check the first parameter by skipping any spaces after the command. If more than one parameter is needed for the function, the value of cons_i will be increased continuously.

In special cases that a parameter can be more than one types, for example, either a word or a number is acceptable. Let's take CmdSetDate for an example.

void CmdSetDate(unsigned int *cons_i)
	int num,i;
 	date_t date;
	unsigned int cons_i_buf;
	char str[16];
	//check for day of week, can be 0-6, SUN-SAT or sun-sat
	cons_i_buf = *cons_i;
	if (get_para_num(&cons_i_buf,&num) == 0)
		if (num >= 7)
			saystr("Please enter day in 0-6, SUN-SAT or sun-sat\r\n");
		} = num;
		*cons_i = cons_i_buf;
		if (get_para_word(cons_i,str) == 0)

At the beginning cons_i is copied to cons_i_buf, then get_para_num is used to check received parameter. If received parameter is not a number, it's check whether it's a word or not using get_para_word. Notice that cons_i is used here without any change from the very beginning.

Notice that all parameters are returned by reference, for example, "unsigned char *p" in get_para_1, "int *p" in get_para_num. An instance must be created before passing to the function. For example, to get a number using get_para_num, an integer must be created. And then pass it with '&' so its address is passed instead of its value.

int a;

Personal tools
Articles and Tutorials
Shortcut (mini SUN7)