John Mitchell / lpc4088_displaymodule_GC500_2_5inch

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DebugCommandsPageHandler.cpp Source File

DebugCommandsPageHandler.cpp

00001 #include "DebugCommandsPageHandler.h"
00002 #include "EasyGUITouchAreaIndices.h"
00003 #include "GetGCStatusLoop.h"
00004 #include "DebugCommandsPageHandler.h"
00005 #include "USBHostGCUtilities.h"
00006 
00007 // main.cpp
00008 void SpecialDebugPrint(char *stuffToPrint, GuiConst_INT16S X, GuiConst_INT16S Y);
00009 
00010 /*
00011     Draws the default background bitmap, i.e. *without* the Ellutia logo.
00012     Defined in main.cpp
00013 */
00014 void DrawBackgroundBitmap(void);
00015 
00016 
00017 /*                      
00018     Note that this class is a singleton - we do not need or want there to be more than one instance of it
00019     (we do not want multiple values for the same nudge and damp parameters, etc, nor will we show 
00020     more than one easyGUI page to the user at the same time).
00021 */
00022 DebugCommandsPageHandler * DebugCommandsPageHandler::theDebugCommandsPageHandlerInstance = NULL;
00023 
00024 
00025 /*
00026     Singleton class - return the one and only instance, first creating it if necessary.
00027 */
00028 DebugCommandsPageHandler * DebugCommandsPageHandler::GetInstance(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00029 {
00030     if (theDebugCommandsPageHandlerInstance == NULL) {
00031         theDebugCommandsPageHandlerInstance = new DebugCommandsPageHandler(newUsbDevice, newUsbHostGC);
00032     }
00033     
00034     return theDebugCommandsPageHandlerInstance;
00035 }
00036 
00037 /*
00038     Overriden version of the above, that does not take any arguments and does not create the instance 
00039     if it does not already exist. Caller *must* check for NULL.
00040     
00041     Provided for callers that do not have the 'usbDevice' and 'usbHostGC' pointers, and just want access 
00042     to the instance if it exists
00043 */
00044 DebugCommandsPageHandler * DebugCommandsPageHandler::GetInstance(void)
00045 {
00046     return theDebugCommandsPageHandlerInstance;
00047 }
00048 
00049 
00050 // Singleton class - private constructor
00051 DebugCommandsPageHandler::DebugCommandsPageHandler(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00052 {
00053     usbDevice = newUsbDevice;
00054     usbHostGC = newUsbHostGC;
00055     
00056     // Set this true immediately after executing a GC command.
00057     // If the user types another char, clear the command first.
00058     commandJustExecuted = false; 
00059 }
00060 
00061 
00062 // Private destructor also
00063 DebugCommandsPageHandler::~DebugCommandsPageHandler()
00064 {
00065 }
00066 
00067 /*
00068     As the name implies, sends a command to the GC and displays the response.
00069     Expects the command to be in the easyGUI variable "GuiVar_debugCommandTx",
00070     and places the response in the easyGUI variable "GuiVar_debugCommandRx".
00071     It is up to the caller to (re)display the Debug Commands easyGUI page
00072     to show the updated values.
00073     
00074     Args: pointer to a buffer containing the command, as a null-terminated string
00075           pointer to a buffer to contain the response, as a null-terminated string
00076           
00077     No return code (it is up to the caller to examine the response to see whether 
00078     the command succeeded or failed)
00079 */
00080 void DebugCommandsPageHandler::SendCommandToGCAndDisplayResponse(void)
00081 {
00082     char command[20];
00083     char response[20];
00084     
00085     strcpy(command, GuiVar_debugCommandTx);
00086     
00087     bool expect4Chars = ((command[0] == 'C') || (command[0] == 'S'));
00088     // Else expect 8 chars (or "EPKT")
00089     
00090 #define USE_GC_UTILS // Testing new class
00091 #ifdef USE_GC_UTILS
00092     USBHostGCUtilities::SendCommandToGCAndGetResponse(usbDevice, usbHostGC, command, response);
00093 #else
00094     while(usbHostGC->ExecutingSetDeviceReport()) {}
00095 
00096     usbHostGC->SetDeviceReport(usbDevice, command, response);
00097 #endif // USE_GC_UTILS
00098 
00099     // There always seems to be garbage at the end of the response
00100     if(expect4Chars || (response[0] == 'E')) { // If 'E', assume "EPKT"
00101         response[4] = '\0';
00102     } else {
00103         response[8] = '\0';
00104     }
00105     
00106 //#define DEBUG_PRINT_HERE
00107 #ifdef DEBUG_PRINT_HERE
00108     char dbg[100];
00109     sprintf(dbg, "DCPH cmd \"%s\", response \"%s\"", command, response);
00110     SpecialDebugPrint(dbg, 10, 275);
00111 #endif // DEBUG_PRINT_HERE
00112 
00113     strcpy(GuiVar_debugCommandRx, response);
00114     
00115     commandJustExecuted = true;
00116 }
00117 
00118 
00119 /*
00120     Tells the caller whether or not the specified touch index is on the Debug Commands page,
00121     and therefore needs to be handled by this class.
00122     
00123     Args: the touch area index in question
00124     
00125     Return code: true if the touch area is 'one of ours', false if not
00126 */
00127 bool DebugCommandsPageHandler::TouchAreaIsOnDebugCommandsPage(int touchAreaIndex)
00128 {
00129     if((touchAreaIndex >= MIN_DEBUG_COMMANDS_TOUCHINDEX) && (touchAreaIndex <= MAX_DEBUG_COMMANDS_TOUCHINDEX)) {
00130         return true;
00131     }
00132 
00133     // 'else'
00134     return false;
00135 }
00136 
00137 
00138 /*
00139     Returns the character that matches the specified touch area index.
00140     This should be one of the character keys on the easyGUI DebugCommandsPage.
00141     If not, defaults to '0'.
00142     
00143     Args: the touch area index
00144     
00145     Returns the corresponding character if there is one, '0' if not
00146 */
00147 char DebugCommandsPageHandler::GetCharCodeForTouchAreaIndex(int touchAreaIndex)
00148 {
00149     if((touchAreaIndex >= DEBUG_COMMANDS_0) && (touchAreaIndex <= DEBUG_COMMANDS_9)) {
00150         return (touchAreaIndex - DEBUG_COMMANDS_0 + '0');
00151     }
00152     
00153     // else...
00154     if((touchAreaIndex >= DEBUG_COMMANDS_A) && (touchAreaIndex <= DEBUG_COMMANDS_Z)) {
00155         return (touchAreaIndex - DEBUG_COMMANDS_A + 'A');
00156     }
00157 
00158     // else...
00159     return '0';    
00160 }
00161 
00162 
00163 /*
00164     If the specified touch area represents a key or field on the easyGUI Debug Commands page,
00165     this function performs whatever action is appropriate for it. Provided so that the caller
00166     can (in effect) say "if this is one of yours, deal with it", without needing to know 
00167     anything about what this class does, or how it handles the touch areas on that easyGUI page.
00168 
00169     Args: the touch area index in question
00170     
00171     Returns true if it dealt with the touch area (so the caller need not do anything else 
00172     with the value), or false if it did not deal with the touch area (implying that it 
00173     was not a touch area on the easyGUI GC/Debug Commands  page, and so the caller
00174     must deal with it some other way).
00175 */
00176 bool DebugCommandsPageHandler::DealWithTouch(int touchAreaIndex)
00177 {
00178     if(touchAreaIndex == DEBUG_COMMANDS_SEND) {
00179         
00180         // Clear response first, so user sees something happen
00181         GuiVar_debugCommandRx[0] = '\0';
00182         DisplayEasyGUIPage();
00183 
00184         SendCommandToGCAndDisplayResponse();
00185         DisplayEasyGUIPage();
00186 
00187         return true;
00188     }
00189 
00190     // If a character, append it to GuiVar_debugCommandTx, unless that is already at MAX_COMMAND_LENGTH
00191     if((touchAreaIndex >= MIN_DEBUG_COMMANDS_CHAR) && (touchAreaIndex <= MAX_DEBUG_COMMANDS_CHAR)) {
00192 
00193         // If we have just executed a command, start a new one as soon as the user types another character,
00194         // but not if he types Delete or Clear (since he is in effect editing the previous command)
00195         if(commandJustExecuted) {
00196             GuiVar_debugCommandTx[0] = '\0'; // Clear the command we have just executed
00197             
00198             commandJustExecuted = false; // So we know to append subsequent characters to this one
00199         }
00200 
00201         int len = strlen(GuiVar_debugCommandTx);
00202         if(len < MAX_COMMAND_LENGTH) {
00203             // Append the new character to the command
00204             GuiVar_debugCommandTx[len] = GetCharCodeForTouchAreaIndex(touchAreaIndex);
00205             GuiVar_debugCommandTx[len + 1] = '\0';
00206 
00207             DisplayEasyGUIPage();
00208         }
00209         return true;
00210     }
00211     
00212     // If DEBUG_COMMANDS_DELETE, delete the last character in GuiVar_debugCommandTx, unless it already has zero characters
00213     if((touchAreaIndex == DEBUG_COMMANDS_DELETE)) {
00214         int len = strlen(GuiVar_debugCommandTx);
00215         if(len > 0) {
00216             // Delete the last character in the command
00217             GuiVar_debugCommandTx[len - 1] = '\0';
00218 
00219             DisplayEasyGUIPage();
00220             
00221             commandJustExecuted = false; // User has (in effect) edited this command - do not start a new one if he types another character
00222         }
00223         return true;
00224     }
00225     
00226     // If DEBUG_COMMANDS_CLEAR, clear the command by setting GuiVar_debugCommandTx to an empty string
00227     if((touchAreaIndex == DEBUG_COMMANDS_CLEAR)) {
00228         GuiVar_debugCommandTx[0] = '\0';
00229         DisplayEasyGUIPage();
00230             
00231         commandJustExecuted = false; // User has (in effect) edited this command - do not start a new one if he types another character
00232         return true;
00233     }
00234 
00235     // 'else'...
00236     
00237     return false;
00238 }
00239 
00240 
00241 /*
00242     (Re)display the easyGUI 'DebugCommandsPage' - e.g. after we have updated 
00243     the command to be sent, and want to display the new value to the user.
00244     
00245     No arguments, no return code
00246 */
00247 void DebugCommandsPageHandler::DisplayEasyGUIPage(void)
00248 {
00249     DrawBackgroundBitmap();
00250 
00251     GuiLib_ShowScreen(GuiStruct_DebugCommandsPage_Def, GuiLib_NO_CURSOR, GuiLib_RESET_AUTO_REDRAW);
00252     
00253     GuiLib_Refresh();    
00254 
00255     GetGCStatusLoop *getGCStatusLoop = GetGCStatusLoop::GetInstance();
00256     if(getGCStatusLoop != NULL) {
00257         getGCStatusLoop->SetCurrentPage(GuiStruct_DebugCommandsPage_Def);
00258     }
00259 }
00260