|FreakUSB Tutorial - Adding a Custom Function to the Command Shell||| Print ||
|Written by Akiba|
|Tuesday, 24 November 2009|
I think that there’s a lot of people that might not understand how to use the command line shell that comes inside the FreakUSB release. Here’s a quick tutorial about how to add your own custom command to it.
First I’m going to add the function prototype. The function prototype is used to tell the compiler that the function will be defined later on in the code. The compiler works from top to bottom and since we have the command table near the top of the code, the compiler will complain if I add a command with a function definition that the compiler doesn’t know about. By adding the prototype, we’re telling the compiler that the function exists and will be defined shortly.
After adding the command, then I’m going into the “main” function and adding some code to initialize my function. My function will just toggle a pin, so I need to configure the pin to be an output and set the initial value to 0. The other code above the “while” loop is also initialization code which handles initializing the USB interface, the USB driver, and the command line parser. The while loop is an infinite loop that will just call the functions defined within it forever.
And finally, I’m adding the actual function here. The function requires two arguments, argc (argument count) and argv (or argument values). Argc will inform the function of the total number of arguments entered into the command line. There will always be at least 1 argument which is the command name (“tog” in this case). Argv is a string list (string array for those used to programming) which consists of all arguments entered into the command line, separated by a space (space delimited). The number of strings in the list is equal to argc. The first string in the argv list is the command name and anything after that is parsed based on a space. Hence, if you entered “tog 1” at the command line, then you will have 2 strings in your list:
argv = “tog”
Here’s some screenshots of the command line. First you need to connect to the board using a terminal program. Then you can type commands into the shell: