basic functional test of FT810 LCD via SPI

Dependencies:   FT810 mbed

main.cpp

Committer:
montgojj
Date:
2016-04-01
Revision:
8:886908a6127c
Parent:
7:e525bfa17136
Child:
9:bf787d41b645

File content as of revision 8:886908a6127c:

/*
Title Block

**     Project     : CLC Brew Panel
**     Processor   : MK20
**     Version     : 1.0
**     Compiler    : mbed
**     Date/Time   : 3/27/2016
**     Abstract    : ?
**
**
**
**     Todo :
*/
#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 K20
// used add easy way to reprogram without using the phsyical button.
#define Program_Button
//#define Debug_Swipe

#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 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 PSEpressure_raw(A9);
AnalogIn PFWflowrate_raw(A8);
AnalogIn ITVpressure1_feedback_raw(A3);
AnalogIn ITVpressure2_feedback_raw(A2);
DigitalIn card_present(D7);
#endif

// global Vars
unsigned int r, b, g, CalVal0, CalVal1, CalVal2, CalVal3, CalVal4, curX, curY, pasX, pasY;
float PSEpressure, PFWflowrate = 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;
int16_t velocity, distance, touched, location, paslocation ;
char buffer[50];
uint8_t tcal[24]; //Touch screen Calibration
char tstr[40]; // Temp location for Touch Screen Calibration
ft_int16_t x_size,y_size;

unsigned int err[7];

/***************************************************************************/
/* Show a Screen with Text for 5 seconds                                   */
/* A spinner shows the delay                                               */
/***************************************************************************/

ft_void_t Start_Screen(ft_char8_t *str)
{
    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(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,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

    TFT.WaitCmdfifo_empty();                               // Wait till coprocessor completes the operation
    TFT.Sleep(1000);                                       // Wait 5 s to show
}

// construct the screen and download it to the LCD
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
    #ifdef Program_Button
        TFT.DL(TAG(200));                        // assign TAG value 200
        TFT.Button(710, 10, 76, 22, 26, 0, "Reprogram");
        TFT.DL(TAG(3));
    #endif
    /***************************************************************************************************/
    // some temp location holder for each page showing the transitions
    /*TFT.DL(VERTEX_TRANSLATE_X(location* 16+12800));
    TFT.Text(300, 200, 31, 0, "Page 2");
    TFT.DL(VERTEX_TRANSLATE_X(location* 16+25600));
    TFT.Text(300, 200, 31, 0, "Page 3");
    TFT.DL(VERTEX_TRANSLATE_X(location* 16+38400));
    TFT.Text(300, 200, 31, 0, "Page 4");
    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
    TFT.DL(VERTEX_TRANSLATE_X(location* 16));*/

    /*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());
    /***************************************************************************************************/

    /***************************************************************************************************/
    /*ITV 1 Screen Display*/
    TFT.DL(COLOR_RGB(255,255,255));
    TFT.DL(VERTEX_TRANSLATE_X(location* 16));
    
    TFT.DL(BEGIN(BITMAPS));
    TFT.DL(VERTEX2II(265,50,0,0));
    TFT.DL(VERTEX2II(75,50,1,0));
    TFT.DL(VERTEX_TRANSLATE_X((149+location)*16));
    TFT.DL(VERTEX2II(511,50,2,0));
    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16));
    TFT.DL(END());
    
    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
    TFT.Text(265, 200, 31, 0, "ITV 1 Pressure");
    TFT.DL(BEGIN(RECTS));
    TFT.DL(VERTEX2F(300*16,265*16));
    TFT.DL(VERTEX2F(505*16,330*16));
    TFT.DL(END());
    TFT.DL(COLOR_RGB(255,255,255));
    sprintf(buffer, "%.1f", ITVpressure1_feedback);
    TFT.Text(325,275,31,0,buffer);
    TFT.Text(415, 275, 31, 0, " PSI");
    ITVpressure1_feedback = (ITVpressure1_feedback_raw.read_u16() - 9430) * (70.0) / (48430 - 9430) + 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(225,375,350,30,0,ITVpressure1_user_input,2355);
    TFT.DL(TAG(3));
    /***************************************************************************************************/

    /***************************************************************************************************/
    /*ITV 2 and PF3W Screen Display*/
    TFT.DL(COLOR_RGB(255,255,255));
    TFT.DL(VERTEX_TRANSLATE_X(location* 16)+12800);
    TFT.DL(BEGIN(BITMAPS));
    //TFT.DL(VERTEX2II(265,50,0,0));
    TFT.DL(VERTEX2II(30,75,3,0));
    TFT.DL(VERTEX_TRANSLATE_X((149+location)*16)+12800);
    TFT.DL(VERTEX2II(411,75,4,0));
    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16)+12800);
    TFT.DL(END());
    
    TFT.DL(VERTEX_TRANSLATE_X(location* 16+12800));
                  // Slide
    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
    TFT.Text(265, 200, 31, 0, "ITV 2 Pressure");
    TFT.DL(BEGIN(RECTS));
    TFT.DL(VERTEX2F(300*16,265*16));
    TFT.DL(VERTEX2F(505*16,330*16));
    TFT.DL(END());
    TFT.DL(COLOR_RGB(255,255,255));
    sprintf(buffer, "%.1f", ITVpressure2_feedback);
    TFT.Text(325,275,31,0,buffer);
    TFT.Text(415, 275, 31, 0, " PSI");
    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(2));                        // assign TAG value 2
    TFT.Slider(225,375,350,30,0,ITVpressure2_user_input,2387);
    TFT.DL(TAG(3));
    
    TFT.DL(VERTEX_TRANSLATE_X(location* 16+12800));
    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
    TFT.Text(250, 20, 31, 0, "PF3W Flow Rate");
    TFT.DL(BEGIN(RECTS));
    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;
    TFT.DL(COLOR_RGB(255,255,255));
    sprintf(buffer, "%.1f", PFWflowrate);
    TFT.Text(285,95,31,0,buffer);
    TFT.Text(385, 95, 31, 0, " L/min");
    /***************************************************************************************************/

    /***************************************************************************************************/
    /*ISE Screen Display*/
    TFT.DL(COLOR_RGB(255,255,255));
    TFT.DL(VERTEX_TRANSLATE_X(location* 16)+25600);
    TFT.DL(BEGIN(BITMAPS));
    TFT.DL(TAG(3));
    TFT.DL(VERTEX2II(265,50,0,0));
    TFT.DL(VERTEX2II(75,50,5,0));
    TFT.DL(VERTEX_TRANSLATE_X((99+location)*16)+25600);
    TFT.DL(VERTEX2II(511,85,6,0));
    TFT.DL(VERTEX_TRANSLATE_X((0+location)*16)+25600);
    TFT.DL(END());
    
    TFT.DL(VERTEX_TRANSLATE_X(location* 16+25600)); //page 3
    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
    TFT.Text(280, 200, 31, 0, "PSE Pressure");
    TFT.DL(BEGIN(RECTS));
    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;;
    TFT.DL(COLOR_RGB(255,255,255));
    sprintf(buffer, "%.2f", PSEpressure);
    //test = ISEpressure_raw.read_u16();
    //sprintf(buffer, "%d",  test);
    TFT.Text(300,275,31,0,buffer);
    TFT.Text(405, 275, 31, 0, " MPa");
    /***************************************************************************************************/

    /***************************************************************************************************/
    /*PFM Screen Display*/
    /*TFT.DL(VERTEX_TRANSLATE_X(location* 16+38400)); // page 4
    TFT.DL(COLOR_RGB(0x00,0x7C,0xC4));
    TFT.Text(265, 200, 31, 0, "PFW Flow Rate");
    TFT.DL(BEGIN(RECTS));
    TFT.DL(VERTEX2F(300*16,265*16));
    TFT.DL(VERTEX2F(505*16,330*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(295,275,31,0,buffer);
    TFT.Text(385, 275, 31, 0, " L/min");*/
    /***************************************************************************************************/


    // add button to make for easy reprogramming


#ifdef Debug_Swipe
    TFT.DL(COLOR_RGB(0,0,0));
    //sprintf(buffer, "%.2f", distance);
    //TFT.Text(20, 450, 26, 0, buffer);
    TFT.Number(20, 450, 26, 0, distance);
    TFT.Number(80, 450, 26, 0, velocity);
#endif
    // Debug the touch up load
#ifdef Debug_Touch_File
    TFT.DL(COLOR_RGB(0,0,0));
    int i;
    for (i = 0; i < 24; i++) {
        TFT.Number(i*30+20, 450, 26, 0, tcal[i]);

    }
#endif

    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);
}

void touchscreen_Calibration()
{
    int i = 0;
    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
        /* no longer needed
        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");
        //Save integers one at a time to the file
        for (i = 0; i < 25; i++) {
            fprintf(fp, "%c", tcal[i] );
        }
        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
    }
}

void initialize_Images()
{
    TFT.DLstart();
    TFT.DL(CLEAR_COLOR_RGB(255,255,255));
    TFT.DL(CLEAR(1,1,1));

    TFT.DL(BITMAP_HANDLE(0));
    err[0] = TFT.Load_jpg("/sd/Logo.jpg",& x_size,& y_size, 0);
    TFT.DL(BITMAP_HANDLE(1));
    err[1] = TFT.Load_jpg("/sd/ITV_1.jpg",& x_size,& y_size, 45000);
    TFT.DL(BITMAP_HANDLE(2));
    err[2] = TFT.Load_jpg("/sd/ITV_2.jpg",& x_size,& y_size, 60000);
    TFT.DL(BITMAP_HANDLE(3));
    err[3] = TFT.Load_jpg("/sd/PF3W_1.jpg",& x_size,& y_size, 85000);
    TFT.DL(BITMAP_HANDLE(4));
    err[4] = TFT.Load_jpg("/sd/PF3W_2.jpg",& x_size,& y_size, 150000);
    TFT.DL(BITMAP_HANDLE(5));
    err[5] = TFT.Load_jpg("/sd/PSE_1.jpg",& x_size,& y_size, 230000);
    TFT.DL(BITMAP_HANDLE(6));
    err[6] = TFT.Load_jpg("/sd/PSE_2.jpg",& x_size,& y_size, 260000);

    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();
}

int main()
{
    dac1.wakeup();
    dac2.wakeup();
    dac1.write_u12(0);
    dac2.write_u12(0);
    ft_uint32_t TrackRegisterVal = 0;              // touch track

    TFT.MemWrite(REG_ROTATE, 1);
    TFT.Rotate(1);

    Start_Screen("Starting...");                // Show start screen

    while(card_present.read()) {
        error_screen_SD();
    }

    touchscreen_Calibration();

    initialize_Images();
    TFT.Track(200, 400, 375, 30, 1);
    TFT.Track(200, 400, 375, 30, 2);
    TFT.Flush_Co_Buffer();                         // Download the commands into fifo
    TFT.WaitCmdfifo_empty();  
    /* the demo is updating the screen in a endless loop*/
    while(1) {
        // Touch position
        curY = TFT.Rd32(REG_TOUCH_SCREEN_XY)& 0xffff;
        curX = TFT.Rd32(REG_TOUCH_SCREEN_XY)>>16;
        ft_uint8_t tagval = 0;
        TrackRegisterVal = TFT.Rd32(REG_TRACKER);    // check if one of the two tracking fields is touched
        tagval = TrackRegisterVal & 0xff;
        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);
        } else if (200 == tagval) {
            break;
        } else if(curX !=0x8000 && touched>0) {
            touched++;
            distance = curX - pasX;
            location = (int16_t)paslocation + distance;


        } else if(curX !=0x8000 && !touched) {
            touched++;
            pasX = curX;
            //
        } else if(curX ==0x8000 && touched) {
            velocity = distance/touched;
            paslocation = location;
            touched=0;



        } else {
            //page 4 location
            if (location < - 2300 && location > -2500) {
                if (velocity) {
                    velocity = (location + 2400)/-5;
                    location += (int16_t) velocity;
                    paslocation = location;
                } else {
                    location = -2400;
                }

            }
            //page 3 location
            else if (location < - 1500 && location > -1700) {
                if (velocity) {
                    velocity = (location + 1600)/-5;
                    location += (int16_t) velocity;
                    paslocation = location;
                } else {
                    location = -1600;
                }

            }
            //page 2
            else if (location < - 700 && location > -900) {
                if (velocity) {
                    velocity = (location + 800)/-5;
                    location += (int16_t) velocity;
                    paslocation = location;
                } else {
                    location = -800;
                }
                //page 1
            } else if (location > -100 && location < 100) {

                if (velocity) {
                    velocity = location/-5;
                    location += (int16_t) velocity;
                    paslocation = location;
                } else {
                    location = 0;
                }
                //no page 0
            } else if (location >60) {
                velocity = -10;
                location += (int16_t) velocity;
                paslocation = location;
                //no page 5
            } else if (location <-1700) {
                velocity = 10;
                location += (int16_t) velocity;
                paslocation = location;
                //between pages
            } else if (velocity) {
                location += (int16_t) velocity;
                paslocation = location;
                //acceleration between pages
                if (velocity >0) {
                    velocity+= 2;
                } else {
                    velocity-= 2;
                }
            }
        }

        screen_1();                  // paint new screen
        TFT.Sleep(10);               // wait 10ms for next time to repaint the screen
    }  // end of display loop
}