To create a command

From ThaiEasyElecWiki
Jump to: navigation, search

To create a console command, go to app_console.c, and insert your command in cmd_table[][]. It's recommended that new command should be inserted before "HELP".

const char cmd_table[MAX_CMD][MAX_CMD_LEN+2] =
{
	"LS",
	"CD",
	"READ",
	...
	"SNAPSHOT",
	''//insert new command here''
	"HELP",
};

A command word can contain only uppercase characters because incoming characters received from serial port will be converted to uppercase prior to be compared with words in the command table. And don't forget ',' at the end.


Then insert the target function name to func_table[][] in the same way.

void (*func_table[MAX_CMD])(unsigned int *cons_i) =
{
	CmdLs,
	CmdCd,
	CmdRead,
	...
	CmdSnapShot,
	''//insert target function here''
	CmdHelp
}; 

Target function is the function called when the command word is received. Note that target function must be inserted in the same order with the command word.


Let's see more detail about the function, first, it must be "void command_name(unsigned int *cons_i)". Create its body anywhere in app_console.c. And its prototype must be declared in app_console.h.

void command_name(unsigned int *cons_i)
{
	//put your code here
}

"*cons_i" is the pointer for next valid character received from serial port. It can be used to get a character, a number, a word (cut by space) or a string (including spaces) next to the command word. You can get any of numbers or words before <enter> is found. But once a string is expected, all characters are concatenated until <enter> is found.


Let's take CmdOpenWrite for an example. The command is used to open a file to append or to create a file to write. Either 'a'(append) or 'w'(write/create) is needed followed by target file name. Below are example console commands to create a file called "test.txt", write it once with "123" and then append it with "456".

openwrite w test.txt
write 123
openwrite a test.txt
write 456

And here is the code of the function.

void CmdOpenWrite(unsigned int *cons_i)
{
	unsigned char mode;
	char str[128];
	if (get_para_1(cons_i,&mode) == 0)
	{
		mode = to_upper(mode);
		if (get_para_end(cons_i,str))
		{
			sd_open_write(mode,str);
			return;
		}
	}
	saytxt(_TXT_INV_PARA_);
}

Using get_para_1, 'w' and 'a' will be copied to "mode" and then all characters next to it (after a space) will be copied to "str" using get_para_end. Then both "mode" and "str" (file name) are used with sd_open_write. See more functions used to get parameters at To get parameters.


Optionally, you can also insert a description for your command to cmd_descrip[][]. This will be shown when "HELP" is received.

Personal tools
Namespaces
Variants
Actions
Navigation
Articles and Tutorials
Shortcut (mini SUN7)
Toolbox