John Mitchell / lpc4088_displaymodule_GC500_2_5inch

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetKeypadPageHandler.cpp Source File

EthernetKeypadPageHandler.cpp

00001 #include "EthernetKeypadPageHandler.h"
00002 #include "EasyGUITouchAreaIndices.h"
00003 #include "GetGCStatusLoop.h"
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 
00008 
00009 /*
00010     Draws the background bitmap. It fills the screen, so you do not need to call GuiLib_Clear.
00011     Defined in main.cpp
00012 */
00013 extern void DrawBackgroundBitmap(void);
00014 #define USING_BACKGROUND_BITMAP
00015 
00016 /*
00017     For Display (LPC4088) Bug #11, draw a background bitmap without a grey bar at the bottom.
00018     For now, fake this with a page full of one colour.
00019     
00020     We use the same (fake) background bitmap here, as in the 'ordinary' numeric keypad
00021     
00022     Defined in main.cpp
00023 */
00024 void DrawFakeBackgroundBitmapForNumericKeypadPage(void);
00025 
00026 
00027 
00028 
00029 /*
00030     Displays the specified easyGUI 'structure' (or page, to use a more easily understood term).
00031     Defined in main.cpp
00032 */
00033 extern void DisplayEasyGuiStructure(int structureIndex, USBDeviceConnected* usbDevice, USBHostGC* usbHostGC, bool updateEasyGUIVariables = true);
00034 
00035 
00036 
00037 /*                      
00038     Note that this class is a singleton - we do not need or want there to be more than one instance of it
00039     (we will not show more than one "EthernetKeypadPage" to the user at the same time).
00040 */
00041 EthernetKeypadPageHandler * EthernetKeypadPageHandler::theEthernetKeypadPageHandlerInstance = NULL;
00042 
00043 const int EthernetKeypadPageHandler::minimumOctetValue = 0;
00044 const int EthernetKeypadPageHandler::maximumOctetValue = 255;
00045 
00046 #define TESTING_COLOURS
00047 #ifdef  TESTING_COLOURS
00048 const GuiConst_INTCOLOR EthernetKeypadPageHandler::inactiveOctetBackgroundColour = 0xFFFF; // RGB565 value for white - same colour as background
00049 const GuiConst_INTCOLOR EthernetKeypadPageHandler::activeOctetBackgroundColour   = 0x07FF; // RGB565 value for yellow (255, 255, 0)
00050 #else
00051 const GuiConst_INTCOLOR EthernetKeypadPageHandler::inactiveOctetBackgroundColour = 0xE71C; // RGB565 value for light grey (224, 224, 224)
00052 const GuiConst_INTCOLOR EthernetKeypadPageHandler::activeOctetBackgroundColour   = 0xFFFF; // RGB565 value for white (255, 255, 255)
00053 #endif //  TESTING_COLOURS
00054 //#define USE_BACKGROUND_COLOURS // else use box around active octet
00055 
00056 /*
00057     Singleton class - return the one and only instance, first creating it if necessary.
00058 */
00059 EthernetKeypadPageHandler * EthernetKeypadPageHandler::GetInstance(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00060 {
00061     if (theEthernetKeypadPageHandlerInstance == NULL) {
00062         theEthernetKeypadPageHandlerInstance = new EthernetKeypadPageHandler(newUsbDevice, newUsbHostGC);
00063     }
00064     
00065     return theEthernetKeypadPageHandlerInstance;
00066 }
00067 
00068 // Singleton class - private constructor
00069 EthernetKeypadPageHandler::EthernetKeypadPageHandler(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00070 {
00071     // Note that we do not use these values here - 
00072     // but we do need to pass them to the (main.cpp) function DisplayEasyGUIStructure
00073     // when the user presses Apply or Cancel
00074     usbDevice = newUsbDevice;
00075     usbHostGC = newUsbHostGC;
00076 
00077     easyGUICallingPage = -1; // i.e. invalid, none
00078     indexOfOctetBeingEdited = -1; // as above - invalid, none
00079     
00080     for (int index = 0; index < 4; ++index){
00081         callerEasyGUIOctetPtr[index] = NULL;
00082         callerInternalOctetPtr[index] = NULL;
00083     }
00084     
00085     easyGUIOctetValues[0] = GuiVar_ethernetOctet1;
00086     easyGUIOctetValues[1] = GuiVar_ethernetOctet2;
00087     easyGUIOctetValues[2] = GuiVar_ethernetOctet3;
00088     easyGUIOctetValues[3] = GuiVar_ethernetOctet4;
00089     
00090     easyGUIOctetBackgroundColourVariables[0] = &GuiVar_ethernetOctet1BackgroundColour;
00091     easyGUIOctetBackgroundColourVariables[1] = &GuiVar_ethernetOctet2BackgroundColour;
00092     easyGUIOctetBackgroundColourVariables[2] = &GuiVar_ethernetOctet3BackgroundColour;
00093     easyGUIOctetBackgroundColourVariables[3] = &GuiVar_ethernetOctet4BackgroundColour;
00094     
00095     activeOctetOutline[0].X1 = 270; activeOctetOutline[0].Y1 = 35, activeOctetOutline[0].X2 = 335, activeOctetOutline[0].Y2 = 80;
00096     activeOctetOutline[1].X1 = 350; activeOctetOutline[1].Y1 = 35, activeOctetOutline[1].X2 = 415, activeOctetOutline[1].Y2 = 80;
00097     activeOctetOutline[2].X1 = 430; activeOctetOutline[2].Y1 = 35, activeOctetOutline[2].X2 = 495, activeOctetOutline[2].Y2 = 80;
00098     activeOctetOutline[3].X1 = 510; activeOctetOutline[3].Y1 = 35, activeOctetOutline[3].X2 = 575, activeOctetOutline[3].Y2 = 80;
00099 }
00100 
00101 // Private destructor also
00102 EthernetKeypadPageHandler::~EthernetKeypadPageHandler()
00103 {
00104 }
00105 
00106 
00107 /*
00108     Tells the caller whether or not the specified touch index is on the easyGUI 'NumericKeypadPage',
00109     and therefore needs to be handled by this class.
00110     
00111     Args: the touch area index in question
00112     
00113     Return code: true if the touch area is 'one of ours', false if not
00114 */
00115 bool EthernetKeypadPageHandler::TouchAreaIsOnEthernetKeypadPage(int touchAreaIndex)
00116 {
00117     if((touchAreaIndex >= MIN_ETHERNET_KEYPAD_TOUCHINDEX) && (touchAreaIndex <= MAX_ETHERNET_KEYPAD_TOUCHINDEX)) {
00118         return true;
00119     }
00120     
00121     // 'else'
00122     return false;
00123 }
00124 
00125 
00126 /*
00127     If the specified touch area represents a key or field on the easyGUI 'EtherneKeypadPage',
00128     this function performs whatever action is appropriate for it. Provided so that the caller
00129     can (in effect) say "if this is one of yours, deal with it", without needing to know 
00130     anything about what this class does, or how it handles the touch areas on that easyGUI page.
00131 
00132     Args: the touch area index in question
00133     
00134     Returns true if it dealt with the touch area (so the caller need not do anything else 
00135     with the value), or false if it did not deal with the touch area (implying that it 
00136     was not a touch area on the easyGUI 'EthernetKeypadPage', and so the caller
00137     must deal with it some other way).
00138 */
00139 bool EthernetKeypadPageHandler::DealWithTouch(int touchAreaIndex)
00140 {
00141     if((touchAreaIndex >= ETHERNET_KEYPAD_BUTTON_0) && (touchAreaIndex <= ETHERNET_KEYPAD_BUTTON_9)) {
00142     
00143         DealWithNumberKey(touchAreaIndex);
00144         
00145         return true;
00146     }
00147 
00148     if(touchAreaIndex == ETHERNET_KEYPAD_DELETE_BUTTON) {
00149 
00150         DealWithDeleteKey();
00151         
00152         return true;
00153     }
00154 
00155     if(touchAreaIndex == ETHERNET_KEYPAD_APPLY_BUTTON) {
00156         
00157         DealWithApplyKey();
00158                 
00159         return true;
00160     }
00161     
00162     if(touchAreaIndex == ETHERNET_KEYPAD_CANCEL_BUTTON) {
00163         
00164         DealWithCancelKey();
00165                 
00166         return true;
00167     }
00168     
00169     if(touchAreaIndex == ETHERNET_KEYPAD_CLEAR_BUTTON) {
00170         
00171         DealWithClearKey();
00172                 
00173         return true;
00174     }
00175     
00176     if(touchAreaIndex == ETHERNET_KEYPAD_DOT_BUTTON) {
00177         
00178         DealWithDotKey();
00179                 
00180         return true;
00181     }
00182     
00183     // 'else' - none of the above
00184     return false;
00185 }
00186 
00187 
00188 /*
00189     This function allows the caller to tell us which of its easyGUI variables to update when the user presses the Apply button
00190     
00191     Args: pointers to the easyGUI variables for each of the four octets - we assume these are all easyGUI strings
00192     
00193     No return code 
00194 */
00195 void EthernetKeypadPageHandler::SetEasyGUIOctetVariablesToEdit(GuiConst_TEXT* easyGUIOctet1, GuiConst_TEXT* easyGUIOctet2, GuiConst_TEXT* easyGUIOctet3, GuiConst_TEXT* easyGUIOctet4)
00196 {
00197     callerEasyGUIOctetPtr[0] = easyGUIOctet1;
00198     callerEasyGUIOctetPtr[1] = easyGUIOctet2;
00199     callerEasyGUIOctetPtr[2] = easyGUIOctet3;
00200     callerEasyGUIOctetPtr[3] = easyGUIOctet4;
00201 }
00202 
00203 
00204 /*
00205     This function allows the caller to tell us which of its internal variables to update when the user presses the Apply button
00206     
00207     Args: pointers to the internal variables for each of the four octets - these must all be unsigned int's
00208     
00209     No return code 
00210 */
00211 void EthernetKeypadPageHandler::SetInternalOctetVariablesToEdit(unsigned int *internalOctet1, unsigned int *internalOctet2, unsigned int *internalOctet3, unsigned int *internalOctet4)
00212 {
00213     callerInternalOctetPtr[0] = internalOctet1;
00214     callerInternalOctetPtr[1] = internalOctet2;
00215     callerInternalOctetPtr[2] = internalOctet3;
00216     callerInternalOctetPtr[3] = internalOctet4;
00217 }
00218 
00219 /*
00220     Actually start editing.
00221     Caller *must* have called 'SetEasyGUIOctetVariablesToEdit' and 'SetInternalOctetVariablesToEdit' 
00222     before calling this function
00223     ****************************
00224     
00225     Args: the name/title of the Ethernet parameter we are editing (e.g. "IP Address", etc)
00226     
00227     No return code
00228 */
00229 void EthernetKeypadPageHandler::StartEditing(char *title)
00230 {
00231     strcpy(GuiVar_ethernetKeypadTitle, title);
00232     
00233     for (int index = 0; index < 4; ++index) {
00234         currentOctetValues[index] = *callerInternalOctetPtr[index];
00235     }
00236 
00237     UpdateEasyGUIOctetValues();
00238     
00239     indexOfOctetBeingEdited = 0;
00240     
00241 #ifdef USE_BACKGROUND_COLOURS
00242     UpdateEasyGUIBackgroundsForCurrentOctetSelection();
00243 #endif // USE_BACKGROUND_COLOURS
00244     
00245     UpdateEasyGUIPage();
00246 }
00247 
00248 
00249 /*
00250     Update the easyGUI variables that display the octet values to the user,
00251     from our internal values
00252     
00253     No arguments, no return code
00254 */
00255 void EthernetKeypadPageHandler::UpdateEasyGUIOctetValues(void)
00256 {
00257     for (int index = 0; index < 4; ++index) {
00258         sprintf(easyGUIOctetValues[index], "%d", currentOctetValues[index]);
00259     }
00260 }
00261 
00262 
00263 /*
00264     In response to the user pressing one of the number keys on the easyGUI 'EthernetKeypadPage',
00265     edits the current octet appropriately. Note that we rely on the touch area indices being consecutive, 
00266     and in the same order as the numbers they represent.
00267     
00268     Argument: the index of the touch area for the key in question
00269               (note that it is up to the caller to verify that it is a number key)
00270 
00271     No return value.
00272 */
00273 void EthernetKeypadPageHandler::DealWithNumberKey(int touchAreaIndex)
00274 {
00275     if((indexOfOctetBeingEdited >= 0) && (indexOfOctetBeingEdited < 4)) {
00276         int digitToInsert = touchAreaIndex - ETHERNET_KEYPAD_BUTTON_0;
00277         if(digitToInsert > 9) digitToInsert = 9;
00278         if(digitToInsert < 0) digitToInsert = 0;
00279         
00280         int temp = currentOctetValues[indexOfOctetBeingEdited];
00281             
00282         if(temp < maximumOctetValue) { // else we can't increase it any further
00283          
00284             temp *= 10;
00285             temp += digitToInsert;
00286                  
00287             if(temp < maximumOctetValue) {
00288                 currentOctetValues[indexOfOctetBeingEdited] = temp;
00289             } else {
00290                 currentOctetValues[indexOfOctetBeingEdited] = maximumOctetValue;
00291             }
00292                 
00293             UpdateEasyGUIOctetValues();
00294             
00295             UpdateEasyGUIPage();
00296         }
00297     }
00298 }
00299 
00300 
00301 /*
00302     In response to the user pressing the dot key on the easyGUI 'EthernetKeypadPage',
00303     moves editing to the next octet, or back to the first if we are currently at the fourth. 
00304     Note that it is up to the caller to verify that the user actually has pressed the dot key
00305 
00306     No arguments, no return value.
00307 */
00308 void EthernetKeypadPageHandler::DealWithDotKey(void)
00309 {
00310     if((indexOfOctetBeingEdited >= 0) && (indexOfOctetBeingEdited < 3)) {
00311         // Can move to next octet
00312         ++indexOfOctetBeingEdited;
00313     } else {
00314         // Default to first octet
00315         indexOfOctetBeingEdited = 0;
00316     }
00317     
00318 #ifdef USE_BACKGROUND_COLOURS
00319     UpdateEasyGUIBackgroundsForCurrentOctetSelection();
00320 #endif // USE_BACKGROUND_COLOURS
00321             
00322     UpdateEasyGUIPage();
00323 }
00324 
00325 
00326 /*
00327     (Re)display the easyGUI 'EthernetKeypadPage' - e.g. after we have updated 
00328     the octet being edited, and want to display the new value to the user.
00329     
00330     No arguments, no return code
00331 */
00332 void EthernetKeypadPageHandler::UpdateEasyGUIPage(void)
00333 {
00334 #define WANT_GUILIB_CLEAR
00335 #ifdef WANT_GUILIB_CLEAR
00336 #ifdef USING_BACKGROUND_BITMAP
00337     DrawFakeBackgroundBitmapForNumericKeypadPage();
00338 #else
00339     GuiLib_Clear();
00340 #endif
00341 #undef WANT_GUILIB_CLEAR
00342 #endif
00343     GuiLib_ShowScreen(GuiStruct_EthernetKeypadPage_Def, GuiLib_NO_CURSOR, GuiLib_RESET_AUTO_REDRAW);
00344     
00345 #ifndef USE_BACKGROUND_COLOURS
00346     DrawOutlineAroundActiveOctet();
00347 #endif
00348 
00349     GuiLib_Refresh();    
00350 }
00351 
00352 
00353 /*
00354     In response to the user pressing the Delete key on the easyGUI 'EthernetKeypadPage',
00355     edits the current octet appropriately. Note that it is up to the caller to verify 
00356     that the user actually has pressed the Delete key
00357 
00358     No arguments, no return value.
00359 */
00360 void EthernetKeypadPageHandler::DealWithDeleteKey(void)
00361 {
00362     if((indexOfOctetBeingEdited >= 0) && (indexOfOctetBeingEdited < 4)) {
00363 
00364         int temp = currentOctetValues[indexOfOctetBeingEdited];
00365             
00366         if(temp > minimumOctetValue) { // else we can't reduce it any further
00367             
00368             temp /= 10;
00369                  
00370             if(temp > minimumOctetValue) {
00371                 currentOctetValues[indexOfOctetBeingEdited] = temp;
00372             } else {
00373                 currentOctetValues[indexOfOctetBeingEdited] = minimumOctetValue;
00374             }
00375                 
00376             UpdateEasyGUIOctetValues();
00377             
00378             UpdateEasyGUIPage();
00379         }
00380     }
00381 }
00382 
00383 
00384 /*
00385     In response to the user pressing the Clear key on the easyGUI 'EthernetKeypadPage',
00386     edits the current octet appropriately. Note that it is up to the caller to verify 
00387     that the user actually has pressed the Clear key
00388 
00389     No arguments, no return value.
00390 */
00391 void EthernetKeypadPageHandler::DealWithClearKey(void)
00392 {
00393     if((indexOfOctetBeingEdited >= 0) && (indexOfOctetBeingEdited < 4)) {
00394 
00395         currentOctetValues[indexOfOctetBeingEdited] = minimumOctetValue;
00396                 
00397         UpdateEasyGUIOctetValues();
00398     
00399         UpdateEasyGUIPage();
00400     }
00401 }
00402 
00403 
00404 /*
00405     In response to the user pressing the Apply key on the easyGUI 'EthernetKeypadPage',
00406     updates the octet values on the calling page (which we currently assume 
00407     is always the Ethernet Parameters page), and redisplays the calling page.
00408     
00409     Note that it is up to the caller (of this function) to verify that the user actually has pressed the Apply key.
00410 
00411     No arguments, no return value.
00412 */
00413 void EthernetKeypadPageHandler::DealWithApplyKey(void)
00414 {
00415     //Update the octet values in the Ethernet Parameters page
00416     for (int index = 0; index < 4; ++index) {
00417         if(callerEasyGUIOctetPtr[index] != NULL) {
00418             sprintf(callerEasyGUIOctetPtr[index], "%d", currentOctetValues[index]);
00419         }
00420     
00421         if(callerInternalOctetPtr[index]  != NULL) {
00422             *callerInternalOctetPtr[index] = currentOctetValues[index];
00423         }
00424     }    
00425     
00426     DisplayEasyGuiStructure(GuiStruct_EthernetParametersPage_50, usbDevice, usbHostGC, false);
00427 }
00428 
00429     
00430 /*
00431     In response to the user pressing the Cancel key on the easyGUI 'EthernetKeypadPage',
00432     (re)displays the calling page (which we currently assume is always the Ethernet Parameters page) 
00433     *without* updating its octet values.
00434     
00435     Note that it is up to the caller (of this function) to verify that the user actually has pressed the Cancel key.
00436 
00437     No arguments, no return value.
00438 */
00439 void EthernetKeypadPageHandler::DealWithCancelKey(void)
00440 {
00441     DisplayEasyGuiStructure(GuiStruct_EthernetParametersPage_50, usbDevice, usbHostGC);
00442 }
00443 
00444 
00445 /*
00446     Update the easyGUI background colour variables to show which octet is 'active',
00447     i.e. currently being edited.
00448     
00449     No arguments, no return value.
00450 */
00451 void EthernetKeypadPageHandler::UpdateEasyGUIBackgroundsForCurrentOctetSelection(void)
00452 {
00453     for (int index = 0; index < 4; ++index) {
00454         if(index == indexOfOctetBeingEdited) {
00455             *easyGUIOctetBackgroundColourVariables[index] = activeOctetBackgroundColour;
00456         } else {
00457             *easyGUIOctetBackgroundColourVariables[index] = inactiveOctetBackgroundColour;
00458         }
00459     }
00460 }
00461 
00462 
00463 /*
00464     Draw an outline box around the octet value the user is currently editing (if any),
00465     to show which one it is.
00466     
00467     No arguments, no return value.
00468 */
00469 void EthernetKeypadPageHandler::DrawOutlineAroundActiveOctet(void)
00470 {
00471     if((indexOfOctetBeingEdited >= 0) && (indexOfOctetBeingEdited < 4)) {
00472         GuiLib_Box(activeOctetOutline[indexOfOctetBeingEdited].X1,
00473                    activeOctetOutline[indexOfOctetBeingEdited].Y1,
00474                    activeOctetOutline[indexOfOctetBeingEdited].X2,
00475                    activeOctetOutline[indexOfOctetBeingEdited].Y2,
00476                    0 // Box colour - black
00477                    ); 
00478     }
00479 }
00480