Step 5Customizing the Shell
To add in your own custom commands, do the following:
1. Add your command to the parse list
The command parser will parse the command line and give you the command and any arguments separately. The arguments are passed as pointers to pointers, or an array of pointers, however you like to work with them. This is found in shell.cpp. Open up shell.cpp and find the ExecCmd method of the AVRShell class.
You may wish to add the command to program memory. If you do, add the command in progmem.h and progmem.cpp. You can add the command to program memory directly using the PSTR() macro, but you'll generate another warning of the type mentioned earlier. Again, this is a known bug working with C++, but you can get around this by adding the command directly in the progmem.* files, as I have done. If you don't mind adding to your SRAM usage, you can add the command as I have illustrated with the "clock" command.
Say you wanted to add a new command called "newcmd." Go to AVRShell::ExecCmd and find a convenient place to insert the following code:
else if (!strcmp(c,"newcmd")) cmdNewCmd(args);This will add your command and call the cmdNewCmd method that you will write in the next step.
2. Write your custom command code
In the same file, add your custom command code. This is the method definition. You will still want to add the declaration to shell.h. Just append it to the other commands. In the previous example, the code might look something like this:
voidAVRShell::cmdNewCmd(char ** args){ sprintf_P(buff,PSTR("Your command is %s\r\n",args[0]); WriteRAM(buff);}There are several things here. First, "buff" is a 40-character array buffer provided in the code for your use. We use the program memory version of sprintf since we're passing it a PSTR. You can use the regular version if you like, but ensure you don't pass the format in a PSTR. Also, the arguments are in the args array. If you typed "newcmd arg1 arg2" you can get at these arguments with the args[0] and args[1] subscripts. You can pass a maximum of MAX_ARGS arguments, as defined in the code. Feel free to change that value when you recompile if you need many more arguments to be passed at once.The WriteLine and WriteRAM are global functions that return the UART's methods of the same name. The 2nd argument to this function is implicit. If you pass nothing, a command prompt will be written afterwards. If yo pass a 0 as the 2nd argument, a prompt will not be written. This is useful when you want to write several separate strings to output before the command prompt is returned to the user.
3. Have the shell execute the command code
You have already told the shell executor to execute the method cmdNewCmd when you setup the new command, but add it to the shell.h file to have it understood by the shell object. Just add it below the last command or in front of the first command, or anywhere in there.
And that's it. Recompile and upload the firmware to your Arduino and your new command is available from the shell at the prompt.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|











































