Repository for import to local machine

Dependencies:   DMBasicGUI DMSupport

Committer:
jmitc91516
Date:
Mon Jul 31 15:37:57 2017 +0000
Revision:
8:26e49e6955bd
Parent:
1:a5258871b33d
Method ramp scrolling improved, and more bitmaps moved to QSPI memory

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmitc91516 1:a5258871b33d 1 #include "DetectorIgnitionHandler.h"
jmitc91516 1:a5258871b33d 2 #include "EasyGUITouchAreaIndices.h"
jmitc91516 1:a5258871b33d 3 #include "GetGCStatusLoop.h"
jmitc91516 1:a5258871b33d 4 #include "USBHostGCUtilities.h"
jmitc91516 1:a5258871b33d 5
jmitc91516 1:a5258871b33d 6 #include <stdio.h>
jmitc91516 1:a5258871b33d 7 #include <stdlib.h>
jmitc91516 1:a5258871b33d 8 #include <math.h>
jmitc91516 1:a5258871b33d 9 #include <time.h>
jmitc91516 1:a5258871b33d 10
jmitc91516 1:a5258871b33d 11 #define DRAW_TEXT_ON_IGNITE_BUTTON
jmitc91516 1:a5258871b33d 12 //#define PERFORM_FULL_IGNITION_SEQUENCE // not #define'd currently - we display the ignition state on the Detector page anyway
jmitc91516 1:a5258871b33d 13
jmitc91516 1:a5258871b33d 14 /*
jmitc91516 1:a5258871b33d 15 Passed three 8-bit colour components - red, green and blue.
jmitc91516 1:a5258871b33d 16 Returns the corresponding 16-bit colour value (5 bits for red, 6 bits for green, 5 bits for blue).
jmitc91516 1:a5258871b33d 17
jmitc91516 1:a5258871b33d 18 Defined in main.cpp
jmitc91516 1:a5258871b33d 19 */
jmitc91516 1:a5258871b33d 20 extern GuiConst_INTCOLOR SixteenBitColorValue(GuiConst_INT8U red, GuiConst_INT8U green, GuiConst_INT8U blue);
jmitc91516 1:a5258871b33d 21
jmitc91516 1:a5258871b33d 22
jmitc91516 1:a5258871b33d 23 /*
jmitc91516 1:a5258871b33d 24 Note that this class is a singleton - we do not need or want there to be more than one instance of it
jmitc91516 1:a5258871b33d 25 (we do not want multiple ignition attempts going on simultaneously, and nor will we show
jmitc91516 1:a5258871b33d 26 more than one easyGUI Detector page to the user at the same time).
jmitc91516 1:a5258871b33d 27 */
jmitc91516 1:a5258871b33d 28 DetectorIgnitionHandler * DetectorIgnitionHandler::theDetectorIgnitionHandlerInstance = NULL;
jmitc91516 1:a5258871b33d 29
jmitc91516 1:a5258871b33d 30 /*
jmitc91516 1:a5258871b33d 31 Tell the world whether or not we are currently trying to ignite the detector
jmitc91516 1:a5258871b33d 32 */
jmitc91516 1:a5258871b33d 33 bool DetectorIgnitionHandler::igniting = false;
jmitc91516 1:a5258871b33d 34
jmitc91516 1:a5258871b33d 35 /*
jmitc91516 1:a5258871b33d 36 Singleton class - return the one and only instance, first creating it if necessary.
jmitc91516 1:a5258871b33d 37 */
jmitc91516 1:a5258871b33d 38 DetectorIgnitionHandler * DetectorIgnitionHandler::GetInstance(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
jmitc91516 1:a5258871b33d 39 {
jmitc91516 1:a5258871b33d 40 if (theDetectorIgnitionHandlerInstance == NULL) {
jmitc91516 1:a5258871b33d 41 theDetectorIgnitionHandlerInstance = new DetectorIgnitionHandler(newUsbDevice, newUsbHostGC);
jmitc91516 1:a5258871b33d 42 }
jmitc91516 1:a5258871b33d 43
jmitc91516 1:a5258871b33d 44 return theDetectorIgnitionHandlerInstance;
jmitc91516 1:a5258871b33d 45 }
jmitc91516 1:a5258871b33d 46
jmitc91516 1:a5258871b33d 47 /*
jmitc91516 1:a5258871b33d 48 Overriden version of the above, that does not take any arguments and does not create the instance
jmitc91516 1:a5258871b33d 49 if it does not already exist.
jmitc91516 1:a5258871b33d 50
jmitc91516 1:a5258871b33d 51 Provided for callers that do not have the 'usbDevice' and'usbHostGC' pointers, and just want access
jmitc91516 1:a5258871b33d 52 to the instance if it exists
jmitc91516 1:a5258871b33d 53 */
jmitc91516 1:a5258871b33d 54 DetectorIgnitionHandler * DetectorIgnitionHandler::GetInstance(void)
jmitc91516 1:a5258871b33d 55 {
jmitc91516 1:a5258871b33d 56 return theDetectorIgnitionHandlerInstance;
jmitc91516 1:a5258871b33d 57 }
jmitc91516 1:a5258871b33d 58
jmitc91516 1:a5258871b33d 59
jmitc91516 1:a5258871b33d 60 // Singleton class - private constructor
jmitc91516 1:a5258871b33d 61 DetectorIgnitionHandler::DetectorIgnitionHandler(USBDeviceConnected* newUsbDevice, USBHostGC* newUsbHostGC)
jmitc91516 1:a5258871b33d 62 {
jmitc91516 1:a5258871b33d 63 usbDevice = newUsbDevice;
jmitc91516 1:a5258871b33d 64 usbHostGC = newUsbHostGC;
jmitc91516 1:a5258871b33d 65 }
jmitc91516 1:a5258871b33d 66
jmitc91516 1:a5258871b33d 67 // Private destructor also
jmitc91516 1:a5258871b33d 68 DetectorIgnitionHandler::~DetectorIgnitionHandler()
jmitc91516 1:a5258871b33d 69 {
jmitc91516 1:a5258871b33d 70 }
jmitc91516 1:a5258871b33d 71
jmitc91516 1:a5258871b33d 72
jmitc91516 1:a5258871b33d 73 /*
jmitc91516 1:a5258871b33d 74 Tells the caller whether or not the specified touch area is the one this class handles
jmitc91516 1:a5258871b33d 75
jmitc91516 1:a5258871b33d 76 Args: the index of the touch area in question
jmitc91516 1:a5258871b33d 77
jmitc91516 1:a5258871b33d 78 Returns true if this class handles that touch area, false if not
jmitc91516 1:a5258871b33d 79 */
jmitc91516 1:a5258871b33d 80 bool DetectorIgnitionHandler::TouchAreaIsDetectorIgniteButton(int touchAreaIndex)
jmitc91516 1:a5258871b33d 81 {
jmitc91516 1:a5258871b33d 82 return (touchAreaIndex == DETECTOR_IGNITE_BUTTON);
jmitc91516 1:a5258871b33d 83 }
jmitc91516 1:a5258871b33d 84
jmitc91516 1:a5258871b33d 85
jmitc91516 1:a5258871b33d 86 /*
jmitc91516 1:a5258871b33d 87 If the touch area is the detector ignite button, deals with it and returns true.
jmitc91516 1:a5258871b33d 88 Else returns false.
jmitc91516 1:a5258871b33d 89
jmitc91516 1:a5258871b33d 90 Args: the index of the touch area in question
jmitc91516 1:a5258871b33d 91
jmitc91516 1:a5258871b33d 92 Returns: true if it dealt with the touch area, false if not
jmitc91516 1:a5258871b33d 93 */
jmitc91516 1:a5258871b33d 94 bool DetectorIgnitionHandler::DealWithTouch(int touchAreaIndex)
jmitc91516 1:a5258871b33d 95 {
jmitc91516 1:a5258871b33d 96 if (touchAreaIndex == DETECTOR_IGNITE_BUTTON) {
jmitc91516 1:a5258871b33d 97
jmitc91516 1:a5258871b33d 98 // Ignore button press if we are already igniting
jmitc91516 1:a5258871b33d 99 if(!igniting) {
jmitc91516 1:a5258871b33d 100 PerformIgnitionSequence();
jmitc91516 1:a5258871b33d 101 }
jmitc91516 1:a5258871b33d 102
jmitc91516 1:a5258871b33d 103 return true;
jmitc91516 1:a5258871b33d 104 }
jmitc91516 1:a5258871b33d 105
jmitc91516 1:a5258871b33d 106 // 'else' ...
jmitc91516 1:a5258871b33d 107 return false;
jmitc91516 1:a5258871b33d 108 }
jmitc91516 1:a5258871b33d 109
jmitc91516 1:a5258871b33d 110
jmitc91516 1:a5258871b33d 111 /*
jmitc91516 1:a5258871b33d 112 The FID and FPD detectors require to be ignited before use. The sequence is:
jmitc91516 1:a5258871b33d 113
jmitc91516 1:a5258871b33d 114 send "CIGN" to the GC
jmitc91516 1:a5258871b33d 115
jmitc91516 1:a5258871b33d 116 then poll every 1 second (say) with "QIGN"
jmitc91516 1:a5258871b33d 117
jmitc91516 1:a5258871b33d 118 This will return "DIGN0002" while trying to ignite, then "DIGN0003" if lit or "DIGN0001" if failed to light.
jmitc91516 1:a5258871b33d 119
jmitc91516 1:a5258871b33d 120 We indicate the status by displaying text on top of the ignite button.
jmitc91516 1:a5258871b33d 121 */
jmitc91516 1:a5258871b33d 122 void DetectorIgnitionHandler::PerformIgnitionSequence(void)
jmitc91516 1:a5258871b33d 123 {
jmitc91516 1:a5258871b33d 124 if(TellGCToIgniteDetector()) {
jmitc91516 1:a5258871b33d 125
jmitc91516 1:a5258871b33d 126 #ifdef PERFORM_FULL_IGNITION_SEQUENCE
jmitc91516 1:a5258871b33d 127 igniting = true;
jmitc91516 1:a5258871b33d 128
jmitc91516 1:a5258871b33d 129 #ifdef DRAW_TEXT_ON_IGNITE_BUTTON
jmitc91516 1:a5258871b33d 130 DrawIgnitionLightingText();
jmitc91516 1:a5258871b33d 131 #endif
jmitc91516 1:a5258871b33d 132
jmitc91516 1:a5258871b33d 133 Thread::wait(1000); // 1 second
jmitc91516 1:a5258871b33d 134
jmitc91516 1:a5258871b33d 135 IgnitionState ignitionState = GetDetectorIgnitionState();
jmitc91516 1:a5258871b33d 136
jmitc91516 1:a5258871b33d 137 while(ignitionState == LIGHTING) {
jmitc91516 1:a5258871b33d 138
jmitc91516 1:a5258871b33d 139 Thread::wait(1000); // 1 second
jmitc91516 1:a5258871b33d 140
jmitc91516 1:a5258871b33d 141 ignitionState = GetDetectorIgnitionState();
jmitc91516 1:a5258871b33d 142 }
jmitc91516 1:a5258871b33d 143
jmitc91516 1:a5258871b33d 144 #ifdef DRAW_TEXT_ON_IGNITE_BUTTON
jmitc91516 1:a5258871b33d 145 if(ignitionState == LIT) {
jmitc91516 1:a5258871b33d 146 DrawIgnitionLitText();
jmitc91516 1:a5258871b33d 147 } else {
jmitc91516 1:a5258871b33d 148 // Assume not lit
jmitc91516 1:a5258871b33d 149 DrawIgnitionNotLitText();
jmitc91516 1:a5258871b33d 150 }
jmitc91516 1:a5258871b33d 151 #endif
jmitc91516 1:a5258871b33d 152
jmitc91516 1:a5258871b33d 153 igniting = false;
jmitc91516 1:a5258871b33d 154 #endif // PERFORM_FULL_IGNITION_SEQUENCE
jmitc91516 1:a5258871b33d 155
jmitc91516 1:a5258871b33d 156 }
jmitc91516 1:a5258871b33d 157 }
jmitc91516 1:a5258871b33d 158
jmitc91516 1:a5258871b33d 159
jmitc91516 1:a5258871b33d 160 /*
jmitc91516 1:a5258871b33d 161 As the name implies, sends a command to the GC and returns the response.
jmitc91516 1:a5258871b33d 162
jmitc91516 1:a5258871b33d 163 Args: pointer to a buffer containing the command, as a null-terminated string
jmitc91516 1:a5258871b33d 164 pointer to a buffer to contain the response, as a null-terminated string
jmitc91516 1:a5258871b33d 165
jmitc91516 1:a5258871b33d 166 No return code (it is up to the caller to examine the response to see whether
jmitc91516 1:a5258871b33d 167 the command succeeded or failed)
jmitc91516 1:a5258871b33d 168 */
jmitc91516 1:a5258871b33d 169 void DetectorIgnitionHandler::SendCommandToGCAndGetResponse(char* command, char* response)
jmitc91516 1:a5258871b33d 170 {
jmitc91516 1:a5258871b33d 171 #define USE_GC_UTILS // Testing new class
jmitc91516 1:a5258871b33d 172 #ifdef USE_GC_UTILS
jmitc91516 1:a5258871b33d 173 USBHostGCUtilities::SendCommandToGCAndGetResponse(usbDevice, usbHostGC, command, response);
jmitc91516 1:a5258871b33d 174 #else
jmitc91516 1:a5258871b33d 175 while(usbHostGC->ExecutingSetDeviceReport()) {}
jmitc91516 1:a5258871b33d 176
jmitc91516 1:a5258871b33d 177 usbHostGC->SetDeviceReport(usbDevice, command, response);
jmitc91516 1:a5258871b33d 178 #endif // USE_GC_UTILS
jmitc91516 1:a5258871b33d 179 }
jmitc91516 1:a5258871b33d 180
jmitc91516 1:a5258871b33d 181
jmitc91516 1:a5258871b33d 182 /*
jmitc91516 1:a5258871b33d 183 Tells the GC to ignite, i.e. sends it a "CIGN" command
jmitc91516 1:a5258871b33d 184
jmitc91516 1:a5258871b33d 185 Returns true if the GC responded with "DACK", false for anything else
jmitc91516 1:a5258871b33d 186 */
jmitc91516 1:a5258871b33d 187 bool DetectorIgnitionHandler::TellGCToIgniteDetector(void)
jmitc91516 1:a5258871b33d 188 {
jmitc91516 1:a5258871b33d 189 char response[50];
jmitc91516 1:a5258871b33d 190 SendCommandToGCAndGetResponse("CIGN", response);
jmitc91516 1:a5258871b33d 191 // We expect a response like this: "DACK" for success, "DNAK" for failure, "EPKT" for error
jmitc91516 1:a5258871b33d 192
jmitc91516 1:a5258871b33d 193 return (response[1] == 'A');
jmitc91516 1:a5258871b33d 194 }
jmitc91516 1:a5258871b33d 195
jmitc91516 1:a5258871b33d 196
jmitc91516 1:a5258871b33d 197 /*
jmitc91516 1:a5258871b33d 198 Gets the detector ignition state (not lit, igniting, lit), and returns it
jmitc91516 1:a5258871b33d 199 as a value in the IgnitionState enumeration.
jmitc91516 1:a5258871b33d 200
jmitc91516 1:a5258871b33d 201 Args: none
jmitc91516 1:a5258871b33d 202
jmitc91516 1:a5258871b33d 203 Returns: the ignition state (not lit, lighting, lit);
jmitc91516 1:a5258871b33d 204 */
jmitc91516 1:a5258871b33d 205 IgnitionState DetectorIgnitionHandler::GetDetectorIgnitionState(void)
jmitc91516 1:a5258871b33d 206 {
jmitc91516 1:a5258871b33d 207 char response[50];
jmitc91516 1:a5258871b33d 208 SendCommandToGCAndGetResponse("QIGN", response);
jmitc91516 1:a5258871b33d 209
jmitc91516 1:a5258871b33d 210 // We expect a response like this: "DIGN0001" for "not lit",
jmitc91516 1:a5258871b33d 211 // "DIGN0002" for "igniting", "DIGN0003" for "lit"
jmitc91516 1:a5258871b33d 212
jmitc91516 1:a5258871b33d 213 // Check for "EPKT" first
jmitc91516 1:a5258871b33d 214 if(response[0] == 'E') {
jmitc91516 1:a5258871b33d 215 return NOT_LIT;
jmitc91516 1:a5258871b33d 216 }
jmitc91516 1:a5258871b33d 217
jmitc91516 1:a5258871b33d 218 // 'else'...
jmitc91516 1:a5258871b33d 219 switch (response[7]) {
jmitc91516 1:a5258871b33d 220 case '2':
jmitc91516 1:a5258871b33d 221 return LIGHTING;
jmitc91516 1:a5258871b33d 222 case '3':
jmitc91516 1:a5258871b33d 223 return LIT;
jmitc91516 1:a5258871b33d 224 default:
jmitc91516 1:a5258871b33d 225 return NOT_LIT;
jmitc91516 1:a5258871b33d 226 }
jmitc91516 1:a5258871b33d 227 }
jmitc91516 1:a5258871b33d 228
jmitc91516 1:a5258871b33d 229
jmitc91516 1:a5258871b33d 230 /*
jmitc91516 1:a5258871b33d 231 Draws the specified text on top of the Ignite button - i.e. at the bottom of the display,
jmitc91516 1:a5258871b33d 232 in the centre, in the specified colour.
jmitc91516 1:a5258871b33d 233
jmitc91516 1:a5258871b33d 234 Args: boolean, true to display the text, false to erase it
jmitc91516 1:a5258871b33d 235 pointer to the (null-terminated) text
jmitc91516 1:a5258871b33d 236 the foregound (i.e. text) colour
jmitc91516 1:a5258871b33d 237 the background colour
jmitc91516 1:a5258871b33d 238
jmitc91516 1:a5258871b33d 239 No return code.
jmitc91516 1:a5258871b33d 240 */
jmitc91516 1:a5258871b33d 241 void DetectorIgnitionHandler::DrawTextOnIgniteButton(char* text, GuiConst_INTCOLOR foreColor, GuiConst_INTCOLOR backColor)
jmitc91516 1:a5258871b33d 242 {
jmitc91516 1:a5258871b33d 243 const GuiConst_INT16U fontNo = GuiFont_Helv20Bold;
jmitc91516 1:a5258871b33d 244
jmitc91516 1:a5258871b33d 245 const GuiConst_INT16S X = 400; // Centre of display
jmitc91516 1:a5258871b33d 246 const GuiConst_INT16S Y = 450; // Over the Ignite button
jmitc91516 1:a5258871b33d 247
jmitc91516 1:a5258871b33d 248 GuiLib_DrawStr(
jmitc91516 1:a5258871b33d 249 X, //GuiConst_INT16S X,
jmitc91516 1:a5258871b33d 250 Y, //GuiConst_INT16S Y,
jmitc91516 1:a5258871b33d 251 fontNo, //GuiConst_INT16U FontNo,
jmitc91516 1:a5258871b33d 252 text, //GuiConst_TEXT PrefixLocate *String,
jmitc91516 1:a5258871b33d 253 GuiLib_ALIGN_CENTER, //GuiConst_INT8U Alignment,
jmitc91516 1:a5258871b33d 254 GuiLib_PS_ON, //GuiConst_INT8U PsWriting,
jmitc91516 1:a5258871b33d 255 GuiLib_TRANSPARENT_ON, //GuiConst_INT8U Transparent,
jmitc91516 1:a5258871b33d 256 GuiLib_UNDERLINE_OFF, //GuiConst_INT8U Underlining,
jmitc91516 1:a5258871b33d 257 0, //GuiConst_INT16S BackBoxSizeX,
jmitc91516 1:a5258871b33d 258 0, //GuiConst_INT16S BackBoxSizeY1,
jmitc91516 1:a5258871b33d 259 0, //GuiConst_INT16S BackBoxSizeY2,
jmitc91516 1:a5258871b33d 260 GuiLib_BBP_NONE, //GuiConst_INT8U BackBorderPixels,
jmitc91516 1:a5258871b33d 261 foreColor, //GuiConst_INTCOLOR ForeColor,
jmitc91516 1:a5258871b33d 262 backColor //GuiConst_INTCOLOR BackColor
jmitc91516 1:a5258871b33d 263 );
jmitc91516 1:a5258871b33d 264 }
jmitc91516 1:a5258871b33d 265
jmitc91516 1:a5258871b33d 266 /*
jmitc91516 1:a5258871b33d 267 Draws "Lighting" on top of the ignite button, in yellow
jmitc91516 1:a5258871b33d 268
jmitc91516 1:a5258871b33d 269 No arguments, no return code
jmitc91516 1:a5258871b33d 270 */
jmitc91516 1:a5258871b33d 271 void DetectorIgnitionHandler::DrawIgnitionLightingText(void)
jmitc91516 1:a5258871b33d 272 {
jmitc91516 1:a5258871b33d 273 DrawTextOnIgniteButton("Lighting", SixteenBitColorValue(236, 219, 0), SixteenBitColorValue(0, 0, 0));
jmitc91516 1:a5258871b33d 274 }
jmitc91516 1:a5258871b33d 275
jmitc91516 1:a5258871b33d 276 /*
jmitc91516 1:a5258871b33d 277 Draws "Lit" on top of the ignite button, in green
jmitc91516 1:a5258871b33d 278
jmitc91516 1:a5258871b33d 279 No arguments, no return code
jmitc91516 1:a5258871b33d 280 */
jmitc91516 1:a5258871b33d 281 void DetectorIgnitionHandler::DrawIgnitionLitText(void)
jmitc91516 1:a5258871b33d 282 {
jmitc91516 1:a5258871b33d 283 DrawTextOnIgniteButton("Lit", SixteenBitColorValue(0, 236, 5), SixteenBitColorValue(0, 0, 0));
jmitc91516 1:a5258871b33d 284 }
jmitc91516 1:a5258871b33d 285
jmitc91516 1:a5258871b33d 286 /*
jmitc91516 1:a5258871b33d 287 Draws "Not Lit" on top of the ignite button, in red
jmitc91516 1:a5258871b33d 288
jmitc91516 1:a5258871b33d 289 No arguments, no return code
jmitc91516 1:a5258871b33d 290 */
jmitc91516 1:a5258871b33d 291 void DetectorIgnitionHandler::DrawIgnitionNotLitText(void)
jmitc91516 1:a5258871b33d 292 {
jmitc91516 1:a5258871b33d 293 DrawTextOnIgniteButton("Not Lit", SixteenBitColorValue(255, 0, 0), SixteenBitColorValue(0, 0, 0));
jmitc91516 1:a5258871b33d 294 }
jmitc91516 1:a5258871b33d 295