Rihards Balass / 4DGL-mbed-32PTU

Picaso_4DGL-32PTU_main.cpp

Committer:
CaptainR
Date:
2016-09-12
Revision:
6:a1a85f2bc04b
Parent:
5:890ddd974624
Child:
11:3ebd2263f3e9

File content as of revision 6:a1a85f2bc04b:

//
//  Picaso_4DGL-32PTU is a class to drive 4D Systems TFT touch screens with PICASO processor
//  Tested with NUCLEO L152RE development board
//  Copyright (C) <2016> Rihards Balass <rihards.balass@gmail.com>
//
// Picaso_4DGL-32PTU 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.
//
// Picaso_4DGL-32PTU 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 can see GNU General Public License at <http://www.gnu.org/licenses/>.
//

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

DigitalOut led1(LED1);

//******************************************************************************************************
PICASO_4DGL :: PICASO_4DGL(PinName tx, PinName rx, PinName rst) : pc(USBTX, USBRX), _cmd(tx, rx), _rst(rst) { // Constructor
    
    rxBuf[RXBUFLEN] = 0; // create buffer
    index = 0;
    //trig = 0;
    _cmd.attach(this, &PICASO_4DGL::rxCallback, Serial::RxIrq); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //responseBuild(10);
    
#if DEBUGMODE
    pc.baud(115200);

    pc.printf("\n\n\n");
    pc.printf("********************\n\r");
    pc.printf("PICASO_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");
#endif
    wait_ms(3000);

#if DEBUGMODE
    pc.printf("set baudrate to 9600...\n\r");
#endif
    baudrate(9600); 
    
#if DEBUGMODE
    pc.printf("clear screen...\n\r");
#endif
    cls();              // clear screen
    wait_ms(1000);
    screenOrientation(1);
    setFont(3);
    //graphicsDemo();
    
#if DEBUGMODE
    pc.printf("Start Demo...\n\r");
    wait_ms(1000);
    //mainDemo();
    //textDemo();
    graphicsDemo();
#endif   
}

//******************************************************************************************************
void PICASO_4DGL :: rxCallback() {
    
    while(1) {
        if(_cmd.readable()) {
            rxBuf[index++] = _cmd.getc();
            break;
        }
    }
}

//******************************************************************************************************
void PICASO_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 PICASO_4DGL :: freeBUFFER(void) {       // Clear serial buffer before writing command

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

//******************************************************************************************************
void PICASO_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
}

//**************************************************************************
void PICASO_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 PICASO_4DGL :: baudrate(long speed) {  // set screen baud rate
    
    char command[4]= "";
    command[0] = (BAUDRATE >> (8*1)) & 0xff;
    command[1] = (BAUDRATE >> (8*0)) & 0xff;
    switch (speed) {
        case  300 :
            command[2] = (BAUD_300 >> (8*1)) & 0xff;
            command[3] = (BAUD_300 >> (8*0)) & 0xff;
            break;
        case  600 :
            command[2] = (BAUD_600 >> (8*1)) & 0xff;
            command[3] = (BAUD_600 >> (8*0)) & 0xff;
            break;
        case 1200 :
            command[2] = (BAUD_1200 >> (8*1)) & 0xff;
            command[3] = (BAUD_1200 >> (8*0)) & 0xff;
            break;
        case 2400 :
            command[2] = (BAUD_2400 >> (8*1)) & 0xff;
            command[3] = (BAUD_2400 >> (8*0)) & 0xff;
            break;
        case 4800 :
            command[2] = (BAUD_4800 >> (8*1)) & 0xff;
            command[3] = (BAUD_4800 >> (8*0)) & 0xff;
            break;
        case 9600 :
            command[2] = (BAUD_9600 >> (8*1)) & 0xff;
            command[3] = (BAUD_9600 >> (8*0)) & 0xff;
            break;
        case 14400 :
            command[2] = (BAUD_14400 >> (8*1)) & 0xff;
            command[3] = (BAUD_14400 >> (8*0)) & 0xff;
            break;
        case 19200 :
            command[2] = (BAUD_19200 >> (8*1)) & 0xff;
            command[3] = (BAUD_19200 >> (8*0)) & 0xff;
            break;
        case 31250 :
            command[2] = (BAUD_31250 >> (8*1)) & 0xff;
            command[3] = (BAUD_31250 >> (8*0)) & 0xff;
            break;
        case 38400 :
            command[2] = (BAUD_38400 >> (8*1)) & 0xff;
            command[3] = (BAUD_38400 >> (8*0)) & 0xff;
            break;
        case 56000 :
            command[2] = (BAUD_56000 >> (8*1)) & 0xff;
            command[3] = (BAUD_56000 >> (8*0)) & 0xff;
            break;
        case 57600 :
            command[2] = (BAUD_57600 >> (8*1)) & 0xff;
            command[3] = (BAUD_57600 >> (8*0)) & 0xff;
            break;
        case 115200 :
            command[2] = (BAUD_115200 >> (8*1)) & 0xff;
            command[3] = (BAUD_115200 >> (8*0)) & 0xff;
            break;
        case 128000 :
            command[2] = (BAUD_128000 >> (8*1)) & 0xff;
            command[3] = (BAUD_128000 >> (8*0)) & 0xff;
            break;
        case 256000 :
            command[2] = (BAUD_256000 >> (8*1)) & 0xff;
            command[3] = (BAUD_256000 >> (8*0)) & 0xff;
            break;
        case 300000 :
            command[2] = (BAUD_300000 >> (8*1)) & 0xff;
            command[3] = (BAUD_300000 >> (8*0)) & 0xff;
            break;
        case 375000 :
            command[2] = (BAUD_375000 >> (8*1)) & 0xff;
            command[3] = (BAUD_375000 >> (8*0)) & 0xff;
            break;
        case 500000 :
            command[2] = (BAUD_500000 >> (8*1)) & 0xff;
            command[3] = (BAUD_500000 >> (8*0)) & 0xff;
            break;
        case 600000 :
            command[2] = (BAUD_600000 >> (8*1)) & 0xff;
            command[3] = (BAUD_600000 >> (8*0)) & 0xff;
            break;
        default :
            command[2] = (BAUD_9600 >> (8*1)) & 0xff;
            command[3] = (BAUD_9600 >> (8*0)) & 0xff;
            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(1);
}

//**************************************************************************
void PICASO_4DGL :: getResponse(int count) {
    
    pc.printf("\n\r Wait for answer...");
    while (index < count) wait_ms(100);               // wait for screen answer
    
    pc.printf("\n\r INDEX = %i\n\r", index);
    pc.printf("\n\r Answer = ");
    for (int i = 0; i < index; i++) {
        pc.printf("%02X ", rxBuf[i]);
    }
    pc.printf("\n\r");
    index = 0;
}

//**************************************************************************
void PICASO_4DGL :: calculateOrbitResponse() {
    
    pc.printf("\n\r Wait for answer...");
    while (index < 5) wait_ms(100);               // wait for screen answer
    
    pc.printf("\n\r INDEX = %i\n\r", index);
    pc.printf("\n\r Answer = ");
    for (int i = 0; i < index; i++) {
        pc.printf("%02X ", rxBuf[i]);
    }
    pc.printf("\n\r");
    Xdest = rxBuf[1] << 8 | rxBuf[2];
    Ydest = rxBuf[3] << 8 | rxBuf[4];
    pc.printf("\n\r New coordiantes = %i x %i\n\r", Xdest, Ydest);
    index = 0;
}

//**************************************************************************
void PICASO_4DGL :: screenOrientation(char c) {   // select screen orientation
    char command[4] = "";
    command[0] = (ORIENTATION >> (8*1)) & 0xff;
    command[1] = (ORIENTATION >> (8*0)) & 0xff;
    
    switch (c) {
        case 1 :
            command[2] = (LANDSCAPE >> (8*1)) & 0xff;
            command[3] = (LANDSCAPE >> (8*0)) & 0xff;
            currentOrientation = 1;
            break;
        case 2 :
            command[2] = (LANDSCAPE_R >> (8*1)) & 0xff;
            command[3] = (LANDSCAPE_R >> (8*0)) & 0xff;
            currentOrientation = 2;
            break;
        case 3 :
            command[2] = (PORTRAIT >> (8*1)) & 0xff;
            command[3] = (PORTRAIT >> (8*0)) & 0xff;
            currentOrientation = 3;
            break;
        case 4 :
            command[2] = (PORTRAIT_R >> (8*1)) & 0xff;
            command[3] = (PORTRAIT_R >> (8*0)) & 0xff;
            currentOrientation = 4;
            break;
    }
    
    writeCOMMAND(command, 4);
    getResponse(3);
}