Rihards Balass / 4DGL-mbed-32PTU

Picaso_4DGL-32PTU_main.cpp

Committer:
CaptainR
Date:
2016-09-08
Revision:
0:a5ef6bc3c2e8
Child:
1:e2337e2653e1

File content as of revision 0:a5ef6bc3c2e8:

//
// TFT_4DGL is a class to drive 4D Systems TFT touch screens
//
// Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr>
//
// TFT_4DGL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TFT_4DGL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TFT_4DGL.  If not, see <http://www.gnu.org/licenses/>.

#include "mbed.h"
#include "Picaso_4DGL-32PTU.h"

//Serial pc(USBTX,USBRX);
DigitalOut led1(LED1), led2(LED2);
#define SHORT_WAIT 500
#define LONG_WAIT 2000

//******************************************************************************************************
TFT_4DGL :: TFT_4DGL(PinName tx, PinName rx, PinName rst) : pc(USBTX, USBRX), _cmd(tx, rx), _rst(rst) { // Constructor

responseBuild(10, '0');

#if DEBUGMODE
    pc.baud(115200);

    pc.printf("\n\n\n");
    pc.printf("********************\n\r");
    pc.printf("TFT_4DGL CONSTRUCTOR\n\r");
    pc.printf("********************\n\r");
#endif

    _rst = 1;    // put RESET pin to high to start TFT screen
    //wait_ms(10000);
    //reset();
#if DEBUGMODE
    pc.printf("Wait 3 seconds for startup...\n\r");
    wait_ms(1000);
    pc.printf("2\n\r");
    wait_ms(1000);
    pc.printf("1\n\r");
    wait_ms(1000);
#else
    wait_ms(3000);
#endif
    //autobaud();         // send autobaud command
#if DEBUGMODE
    pc.printf("set baudrate to 9600...\n\r");
#endif
    baudrate(9600);   // set the initial baudrate to 256kbps - fastest supported by uLCD-32PT
#if DEBUGMODE
    pc.printf("clear screen...\n\r");
#endif
    cls();              // clear screen
    screenOrientation(1);
    setFont(3);
    
#if DEBUGMODE
    pc.printf("Start Demo...\n\r");
    wait_ms(1000);
    //mainDemo();
    //textDemo();
    graphicsDemo();
#endif   

}

//******************************************************************************************************
void TFT_4DGL :: responseBuild(int size, char c) {
    
    response[size] = c;
    respLen = 0;
}

//******************************************************************************************************
void TFT_4DGL :: writeBYTE(char c) { // send a BYTE command to screen

    _cmd.putc(c);

#if DEBUGMODE
    pc.printf("   Char sent : 0x%02X  ",c);
    pc.putc(c);
    pc.printf(" \n\r");
#endif

}

//******************************************************************************************************
void TFT_4DGL :: freeBUFFER(void) {       // Clear serial buffer before writing command

    while (_cmd.readable()) _cmd.getc();  // clear buffer garbage
}

//******************************************************************************************************
void TFT_4DGL :: writeCOMMAND(char *command, int number) { // send several BYTES making a command and return an answer

#if DEBUGMODE
    pc.printf("\n\r");
    pc.printf("New COMMAND : 0x%02X%02X\n\r", command[0], command[1]);
#endif
    int i;
    freeBUFFER();

    for (i = 0; i < number; i++) writeBYTE(command[i]); // send command to serial port

    getResponse();
}

//**************************************************************************
void TFT_4DGL :: reset() {  // Reset Screen

    _rst = 0;               // put RESET pin to low
    wait_ms(TEMPO);         // wait a few milliseconds for command reception
    _rst = 1;               // put RESET back to high
    wait(3);                // wait 3s for screen to restart

    freeBUFFER();           // clean buffer from possible garbage
}

//**************************************************************************
void TFT_4DGL :: baudrate(long speed) {  // set screen baud rate
    
    char command[4]= "";
    command[0] = BAUDRATE_MSB;
    command[1] = BAUDRATE_LSB;
    switch (speed) {
        case  300 :
            command[2] = BAUD_300_MSB;
            command[3] = BAUD_300_LSB;
            break;
        case  600 :
            command[2] = BAUD_600_MSB;
            command[3] = BAUD_600_LSB;
            break;
        case 1200 :
            command[2] = BAUD_1200_MSB;
            command[3] = BAUD_1200_LSB;
            break;
        case 2400 :
            command[2] = BAUD_2400_MSB;
            command[3] = BAUD_2400_LSB;
            break;
        case 4800 :
            command[2] = BAUD_4800_MSB;
            command[3] = BAUD_4800_LSB;
            break;
        case 9600 :
            command[2] = BAUD_9600_MSB;
            command[3] = BAUD_9600_LSB;
            break;
        case 14400 :
            command[2] = BAUD_14400_MSB;
            command[3] = BAUD_14400_LSB;
            break;
        case 19200 :
            command[2] = BAUD_19200_MSB;
            command[3] = BAUD_19200_LSB;
            break;
        case 38400 :
            command[2] = BAUD_38400_MSB;
            command[3] = BAUD_38400_LSB;
            break;
        case 56000 :
            command[2] = BAUD_56000_MSB;
            command[3] = BAUD_56000_LSB;
            break;
        case 57600 :
            command[2] = BAUD_57600_MSB;
            command[3] = BAUD_57600_LSB;
            break;
        case 115200 :
            command[2] = BAUD_115200_MSB;
            command[3] = BAUD_115200_LSB;
            break;
        case 128000 :
            command[2] = BAUD_128000_MSB;
            command[3] = BAUD_128000_LSB;
            break;
        case 256000 :
            command[2] = BAUD_256000_MSB;
            command[3] = BAUD_256000_LSB;
            break;
        case 300000 :
            command[2] = BAUD_300000_MSB;
            command[3] = BAUD_300000_LSB;
            break;
        case 375000 :
            command[2] = BAUD_375000_MSB;
            command[3] = BAUD_375000_LSB;
            break;
        case 500000 :
            command[2] = BAUD_500000_MSB;
            command[3] = BAUD_500000_LSB;
            break;
        case 600000 :
            command[2] = BAUD_600000_MSB;
            command[3] = BAUD_600000_LSB;
            break;
        default :
            command[2] = BAUD_9600_MSB;
            command[3] = BAUD_9600_LSB;
            speed = 9600;
            break;
    }
    
#if DEBUGMODE
    pc.printf("\n\r");
    pc.printf("New COMMAND : 0x%02X%02X\n\r", command[0], command[1]);
#endif

    int i;
    freeBUFFER();
    
    //Change baudrates - as instructed by 4DGL
    if (speed == 128000) speed = 133928; 
    if (speed == 256000) speed = 282353; 
    if (speed == 300000) speed = 312500; 
    if (speed == 375000) speed = 401785; 
    if (speed == 500000) speed = 562500; 
    if (speed == 600000) speed = 703125; 
    
    for (i = 0; i <4; i++) writeBYTE(command[i]);      // send command to serial port
    _cmd.baud(speed);                                  // set mbed to same speed
    
    getResponse();
}

//**************************************************************************
void TFT_4DGL :: getResponse() {
    
    while (!_cmd.readable()) wait_ms(TEMPO);           // wait for screen answer
    
    while (_cmd.readable()) {
        resp = _cmd.getc();           // read response if any
    }
    switch (resp) {
        case ACK :
            resp = 1;
            break;
        case NACK :
            resp = -1;
            break;
        default :
            resp = 0;
            break;
    }
    pc.printf("\n\r   Answer received : %d\n\r", resp);
}

//**************************************************************************
void TFT_4DGL :: screenOrientation(char c) {   // select screen orientation
    char command[4] = "";
    command[0] = ORIENTATION_MSB;
    command[1] = ORIENTATION_LSB;
    
    switch (c) {
        case 1 :
            command[2] = LANDSCAPE_MSB;
            command[3] = LANDSCAPE_LSB;
            currentOrientation = 1;
            break;
        case 2 :
            command[2] = LANDSCAPE_R_MSB;
            command[3] = LANDSCAPE_R_LSB;
            currentOrientation = 2;
            break;
        case 3 :
            command[2] = PORTRAIT_MSB;
            command[3] = PORTRAIT_LSB;
            currentOrientation = 3;
            break;
        case 4 :
            command[2] = PORTRAIT_R_MSB;
            command[3] = PORTRAIT_R_LSB;
            currentOrientation = 4;
            break;
    }
    
    writeCOMMAND(command, 4);
}