3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

AVRSH: A Command Interpreter Shell for Arduino/AVR.

Step 5Customizing the Shell

Customizing the Shell
You are free to hack at the code and customize it to your own needs, if you like. If I had known I'd be releasing this code, I would have made a separate command interpreter class and command structure and simply iterated through this calling a function pointer. It would reduce the amount of code, but as it stands the shell parses the command line and calls the appropriate shell method.

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 StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
62
Followers
22
Author:nevdull(The Bold Scientist)
Gian is the VP Research & Development at Open Design Strategies and holds a BA in Molecular/Cellular Biology and an MS in Computer Science. He has a collection of 8-bit microcontrollers and a room fu...
more »