//
//  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"

#ifndef DEBUGMODE
#define DEBUGMODE 1
#endif

// functions
#define CHECK_BIT(var,pos) ((var) & (1<<(pos))) // to check if required bit is set

// Common WAIT value in millisecond
#define TEMPO 1

// Main Functions values
#define CLS                 0xFFCD
#define BAUDRATE            0x0026
#define ORIENTATION         0xFF9E

// text functions values
#define TEXT_BG_COLOR      0xFFE6
#define SET_FONT            0xFFE5
#define PUT_CHAR            0xFFFE
#define PUT_STRING          0x0018
#define MOVE_CURSOR         0xFFE9
#define TEXT_FG_COLOR       0xFFE7
#define TEXT_WIDTH          0xFFE4
#define TEXT_HEIGHT         0xFFE3
#define TEXT_X_GAP          0xFFE2
#define TEXT_Y_GAP          0xFFE1
#define TEXT_BOLD           0xFFDE
#define TEXT_INVERSE        0xFFDC
#define TEXT_ITALIC         0xFFDD
#define TEXT_OPACITY        0xFFDF
#define TEXT_UNDERLINE      0xFFDB
#define TEXT_ATTRIBUTES     0xFFDA

// graphics functions values
#define CHANGE_COLOR       0xFFB4
#define DRAW_CIRCLE         0xFFC3
#define CIRCLE_FILLED       0xFFC2
#define DRAW_LINE           0xFFC8
#define DRAW_RECTANGLE      0xFFC5
#define RECTANGLE_FILLED    0xFFC4
#define DRAW_POLYLINE       0x0015
#define DRAW_POLYGON        0x0013
#define POLYGON_FILLED      0x0014
#define DRAW_TRIANGLE       0xFFBF
#define TRIANGLE_FILLED     0xFFA9



// Screen answers
#define ACK     0x06
#define NACK     0x15

// Predefined Fonts
#define FONT1   0x0000
#define FONT2   0x0001
#define FONT3   0x0002

// Line and column values depending on orientation and font F1LL = font1 landscape line
#define F1LL    29
#define F1LC    44
#define F1PL    39
#define F1PC    33

#define F2LL    29
#define F2LC    39
#define F2PL    39
#define F2PC    29

#define F3LL    19
#define F3LC    39
#define F3PL    26
#define F3PC    29

// Data speed
#define BAUD_300     0x0001
#define BAUD_600     0x0002
#define BAUD_1200    0x0003
#define BAUD_2400    0x0004
#define BAUD_4800    0x0005
#define BAUD_9600    0x0006
#define BAUD_14400   0x0007
#define BAUD_19200   0x0008
#define BAUD_31250   0x0009
#define BAUD_38400   0x000A
#define BAUD_56000   0x000B
#define BAUD_57600   0x000C
#define BAUD_115200  0x000D
#define BAUD_128000  0x000E
#define BAUD_256000  0x000F
#define BAUD_300000  0x0010
#define BAUD_375000  0x0011
#define BAUD_500000  0x0012
#define BAUD_600000  0x0013

// Defined colors
#define Black       0x0000
#define Navy        0x000F
#define DGreen      0x03E0
#define DCyan       0x03EF
#define Purple      0x780F
#define Olive       0x7BE0
#define Grey        0x8410
#define Blue        0x001F
#define Green       0x07E0
#define Red         0xF800
#define Yellow      0xFFE0
#define White       0xFFFF
#define Orange      0xFD20
#define GYellow     0xAFE5
#define Pink        0xF81F
#define Violet      0x901A
#define Aqua        0x07FF
#define Cream       0xFFDE

// Screen orientation
#define LANDSCAPE   0x0000
#define LANDSCAPE_R 0x0001
#define PORTRAIT    0x0002
#define PORTRAIT_R  0x0003

// Parameters
#define ENABLE          1
#define DISABLE         0

#define landscape       1
#define landscapeRew    2
#define portrait        3
#define portraitRew     4

#define bold            16
#define italic          32
#define inverse         64
#define underline       128

#define font1           1
#define font2           2
#define font3           3


//**************************************************************************
class PICASO_4DGL {

public :

    Serial pc; // serial variable for debug information
    PICASO_4DGL(PinName tx, PinName rx, PinName rst); // LCD serial construnctor
    int resp, respLen; // received response from screen
    char response[];
    
// General Commands
    void getResponse();
    void responseBuild(int, char);
    void reset(); // reset screen
    void baudrate(long); // set baudrate
    void screenOrientation(char); // set desired orientation of a screen
    void mainDemo(); // play demo with text based functions
    void textDemo(); // play demo with text based functions
    void graphicsDemo(); // play demo with text based functions

// Graphics Commands
    void cls(); // clear screen
    void changeColor(short, short); // change one color to another
    void drawCircle(short, short, short, short); // draw a circle (x, y, radius, color)
    void drawFilledCircle(short, short, short, short); // draw a filled circle (x, y, radius, color)
    void drawLine(short, short, short, short, short); // draw a line (x1, y1, x2, y2, color)
    void drawRectangle(short, short, short, short, short); // draw a rectangle (x1, y1, x2, y2, color)
    void drawFilledRectangle(short, short, short, short, short); // draw a solid rectangle (x1, y1, x2, y2, color)
    void drawPolyline(short, short *, short *, short); // draw multiple lines (n, vx1…vxN, vy1…vyN, color)
    void drawPolygon(short, short *, short *, short); // draw a polygon (n, vx1…vxN, vy1…vyN, color)
    void drawFilledPolygon(short, short *, short *, short); // draw a solid polygon (n, vx1…vxN, vy1…vyN, color)
    void drawTriangle(short, short, short, short, short, short, short); // draw a triangle (x1, y1, x2, y2, x3, y3, color)
    void drawFilledTriangle(short, short, short, short, short, short, short); // draw a solid triangle (x1, y1, x2, y2, x3, y3, color)
    
// Texts Commands
    void setFont(char); // set desired font from embedded fonts
    char moveCursor(short, short); // move cursot to a specific location
    char validateMoveCursor(short, short); // check if requested values are not out of range, based on font and orientation
    void textBgColor(short); // set background color of next text messages
    void putc(char); // send a single character to LCD
    void puts(char *); // send a string to LCD
    void textFgColor(short); // set text color
    int textWidth(short); // set width of a text
    int textHeight(short); // set text height
    int textXGap(short); // set gap between characters in pixels (X axis)
    int textYGap(short); // set gap between characters in pixels (Y axis)
    void textBold(short); // set text to bold
    void textInverse(short); // invert text BG and FG colors
    void textItalic(short); // set text to italic
    void textOpacity(short); // set text opacity
    void textUnderline(short); // set text to underline
    void textAttributes(short); // set 4 attributes (bold, italic, inverse, underline)

// Text data
    char currentFont; // stores current used font
    char currentOrientation; // stores current orientation
    
protected :

    Serial     _cmd; // serial variable, to comunicate with screen
    DigitalOut _rst; // screen reset pin

    void freeBUFFER  (void);
    void writeBYTE   (char);
    void writeCOMMAND(char *, int); // send command to screen
};
