TFT ILI9341 Interfacing for mbed os 6.3

Dependencies:   UniGraphic

main.cpp

Committer:
tkurume
Date:
2021-12-24
Revision:
5:122411a28708
Parent:
4:5ed8a8039560

File content as of revision 5:122411a28708:

#include "stdio.h"
#include "mbed.h"
#include "string"
#include "Arial12x12.h"
#include "Arial24x23.h"
#include "Arial43x48_numb.h"
#include <ILI9341.h>


ILI9341 myLCD(SPI_16, 12000000, PA_7,PA_6, PA_5, PB_10, PA_8, PB_6,"myLCD");  // Spi 16bit, 12MHz, mosi, miso, sclk, cs, reset, dc
//DigitalIn LCD_SW_IN(PA_4); 
//DigitalOut LCD_SW(PA_4); 
DigitalOut      backlight(PA_4);   // Display backlight 

Timer t;

//unsigned short backgroundcolor=White;
//unsigned short foregroundcolor=Black;
unsigned short backgroundcolor=Black;
unsigned short foregroundcolor=White;

char orient=1;
int main()
{
    backlight = 1;

//    myLCD.set_contrast(26);//for uc1608
//   myLCD.set_contrast(46);//for ist3020
    myLCD.set_orientation(orient);
    int time, time2;
    printf("\n\nSystem Core Clock = %.3f MHZ\r\n",(float)SystemCoreClock/1000000);
    t.start();
//   myLCD.set_font((unsigned char*) Terminal6x8);
    // myLCD.claim(stdout);      // send stdout to the LCD display
    //myLCD.claim(stderr);      // send stderr to the LCD display
    myLCD.background(backgroundcolor);    // set background to black
    myLCD.foreground(foregroundcolor);    // set chars to white

    while(1) {
        myLCD.set_orientation((orient++)%4);
        myLCD.cls();                // clear the screen
        myLCD.locate(0,30);
        myLCD.printf("Display ID: %.8X\r\n", myLCD.tftID);
        printf("Display ID: %.8X\r\n", myLCD.tftID);
        // mem write/read test
        unsigned short readback;
        unsigned short colorstep = (0x10000/myLCD.width());
        for(unsigned short i=0; i<myLCD.width(); i++) {
            myLCD.pixel(i,0,i*colorstep); // write line
        }
        bool readerror=false;
        for(unsigned short i=0; i<myLCD.width(); i++) { // verify line
            readback = myLCD.pixelread(i,0);
            if(readback!=i*colorstep) {
                readerror=true;
                printf("pix %.4X readback %.4X\r\n", i*colorstep, readback);
            }
        }
        myLCD.locate(0,10);
        myLCD.printf("pixelread test %s\r\n", readerror ? "FAIL":"PASS");
        ThisThread::sleep_for(2s);

        myLCD.cls();
        myLCD.set_font((unsigned char*) Terminal6x8,32,127,false); //variable width disabled
        myLCD.locate(0,0);
        myLCD.printf("Display Test\r\nSome text just to see if auto carriage return works correctly");
        myLCD.set_font((unsigned char*) Terminal6x8);
        myLCD.printf("\r\nDisplay Test\r\nSome text just to see if auto carriage return works correctly");
        printf("  Display Test \r\n");
        ThisThread::sleep_for(3s);
        t.reset();
        myLCD.cls();
        time=t.elapsed_time().count();
        myLCD.locate(2,55);
        myLCD.printf("cls: %.3fms", (float)time/1000);
        printf("cls: %.3fms\r\n", (float)time/1000);
        ThisThread::sleep_for(3s);

        myLCD.cls();
        t.reset();
        // draw some graphics
        //myLCD.cls();
        myLCD.set_font((unsigned char*) Arial24x23);
        myLCD.locate(10,10);
        myLCD.printf("Test");

        myLCD.line(0,0,myLCD.width()-1,0,foregroundcolor);
        myLCD.line(0,0,0,myLCD.height()-1,foregroundcolor);
        myLCD.line(0,0,myLCD.width()-1,myLCD.height()-1,foregroundcolor);

        myLCD.rect(10,30,50,40,foregroundcolor);
        myLCD.fillrect(60,30,100,40,foregroundcolor);

        myLCD.circle(150,32,30,foregroundcolor);
        myLCD.fillcircle(140,20,10,foregroundcolor);

        double s;

        for (unsigned short i=0; i<myLCD.width(); i++) {
            s =10 * sin((long double) i / 10 );
            myLCD.pixel(i,40 + (int)s,foregroundcolor);
        }


        time=t.elapsed_time().count();
        myLCD.locate(2,55);
        myLCD.set_font((unsigned char*) Terminal6x8);
        myLCD.printf("plot: %.3fms", (float)time/1000);
        printf("plot: %.3fms\r\n", (float)time/1000);
        ThisThread::sleep_for(3s);
        myLCD.cls();
        t.reset();
        myLCD.set_font((unsigned char*) Arial43x48_numb, 46, 58, false); //only numbers, variable-width disabled
        t.reset();
        myLCD.locate(0,0);
        myLCD.printf("%d", 12345);
        time=t.elapsed_time().count();
        myLCD.locate(2,55);
        myLCD.set_font((unsigned char*) Terminal6x8);
        myLCD.printf("Big Font: %.3fms", (float)time/1000);
        printf("Big Font: %.3fms\r\n", (float)time/1000);
        ThisThread::sleep_for(3s);
        // sparse pixels test
        myLCD.cls();
        myLCD.FastWindow(true);
        t.reset();
        for(unsigned int i=0; i<20000; i++) {
            myLCD.pixel((i+(i*89)%myLCD.width()), (i+(i*61)%myLCD.height()), White);
        }
        myLCD.copy_to_lcd();
        time=t.elapsed_time().count();
        myLCD.cls();
        myLCD.FastWindow(false);
        t.reset();
        for(unsigned int i=0; i<20000; i++) {
            myLCD.pixel((i+(i*89)%myLCD.width()), (i+(i*61)%myLCD.height()), White);
        }
        myLCD.copy_to_lcd();
        time2=t.elapsed_time().count();
        myLCD.locate(2,55);
        myLCD.printf("std:%.3fms fastw:%.3fms", (float)time2/1000, (float)time/1000);
        printf("std: %.3fms fastw: %.3fms\r\n", (float)time2/1000, (float)time/1000);
        ThisThread::sleep_for(3s);
        
        // scroll test, only for TFT
        myLCD.cls();
        myLCD.set_font((unsigned char*) Arial24x23);
        myLCD.locate(2,10);
        myLCD.printf("Scrolling");
        myLCD.rect(0,0,myLCD.width()-1,myLCD.height()-1,White);
        myLCD.rect(1,1,myLCD.width()-2,myLCD.height()-2,Blue);
        myLCD.setscrollarea(0,myLCD.sizeY());
        ThisThread::sleep_for(1s);
        myLCD.scroll(1); //up 1
        ThisThread::sleep_for(1s);
        myLCD.scroll(0); //center
        ThisThread::sleep_for(1s);
        myLCD.scroll(myLCD.sizeY()-1); //down 1
        ThisThread::sleep_for(1s);
        myLCD.scroll(myLCD.sizeY()); // same as 0, center
        ThisThread::sleep_for(1s);
        myLCD.scroll(myLCD.sizeY()>>1); // half screen
        ThisThread::sleep_for(1s);
        myLCD.scrollreset(); // center
        ThisThread::sleep_for(1s);
        for(unsigned short i=1; i<=myLCD.sizeY(); i++) {
            myLCD.scroll(i);
            ThisThread::sleep_for(2ms);
        }
        ThisThread::sleep_for(2s);
        // color inversion
        for(unsigned short i=0; i<=8; i++) {
            myLCD.invert(i&1);
            ThisThread::sleep_for(200ms);
        }
        ThisThread::sleep_for(2s);
    }
}