Rihards Balass / 4DGL-mbed-32PTU

Picaso_4DGL-32PTU_Text.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) <2016> Rihards Balass
//
// 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"

//****************************************************************************************************
void TFT_4DGL :: setFont(char mode) {   // set font size

    char command[4]= "";
    
    command[0] = SET_FONT_MSB;
    command[1] = SET_FONT_LSB;
    
    switch (mode) {
        case 1 :
            command[2] = FONT1_MSB;
            command[3] = FONT1_LSB;
            currentFont = 1;
            break;
        case 2 :
            command[2] = FONT2_MSB;
            command[3] = FONT2_LSB;
            currentFont = 2;
            break;
        case 3 :
            command[2] = FONT3_MSB;
            command[3] = FONT3_LSB;
            currentFont = 3;
            break;
    }

    writeCOMMAND(command, 4);
}

//****************************************************************************************************
void TFT_4DGL :: textBgColor(char msb, char lsb) {   // set text color
    
    char command[4]= "";
    
    command[0] = TEXT_BG_COLOR_MSB;
    command[1] = TEXT_BG_COLOR_LSB;
    
    command[2] = msb;
    command[3] = lsb;
    
    writeCOMMAND(command, 4);
}

//****************************************************************************************************
void TFT_4DGL :: putc(char c) {   // place char at current cursor position

    char command[3]= "";

    command[0] = PUT_CHAR_MSB;
    command[1] = PUT_CHAR_LSB;

    command[2] = c;

    writeCOMMAND(command, 3);
}

//****************************************************************************************************
void TFT_4DGL :: puts(char *s) {   // place string at current cursor position

    char command[1000]= "";
    
    int size = strlen(s);
    int i = 0;

    command[0] = PUT_STRING_MSB;
    command[1] = PUT_STRING_LSB;

    for (i=0; i<size; i++) command[2+i] = s[i]; 

    command[2+size] = 0;

    writeCOMMAND(command, 3 + size);
}

char TFT_4DGL :: validateMoveCursor(short line, short col) {
    
    switch (currentFont) {
        case 1 :
            if (currentOrientation == 1 || currentOrientation == 2) { // landscape or rewersed landscape
                if (line > F1LL) {
                    #if DEBUGMODE
                        pc.printf("\n\r Line must be less than %i...\n\r", F1LL);
                    #endif
                    return 0;
                }
                else if (col > F1LC) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F1LC);
                    #endif
                    return 0;
                }
                else return 1; // everything is ok move the cursor
            }
            else { // portrait or rewersed portrait
                if (line > F1PL) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F1PL);
                    #endif
                    return 0;
                }
                else if (col > F1PC) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F1PC);
                    #endif
                    return 0;
                }
                else return 1;
            }
        case 2 :
            if (currentOrientation == 1 || currentOrientation == 2) { // landscape or rewersed landscape
                if (line > F2LL) {
                    #if DEBUGMODE
                        pc.printf("\n\r Line must be less than %i...\n\r", F2LL);
                    #endif
                    return 0;
                }
                else if (col > F2LC) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F2LC);
                    #endif
                    return 0;
                }
                else return 1; // everything is ok move the cursor
            }
            else { // portrait or rewersed portrait
                if (line > F2PL) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F2PL);
                    #endif
                    return 0;
                }
                else if (col > F2PC) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F2PC);
                    #endif
                    return 0;
                }
                else return 1;
            }
        case 3 :
            if (currentOrientation == 1 || currentOrientation == 2) { // landscape or rewersed landscape
                if (line > F3LL) {
                    #if DEBUGMODE
                        pc.printf("\n\r Line must be less than %i...\n\r", F3LL);
                    #endif
                    return 0;
                }
                else if (col > F3LC) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F3LC);
                    #endif
                    return 0;
                }
                else return 1; // everything is ok move the cursor
            }
            else { // portrait or rewersed portrait
                if (line > F3PL) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F3PL);
                    #endif
                    return 0;
                }
                else if (col > F3PC) {
                    #if DEBUGMODE
                        pc.printf("\n\r Column must be less than %i...\n\r", F3PC);
                    #endif
                    return 0;
                }
                else return 1;
            }
    }
    return 0;
}

//****************************************************************************************************
char TFT_4DGL :: moveCursor(short line, short col) {
    
    char command[6]= "";
    
    command[0] = MOVE_CURSOR_MSB;
    command[1] = MOVE_CURSOR_LSB;
    
    if (validateMoveCursor(line, col) == 1) {
        command[3] = line;
        command[5] = col;
        writeCOMMAND(command, 6);
        return 1;
    }
    else return 0;
}

//****************************************************************************************************
void TFT_4DGL :: textFgColor(char msb, char lsb) {
    
    char command[4]= "";
    
    command[0] = TEXT_FG_COLOR_MSB;
    command[1] = TEXT_FG_COLOR_LSB;
    
    command[2] = msb;
    command[3] = lsb;
    
    writeCOMMAND(command, 4);
}

//****************************************************************************************************
int TFT_4DGL :: textWidth(short multiplier) {
    
    if (multiplier > 0 && multiplier <= 16) {
        char command[4]= "";
        
        command[0] = TEXT_WIDTH_MSB;
        command[1] = TEXT_WIDTH_LSB;
        
        command[2] = (multiplier >> (8*1)) & 0xff;
        command[3] = (multiplier >> (8*0)) & 0xff;
        
        writeCOMMAND(command, 4);
        return 1;
    } 
    else return 0;
}

//****************************************************************************************************
int TFT_4DGL :: textHeight(short multiplier) {
    
    if (multiplier > 0 && multiplier <= 16) {
        char command[4]= "";
        
        command[0] = TEXT_HEIGHT_MSB;
        command[1] = TEXT_HEIGHT_LSB;
        
        command[2] = (multiplier >> (8*1)) & 0xff;
        command[3] = (multiplier >> (8*0)) & 0xff;
        
        writeCOMMAND(command, 4);
        return 1;
    } 
    else return 0;
}

//****************************************************************************************************
int TFT_4DGL :: textXGap(short pCount) {
    
    if (pCount <= 32) {
        char command[4]= "";
        
        command[0] = TEXT_X_GAP_MSB;
        command[1] = TEXT_X_GAP_LSB;
        
        command[2] = (pCount >> (8*1)) & 0xff;
        command[3] = (pCount >> (8*0)) & 0xff;
        
        writeCOMMAND(command, 4);
        return 1;
    } 
    else return 0;
}

//****************************************************************************************************
int TFT_4DGL :: textYGap(short pCount) {
    
    if (pCount <= 32) {
        char command[4]= "";
        
        command[0] = TEXT_Y_GAP_MSB;
        command[1] = TEXT_Y_GAP_LSB;
        
        command[2] = (pCount >> (8*1)) & 0xff;
        command[3] = (pCount >> (8*0)) & 0xff;
        
        writeCOMMAND(command, 4);
        return 1;
    } 
    else return 0;
}
   
//****************************************************************************************************
void TFT_4DGL :: textBold(short mode) {
    
    char command[4]= "";
    command[0] = TEXT_BOLD_MSB;
    command[1] = TEXT_BOLD_LSB;
    command[2] = (mode >> (8*1)) & 0xff;
    command[3] = (mode >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 4);
} 
       
//****************************************************************************************************
void TFT_4DGL :: textInverse(short mode) {
    
    char command[4]= "";
    command[0] = TEXT_INVERSE_MSB;
    command[1] = TEXT_INVERSE_LSB;
    command[2] = (mode >> (8*1)) & 0xff;
    command[3] = (mode >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 4);
} 
       
//****************************************************************************************************
void TFT_4DGL :: textItalic(short mode) {
    
    char command[4]= "";
    command[0] = TEXT_ITALIC_MSB;
    command[1] = TEXT_ITALIC_LSB;
    command[2] = (mode >> (8*1)) & 0xff;
    command[3] = (mode >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 4);
} 
       
//****************************************************************************************************
void TFT_4DGL :: textOpacity(short mode) {
    
    char command[4]= "";
    command[0] = TEXT_OPACITY_MSB;
    command[1] = TEXT_OPACITY_LSB;
    command[2] = (mode >> (8*1)) & 0xff;
    command[3] = (mode >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 4);
} 
       
//****************************************************************************************************
// The “Text Y-gap” command is required to be at least 2 for the underline to be visible,
//****************************************************************************************************
void TFT_4DGL :: textUnderline(short mode) {
    
    char command[4]= "";
    if (mode == 1) textYGap(2); 
    command[0] = TEXT_UNDERLINE_MSB;
    command[1] = TEXT_UNDERLINE_LSB;
    command[2] = (mode >> (8*1)) & 0xff;
    command[3] = (mode >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 4);
    
} 
    
//****************************************************************************************************
// (bit 5 or) DEC 16 for BOLD
// (bit 6 or) DEC 32 for ITALIC
// (bit 7 or) DEC 64 for INVERSE
// (bit 8 or) DEC 128 for UNDERLINED
//****************************************************************************************************
void TFT_4DGL :: textAttributes(short value) {
    
    char command[4]= "";
    if (CHECK_BIT(value, 7)) textYGap(2); 
    command[0] = TEXT_ATTRIBUTES_MSB;
    command[1] = TEXT_ATTRIBUTES_LSB;
    command[2] = (value >> (8*1)) & 0xff;
    command[3] = (value >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 4);
}