basic functional test of FT810 LCD via SPI

Dependencies:   FT810 mbed

Revision:
3:f47fe17b34a1
Parent:
2:a4c01cf97666
--- a/main.cpp	Wed Mar 16 12:06:43 2016 +0000
+++ b/main.cpp	Fri Mar 18 16:48:19 2016 +0000
@@ -7,6 +7,7 @@
 #include "FT_Platform.h"
 #include "FT_color.h"
 #include "stdio.h"
+#include "SDFileSystem.h"
 
 
 #define SAMAPP_DELAY_BTW_APIS (1000)
@@ -14,12 +15,12 @@
 #define SAMAPP_ENABLE_DELAY_VALUE(x) Ft_Gpu_Hal_Sleep(x)
 
 //#define Nucleo_F303K8
-#define K20
+#define K20B
 
 
 #ifdef Nucleo_F303K8
 FT800 TFT(PB_5,PB_4,PB_3,PA_11,PA_8,PF_1); // the FT800 is connected to SPI 5-7, then we have CS, INT, PD
-//SDFileSystem sd(D11, D12, D13, D7, "sd"); 
+//SDFileSystem sd(D11, D12, D13, D7, "sd");
 #endif
 
 #ifdef K22
@@ -28,19 +29,28 @@
 
 
 #ifdef K20  //this is the teensy 3.1/3.2
+FT800 TFT(D11,D12,D13,D9,D8,D14); // the FT800 is connected to SPI 5-7, then we have CS, INT, PD
+SDFileSystem sd(D11, D12, D13, D10, "sd", D7); // the pinout on the mbed Cool Components workshop board
+#endif
+
+#ifdef K20B  //this is the teensy 3.1/3.2 Bench
 FT800 TFT(D11,D12,D13,D10,D9,D8); // the FT800 is connected to SPI 5-7, then we have CS, INT, PD
+SDFileSystem sd(D11, D12, D13, D6, "sd", D5); // the pinout on the mbed Cool Components workshop board
+#include "USBSerial.h"
+USBSerial  spc;          // Virtual serial port over USB
 #endif
 
 
 //Analog inputs and outputs are normalized
-AnalogIn Feedback(A0);  
+AnalogIn Feedback(A0);
 AnalogOut Control(DAC);
 
 // global Vars
 unsigned int r,b,g,PressDown, PressUp, CalVal0, CalVal1, CalVal2, CalVal3, CalVal4 ;
 double xyz = 10.13;
 char buffer[50];
-uint8_t cali[24];
+uint8_t tcal[24]; //Touch screen Calibration
+char tstr[30]; // Temp location for Touch Screen Calibration
 
 // function to convert hue , saturation and  value to RGB
 // see http://en.wikipedia.org/wiki/HSL_and_HSV
@@ -49,27 +59,27 @@
     double f,h,p,q,t;
     int i;
     if( S == 0.0) {
-        r = V * 255;  
+        r = V * 255;
         g = V * 255;
         b = V * 255;
         return;
     }
     if(H > 480.0) H = 0.0;   // check values
-    if(S > 1.0) S = 1.0; 
+    if(S > 1.0) S = 1.0;
     if(S < 0.0) S = 0.0;
     if(V > 1.0) V = 1.0;
     if(V < 0.0) V = 0.0;
-    
+
     h = H / 60.0;
     i = (int) h;
     f = h - i;
     p = V * (1.0 - S);
     q = V * (1.0 - (S * f));
     t = V * (1.0 - (S * (1.0 - f)));
- 
+
     switch(i) {
         case 0:
-            r = V * 255;  
+            r = V * 255;
             g = t * 255;
             b = p * 255;
             break;
@@ -99,7 +109,7 @@
             g = p * 255;
             b = q * 255;
             break;
-    }  
+    }
 }
 
 
@@ -114,7 +124,7 @@
     TFT.DL(CLEAR_COLOR_RGB(255,255,255));      // set the clear color to white
     TFT.DL(CLEAR(1,1,1));                      // clear buffers -> color buffer,stencil buffer, tag buffer
 
-    
+
     TFT.DL(COLOR_RGB(0x80,0x80,0x00));         // set current color
     TFT.Text((TFT.DispWidth/2), TFT.DispHeight/2, 31, OPT_CENTERX, str); // draw Text with font 31
     TFT.Text((TFT.DispWidth/2), 350, 31, OPT_CENTERX, "Brew Panel!"); // draw Text with font 31
@@ -137,62 +147,62 @@
     TFT.DL(COLOR_RGB(0xff,0xff,0xff));     // set current color to white
     sprintf(buffer, "%.2f", xyz);
     TFT.Text(500, 71, 31, 0, buffer);
-    TFT.Number(500, 120, 31,0, cali[0]);
-   
+
+
     //TFT.Number(545, 135, 31, 0, printf(buffer, "%f", xyz));
     TFT.DL(TAG(1));                        // assign TAG value 1
     //TFT.FgColor(COLOR_RGB(0xff,0,0));      // forground color red
-    TFT.Dial(249, 86, 55, 0, color);       // dial for color 
+    TFT.Dial(249, 86, 55, 0, color);       // dial for color
     TFT.DL(TAG(2));                        // assign TAG value 2
     //TFT.FgColor(COLOR_RGB(0,0xff,0));      // forground color green
-    TFT.Slider(196, 215, 236, 21, 0, bright , 255);       // slider for brightness 
-        TFT.Text(22, 84, 30, 0, "Color");      // text Color
+    TFT.Slider(196, 215, 236, 21, 0, bright , 255);       // slider for brightness
+    TFT.Text(22, 84, 30, 0, "Color");      // text Color
     TFT.Text(22, 208, 30, 0, "Brightness");// text Brightness
-     TFT.DL(TAG(3));                        // assign TAG value 2
+    TFT.DL(TAG(3));                        // assign TAG value 2
     TFT.Button(22, 300,200,100,31,PressDown,"Down!"); //test button
     TFT.GradColor(COLOR_RGB(0xff,0,0));
-     TFT.DL(TAG(4));                        // assign TAG value 2
+    TFT.DL(TAG(4));                        // assign TAG value 2
     TFT.Button(250, 300,200,100,31,PressUp,"Up!"); //test button
     TFT.DL(COLOR_RGB(0xff,0xff,0xff));     // set current color to white
 
-    
-    TFT.DL(POINT_SIZE(100));               // color point around the dial 
-    TFT.DL(BEGIN(FTPOINTS));                  
-    TFT.DL(COLOR_RGB(0x0,0x0,0xff));       // color of next point 
-    TFT.DL(VERTEX2II(183,47,0,0));         // set point x,y 
-    TFT.DL(COLOR_RGB(0xff,0x0,0x0)); 
+
+    TFT.DL(POINT_SIZE(100));               // color point around the dial
+    TFT.DL(BEGIN(FTPOINTS));
+    TFT.DL(COLOR_RGB(0x0,0x0,0xff));       // color of next point
+    TFT.DL(VERTEX2II(183,47,0,0));         // set point x,y
+    TFT.DL(COLOR_RGB(0xff,0x0,0x0));
     TFT.DL(VERTEX2II(249,162,0,0));
-    TFT.DL(COLOR_RGB(0xff,0x0,0xff)); 
+    TFT.DL(COLOR_RGB(0xff,0x0,0xff));
     TFT.DL(VERTEX2II(183,123,0,0));
-    TFT.DL(COLOR_RGB(0xff,0xff,0x0)); 
+    TFT.DL(COLOR_RGB(0xff,0xff,0x0));
     TFT.DL(VERTEX2II(317,123,0,0));
-    TFT.DL(COLOR_RGB(0x0,0xff,0xff)); 
+    TFT.DL(COLOR_RGB(0x0,0xff,0xff));
     TFT.DL(VERTEX2II(249,11,0,0));
-    TFT.DL(COLOR_RGB(0x0,0xff,0x00)); 
+    TFT.DL(COLOR_RGB(0x0,0xff,0x00));
     TFT.DL(VERTEX2II(317,50,0,0));
 
     TFT.DL(SCISSOR_XY(10,10));             // define sub area starting at 10,10
-    TFT.DL(SCISSOR_SIZE(50,50));           // size 50,50  
-    hsv2rgb(color /65536.0 * 360,1.0,bright / 255.0);  // calculate rgb color from settings 
+    TFT.DL(SCISSOR_SIZE(50,50));           // size 50,50
+    hsv2rgb(color /65536.0 * 360,1.0,bright / 255.0);  // calculate rgb color from settings
     TFT.DL(CLEAR_COLOR_RGB(r,b,g));        // set filling color to r,g,b
-    TFT.DL(CLEAR(1,1,1));                  // fill the area  
-    
-    
-    
-    
+    TFT.DL(CLEAR(1,1,1));                  // fill the area
+
+
+
+
 
- /*   TFT.DL(SCISSOR_XY(500,10));             // define sub area starting at 10,10
-    TFT.DL(SCISSOR_SIZE(200,200));           // size 200,200    
-    hsv2rgb(color /65536.0 * 360,1.0,bright / 255.0);  // calculate rgb color from settings 
-    TFT.DL(CLEAR_COLOR_RGB(r,b,g));        // set filling color to r,g,b
-    TFT.DL(CLEAR(1,1,1));                  // fill the area  
+    /*   TFT.DL(SCISSOR_XY(500,10));             // define sub area starting at 10,10
+       TFT.DL(SCISSOR_SIZE(200,200));           // size 200,200
+       hsv2rgb(color /65536.0 * 360,1.0,bright / 255.0);  // calculate rgb color from settings
+       TFT.DL(CLEAR_COLOR_RGB(r,b,g));        // set filling color to r,g,b
+       TFT.DL(CLEAR(1,1,1));                  // fill the area
 
-    TFT.DL(SCISSOR_XY(500,210));             // define sub area starting at 10,10
-    TFT.DL(SCISSOR_SIZE(200,200));           // size 200,200    
-    hsv2rgb(color /65536.0 * 360,1.0,bright / 255.0);  // calculate rgb color from settings 
-    TFT.DL(CLEAR_COLOR_RGB(r,b,g));        // set filling color to r,g,b
-    TFT.DL(CLEAR(1,1,1));                  // fill the area  
-  */  
+       TFT.DL(SCISSOR_XY(500,210));             // define sub area starting at 10,10
+       TFT.DL(SCISSOR_SIZE(200,200));           // size 200,200
+       hsv2rgb(color /65536.0 * 360,1.0,bright / 255.0);  // calculate rgb color from settings
+       TFT.DL(CLEAR_COLOR_RGB(r,b,g));        // set filling color to r,g,b
+       TFT.DL(CLEAR(1,1,1));                  // fill the area
+     */
     TFT.DL(DISPLAY());                     // Display the image
     TFT.Swap();                            // Swap the current display list
     TFT.Flush_Co_Buffer();                 // Download the command list into fifo
@@ -208,12 +218,12 @@
 
     unsigned int color = 0,bright = 0;
     ft_uint32_t TrackRegisterVal = 0;              // touch track
-    
-     // Flip Screen 180 degree so viewing angle is better
+
+    // Flip Screen 180 degree so viewing angle is better
     TFT.MemWrite(REG_ROTATE, 1);
-    TFT.Rotate(1);                   
-   
-    
+    TFT.Rotate(1);
+
+
 
     Start_Screen("5");                // Show start screen
     Start_Screen("4");                // Show start screen
@@ -221,15 +231,53 @@
     Start_Screen("2");                // Show start screen
     Start_Screen("1");                // Show start screen
 
+
+    int i;
+    
+    
+    // Open Touch Screen Calibration data
+    FILE *fp = fopen("/sd/TCal/TCalData.txt", "a");
+    fp= fopen("/sd/TCal/TCalData.txt", "r");
+    if(!fp) {
+        spc.printf("Could not open file for read\n");
+        //Could not open file for read
+
+        TFT.Calibrate();                  // calibrate the touch screen
+        spc.printf("Calibrating\n");
+        TFT.read_calibrate(tcal);         // Read in cal data from screen
+        //fclose(fp);
+        //Convert from Int to Char to save to SD card
+        for (i = 0; i < 25; i++) {
+            tstr[i] = tcal[i];
+        }
+
+        //Open file for to save calibration data
+        mkdir("/sd/TCal", 0777);
+        FILE *fp = fopen("/sd/TCal/TCalData.txt", "w");
+        fprintf(fp, tstr);
+        fclose(fp);
+    } 
+    else {
+        spc.printf("Reading in Data\n");
+        //Read in calibration Data
+        fread(tstr,24,1,fp);
+        //Convert from Char to Int
+        for (i = 0; i < 24; i++) {
+            tcal[i] = tstr[i];
+        }
+        TFT.write_calibrate(tcal);         // write cal data to screen
+
+    }
+
     //if we have calibration data then write else calibrate
     //Read from SD
     //if good{
     //TFT.write_calibrate(cali);    }
     //else
-    TFT.Calibrate();                  // calibrate the touch screen
-    TFT.read_calibrate(cali);
+    //TFT.Calibrate();                  // calibrate the touch screen
+    //TFT.read_calibrate(cali);
     //save cal to SD card
-   
+
 
     /* Set the tracker for the dial -  define the Area for touching */
     TFT.Track(249, 86, 1, 1, 1);                   // Dial  - dimension 1,1 means rotary
@@ -239,8 +287,8 @@
     TFT.RegRead(0x8001,CalVal0);                    // Read Calibration Value
     TFT.RegRead(0x8002,CalVal1);                    // Read Calibration Value
     TFT.RegRead(0x8003,CalVal2);                    // Read Calibration Value
-    
-    
+
+
     screen_1(color,bright);                        // paint screen
 
     /* the demo is updating the screen in a endless loop                                    */
@@ -256,36 +304,31 @@
                 bright = bright * 255/65536;         // scale it down to 255
             } else if(3 == tagval) {                 // the slider is touched
                 PressDown = OPT_FLAT;
-                if(Control <= 0)
-                {
+                if(Control <= 0) {
                     Control = 0;
-                }
-                else{
+                } else {
                     Control = Control - 0.01;
-                     }
-               // bright = 0;                           // scale it down to 255
                 }
-              else if(4 == tagval) {                 // the slider is touched
-              PressUp = OPT_FLAT;
-                if(Control >= 1)
-                {
+                // bright = 0;                           // scale it down to 255
+            } else if(4 == tagval) {               // the slider is touched
+                PressUp = OPT_FLAT;
+                if(Control >= 1) {
                     Control = 1;
-                }
-                else{
+                } else {
                     Control = Control + 0.01;
-                     }
-             //   bright = 255;                        // scale it down to 255
+                }
+                //   bright = 255;                        // scale it down to 255
             }
-            
+
             //xyz = Control;
-           // screen_1(color,bright);                  // paint new screen
-           // TFT.Sleep(10);                           // wait 10ms for next check
+            // screen_1(color,bright);                  // paint new screen
+            // TFT.Sleep(10);                           // wait 10ms for next check
         }
-        
+
         xyz = Feedback;
         screen_1(color,bright);                  // paint new screen
         TFT.Sleep(10);                           // wait 10ms for next time to repaint the screen
-        PressDown = 0;                           // Clear Buttons being pressed 
+        PressDown = 0;                           // Clear Buttons being pressed
         PressUp = 0;
     }  // end of display loop
 }