basic functional test of FT810 LCD via SPI

Dependencies:   FT810 mbed

Revision:
15:25cbc8345067
Parent:
14:52a03b4a0365
Child:
16:19ade236df07
--- a/main.cpp	Fri Apr 08 19:54:19 2016 +0000
+++ b/main.cpp	Wed Apr 27 11:38:56 2016 +0000
@@ -57,6 +57,18 @@
 char tstr[40];                                                                                                      // temp location for Touch Screen Calibration
 ft_int16_t x_size, y_size;                                                                                          // stores size data for loaded jpegs, not currently used
 unsigned int err[7] = {0,0,0,0,0,0,0};                                                                              // array to store returned values from Load_jpg function, possible future error handling
+/****************************************************************************************/
+/* The DeviceCal array stores the external hardware calibration data.                   */
+/* The array format is as follows:                                                      */
+/* DeviceCal[0] = 5V input from device 1, DeviceCal[1] = 1V input from device 1         */
+/* DeviceCal[2] = 20mA input from device 2, DeviceCal[3] = 4mA input from device 2      */
+/* DeviceCal[4] = 5V input from device 3, DeviceCal[5] = 1V input from device 3         */
+/* DeviceCal[6] = 20mA output from device 3, DeviceCal[7] = 4mA output from device 3    */
+/* DeviceCal[8] = 5V input from device 4, DeviceCal[9] = 1V input from device 4         */
+/* DeviceCal[10] = 20mA output from device 4, DeviceCal[11] = 4mA output from device 4  */
+/****************************************************************************************/
+uint16_t DeviceCal[12];                     // array that stores calibraton data
+unsigned char DeviceCal_buffer[50];         // buffer to store char data read back from SD card before it's converted into an integer
 
 /****************************************************************************/
 /* Serves as the bootup screen as the images are loaded from the SD card    */
@@ -143,7 +155,7 @@
     TFT.DL(VERTEX2F(300*16,265*16));
     TFT.DL(VERTEX2F(505*16,330*16));
     TFT.DL(END());
-    ITVpressure1_feedback = (ITVpressure1_feedback_raw.read_u16() - 9430) * (70.0) / (48430 - 9430) + 0;        // calibration and scaling math for the ITV feedback, 48430 = 20mA, 9430 = 4mA
+    ITVpressure1_feedback = (ITVpressure1_feedback_raw.read_u16() - DeviceCal[5]) * (70.0) / (DeviceCal[4] - DeviceCal[5]) + 0;        // calibration and scaling math for the ITV feedback
     ITV1_fcal = ITVpressure1_feedback;                                  // moving average filter
     ITV1_fcal *= avg1;
     ITV1_fcal += ITV1_pre * avg2;
@@ -180,7 +192,7 @@
     TFT.DL(VERTEX2F(505*16,330*16));
     TFT.DL(END());
 
-    ITVpressure2_feedback = (ITVpressure2_feedback_raw.read_u16()- 9365) * (70.0) / (48420 - 9365) + 0; // calibration and scaling math for the ITV feedback, 48420 = 20mA, 9365 = 4mA
+    ITVpressure2_feedback = (ITVpressure2_feedback_raw.read_u16()- DeviceCal[9]) * (70.0) / (DeviceCal[8] - DeviceCal[9]) + 0; // calibration and scaling math for the ITV feedback
     ITV2_fcal = ITVpressure2_feedback;                                  // moving average filter
     ITV2_fcal *= avg1;
     ITV2_fcal += ITV2_pre * avg2;
@@ -202,7 +214,7 @@
     TFT.DL(BEGIN(RECTS));                                               // draw rounded rectangle in SMC blue to act as background for where the feedback will be written
     TFT.DL(VERTEX2F(300*16,85*16));
     TFT.DL(VERTEX2F(505*16,150*16));
-    PFWflowrate = (PFWflowrate_raw.read_u16() - 10355) * (40.0) / (51920 - 10355) + 0; // calibration and scaling math for the PFW feedback, 51920 = 20mA, 10355 = 4mA
+    PFWflowrate = (PFWflowrate_raw.read_u16() - DeviceCal[3]) * (40.0) / (DeviceCal[2] - DeviceCal[3]) + 0; // calibration and scaling math for the PFW feedback
     PFW_fcal = PFWflowrate;                                             // moving average filter
     PFW_fcal *= avg1;
     PFW_fcal += PFW_pre * avg2;
@@ -232,7 +244,7 @@
     TFT.DL(VERTEX2F(295*16,265*16));
     TFT.DL(VERTEX2F(505*16,330*16));
     TFT.DL(END());
-    PSEpressure = ((PSEpressure_raw.read_u16() - 10440) * (1.0) / (52350 - 10440) + 0)*145.0377; // calibration and scaling math for the PFW feedback, 52350 = 20mA, 10440 = 4mA
+    PSEpressure = ((PSEpressure_raw.read_u16() - DeviceCal[1]) * (1.0) / (DeviceCal[0] - DeviceCal[1]) + 0)*145.0377; // calibration and scaling math for the PSE feedback
     PSE_fcal = PSEpressure;                                             // moving average filter
     PSE_fcal *= avg1;
     PSE_fcal += PSE_pre * avg2;
@@ -296,17 +308,17 @@
 }
 
 /****************************************************************************/
-/* Checks for an SD card and reads/writes the touchscreen calibration data  */
+/* Checks for an SD card and reads/writes the calibration data              */
 /* from the SD card                                                         */
 /****************************************************************************/
-void touchscreen_Calibration()
+void read_Calibration()
 {
-    int i = 0;                                                  // loop counter
+    int i = 0;
+    int k = -4;                                                 // loop counter
     while(card_present.read()) {                                // checks for SD card, if not present hold program until a SD card is inserted
         error_screen("Error!","Insert SD card!");               // write error screen to LCD
     }
-    FILE *fp = fopen("/sd/TCal/TCalData.txt", "a");             // open touchscreen calibration data from SD card
-    fp= fopen("/sd/TCal/TCalData.txt", "r");
+    FILE *fp= fopen("/sd/TCal/TCalData.txt", "r");
     if(!fp) {                                                   // could not open file for read so generate data then store on SD card
         TFT.Calibrate();                                        // calibrate the touch screen
         TFT.read_calibrate(tcal);                               // read in cal data from screen
@@ -315,7 +327,6 @@
         for (i = 0; i < 25; i++) {                              // save integers one at a time to the file
             fprintf(fp, "%c", tcal[i] );
         }
-        fclose(fp);                                             // close SD card file
     } else {                                                    // file found, read in data
         fread(tstr,24,1,fp);
         for (i = 0; i < 24; i++) {                              // convert from int to char
@@ -323,6 +334,15 @@
         }
         TFT.write_calibrate(tcal);                              // write cal data to screen
     }
+    fclose(fp);                                                 // close SD card file
+    
+    FILE *fp2= fopen("/sd/DeviceCalData.txt", "r");             // read DeviceCalData.txt where the external hardware calibration data where is integer is stored in 4 sets of 4 bit chars.                             
+    fread(DeviceCal_buffer,48,1,fp2);                           // read the data
+    for (i = 0; i < 12; i++) {                                  // convert from int to char
+        DeviceCal[i] = (uint16_t)((DeviceCal_buffer[4+k]<<12) | (DeviceCal_buffer[k+5]<<8) | (DeviceCal_buffer[6+k]<<4) | (DeviceCal_buffer[k+7]<<0));      // take 4 sets of 4 bit chars and rebuild it into 1 integer
+        k=k+4;      // increment loop
+    }
+    fclose(fp2);    // close file
 }
 
 /****************************************************************************/
@@ -363,11 +383,11 @@
 /****************************************************************************/
 void SupplyPressure_Check(float check1, float check2)
 {
-    while(((PSEpressure*145.0377) < (check1 - 10)) || ((PSEpressure*145.0377) < (check2 - 10))) {
+    while(((PSEpressure) < (check1 - 10)) || ((PSEpressure) < (check2 - 10))) {
         dac1.write_u12(0);                                                                  // turn off ITVs
         dac2.write_u12(0);
         error_screen("Supply pressure too low!", "");                                       // display error
-        PSEpressure = (PSEpressure_raw.read_u16() - 10440) * (1.0) / (52350 - 10440) + 0;   // check for supply pressure
+        PSEpressure = ((PSEpressure_raw.read_u16() - DeviceCal[1]) * (1.0) / (DeviceCal[0] - DeviceCal[1]) + 0)*145.0377; // calibration and scaling math for the PSE feedback
     }
     dac1.write_u12(ITVpressure1_input_raw);                                                 // restore ITV pressure
     dac2.write_u12(ITVpressure2_input_raw);
@@ -389,7 +409,7 @@
     TFT.Rotate(1);
 
     SupplyPressure_Check(0,0);                                          // check for any supply pressure
-    touchscreen_Calibration();                                          // load touchscreen calibration data
+    read_Calibration();                                                 // load touchscreen calibration data
     initialize_Images();                                                // load and initialize images
     Start_Screen("Starting...");                                        // show start screen
 
@@ -399,7 +419,7 @@
     TFT.WaitCmdfifo_empty();                                            // wait till coprocessor completes the operation
 
     while(1) {
-        Main_Screen();                  // paint new screen
+        Main_Screen();                                                  // paint new screen
         curY = TFT.Rd32(REG_TOUCH_SCREEN_XY)& 0xffff;                   // get the current touch screen position
         curX = TFT.Rd32(REG_TOUCH_SCREEN_XY)>>16;                       // if not touch the results are 0x8000 for x and y
         ft_uint8_t tagval = 0;                                          // initialize return tag value