#include "mbed.h"
#include "ILI9341.h"
#include "Touch.h"

ILI9341 TFT(SPI_16, 12000000, D11, D12, D13, D10, D9, D8, "TFT"); // Spi 16bit, 12MHz, mosi, miso, sclk, cs, reset, dc

TouchScreenADS7843 TP(D11, D12, D13, D14, D15, &TFT); // ADS7843 -> mosi, miso, sclk, cs, irq, SPI_TFT

int main()
{
    TFT.claim(stdout);
    Matrix matrix;
    Coordinate ScreenSample[3];
    TP.TouchPanel_Calibrate();
    TP.GetCalibration(&matrix, &ScreenSample[0]);
    TP.SetCalibration(&matrix, &ScreenSample[0]);
    for(;;) {

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

    }
}