I wanted to post this simply because it working on it brought my a dry nostalgic joy. When I was young, 8-9, my parents got a old computer. All I remember was its screen was orange and black; having a Hercules graphics card. I quickly learned to get around from the command prompt. But I was always thrilled to run into menu driven program. It was like going to a fancy restaurant of abstractness. Anyway, when I wanted my code to slow down a bit and branch directions based upon user input, a command menu was a natural choice.
A Break from the LPC1114 Uploader
I thought I'd take some time away from coding my LPC1114 Uploader and verbally process a few things I've learned. As always, feel free to critique any of it; it'll only serve to make my code more robust in the end.
This post will be a series of post leading up to the large post I'll make on writing the uploader. All posts will rely on the GCC compiler.
Setting Up the GCC Compiler
I setup a C environment as basic I could. There may be easier ways to go about this, but I wanted to use GCC to compile.
To setup the environment:
1. I downloaded and setup MinGW32.
2. I added these includes to make the code go.
I used this line to build it:
$ gcc -o main main.c
As for editing, I've really grown to love Sublime Text 2.
If you have issues, make sure directory containing your files is in your PATH environment variable (I go over how to add the directory to your environment variables in this post).
How to Write a Command Line Menu
There really isn't much to the code here. Basically, it it prints out the options you want your user to know. Then, it starts a do-while loop until the user selects an appropriate number.
Hmm. Not really much to it, not sure it deserves its own post. But what the hell.
6 and 54: This is the beginning and the end of the do-while loop. Basically, the do-while is a fancy loop which says to do everything in the brackets over and over, until the boolean variable is met (line 54).
The do-while loop if the equivalent in effect to the follow code,
- 8: Clears the screen. This removes the command prompt; giving a clean slate to paint our menu.
- 9: I put a function in to paint my menu header. This allows me easily change the header for a menu. It also makes the menu code easier to read.
- 12-20: We print the menu options for the user.
- 22: scanf is a tricky command. It pauses and waits for the user to input followed by 'n'. It takes a variable respective to the data type you want to get from the user. Here, we are hoping the user enters one or two numbers. These are put into the string provided.
- 23: We use the atoi function to take a string and turn it into an integer. This value we store in the integer int_choice.
- 25: The beginning of the switch-statement which does the real work for us. It test the int_choice value you against predefined values (1-9 here). If the one of the values is equal, it executes the code found there before breaking from the switch-statement.
- 51: If a number besides 1-9 is entered the default will be true. Let's gripe at the user for selecting a bad number.
And that's it. You simply put the functions you want to be called in the appropriate values in the switch-statement. Because of the do-while loop, once a selection has been made and executed, the menu will be displayed again.
You can also make limit a selection to showing by doing something like the following,
Here, option "3. Connect Device" doesn't show until option "2. Device List" is run. On line 34 the connect_device() function sets the connected_flag variable to true if the function was successful. Then, after the break is hit and the menu is repainted the option "3. Connect Device" will show. Also, '3' will become a valid user selection.
One last note. If you want to make a sub-menu, you simply use the same code as above, just take the do-while loop out. This states you only want the sub-menu to run once, the return to the main menu.