basic functional test of FT810 LCD via SPI

Dependencies:   FT810 mbed

Revision:
4:a48fc7a3bda9
Parent:
0:aa55c6eb6867
Child:
5:e2e04cb5eada
--- a/main.cpp	Thu Mar 03 16:32:08 2016 +0000
+++ b/main.cpp	Tue Mar 29 13:41:12 2016 +0000
@@ -1,40 +1,44 @@
-/* Demo for mbed Library for FTDI FT800  Enbedded Video Engine "EVE"
-Updated for FTDI FT810 driver and 800x480 display.
- * by Peter Drescher, DC2PD 2014
- * Released under the MIT License: http://mbed.org/license/mit */
-
 #include "mbed.h"
 #include "FT_Platform.h"
 #include "FT_color.h"
 #include "stdio.h"
+#include "float.h"
+#include "SDFileSystem.h"
+#include "MCP4725.h"
 
 #define SAMAPP_DELAY_BTW_APIS (1000)
 #define SAMAPP_ENABLE_DELAY() Ft_Gpu_Hal_Sleep(SAMAPP_DELAY_BTW_APIS)
 #define SAMAPP_ENABLE_DELAY_VALUE(x) Ft_Gpu_Hal_Sleep(x)
 
-#define Nucleo_F303K8
-//#define K22
-
+//#define Nucleo_F303K8
+#define K20
 
 #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
 #endif
 
-#ifdef K22
-FT800 TFT(D11,D12,D13,D10,D9,D8); // the FT800 is connected to SPI 5-7, then we have CS, INT, PD
+#ifdef K20
+FT800 TFT(D11,D12,D13,D9,D8,D14);
+SDFileSystem sd(D11, D12, D13, D10, "sd");
+MCP4725 dac1(D18, D19, MCP4725::ADDRESS_2);
+MCP4725 dac2(D18, D19, MCP4725::ADDRESS_3);
+
+AnalogIn ISEpressure_raw(A9);
+AnalogIn PFMflowrate_raw(A8);
+AnalogIn ITVpressure1_feedback_raw(A3);
+AnalogIn ITVpressure2_feedback_raw(A2);
+DigitalIn card_present(D7);
 #endif
 
-//Analog inputs and outputs are normalized
-AnalogIn Feedback(A0);  
-AnalogOut Control(A3);
+// global Vars
+unsigned int r, b, g, CalVal0, CalVal1, CalVal2, CalVal3, CalVal4;
+float ISEpressure, PFMflowrate = 0.0; 
+float ITVpressure1_feedback, ITVpressure2_feedback, ITVpressure1_user_input, ITVpressure2_user_input = 0.0;
+uint16_t ITVpressure1_input_raw, ITVpressure2_input_raw, test = 0;
+char buffer[50];
+uint8_t tcal[24]; //Touch screen Calibration
+char tstr[30]; // Temp location for Touch Screen Calibration
 
-// global Vars
-unsigned int r,b,g,PressDown, PressUp ;
-double xyz = 10.13;
-char buffer[50];
-
-// function to convert hue , saturation and  value to RGB
-// see http://en.wikipedia.org/wiki/HSL_and_HSV
 void hsv2rgb(double H,double S, double V)
 {
     double f,h,p,q,t;
@@ -104,12 +108,28 @@
     TFT.DLstart();                                         // start a new display command list
     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.DL(COLOR_RGB(0x00,0x7C,0xC4));   
+      
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));     
+    TFT.DL(BEGIN(LINES));
+    TFT.DL(LINE_WIDTH(8 * 16));
+    TFT.DL(VERTEX2F(0*16,0*16));
+    TFT.DL(VERTEX2F(799*16,0*16));
+    
+    TFT.DL(VERTEX2F(799*16,0*16));
+    TFT.DL(VERTEX2F(799*16,479*16));
+    
+    TFT.DL(VERTEX2F(799*16,479*16));
+    TFT.DL(VERTEX2F(0*16,479*16));
+    
+    TFT.DL(VERTEX2F(0*16,479*16));
+    TFT.DL(VERTEX2F(0*16,0*16));
+    
     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
-    TFT.DL(COLOR_RGB(0x00,0xFF,0x00));         // change current color
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));         // change current color
     TFT.Spinner((TFT.DispWidth/2),TFT.DispHeight/4, 0,0);  // draw a animated spinner
-
+    
     TFT.DL(DISPLAY());                         // Display the image
     TFT.Swap();                                            // Swap the current display list
     TFT.Flush_Co_Buffer();                                 // Download the command list into fifo
@@ -119,148 +139,198 @@
 }
 
 // construct the screen and download it to the LCD
-void screen_1(unsigned int color,unsigned int bright)
+void screen_1()
 {
     TFT.DLstart();                         // start a new display command list
+    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
     TFT.DL(CLEAR(1,1,1));                  // clear buffers -> color buffer,stencil buffer, tag buffer
-    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(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.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.Text(22, 208, 30, 0, "Brightness");// text Brightness
-     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.Button(250, 300,200,100,31,PressUp,"Up!"); //test button
-    TFT.DL(COLOR_RGB(0xff,0xff,0xff));     // set current color to white
+    
+    /***************************************************************************************************/
+    
+    /*Border Creation*/
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
+    TFT.DL(BEGIN(LINES));
+    TFT.DL(LINE_WIDTH(8 * 16));
+    TFT.DL(VERTEX2F(0*16,0*16));
+    TFT.DL(VERTEX2F(799*16,0*16));
+    TFT.DL(VERTEX2F(799*16,0*16));
+    TFT.DL(VERTEX2F(799*16,479*16));
+    TFT.DL(VERTEX2F(799*16,479*16));
+    TFT.DL(VERTEX2F(0*16,479*16));
+    TFT.DL(VERTEX2F(0*16,479*16));
+    TFT.DL(VERTEX2F(0*16,0*16)); 
+    TFT.DL(END());
+    /***************************************************************************************************/
 
+    /***************************************************************************************************/
+    
+    /*ISE Screen Display*/
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
+    TFT.Text(90, 20, 31, 0, "ISE Pressure");
+    TFT.DL(BEGIN(RECTS));
+    TFT.DL(VERTEX2F(110*16,85*16));
+    TFT.DL(VERTEX2F(315*16,150*16));
+    TFT.DL(END());
+    ISEpressure = (ISEpressure_raw.read_u16() - 10440) * (1.0) / (52350 - 10440) + 0;;
+    TFT.DL(COLOR_RGB(255,255,255));
+    sprintf(buffer, "%.2f", ISEpressure);
+    //test = ISEpressure_raw.read_u16();
+    //sprintf(buffer, "%d",  test);
+    TFT.Text(135,95,31,0,buffer);
+    TFT.Text(225, 95, 31, 0, " MPa");
+    /***************************************************************************************************/
+    
+    /***************************************************************************************************/
     
-    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(VERTEX2II(183,123,0,0));
-    TFT.DL(COLOR_RGB(0xff,0xff,0x0)); 
-    TFT.DL(VERTEX2II(317,123,0,0));
-    TFT.DL(COLOR_RGB(0x0,0xff,0xff)); 
-    TFT.DL(VERTEX2II(249,11,0,0));
-    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(CLEAR_COLOR_RGB(r,b,g));        // set filling color to r,g,b
-    TFT.DL(CLEAR(1,1,1));                  // fill the area  
+    /*PFM Screen Display*/
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
+    TFT.Text(455, 20, 31, 0, "PFM Flow Rate");
+    TFT.DL(BEGIN(RECTS));
+    TFT.DL(VERTEX2F(465*16,85*16));
+    TFT.DL(VERTEX2F(720*16,150*16));
+    TFT.DL(END());
+    PFMflowrate = (PFMflowrate_raw.read_u16() - 10355) * (40.0) / (51920 - 10355) + 0;
+    TFT.DL(COLOR_RGB(255,255,255));
+    sprintf(buffer, "%.1f", PFMflowrate);
+    TFT.Text(485,95,31,0,buffer);
+    TFT.Text(575, 95, 31, 0, " L/min");
+    /***************************************************************************************************/
+    
+    /***************************************************************************************************/
+    
+    /*ITV Screen Display*/
+    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
+    TFT.Text(75, 200, 31, 0, "ITV 1 Pressure");
+    TFT.Text(455, 200, 31, 0, "ITV 2 Pressure");    
     
-    
-    
+    TFT.DL(BEGIN(RECTS));
+    TFT.DL(VERTEX2F(110*16,265*16));
+    TFT.DL(VERTEX2F(315*16,330*16));
+    TFT.DL(VERTEX2F(485*16,265*16));
+    TFT.DL(VERTEX2F(690*16,330*16));
+    TFT.DL(END());
+    TFT.DL(COLOR_RGB(255,255,255));
+    sprintf(buffer, "%.1f", ITVpressure1_feedback);
+    TFT.Text(135,275,31,0,buffer);
+    TFT.Text(225, 275, 31, 0, " PSI");
+    sprintf(buffer, "%.1f", ITVpressure2_feedback);
+    TFT.Text(515,275,31,0,buffer);
+    TFT.Text(605, 275, 31, 0, " PSI");
+    ITVpressure1_feedback = (ITVpressure1_feedback_raw.read_u16() - 9430) * (70.0) / (48430 - 9430) + 0;
+    ITVpressure2_feedback = (ITVpressure2_feedback_raw.read_u16()- 9365) * (70.0) / (48420 - 9365) + 0;
+    TFT.DL(COLOR_RGB(255,255,255));  
+    TFT.FgColor(COLOR_RGB(0,124,196));
+    TFT.BgColor(COLOR_RGB(0,124,196));
+    TFT.DL(TAG(1));                        // assign TAG value 1
+    TFT.Slider(70,375,280,30,0,ITVpressure1_user_input,2355);
+    TFT.DL(TAG(2));                        // assign TAG value 2
+    TFT.Slider(450,375,280,30,0,ITVpressure2_user_input,2387);
+    /***************************************************************************************************/
     
-
- /*   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(DISPLAY());                     // Display the image
     TFT.Swap();                            // Swap the current display list
     TFT.Flush_Co_Buffer();                 // Download the command list into fifo
     TFT.WaitCmdfifo_empty();               // Wait till coprocessor completes the operation
 }
 
-
+void error_screen_SD() {
+    TFT.DLstart();                         // start a new display command list
+    TFT.DL(CLEAR_COLOR_RGB(255,242,0));
+    TFT.DL(CLEAR(1,1,1));                  // clear buffers -> color buffer,stencil buffer, tag buffer
+    TFT.DL(COLOR_RGB(0,0,0));
+    TFT.Text((TFT.DispWidth/2), 170, 31, OPT_CENTERX, "Error!"); // draw Text with font 31
+    TFT.Text((TFT.DispWidth/2), 215, 31, OPT_CENTERX, "Insert SD Card!"); // draw Text with font 31
+    TFT.DL(DISPLAY());                     // Display the image
+    TFT.Swap();                            // Swap the current display list
+    TFT.Flush_Co_Buffer();                 // Download the command list into fifo
+    TFT.WaitCmdfifo_empty();               // Wait till coprocessor completes the operation
+    TFT.Sleep(1000);
+    
+    TFT.DLstart();                         // start a new display command list
+    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
+    TFT.DL(CLEAR(1,1,1));                  // clear buffers -> color buffer,stencil buffer, tag buffer;
+    TFT.DL(COLOR_RGB(0,0,0));
+    TFT.Text((TFT.DispWidth/2), 170, 31, OPT_CENTERX, "Error!"); // draw Text with font 31
+    TFT.Text((TFT.DispWidth/2), 215, 31, OPT_CENTERX, "Insert SD Card!"); // draw Text with font 31
+    TFT.DL(DISPLAY());                     // Display the image
+    TFT.Swap();                            // Swap the current display list
+    TFT.Flush_Co_Buffer();                 // Download the command list into fifo
+    TFT.WaitCmdfifo_empty();               // Wait till coprocessor completes the operation
+    TFT.Sleep(1000);
+}
 
 int main()
-{
-
-
-    unsigned int color = 0,bright = 0;
+{ 
+    dac1.wakeup();
+    dac2.wakeup();
+    dac1.write_u12(0);
+    dac2.write_u12(0);
     ft_uint32_t TrackRegisterVal = 0;              // touch track
+    int i;
+    
+    TFT.MemWrite(REG_ROTATE, 1);
+    TFT.Rotate(1);
 
-   
-   
-    
-    
-
-    Start_Screen("5");                // Show start screen
-    Start_Screen("4");                // Show start screen
     Start_Screen("3");                // Show start screen
     Start_Screen("2");                // Show start screen
     Start_Screen("1");                // Show start screen
-    TFT.Calibrate();                               // calibrate the touch screen
+    
+    while(card_present.read()) {
+            error_screen_SD();
+    }
+    
+    FILE *fp = fopen("/sd/TCal/TCalData.txt", "a");
+    fp= fopen("/sd/TCal/TCalData.txt", "r");
+    if(!fp) {
+        //Could not open file for read
+        TFT.Calibrate();                  // calibrate the touch screen
+        TFT.read_calibrate(tcal);         // Read in cal data from screen
+        //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 {
+        //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
+    }
 
     /* Set the tracker for the dial -  define the Area for touching */
-    TFT.Track(249, 86, 1, 1, 1);                   // Dial  - dimension 1,1 means rotary
-    TFT.Track(179, 199, 277, 48, 2);               // Slider
+    TFT.Track(70, 375, 280, 30, 1);
+    TFT.Track(450, 375, 280, 30, 2);               // Slider
     TFT.Flush_Co_Buffer();                         // Download the commands into fifo
     TFT.WaitCmdfifo_empty();                       // Wait till coprocessor completes the operation
-    screen_1(color,bright);                        // paint screen
+    screen_1();                 // paint screen
 
-    /* the demo is updating the screen in a endless loop                                    */
+    /* the demo is updating the screen in a endless loop*/
     while(1) {
         ft_uint8_t tagval = 0;
         TrackRegisterVal = TFT.Rd32(REG_TRACKER);    // check if one of the two tracking fields is touched
         tagval = TrackRegisterVal & 0xff;
         if(0 != tagval) {                            // touch -> get new values
-            if(1 == tagval) {                        // the dial touched
-                color = TrackRegisterVal>>16;        // get the new value
-            } else if(2 == tagval) {                 // the slider is touched
-                bright = TrackRegisterVal>>16;       // get new slider value
-                bright = bright * 255/65536;         // scale it down to 255
-            } else if(3 == tagval) {                 // the slider is touched
-                PressDown = OPT_FLAT;
-                if(Control <= 0)
-                {
-                    Control = 0;
-                }
-                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)
-                {
-                    Control = 1;
-                }
-                else{
-                    Control = Control + 0.01;
-                     }
-             //   bright = 255;                        // scale it down to 255
-            }
-            
-            //xyz = Control;
-           // screen_1(color,bright);                  // paint new screen
-           // TFT.Sleep(10);                           // wait 10ms for next check
+            if(1 == tagval) {
+                ITVpressure1_user_input = (TrackRegisterVal>>20) * (2356.0/4095);                      
+                ITVpressure1_input_raw = (TrackRegisterVal>>20) * (2356.0/4095) + 590;
+                dac1.write_u12(ITVpressure1_input_raw); 
+            } 
+            else if(2 == tagval) {                 // the slider is touched
+                ITVpressure2_user_input = (TrackRegisterVal>>20) * (2386.0/4095);                      
+                ITVpressure2_input_raw = (TrackRegisterVal>>20) * (2386.0/4095) + 585;
+                dac2.write_u12(ITVpressure2_input_raw);       
+            } 
         }
-        
-        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 
-        PressUp = 0;
+    
+        screen_1();                  // paint new screen
+        TFT.Sleep(10);               // wait 10ms for next time to repaint the screen
     }  // end of display loop
-}
-
-
-
+}
\ No newline at end of file