Ashley Mills / ATConsole

Dependencies:   mbed-rtos VodafoneUSBModem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Console.h Source File

Console.h

00001 /* Console.h */
00002 /* Copyright (C) 2012 Ashley Mills, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019  
00020 #pragma once
00021 #include "mbed.h"
00022 
00023 #define BUFFER_WAIT_TIMEOUT_MS 2000
00024 
00025 /** Console interface with line editing and command history.
00026  */
00027 class Console {
00028                
00029     public:
00030         /** Construct an interface to a serial terminal.
00031          * 
00032          * @param terminal The serial device to interface to.
00033          * @param lineLength The maximum line-length to process.
00034          * @param numLines The number of lines to hold in the command-history.
00035          * @param characterBased true for character based serial devices, false for line based.
00036          */
00037         Console(Serial *terminal, int lineLength, int numLines, bool characterBased);
00038         ~Console();
00039         /** This is called by the instantiating program to update the console.
00040          */
00041         virtual void update();
00042     
00043     protected:
00044         char *_lineBuffer;             //< Buffer for current-line.
00045         int _lineLength;               //< Length of current-line buffer.
00046         int _numLines;                 //< Number of lines in the command history.
00047         Serial *_terminal;             //< Pointer to terminal interface.
00048         /// Whether or not this console is character based (true) or line based (false).
00049         bool _characterBased;
00050         
00051     protected:
00052         void storeBuffer();            //< Store the current-line in the command history.
00053         void clearBuffer();            //< Clears the current-line buffer.
00054         
00055     private:
00056         char **_lines;                 //< Command history
00057         int _currentLine;              //< Index of current line in command history.
00058         int _usedLines;                //< Number of lines that have been used in the command history.
00059         
00060         long _bufferWaitTimeout_ms;    //< This is used when multi-byte sequences are predicted.
00061       
00062         int _linePos;                  //< Keeps track of the cursor in the current-line.
00063         int _prevLinePos;              //< Used when a new line is selected from command history, to clear old line.
00064 
00065         bool waitForInput();           //< Waits for _bufferWaitTimeout_ms for input from the console.
00066         void loadPreviousBuffer();     //< Load the previous line in the command history.
00067         void loadNextBuffer();         //< Load the next line in the command history.
00068         
00069         void printLineBuffer();        //< Prints the current-line buffer.
00070         void printHistory();           //< Print the entire command-history.
00071         void printBuffer(int index);   //< Print buffer at index in the command-history.
00072         void reprintBuffer();          //< Clears old buffer and reprints it, used for insertion etc.
00073         
00074         void updateCharacterBased();   //< Called by update() for character-based consoles.
00075         void updateLineBased();        //< Called by update() for line-based consoles.
00076     
00077         virtual void processCommand(); //< Process a command retrieved from the console.
00078           
00079 };