basic functional test of FT810 LCD via SPI

Dependencies:   FT810 mbed

main.cpp

Committer:
cratliff
Date:
2016-03-30
Revision:
6:f698d8ba4cd6
Parent:
5:e2e04cb5eada
Child:
7:e525bfa17136

File content as of revision 6:f698d8ba4cd6:

/*
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
#define Program_Button

#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 ISEpressure_raw(A9);
AnalogIn PFMflowrate_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;
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[40]; // Temp location for Touch Screen Calibration

void hsv2rgb(double H,double S, double V)
{
    double f,h,p,q,t;
    int i;
    if( S == 0.0) {
        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 < 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;
            g = t * 255;
            b = p * 255;
            break;
        case 1:
            r = q * 255;
            g = V * 255;
            b = p * 255;
            break;
        case 2:
            r = p * 255;
            g = V * 255;
            b = t * 255;
            break;
        case 3:
            r = p * 255;
            g = q * 255;
            b = V * 255;
            break;
        case 4:
            r = t * 255;
            g = p * 255;
            b = V * 255;
            break;
        case 5:
        default:
            r = V * 255;
            g = p * 255;
            b = q * 255;
            break;
    }
}


/***************************************************************************/
/* 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

    /***************************************************************************************************/

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

    /***************************************************************************************************/

    /*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);
    /***************************************************************************************************/
// add button to make for easy reprogramming
#ifdef Program_Button
    TFT.DL(TAG(200));                        // assign TAG value 200
    TFT.Button(720, 1, 76, 22, 26, 0, "Reprogram");
#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);
}

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

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

    Start_Screen("3");                // Show start screen
    Start_Screen("2");                // Show start screen
    Start_Screen("1");                // Show start 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
        /* 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
    }

    /* Set the tracker for the dial -  define the Area for touching */
    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();                 // paint screen

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

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