What I'm writing is just a terminal emulator, to work with the serial ports. What it needs to do, though, is let the rest of the program continue to run in the background. For example.....
Code:
Terminal_Print("Press a key: ");
char input = Terminal_Read();
...
Rather than sit at the second line and wait there for the user to provide input, it needs to look at the input queue, check if there is input or not, and then move on, letting the rest of the program continue normally.
After going through a long thought process of how to let this happen, without having the print statements re-print during each loop, and for each of the read statements to only read once, the solution is twofold.
First, there's a global variable that is 1 if any terminal function is waiting on input, and 0 otherwise. Built into each print statement is a simple check, so that the statement is only executed if the waiting flag is 0. This works out so that the flag is always 1 at print statements that have already been printed, and it is also 1 at any statements following the point where it's waiting for input.
However, to keep read statements that have already read input from reading again, I need a global array of bools, each representing one of the calls to Terminal_Read(). And within Terminal_Read() I call a handler that looks at and modifies this array to determine whether that particular call of the function is the one that should be reading the input.
Yes, I could certainly allocate statically, but it would make things a lot easier for me and for other teammates with very little programming experience to not have to redefine the array each time you make add or remove a read statement. Or, I could simply allocate an array of 1000 elements, since I probably would never need that many, but there's already not a whole lot of memory on a PIC.
I think I'll just go with a "Specify how many times you called Read()" approach. After all, it does get to a certain point where you don't change your terminal too much. I'll post it all here when I'm done.