John Mitchell / lpc4088_displaymodule_GC500_2_5inch

Dependencies:   DMBasicGUI DMSupport

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GasCalibrationPageHandler.cpp Source File

GasCalibrationPageHandler.cpp

00001 #include "GasCalibrationPageHandler.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 GuiConst_INTCOLOR SixteenBitColorValue(GuiConst_INT8U red, GuiConst_INT8U green, GuiConst_INT8U blue); 
00024 
00025 /*
00026     We distinguish the 'active' field - i.e. the one being edited by the user - 
00027     from the inactive fields, by its background colour
00028 */
00029 #define EXPERIMENTING_WITH_COLORS
00030 const GuiConst_INTCOLOR GasCalibrationPageHandler::inactiveFieldBackgroundColour = SixteenBitColorValue(192, 192, 192); // Grey
00031 #ifdef EXPERIMENTING_WITH_COLORS
00032 const GuiConst_INTCOLOR GasCalibrationPageHandler::activeFieldBackgroundColour   = SixteenBitColorValue(255, 255, 0); // Yellow
00033 #else
00034 const GuiConst_INTCOLOR GasCalibrationPageHandler::activeFieldBackgroundColour   = SixteenBitColorValue(255, 255, 255); // White
00035 #endif // EXPERIMENTING_WITH_COLORS
00036 
00037 
00038 /*                      
00039     Note that this class is a singleton - we do not need or want there to be more than one instance of it
00040     (we do not want multiple values for the carrier gas selection, etc, and nor will we show 
00041     more than one easyGUI 'GasCalibrationPage' to the user at the same time).
00042 */
00043 GasCalibrationPageHandler * GasCalibrationPageHandler::theGasCalibrationPageHandlerInstance = NULL;
00044 
00045 
00046 /*
00047     Singleton class - return the one and only instance, first creating it if necessary.
00048 */
00049 GasCalibrationPageHandler * GasCalibrationPageHandler::GetInstance(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00050 {
00051     if (theGasCalibrationPageHandlerInstance == NULL) {
00052         theGasCalibrationPageHandlerInstance = new GasCalibrationPageHandler(newUsbDevice, newUsbHostGC);
00053     }
00054     
00055     return theGasCalibrationPageHandlerInstance;
00056 }
00057 
00058 // Singleton class - private constructor
00059 GasCalibrationPageHandler::GasCalibrationPageHandler(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
00060 {
00061     usbDevice = newUsbDevice;
00062     usbHostGC = newUsbHostGC;
00063     
00064 
00065     variablesForTouchArea[0].touchAreaIndex                    = GAS_CALIB_DAC_1_VALUE_EDIT;
00066     variablesForTouchArea[0].easyGUIVariablePtr                = GuiVar_gasCalibDAC1;
00067     variablesForTouchArea[0].maxValue                          = 4095;
00068     variablesForTouchArea[0].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibDAC1BackgroundColour;
00069     strcpy(variablesForTouchArea[0].variableName, "DAC value 1");
00070 
00071     variablesForTouchArea[1].touchAreaIndex                    = GAS_CALIB_FLOW_1_VALUE_EDIT;
00072     variablesForTouchArea[1].easyGUIVariablePtr                = GuiVar_gasCalibFlow1;
00073     variablesForTouchArea[1].maxValue                          = 500;
00074     variablesForTouchArea[1].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibFlow1BackgroundColour;
00075     strcpy(variablesForTouchArea[1].variableName, "Flow value 1");
00076 
00077     variablesForTouchArea[2].touchAreaIndex                    = GAS_CALIB_DAC_2_VALUE_EDIT;
00078     variablesForTouchArea[2].easyGUIVariablePtr                = GuiVar_gasCalibDAC2;
00079     variablesForTouchArea[2].maxValue                          = 4095;
00080     variablesForTouchArea[2].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibDAC2BackgroundColour;
00081     strcpy(variablesForTouchArea[2].variableName, "DAC value 2");
00082 
00083     variablesForTouchArea[3].touchAreaIndex                    = GAS_CALIB_FLOW_2_VALUE_EDIT;
00084     variablesForTouchArea[3].easyGUIVariablePtr                = GuiVar_gasCalibFlow2;
00085     variablesForTouchArea[3].maxValue                          = 500;
00086     variablesForTouchArea[3].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibFlow2BackgroundColour;
00087     strcpy(variablesForTouchArea[3].variableName, "Flow value 2");
00088 
00089     variablesForTouchArea[4].touchAreaIndex                    = GAS_CALIB_DAC_3_VALUE_EDIT;
00090     variablesForTouchArea[4].easyGUIVariablePtr                = GuiVar_gasCalibDAC3;
00091     variablesForTouchArea[4].maxValue                          = 4095;
00092     variablesForTouchArea[4].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibDAC3BackgroundColour;
00093     strcpy(variablesForTouchArea[4].variableName, "DAC value 3");
00094 
00095     variablesForTouchArea[5].touchAreaIndex                    = GAS_CALIB_FLOW_3_VALUE_EDIT;
00096     variablesForTouchArea[5].easyGUIVariablePtr                = GuiVar_gasCalibFlow3;
00097     variablesForTouchArea[5].maxValue                          = 500;
00098     variablesForTouchArea[5].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibFlow3BackgroundColour;
00099     strcpy(variablesForTouchArea[5].variableName, "Flow value 3");
00100 
00101     variablesForTouchArea[6].touchAreaIndex                    = GAS_CALIB_DAC_4_VALUE_EDIT;
00102     variablesForTouchArea[6].easyGUIVariablePtr                = GuiVar_gasCalibDAC4;
00103     variablesForTouchArea[6].maxValue                          = 4095;
00104     variablesForTouchArea[6].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibDAC4BackgroundColour;
00105     strcpy(variablesForTouchArea[6].variableName, "DAC value 4");
00106 
00107     variablesForTouchArea[7].touchAreaIndex                    = GAS_CALIB_FLOW_4_VALUE_EDIT;
00108     variablesForTouchArea[7].easyGUIVariablePtr                = GuiVar_gasCalibFlow4;
00109     variablesForTouchArea[7].maxValue                          = 500;
00110     variablesForTouchArea[7].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibFlow4BackgroundColour;
00111     strcpy(variablesForTouchArea[7].variableName, "Flow value 4");
00112 
00113     variablesForTouchArea[8].touchAreaIndex                    = GAS_CALIB_DAC_5_VALUE_EDIT;
00114     variablesForTouchArea[8].easyGUIVariablePtr                = GuiVar_gasCalibDAC5;
00115     variablesForTouchArea[8].maxValue                          = 4095;
00116     variablesForTouchArea[8].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibDAC5BackgroundColour;
00117     strcpy(variablesForTouchArea[8].variableName, "DAC value 5");
00118 
00119     variablesForTouchArea[9].touchAreaIndex                    = GAS_CALIB_FLOW_5_VALUE_EDIT;
00120     variablesForTouchArea[9].easyGUIVariablePtr                = GuiVar_gasCalibFlow5;
00121     variablesForTouchArea[9].maxValue                          = 500;
00122     variablesForTouchArea[9].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibFlow5BackgroundColour;
00123     strcpy(variablesForTouchArea[9].variableName, "Flow value 5");
00124 
00125     variablesForTouchArea[10].touchAreaIndex                    = GAS_CALIB_DAC_6_VALUE_EDIT;
00126     variablesForTouchArea[10].easyGUIVariablePtr                = GuiVar_gasCalibDAC6;
00127     variablesForTouchArea[10].maxValue                          = 4095;
00128     variablesForTouchArea[10].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibDAC6BackgroundColour;
00129     strcpy(variablesForTouchArea[10].variableName, "DAC value 6");
00130 
00131     variablesForTouchArea[11].touchAreaIndex                    = GAS_CALIB_FLOW_6_VALUE_EDIT;
00132     variablesForTouchArea[11].easyGUIVariablePtr                = GuiVar_gasCalibFlow6;
00133     variablesForTouchArea[11].maxValue                          = 500;
00134     variablesForTouchArea[11].easyGUIBackgroundColorVariablePtr = &GuiVar_gasCalibFlow6BackgroundColour;
00135     strcpy(variablesForTouchArea[11].variableName, "Flow value 6");
00136 }
00137     
00138 // Private destructor also
00139 GasCalibrationPageHandler::~GasCalibrationPageHandler()
00140 {
00141     //SaveCarrierGasSelectionToQSPISettings();
00142 }
00143 
00144 
00145 /*
00146     Tells the caller whether or not the specified touch index is on the easyGUI 'GasCalibrationPage',
00147     and therefore needs to be handled by this class.
00148     
00149     Args: the touch area index in question
00150     
00151     Return code: true if the touch area is 'one of ours', false if not
00152 */
00153 bool GasCalibrationPageHandler::TouchAreaIsOnCalibrationPage(int touchAreaIndex)
00154 {
00155     if((touchAreaIndex >= MIN_GAS_CALIB_TOUCHINDEX) && (touchAreaIndex <= MAX_GAS_CALIB_TOUCHINDEX)) {
00156         return true;
00157     }
00158     
00159     // 'else'
00160     return false;
00161 }
00162 
00163 
00164 /*
00165     If the specified touch area represents a key or field on the easyGUI 'GasCalibrationPage',
00166     this function performs whatever action is appropriate for it. Provided so that the caller
00167     can (in effect) say "if this is one of yours, deal with it", without needing to know 
00168     anything about what this class does, or how it handles the touch areas on that easyGUI page.
00169 
00170     Args: the touch area index in question
00171     
00172     Returns true if it dealt with the touch area (so the caller need not do anything else 
00173     with the value), or false if it did not deal with the touch area (implying that it 
00174     was not a touch area on the easyGUI 'Gas CalibrationPage', and so the caller
00175     must deal with it some other way).
00176 */
00177 bool GasCalibrationPageHandler::DealWithTouch(int touchAreaIndex)
00178 {
00179     bool dealtWithTouch = false;
00180     
00181     // Has the user selected a field to edit - 
00182     // if so, we will have an entry in our 'variablesForTouchArea' array that corresponds with this touch area
00183     int indexOfValueToEdit = GetIndexOfValueToEditForTouchArea(touchAreaIndex);
00184     if(indexOfValueToEdit  != -1) {        
00185         // User has selected a field to edit
00186         indexOfValueCurrentlyBeingEdited = indexOfValueToEdit;
00187         NumericKeypadPageHandler* numericKeypadPageHandler = NumericKeypadPageHandler::GetInstance(usbDevice, usbHostGC);
00188         if(numericKeypadPageHandler != NULL) {
00189             numericKeypadPageHandler->StartEditing(variablesForTouchArea[indexOfValueCurrentlyBeingEdited].easyGUIVariablePtr);
00190             numericKeypadPageHandler->SetEasyGUIVariableToEdit(variablesForTouchArea[indexOfValueCurrentlyBeingEdited].easyGUIVariablePtr);
00191             numericKeypadPageHandler->SetEasyGUICallingPage(GuiStruct_GasCalibrationPage_Def);
00192             numericKeypadPageHandler->SetEditVariableRange(0, variablesForTouchArea[indexOfValueCurrentlyBeingEdited].maxValue);
00193             numericKeypadPageHandler->SetEditVariableName(variablesForTouchArea[indexOfValueCurrentlyBeingEdited].variableName);
00194             numericKeypadPageHandler->DisplayEasyGUIPage();
00195         }
00196 
00197         dealtWithTouch = true;
00198     } 
00199         
00200     if(!dealtWithTouch) {
00201         switch(touchAreaIndex) {
00202             case GAS_CALIB_HELIUM:
00203                 if(GuiVar_gasCalibGasSelection != CARRIER_GAS_HELIUM) {
00204                     GuiVar_gasCalibGasSelection = CARRIER_GAS_HELIUM;
00205                     GetCurrentCalibrationFromGC();
00206                     SetAllFieldBackgroundColoursToInactive();
00207                     indexOfValueCurrentlyBeingEdited = -1; 
00208                     UpdateEasyGUIPage();
00209                     SaveCarrierGasSelectionToQSPISettings();
00210                 }
00211                 dealtWithTouch = true;
00212                 break;
00213                 
00214             case GAS_CALIB_HYDROGEN:
00215                 if(GuiVar_gasCalibGasSelection != CARRIER_GAS_HYDROGEN) {
00216                     GuiVar_gasCalibGasSelection = CARRIER_GAS_HYDROGEN;
00217                     GetCurrentCalibrationFromGC();
00218                     SetAllFieldBackgroundColoursToInactive();
00219                     indexOfValueCurrentlyBeingEdited = -1; 
00220                     UpdateEasyGUIPage();
00221                     SaveCarrierGasSelectionToQSPISettings();
00222                 }
00223                 dealtWithTouch = true;
00224                 break;
00225                 
00226             case GAS_CALIB_NITROGEN:
00227                 if(GuiVar_gasCalibGasSelection != CARRIER_GAS_NITROGEN) {
00228                     GuiVar_gasCalibGasSelection = CARRIER_GAS_NITROGEN;
00229                     GetCurrentCalibrationFromGC();
00230                     SetAllFieldBackgroundColoursToInactive();
00231                     indexOfValueCurrentlyBeingEdited = -1; 
00232                     UpdateEasyGUIPage();
00233                     SaveCarrierGasSelectionToQSPISettings();
00234                 }
00235                 dealtWithTouch = true;
00236                 break;
00237                 
00238             case GAS_CALIB_CANCEL_BUTTON:
00239                 GetCurrentCalibrationFromGC();
00240                 SetAllFieldBackgroundColoursToInactive();
00241                 indexOfValueCurrentlyBeingEdited = -1;
00242                 dealtWithTouch = true;
00243                 UpdateEasyGUIPage();
00244                 break;
00245                 
00246             case GAS_CALIB_APPLY_BUTTON:
00247                 SetCurrentCalibrationInGC();
00248                 // Now deactivate all fields - shows user we have done something - 
00249                 // also says "job done"
00250                 SetAllFieldBackgroundColoursToInactive();
00251                 indexOfValueCurrentlyBeingEdited = -1;
00252                 dealtWithTouch = true;
00253                 UpdateEasyGUIPage();
00254                 break;
00255                                 
00256             default:
00257                 // Leave 'dealtWithTouch' set false
00258                 break;
00259         }
00260     }
00261     
00262     return dealtWithTouch;
00263 }
00264 
00265 
00266 /*
00267     Caller is telling us it is about to display the easyGUI 'GasCalibrationPage',
00268     and that we should do whatever we have to do to get it ready,
00269     before the caller displays it.
00270     
00271     No arguments, no return code
00272 */
00273 void GasCalibrationPageHandler::DisplayingEasyGUIPage(bool updateEasyGUIVariables)
00274 {
00275     ReadCarrierGasSelectionFromQSPISettings();
00276     
00277     if(updateEasyGUIVariables) {
00278         GetCurrentCalibrationFromGC();
00279     }
00280     
00281     SetAllFieldBackgroundColoursToInactive();
00282     
00283     indexOfValueCurrentlyBeingEdited = -1; 
00284 }
00285 
00286 
00287 /*
00288     Saves the current parameter values to QSPI settings,
00289     so they are maintained even when the user powers off
00290     
00291     No arguments, no return value
00292 */
00293 void GasCalibrationPageHandler::SaveCarrierGasSelectionToQSPISettings(void)
00294 {
00295     // Put values to QSPI settings, using SettingsHandler member functions
00296 
00297     SettingsHandler::PutIntegerValueToQSPISettings("CarrierGasSelection", GuiVar_gasCalibGasSelection);
00298 }
00299 
00300 
00301 /*
00302     Reads the current parameter values from QSPI settings,
00303     so they are maintained even when the user powers off
00304     
00305     No arguments, no return value
00306 */
00307 void GasCalibrationPageHandler::ReadCarrierGasSelectionFromQSPISettings(void)
00308 {
00309     // Get values from QSPI settings, using SettingsHandler member functions
00310     
00311     GuiVar_gasCalibGasSelection = SettingsHandler::GetIntegerValueFromQSPISettings("CarrierGasSelection", CARRIER_GAS_HELIUM);
00312 }
00313 
00314 
00315 /*
00316     Gets the current gas calibration DAC and flow values from the GC, 
00317     and puts them into the corresponding easyGUI variables
00318     
00319     No arguments, no return value
00320 */
00321 void GasCalibrationPageHandler::GetCurrentCalibrationFromGC(void)
00322 {
00323     GetCalibrationDACAndFlowValuesFromGC(GuiVar_gasCalibGasSelection, 1, GuiVar_gasCalibDAC1, GuiVar_gasCalibFlow1);
00324     GetCalibrationDACAndFlowValuesFromGC(GuiVar_gasCalibGasSelection, 2, GuiVar_gasCalibDAC2, GuiVar_gasCalibFlow2);
00325     GetCalibrationDACAndFlowValuesFromGC(GuiVar_gasCalibGasSelection, 3, GuiVar_gasCalibDAC3, GuiVar_gasCalibFlow3);
00326     GetCalibrationDACAndFlowValuesFromGC(GuiVar_gasCalibGasSelection, 4, GuiVar_gasCalibDAC4, GuiVar_gasCalibFlow4);
00327     GetCalibrationDACAndFlowValuesFromGC(GuiVar_gasCalibGasSelection, 5, GuiVar_gasCalibDAC5, GuiVar_gasCalibFlow5);
00328     GetCalibrationDACAndFlowValuesFromGC(GuiVar_gasCalibGasSelection, 6, GuiVar_gasCalibDAC6, GuiVar_gasCalibFlow6);
00329 }
00330 
00331 /*
00332     Sets the current gas calibration DAC and flow values in the GC, 
00333     getting the values from the corresponding easyGUI variables
00334     
00335     No arguments, no return value
00336 */
00337 void GasCalibrationPageHandler::SetCurrentCalibrationInGC(void)
00338 {
00339     SetCalibrationDACAndFlowValuesInGC(GuiVar_gasCalibGasSelection, 1, GuiVar_gasCalibDAC1, GuiVar_gasCalibFlow1);
00340     SetCalibrationDACAndFlowValuesInGC(GuiVar_gasCalibGasSelection, 2, GuiVar_gasCalibDAC2, GuiVar_gasCalibFlow2);
00341     SetCalibrationDACAndFlowValuesInGC(GuiVar_gasCalibGasSelection, 3, GuiVar_gasCalibDAC3, GuiVar_gasCalibFlow3);
00342     SetCalibrationDACAndFlowValuesInGC(GuiVar_gasCalibGasSelection, 4, GuiVar_gasCalibDAC4, GuiVar_gasCalibFlow4);
00343     SetCalibrationDACAndFlowValuesInGC(GuiVar_gasCalibGasSelection, 5, GuiVar_gasCalibDAC5, GuiVar_gasCalibFlow5);
00344     SetCalibrationDACAndFlowValuesInGC(GuiVar_gasCalibGasSelection, 6, GuiVar_gasCalibDAC6, GuiVar_gasCalibFlow6);
00345 }
00346 
00347 
00348 /*
00349     Gets the gas calibration DAC and flow values for the specified carrier gas
00350     and the specified index (1-6), and converts them to strings
00351     
00352     Arguments: carrier gas (integer - see enum in header file)
00353                index (1-6)
00354                buffer for DAC value
00355                buffer for flow value
00356                
00357     Returns true if OK, false if error
00358 */
00359 bool GasCalibrationPageHandler::GetCalibrationDACAndFlowValuesFromGC(int carrierGasSelection, int valueIndex, char *bufferForDACValue, char *bufferForFlowValue)
00360 {
00361     char cmd[8];
00362     char response[50];
00363     
00364     
00365     // DAC value first
00366     
00367     if(!ConstructGasCalibrationGCCommand(false, carrierGasSelection, valueIndex, true, cmd)) {
00368         return false;
00369     }
00370     
00371     while(usbHostGC->ExecutingSetDeviceReport()) {}
00372 
00373     usbHostGC->SetDeviceReport(usbDevice, cmd, response);
00374     // We expect a response like this: "DHEAnnnn", when nnnn is the DAC value,
00375     // or "EPKT" if something went wrong
00376     if(response[0] == 'E') {
00377         // Assume "EPKT"
00378         return false;
00379     }
00380     
00381     // 'else' - value received
00382     CopyCommandResponseToBufferWithoutLeadingZeroes(&response[4], bufferForDACValue);
00383     
00384     
00385     // Now the flow value
00386     
00387     if(!ConstructGasCalibrationGCCommand(false, carrierGasSelection, valueIndex, false, cmd)) {
00388         return false;
00389     }
00390         
00391     while(usbHostGC->ExecutingSetDeviceReport()) {}
00392 
00393     usbHostGC->SetDeviceReport(usbDevice, cmd, response);
00394     // We expect a response like this: "DHEGnnnn", when nnnn is the flow value,
00395     // or "EPKT" if something went wrong
00396     if(response[0] == 'E') {
00397         // Assume "EPKT"
00398         return false;
00399     }
00400     
00401     // 'else' - value received
00402     CopyCommandResponseToBufferWithoutLeadingZeroes(&response[4], bufferForFlowValue);
00403     
00404     return true;
00405 }
00406 
00407 /*
00408     Sets the gas calibration DAC and flow values for the specified carrier gas
00409     and the specified index (1-6) in the GC, using the string values passed to it
00410     
00411     Arguments: carrier gas (integer - see enum in header file)
00412                index (1-6)
00413                buffer containing the new DAC value as a string
00414                buffer containing the new flow value as a string
00415                
00416     Returns true if OK, false if error
00417 */
00418 bool GasCalibrationPageHandler::SetCalibrationDACAndFlowValuesInGC(int carrierGasSelection, int valueIndex, char *bufferForDACValue, char *bufferForFlowValue)
00419 {
00420     char cmd[8];
00421     char response[50];    
00422     
00423     // DAC value first
00424     
00425     if(!ConstructGasCalibrationGCCommand(true, carrierGasSelection, valueIndex, true, cmd)) {
00426         return false;
00427     }
00428     
00429     CopyBufferDigitsToCommand(cmd, bufferForDACValue);
00430     
00431     while(usbHostGC->ExecutingSetDeviceReport()) {}
00432 
00433     usbHostGC->SetDeviceReport(usbDevice, cmd, response);
00434     // We expect "DACK" as the response for success, "DNAK" or "EPKT" if something went wrong
00435     if(response[1] != 'A') {
00436         return false;
00437     }
00438     
00439     
00440     // Now the flow value
00441     
00442     if(!ConstructGasCalibrationGCCommand(true, carrierGasSelection, valueIndex, false, cmd)) {
00443         return false;
00444     }
00445         
00446     CopyBufferDigitsToCommand(cmd, bufferForFlowValue);
00447 
00448     while(usbHostGC->ExecutingSetDeviceReport()) {}
00449 
00450     usbHostGC->SetDeviceReport(usbDevice, cmd, response);
00451     // As before, we expect "DACK" as the response for success, "DNAK" or "EPKT" if something went wrong
00452     if(response[1] != 'A') {
00453         return false;
00454     }
00455     
00456     // 'else' success with both commands
00457     return true;
00458 }
00459 
00460 
00461 /*
00462     (Re)display the easyGUI 'GasCalibrationPage' -
00463     e.g. after the caller has updated one or more of the easyGUI variables
00464     included in the page, and wants to display the new value to the user.
00465     
00466     No arguments, no return code
00467 */
00468 void GasCalibrationPageHandler::UpdateEasyGUIPage(void)
00469 {
00470     GetGCStatusLoop *getGCStatusLoop = GetGCStatusLoop::GetInstance();
00471     
00472     if(getGCStatusLoop != NULL) {
00473         // The Gas Calibration page has a status rectangle for the gas - 
00474         // GetGCStatusLoop can display this, we cannot
00475         getGCStatusLoop->ForceUpdateOfGasCalibrationPage();
00476     }
00477 }
00478 
00479 
00480 /*
00481     The gas calibration GC commands are all very similar. This function returns the required command,
00482     based on whether set or get is required, the carrier gas, DAC or Flow, and the index.
00483     
00484     Arguments: a boolean - true for set, false for get
00485                carrier gas (integer - see enum in header file)
00486                index (1-6)
00487                a boolean - true for the DAC command, false for the flow command
00488                pointer to a buffer to contain the command
00489                
00490     Returns true if the index was valid (1-6), false if not
00491 */    
00492 bool GasCalibrationPageHandler::ConstructGasCalibrationGCCommand(bool wantSetCommand, int carrierGasSelection, int valueIndex, bool wantDACCommand, char *commandBuffer)
00493 {
00494     if((valueIndex < 1) || (valueIndex > 6)) {
00495         return false;
00496     }
00497     
00498     commandBuffer[0] = wantSetCommand ? 'S' : 'G';
00499     
00500     switch (carrierGasSelection) {
00501         case CARRIER_GAS_HYDROGEN:
00502             commandBuffer[1] = 'H';
00503             commandBuffer[2] = '2';
00504             break;
00505         case CARRIER_GAS_NITROGEN:
00506             commandBuffer[1] = 'N';
00507             commandBuffer[2] = '2';
00508             break;
00509         default: // Assume helium
00510             commandBuffer[1] = 'H';
00511             commandBuffer[2] = 'E';
00512             break;
00513     }
00514     
00515     if(wantDACCommand) {
00516         switch(valueIndex) {
00517             case 1:
00518                 commandBuffer[3] = 'A';
00519                 break;
00520             case 2:
00521                 commandBuffer[3] = 'B';
00522                 break;
00523             case 3:
00524                 commandBuffer[3] = 'C';
00525                 break;
00526             case 4:
00527                 commandBuffer[3] = 'D';
00528                 break;
00529             case 5:
00530                 commandBuffer[3] = 'E';
00531                 break;
00532             default: // Assume 6
00533                 commandBuffer[3] = 'F';
00534                 break;
00535         }
00536     } else {
00537         // Flow command
00538         switch(valueIndex) {
00539             case 1:
00540                 commandBuffer[3] = 'G';
00541                 break;
00542             case 2:
00543                 commandBuffer[3] = 'H';
00544                 break;
00545             case 3:
00546                 commandBuffer[3] = 'I';
00547                 break;
00548             case 4:
00549                 commandBuffer[3] = 'J';
00550                 break;
00551             case 5:
00552                 commandBuffer[3] = 'K';
00553                 break;
00554             default: // Assume 6
00555                 commandBuffer[3] = 'L';
00556                 break;
00557         }
00558     }
00559     
00560     commandBuffer[4] = '\0';
00561     
00562     return true;
00563 }
00564 
00565 
00566 /*
00567     Sets the background colours of all 'editable' fields to the 'inactive' colour.
00568     
00569     No arguments, no return code
00570 */
00571 void GasCalibrationPageHandler::SetAllFieldBackgroundColoursToInactive(void)
00572 {
00573     GuiVar_gasCalibDAC1BackgroundColour = inactiveFieldBackgroundColour;
00574     GuiVar_gasCalibFlow1BackgroundColour = inactiveFieldBackgroundColour;
00575 
00576     GuiVar_gasCalibDAC2BackgroundColour = inactiveFieldBackgroundColour;
00577     GuiVar_gasCalibFlow2BackgroundColour = inactiveFieldBackgroundColour;
00578 
00579     GuiVar_gasCalibDAC3BackgroundColour = inactiveFieldBackgroundColour;
00580     GuiVar_gasCalibFlow3BackgroundColour = inactiveFieldBackgroundColour;
00581 
00582     GuiVar_gasCalibDAC4BackgroundColour = inactiveFieldBackgroundColour;
00583     GuiVar_gasCalibFlow4BackgroundColour = inactiveFieldBackgroundColour;
00584 
00585     GuiVar_gasCalibDAC5BackgroundColour = inactiveFieldBackgroundColour;
00586     GuiVar_gasCalibFlow5BackgroundColour = inactiveFieldBackgroundColour;
00587 
00588     GuiVar_gasCalibDAC6BackgroundColour = inactiveFieldBackgroundColour;
00589     GuiVar_gasCalibFlow6BackgroundColour = inactiveFieldBackgroundColour;
00590 }
00591 
00592 
00593 /*
00594     Tells the caller if the specified touch area corresponds to a value the user can edit
00595     on the easyGUI 'GasCalibrationPage', and if so, which one.
00596     
00597     Args: the touch area index in question
00598     
00599     Returns the index of the corresponding entry in our 'variablesForTouchArea' array,
00600     or -1 if there is no such entry. It is up to the caller to check for -1
00601                                      **************************************
00602 */
00603 int GasCalibrationPageHandler::GetIndexOfValueToEditForTouchArea(int touchAreaIndex)
00604 {
00605     for (int index = 0; index < COUNT_OF_VARIABLES_FOR_TOUCH_AREAS; ++index) {
00606         if(variablesForTouchArea[index].touchAreaIndex == touchAreaIndex) {
00607             return index;
00608         }
00609     }
00610     
00611     // 'else' no Gas Calibration value corresponds to the specified touch area
00612     return -1;
00613 }
00614 
00615 
00616 /*
00617     The response to a "Get DAC/Flow value n" command will always consist of four digits, 
00618     padded with leading zeroes if necessary. We want to display it to the user 
00619     without the leading zeroes, but - obviously - *with* any zeroes contained in the number itself,
00620     e.g. "0100" must be copied as "100", not "1". Note also that "0000" must be copied as "0" - 
00621     the destination string must not be left blank.
00622     
00623     Arguments: pointer to a character buffer containing the four-digit response [it is up to the caller to ensure this is valid]
00624                pointer to a character buffer to which we are to copy the response without leading zeroes
00625                
00626     No return value
00627 */
00628 void GasCalibrationPageHandler::CopyCommandResponseToBufferWithoutLeadingZeroes(char *commandResponse, char *buffer)
00629 {
00630     int bufferIndex = 0;
00631     bool wantZeroes = false;
00632     
00633     for (int responseIndex = 0; responseIndex < 4; ++responseIndex) {
00634         if(commandResponse[responseIndex] != '0') {
00635             buffer[bufferIndex++] = commandResponse[responseIndex];
00636             wantZeroes = true; // We have had a non-zero character, so we want zeroes from now on
00637         } else if (wantZeroes) {
00638             buffer[bufferIndex++] = commandResponse[responseIndex];
00639         }
00640     }
00641     
00642     if(bufferIndex == 0) {
00643         // Response *is* zero - copied no chars at all so far
00644         buffer[bufferIndex++] = '0';
00645     }
00646     
00647     buffer[bufferIndex] = '\0';
00648 }
00649 
00650 
00651 /*
00652     Gas calibration DAC and Flow values are stored in the easyGUI variables without leading zeroes,
00653     but we must always pass them to the GC as four-digit values, padded with leading zeroes if less than four digits.
00654     The four digits must be appended to the relevant GC 'Set' command, e.g. "SHEAnnnn".
00655     This function does that - takes the raw 1-4 digit string, and appends it to the command as a 4-digit string.
00656     
00657     Arguments: pointer to a buffer containing the command
00658                pointer to a buffer containing the raw (unpadded) value
00659                
00660     No return code
00661 */
00662 void GasCalibrationPageHandler::CopyBufferDigitsToCommand(char *commandBuffer, char *digitsBuffer)
00663 {
00664     int n = strlen(digitsBuffer);
00665     int commandBufferIndex = 4;
00666     int digitsBufferIndex = 0;
00667     
00668     while(n < 4) {
00669         commandBuffer[commandBufferIndex++] = '0';
00670         ++n;
00671     }
00672     
00673     while(digitsBuffer[digitsBufferIndex] != '\0') {
00674         commandBuffer[commandBufferIndex++] = digitsBuffer[digitsBufferIndex++];
00675     }
00676     
00677     commandBuffer[commandBufferIndex] = '\0';
00678 }
00679