LPC1768 Mini-DK board with 2.8" SPI TFT and SPI touch

Dependencies:   Mini-DK SDFileSystem mbed

Fork of LPC1768_Mini-DK by Frank Vannieuwkerke

main.cpp

Committer:
frankvnk
Date:
2014-03-23
Revision:
16:784a071dec0e
Parent:
15:5059751d2fa2

File content as of revision 16:784a071dec0e:

/*
This file contains 3 examples:

ONE   : Original example with paint demo.
TWO   : Example for displaying a bitmap read from SD card.
        Download the house.bmp bitmap file from http://mbed.org/media/uploads/frankvnk/house.bmp
        Copy this file to the root of an SD card, insert it into the Mini-DK.
THREE : Example for copying a bitmap from SD to flash memory, use it as background and
        write text without disrupting the background image.
        Download the house.bmp bitmap file from http://mbed.org/media/uploads/frankvnk/house.bmp
        Copy this file to the root of an SD card, insert it into the Mini-DK.

IMPORTANT:
If copy to flash option is not needed - enable //#define NO_FLASH_BUFFER in Mini_DK.h


UNCOMMENT THE DESIRED CODE TO TEST.

*/

// =========================================================================================================
// ================================================== ONE ==================================================
// =========================================================================================================
#include "stdio.h"
#include "mbed.h"
#include "Mini_DK.h"

extern unsigned char p1[];  // the mbed logo
#define RGB565CONVERT(red, green, blue) (uint16_t)( (( red   >> 3 ) << 11 ) | (( green >> 2 ) << 5  ) | ( blue  >> 3 ))

DigitalOut led(DK_LED1);
// TFT -> mosi, miso, sclk, cs
SPI_TFT TFT(LCD_SDI, LCD_SDO, LCD_SCK, LCD_CS,"TFT");
// ADS7843 -> mosi, miso, sclk, cs, irq, SPI_TFT
TouchScreenADS7843 TP(TP_SDI ,TP_SDO ,TP_SCK ,TP_CS ,TP_IRQ, &TFT);

int main()
{
    // Structs for storing touchpanel calibration data.
    // Can be written to external storage (eg : eeprom),
    // read back on startup and written to the touchpanel.
    // As a result, the real calibration only needs to be done once.
    Matrix matrix;
    Coordinate ScreenSample[3];

    unsigned short LCD_id;
    TFT.claim(stdout);        // send stdout to the TFT display
    // Disable stdout buffering, allows us to omit \n with printf.
    // More info at http://www.cplusplus.com/reference/cstdio/setvbuf/
    setvbuf ( stdout , NULL , _IONBF , NULL );

    TFT.background(Black);    // set background to black
    TFT.foreground(White);    // set chars to white


    // LCD demo
    // first show the 4 directions
    TFT.cls();
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.set_orientation(0);
    TFT.locate(0,0);
    printf("  Hello Mbed 0");
    TFT.set_orientation(1);
    TFT.locate(0,0);
    printf("  Hello Mbed 1");
    TFT.set_orientation(2);
    TFT.locate(0,0);
    printf("  Hello Mbed 2");
    TFT.set_orientation(3);
    TFT.locate(0,0);
    printf("  Hello Mbed 3");
    TFT.set_orientation(1);
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.locate(50,100);
    printf("TFT orientation");

    wait(2);

    // draw some graphics
    TFT.cls();
    TFT.set_orientation(1);
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.locate(120,115);
    printf("Graphic");
    TFT.line(0,0,100,200,Green);
    TFT.rect(100,50,50,50,Red);
    TFT.fillrect(180,25,40,45,Blue);
    TFT.draw_ellipse(80, 150, 33, 33, White);
    TFT.fill_ellipse(80, 50, 33, 33, White);
    wait(2);
    TFT.cls();
    TFT.draw_ellipse(160, 120, 100, 50, Yellow);
    TFT.draw_ellipse(160, 120, 100, 100, Blue);
    TFT.fill_ellipse(160, 120, 80, 40, Green);
    wait(2);

    // bigger text
    TFT.foreground(White);
    TFT.background(Blue);
    TFT.cls();
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.locate(0,0);
    printf("Different Fonts :");

    TFT.set_font((unsigned char*) Neu42x35);
    TFT.locate(0,50);
    printf("Hello");
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.locate(50,100);
    printf("Hello");
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.locate(55,150);
    printf("Hello");

    TFT.set_orientation(2);
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.locate(10,10);
    printf("Hi mbed");
    wait(2);

    // mbed logo
    TFT.set_orientation(1);
    TFT.background(Black);
    TFT.cls();
    TFT.Bitmap(90,90,172,55,p1);

    // Read LCD ID
    TFT.set_orientation(0);
    LCD_id = TFT.Read_ID();
    TFT.locate(10,10);
    printf("LCD: ILI%04X", LCD_id);
    wait(2);

    // RGB color wheel demo (cycle through all colors)
    TFT.cls();
    TFT.foreground(Yellow);    // set chars to yellow
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.locate(10,10);
    printf("RGB color wheel (2x)");

    uint8_t r = 255, g = 0,  b = 0, step = 1, i;
    for (i=0;i<2;i++)
    {
        for(;g<255;g+=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));}      // Cycle from FF0000 to FFFF00 : red to yellow
        for(;r>0;r-=step)   {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));}      // Cycle from FFFF00 to 00FF00 : yellow to green
        for(;b<255;b+=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));}      // Cycle from 00FF00 to 00FFFF : green to cyan
        for(;g>0;g-=step)   {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));}      // Cycle from 00FFFF to 0000FF : cyan to blue
        for(;r<255;r+=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));}      // Cycle from 0000FF to FF00FF : blue to purple
        for(;b>0;b-=step)   {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));}      // Cycle from FF00FF to FF0000 : purple to red
    }
    wait(2);

    // Touchpanel demo
    TP.TouchPanel_Calibrate();
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.set_orientation(0);

    TFT.cls();
    TFT.locate(0,20);
    // read calibration results
    TP.GetCalibration(&matrix, &ScreenSample[0]);
    printf("Read calibration results.\n");
    printf("matrix.An = %d\n", matrix.An);
    printf("matrix.Bn = %d\n", matrix.Bn);
    printf("matrix.Cn = %d\n", matrix.Cn);
    printf("matrix.Dn = %d\n", matrix.Dn);
    printf("matrix.En = %d\n", matrix.En);
    printf("matrix.Fn = %d\n", matrix.Fn);
    printf("matrix.Di = %d\n", matrix.Divider);
    for (i=0;i<3;i++)
        printf("sample x[%d] = %d\nsample y[%d] = %d\n", i, ScreenSample[i].x, i, ScreenSample[i].y);
    // Write calibration results
    printf("\nWrite calibration results...\n");
    TP.SetCalibration(&matrix, &ScreenSample[0]);
    printf("Done.\nTouch panel to read again.\n");
    while(TP._tp_irq);
    // read calibration results
    TFT.cls();
    TFT.locate(0,20);
    printf("Calibration results.\n\n");
    printf("matrix.An = %d\n", matrix.An);
    printf("matrix.Bn = %d\n", matrix.Bn);
    printf("matrix.Cn = %d\n", matrix.Cn);
    printf("matrix.Dn = %d\n", matrix.Dn);
    printf("matrix.En = %d\n", matrix.En);
    printf("matrix.Fn = %d\n", matrix.Fn);
    printf("matrix.Di = %d\n", matrix.Divider);
    for (i=0;i<3;i++)
        printf("sample x[%d] = %d\nsample y[%d] = %d\n", i, ScreenSample[i].x, i, ScreenSample[i].y);

    TFT.locate(0,0);
    printf(" X:");
    TFT.locate(70,0);
    printf(" Y:");
    while (1)
    {
        if (!TP._tp_irq)
        {
            if (TP.Read_Ads7843())
            {
                TP.getDisplayPoint() ;
                TP.TP_DrawPoint(TP.display.x,TP.display.y, Blue);
                TFT.locate(25,0);
                printf("%03d",TP.display.x);
                TFT.locate(95,0);
                printf("%03d",TP.display.y);
                // Touchscreen area is larger than LCD area.
                // We use the bottom area outside the LCD area to clear the screen (y value > 320).
                if (TP.display.y > 320)
                {
                    TFT.cls();
                    TFT.locate(0,0);
                    printf(" X:");
                    TFT.locate(70,0);
                    printf(" Y:");
                }
            }
        }
    }
}

/*
// =========================================================================================================
// ================================================== TWO ==================================================
// =========================================================================================================

#include "stdio.h"
#include "mbed.h"
#include "SDFileSystem.h"
#include "Mini_DK.h"

// SD card
SDFileSystem sd(SD_SDI, SD_SDO, SD_SCK, SD_CS, "sd");
// TFT -> mosi, miso, sclk, cs
SPI_TFT TFT(LCD_SDI, LCD_SDO, LCD_SCK, LCD_CS,"TFT");
// ADS7843 -> mosi, miso, sclk, cs, irq, SPI_TFT
TouchScreenADS7843 TP(TP_SDI ,TP_SDO ,TP_SCK ,TP_CS ,TP_IRQ, &TFT);

int main()
{
    TFT.background(Black);    // set background to black
    TFT.foreground(White);    // set chars to white

    TFT.claim(stdout);        // send stdout to the TFT display
    // Disable stdout buffering, allows us to omit \n with printf.
    // More info at http://www.cplusplus.com/reference/cstdio/setvbuf/
    setvbuf ( stdout , NULL , _IONBF , NULL );

    TFT.cls();
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.set_orientation(1);

    DISABLE_LCD_MISO;
    int dummy = TFT.Bitmap(0,0,"/sd/house.bmp");
    ENABLE_LCD_MISO;
    TFT.locate(0,0);
    printf("%02d",dummy); 
}
*/

/*
// =========================================================================================================
// ================================================= THREE =================================================
// =========================================================================================================

#include "stdio.h"
#include "mbed.h"
#include "SDFileSystem.h"
#include "Mini_DK.h"
 
// SD card
SDFileSystem sd(SD_SDI, SD_SDO, SD_SCK, SD_CS, "sd");
// TFT -> mosi, miso, sclk, cs
SPI_TFT TFT(LCD_SDI, LCD_SDO, LCD_SCK, LCD_CS,"TFT");
// ADS7843 -> mosi, miso, sclk, cs, irq, SPI_TFT
TouchScreenADS7843 TP(TP_SDI ,TP_SDO ,TP_SCK ,TP_CS ,TP_IRQ, &TFT);


Timer timer;
 
int main()
{
    DISABLE_LCD_MISO;
    TFT.background(Black);    // set background to black
    TFT.foreground(White);    // set chars to white
 
    TFT.claim(stdout);        // send stdout to the TFT display
    // Disable stdout buffering, allows us to omit \n with printf.
    // More info at http://www.cplusplus.com/reference/cstdio/setvbuf/
    setvbuf ( stdout , NULL , _IONBF , NULL );

    TFT.cls();
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.set_orientation(1);
    
    TFT.locate(0,200);
    printf("Loading from SD...");
    TFT.Bitmap(0,0,"/sd/house.bmp");
    
    TFT.locate(0,0);
    printf("Copying to flash...");
    TFT.fileToFlash("/sd/house.bmp");
    TFT.locate(0,40);
    printf("Done: CLS in 2 seconds");
    wait(2);
    TFT.backgroundImage(false);
    TFT.cls();
    wait(2);
    TFT.backgroundImage(true);
   
    
    timer.reset();
    timer.start();
    TFT.cls();
    timer.stop();
    TFT.foreground(Black);
    TFT.locate(0,200);
    printf("Wrote background in %d ms",timer.read_ms());
    TFT.foreground(Black);

    TFT.set_orientation(3);
    TFT.locate(0,200);
    printf("Wrote background in %d ms",timer.read_ms());

    wait(5);
    TFT.cls();
    TFT.set_font((unsigned char*) Neu42x35);
    TFT.locate(0,50);
    printf("Orient = 3");
    TFT.set_orientation(0);
    TFT.locate(0,50);
    printf("Orient = 0");
    wait(5);

    TFT.set_orientation(0);
    TFT.cls();
    TFT.locate(0,50);
    printf("Orient = 0");
    
    while(1);
}
*/