John Mitchell / lpc4088_displaymodule_GC500_2_5inch

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GasBackPressureDACPageHandler.cpp Source File

GasBackPressureDACPageHandler.cpp

00001 #include "GasBackPressureDACPageHandler.h"
00002 #include "EasyGUITouchAreaIndices.h"
00003 #include "SettingsHandler.h"
00004 #include "GetGCStatusLoop.h"
00005 #include "NumericKeypadPageHandler.h"
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 
00010 
00011 
00012 /*
00013     Displays the specified easyGUI 'structure' (or page, to use a more easily understood term).
00014     Defined in main.cpp
00015 */
00016 extern void DisplayEasyGuiStructure(int structureIndex, USBDeviceConnected* usbDevice, USBHostGC* usbHostGC, bool updateEasyGUIVariables = true);
00017 
00018 
00019 /*
00020     Converts three eight-bit colour values to the corresponding 16-bit RGB565 value.
00021     Defined in main.cpp
00022 */
00023 extern GuiConst_INTCOLOR SixteenBitColorValue(GuiConst_INT8U red, GuiConst_INT8U green, GuiConst_INT8U blue); 
00024 
00025 
00026 /*                      
00027     Note that this class is a singleton - we do not need or want there to be more than one instance of it
00028     (we do not want multiple values for the gain and offset, etc, and nor will we show 
00029     more than one easyGUI 'GasBackPressureDACPage' to the user at the same time).
00030 */
00031 GasBackPressureDACPageHandler * GasBackPressureDACPageHandler::theGasBackPressureDACPageHandlerInstance = NULL;
00032 
00033 
00034 /*
00035     The minimum and maximum values for the gain and offset (both the same)
00036 */
00037 const int GasBackPressureDACPageHandler::minimumGainAndOffsetValue = 0;
00038 const int GasBackPressureDACPageHandler::maximumGainAndOffsetValue = 9999;
00039     
00040 /*
00041     We distinguish the 'active' field - i.e. the one being edited by the user - 
00042     from the inactive fields, by its background colour
00043 */
00044 const GuiConst_INTCOLOR GasBackPressureDACPageHandler::inactiveFieldBackgroundColour = SixteenBitColorValue(192, 192, 192); // Grey
00045 const GuiConst_INTCOLOR GasBackPressureDACPageHandler::activeFieldBackgroundColour   = SixteenBitColorValue(255, 255, 0); // Yellow
00046 
00047 
00048 /*
00049     Singleton class - return the one and only instance, first creating it if necessary.
00050 */
00051 GasBackPressureDACPageHandler * GasBackPressureDACPageHandler::GetInstance(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00052 {
00053     if (theGasBackPressureDACPageHandlerInstance == NULL) {
00054         theGasBackPressureDACPageHandlerInstance = new GasBackPressureDACPageHandler(newUsbDevice, newUsbHostGC);
00055     }
00056     
00057     return theGasBackPressureDACPageHandlerInstance;
00058 }
00059 
00060 // Singleton class - private constructor
00061 GasBackPressureDACPageHandler::GasBackPressureDACPageHandler(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00062 {
00063     usbDevice = newUsbDevice;
00064     usbHostGC = newUsbHostGC;
00065 }
00066 
00067 // Private destructor also
00068 GasBackPressureDACPageHandler::~GasBackPressureDACPageHandler()
00069 {
00070     //SaveCarrierGasSelectionToQSPISettings();
00071 }
00072 
00073 
00074 /*
00075     Tells the caller whether or not the specified touch index is on the easyGUI 'GasCalibrationPage',
00076     and therefore needs to be handled by this class.
00077     
00078     Args: the touch area index in question
00079     
00080     Return code: true if the touch area is 'one of ours', false if not
00081 */
00082 bool GasBackPressureDACPageHandler::TouchAreaIsOnGasBackPressureDACPage(int touchAreaIndex)
00083 {
00084     if((touchAreaIndex >= MIN_GAS_BACKPRESSURE_DAC_TOUCHINDEX) && (touchAreaIndex <= MAX_GAS_BACKPRESSURE_DAC_TOUCHINDEX)) {
00085         return true;
00086     }
00087     
00088     // 'else'
00089     return false;
00090 }
00091 
00092 /*
00093     If the specified touch area represents a key or field on the easyGUI 'GasBackPressureDACPage',
00094     this function performs whatever action is appropriate for it. Provided so that the caller
00095     can (in effect) say "if this is one of yours, deal with it", without needing to know 
00096     anything about what this class does, or how it handles the touch areas on that easyGUI page.
00097 
00098     Args: the touch area index in question
00099     
00100     Returns true if it dealt with the touch area (so the caller need not do anything else 
00101     with the value), or false if it did not deal with the touch area (implying that it 
00102     was not a touch area on the easyGUI 'GasBackPressureDACPage', and so the caller
00103     must deal with it some other way).
00104 */
00105 bool GasBackPressureDACPageHandler::DealWithTouch(int touchAreaIndex)
00106 {
00107     // Note that we have only two editable fields
00108 
00109     if(touchAreaIndex == GAS_BACKPRESSURE_DAC_GAIN_EDIT) {
00110         NumericKeypadPageHandler* numericKeypadPageHandler = NumericKeypadPageHandler::GetInstance(usbDevice, usbHostGC);
00111         if(numericKeypadPageHandler != NULL) {
00112             numericKeypadPageHandler->StartEditing(GuiVar_gasBackPressureDACGain);
00113             numericKeypadPageHandler->SetEasyGUIVariableToEdit(GuiVar_gasBackPressureDACGain);
00114             numericKeypadPageHandler->SetEasyGUICallingPage(GuiStruct_GasBackPressureDACPage_Def);
00115             numericKeypadPageHandler->SetEditVariableRange(minimumGainAndOffsetValue, maximumGainAndOffsetValue);
00116             numericKeypadPageHandler->SetEditVariableName("0 PSI");
00117             numericKeypadPageHandler->DisplayEasyGUIPage();
00118         }
00119 
00120         return true;
00121     }
00122 
00123     if(touchAreaIndex == GAS_BACKPRESSURE_DAC_OFFSET_EDIT) {
00124         NumericKeypadPageHandler* numericKeypadPageHandler = NumericKeypadPageHandler::GetInstance(usbDevice, usbHostGC);
00125         if(numericKeypadPageHandler != NULL) {
00126             numericKeypadPageHandler->StartEditing(GuiVar_gasBackPressureDACOffset);
00127             numericKeypadPageHandler->SetEasyGUIVariableToEdit(GuiVar_gasBackPressureDACOffset);
00128             numericKeypadPageHandler->SetEasyGUICallingPage(GuiStruct_GasBackPressureDACPage_Def);
00129             numericKeypadPageHandler->SetEditVariableRange(minimumGainAndOffsetValue, maximumGainAndOffsetValue);
00130             numericKeypadPageHandler->SetEditVariableName("50 PSI");
00131             numericKeypadPageHandler->DisplayEasyGUIPage();
00132         }
00133         
00134         return true;
00135     }
00136     
00137     if(touchAreaIndex == GAS_BACKPRESSURE_DAC_GET) {
00138         
00139         GetCurrentGasBackPressureDACValuesFromGC();
00140         SetAllEditableFieldsToInactive();
00141         
00142         UpdateEasyGUIPage();
00143         
00144         return true;
00145     }
00146     
00147     if(touchAreaIndex == GAS_BACKPRESSURE_DAC_SET) {
00148         
00149         SetCurrentGasBackPressureDACValuesInGC();
00150         SetAllEditableFieldsToInactive();
00151         
00152         return true;
00153     }
00154     
00155     // 'else' - none of the above
00156     return false;
00157 }
00158 
00159 
00160 /*
00161     Gets the current gas backpressure gain and offset values from the GC,
00162     and copies them to the relevant easyGUI variables so that they can be displayed.
00163     Note that it is up to the caller to redisplay the page.
00164     
00165     No arguments, no return code
00166 */
00167 bool GasBackPressureDACPageHandler::GetCurrentGasBackPressureDACValuesFromGC(void)
00168 {
00169     char response[50];
00170 
00171     // Gain first
00172     while(usbHostGC->ExecutingSetDeviceReport()) {}
00173     usbHostGC->SetDeviceReport(usbDevice, "GBPG", response);
00174     // We expect a response like this: "DBPGnnnn", when nnnn is the DAC value,
00175     // or "EPKT" if something went wrong
00176     if(response[0] == 'E') {
00177         // Assume "EPKT"
00178         return false;
00179     }
00180     
00181     //Ensure null terminator
00182     response[8] = '\0';
00183     
00184     // Ignore leading zeroes - but make sure we leave the final digit, whether zero or not
00185     int startIndex;
00186     for(startIndex = 4; startIndex < 7; ++startIndex) {
00187         if(response[startIndex] != '0') {
00188             break;
00189         }
00190     }
00191     
00192     strcpy(GuiVar_gasBackPressureDACGain, &response[startIndex]);
00193     
00194     
00195     // Now the offset
00196     while(usbHostGC->ExecutingSetDeviceReport()) {}
00197     usbHostGC->SetDeviceReport(usbDevice, "GBPO", response);
00198     // We expect a response like this: "DBPOnnnn", when nnnn is the DAC value,
00199     // or "EPKT" if something went wrong
00200     if(response[0] == 'E') {
00201         // Assume "EPKT"
00202         return false;
00203     }
00204     
00205     //Ensure null terminator
00206     response[8] = '\0';
00207     
00208     // Ignore leading zeroes - but make sure we leave the final digit, whether zero or not
00209     for(startIndex = 4; startIndex < 7; ++startIndex) {
00210         if(response[startIndex] != '0') {
00211             break;
00212         }
00213     }
00214     
00215     strcpy(GuiVar_gasBackPressureDACOffset, &response[startIndex]);
00216     
00217     return true;
00218 }
00219 
00220 
00221 /*
00222     To set the gas backpressure gain or offset, the GC expects a command of the form:
00223     
00224         "SBPcnnnn"
00225     
00226     where 'c' is the single character 'G' for gain or 'O' for offset, and "nnnn" is the value in question.
00227     This must be four digits, and must therefore be padded with leading zeroes if the actual value is shorter than this. 
00228     
00229     This function constructs the required command.
00230     
00231     Arguments: a buffer to contain the command
00232                a single character for gain or offset (we leave it up to the caller to make sure this is 'G' or 'O')
00233                a pointer to the easyGUI variable containing the value (we expect this to be a null-terminated character string)
00234                
00235     No return code
00236 */
00237 void GasBackPressureDACPageHandler::ConstructSetBackPressureCommand(char *command, char gainOrOffsetCharacter, GuiConst_TEXT* easyGUIVariable)
00238 {
00239     command[0] = 'S';
00240     command[1] = 'B';
00241     command[2] = 'P';
00242     command[3] = gainOrOffsetCharacter;
00243     
00244     int startIndex = 8 - strlen(easyGUIVariable);
00245     
00246     int index = 4;
00247     while(index < startIndex) {
00248         command[index++] = '0';
00249     }
00250     int easyGUIVariableIndex = 0;
00251     while(index < 8) {
00252         command[index++] = easyGUIVariable[easyGUIVariableIndex++];
00253     }
00254     
00255     command[8] = '\0';
00256 }
00257 
00258 /*
00259     Sets the Gas Backpressure DAC gain and offset values in the GC,
00260     from our easyGUIvariables.
00261     
00262     No arguments
00263     
00264     Returns true on success, false on failure
00265 */
00266 bool GasBackPressureDACPageHandler::SetCurrentGasBackPressureDACValuesInGC(void)
00267 {
00268     char command[10];
00269     char response[50];
00270     
00271     // Gain first
00272     ConstructSetBackPressureCommand(command, 'G', GuiVar_gasBackPressureDACGain);
00273     while(usbHostGC->ExecutingSetDeviceReport()) {}
00274     usbHostGC->SetDeviceReport(usbDevice, command, response);
00275     if(response[0] == 'E') {
00276         // Assume "EPKT"
00277         return false;
00278     }
00279     
00280     // Now offset     
00281     ConstructSetBackPressureCommand(command, 'O', GuiVar_gasBackPressureDACOffset);
00282     while(usbHostGC->ExecutingSetDeviceReport()) {}
00283     usbHostGC->SetDeviceReport(usbDevice, command, response);
00284     if(response[0] == 'E') {
00285         // Assume "EPKT"
00286         return false;
00287     }
00288     
00289     return true;
00290 }
00291     
00292 
00293 /*
00294     (Re)display the easyGUI 'GasBackPressureDACPage' -
00295     e.g. after the caller has updated one or more of the easyGUI variables
00296     included in the page, and wants to display the new value to the user.
00297     
00298     No arguments, no return code
00299 */
00300 void GasBackPressureDACPageHandler::UpdateEasyGUIPage(void)
00301 {
00302     GetGCStatusLoop *getGCStatusLoop = GetGCStatusLoop::GetInstance();
00303     
00304     if(getGCStatusLoop != NULL) {
00305         // The Gas Back Pressure DAC page has a status rectangle for the gas - 
00306         // GetGCStatusLoop can display this, we cannot
00307         getGCStatusLoop->ForceUpdateOfGasBackPressureDACPage();
00308     }
00309 }
00310 
00311 /*
00312     Inactivates all editable fields, i.e. sets all the background colours to the 'inactive' colour,
00313     and sets the currently active field selection to 'none'.
00314     
00315     No arguments, no return code
00316 */
00317 void GasBackPressureDACPageHandler::SetAllEditableFieldsToInactive(void)
00318 {
00319     GuiVar_gasBackPressureDACGainBackgroundColour = inactiveFieldBackgroundColour;
00320     GuiVar_gasBackPressureDACOffsetBackgroundColour = inactiveFieldBackgroundColour;
00321     
00322     currentlyActiveField = NONE;
00323 }
00324 
00325 
00326 /*
00327     Caller is telling us it is about to display the easyGUI 'GasBackPressureDACPage',
00328     and that we should do whatever we have to do to get it ready,
00329     before the caller displays it.
00330     
00331     Args: a boolean set true if the easyGUI variables displayed in the page are to be updated
00332           from the GC, false if not. The expectation is that this will be true 
00333           except when the user has just updated one of variables using the numeric keypad.
00334           
00335     No return code
00336 */
00337 void GasBackPressureDACPageHandler::DisplayingEasyGUIPage(bool updateEasyGUIVariables)
00338 {
00339     if(updateEasyGUIVariables) {
00340         GetCurrentGasBackPressureDACValuesFromGC();
00341     }
00342     
00343     SetAllEditableFieldsToInactive();
00344 }
00345 
00346 
00347 /*
00348     Selects one field (gain or offset) as the active field, and if the other field is currently active, inactivates it
00349     
00350     Arguments: 'this' field - i.e. the field to be activated 
00351                the other field
00352                pointer to the background colour easyGUI variable for 'this' field
00353                pointer to the background colour easyGUI variable for the other field
00354                
00355     no return code
00356 */
00357 void GasBackPressureDACPageHandler::SelectActiveField(ActiveField thisField, ActiveField otherField, GuiConst_INTCOLOR *thisFieldBGColour, GuiConst_INTCOLOR* otherFieldBGColour)
00358 {
00359     if(currentlyActiveField == otherField) {
00360         *otherFieldBGColour = inactiveFieldBackgroundColour;
00361     }
00362     
00363     *thisFieldBGColour = activeFieldBackgroundColour;
00364     
00365     currentlyActiveField = thisField;
00366 
00367     UpdateEasyGUIPage();
00368 }
00369