Rihards Balass / 4DGL-mbed-32PTU

Picaso_4DGL-32PTU.h

Committer:
CaptainR
Date:
2016-09-14
Revision:
12:29f5ad896382
Parent:
11:3ebd2263f3e9
Child:
13:1a0800957412

File content as of revision 12:29f5ad896382:

//
//  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

#ifndef DEMO
#define DEMO 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
#define MAX_WAIT    10

// Main commands
#define BAUDRATE            0x0026
#define ORIENTATION         0xFF9E

// text commands
#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 commands
#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
#define CALCULATE_ORBIT     0x0012
#define PUT_PIXEL           0xFFC1
#define MOVE_ORIGIN         0xFFCC
#define LINE_TO             0xFFCA
#define SET_CLIP_WINDOW     0xFFB5
#define CLIPPING            0xFFA2
#define EXTEND_CLIP         0xFFB3
#define DRAW_ELIPSE         0xFFB2
#define ELIPSE_FILLED       0xFFB1
#define DRAW_BUTTON         0x0011
#define BEVEL_SHADOW        0xFF98
#define BEVEL_WIDTH         0xFF99
#define DRAW_PANEL          0xFFAF
#define DRAW_SLIDER         0xFFAE
#define SCREEN_CP           0xFFAD
#define BG_COLOR            0xFFA4
#define OUTLINE_COLOR       0xFF9D
#define CONTRAST            0xFF9C
#define FRAME_DELAY         0xFF9F
#define LINE_PATERN         0xFF9B
#define TRANSPARENCY        0xFFA0
#define TRANSPARENT_COLOR   0xFFA1
#define SET_GRAPHICS        0xFFCE
#define GET_GRAPHICS        0xFFA6
#define CLS                 0xFFCD

// media commands
#define MEDIA_INIT          0xFF89
#define MEDIA_SET_ADD       0xFF93
#define MEDIA_SET_SECTOR    0xFF92
#define MEDIA_READ_SECTOR   0x0016
#define MEDIA_WRITE_SECTOR  0x0017


// 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
// orientation
#define landscape       1
#define landscapeRew    2
#define portrait        3
#define portraitRew     4
// text attributes
#define bold            16
#define italic          32
#define inverse         64
#define underline       128
// Fonts
#define font1           1
#define font2           2
#define font3           3
// Receive buffer length
#define RXBUFLEN        525
#define BUFFER_SIZE     512
// 3D states
#define DEPRESSED   0
#define RECESSED    0
#define INDENTED    0
#define RAISED      1
#define HIDDEN      2
// set graphic parameters
#define objColor        18
#define scrResolution   32
#define pageDisplay     33
#define pageRead        34
#define pageWrite       35
// get graphic parameters
#define currentXMax     0
#define currentYMax     1
#define lastObjLeft     2
#define lastObjTop      3
#define lastObjRight    4
#define lastObjBot      5
// Screen resolution
#define res320x240      0
#define res640x480      1
#define res800x480      2

//**************************************************************************
class PICASO_4DGL {

public :

    Serial pc; // serial variable for debug information
    PICASO_4DGL(PinName tx, PinName rx, PinName rst); // LCD serial construnctor
    int index; // received response from screen
    short Xdest, Ydest;
    char buffer[];
    char rxBuf[];
    char currentFont; // stores current used font
    char currentMode; // stores current orientation
    
// General Commands
    void rxCallback();
    bool getResponse(int); // wait for LCD response
    bool calculateOrbitResponse(); // wait for LCD response
    short getGraphicsResponse(); // wait for LCD response
    short mediaInitResponse(); // response from mediaInit command
    bool readSectorResponse(int); // response from mediaInit command
    void reset(); // reset screen
    void baudrate(long); // set baudrate
    void mainDemo(); // play demo with main functions
    void textDemo(); // play demo with text based functions
    void graphicsDemo(); // play demo with graphics based functions
    void mediaDemo(); // play demo with media 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)
    void drawElipse(short, short, short, short, short); // draw elipse (x1, y1, xrad, yrad, color)
    void drawFilledElipse(short, short, short, short, short); // draw a solid elipse (x1, y1, xrad, yrad, color)
    void calculateOrbit(short, short); // calculate distant point coordinates (angle, distance)
    void putPixel(short, short, short); // draw a pixel (x, y, color)
    void moveOrigin(short, short); // change origin point (x, y)
    void lineTo(short, short); // draw a line from current origin to set point (x, y) 
    void setClipWindow(short, short, short, short); // set a clipping window
    void clipping(short); // enable / disable clipping
    void extendClipRegion(); // extend clipping region to the last text or image shown
    void drawButton(short, short, short, short, short, short, short, short, char *); // draw a 3D button (state, x, y, btnColor, txtColor, font, txtWidth, txtHeight, string)
    bool bevelShadow(short); // change the drawButton bevel shadow depth
    bool bevelWidth(short); // change the drawButton bevel width
    void drawPanel(short, short, short, short, short, short); // draw a 3D panel (state, x, y, width, height, color)
    void drawSlider(short, short, short, short, short, short, short, short); // draw a slider bar (mode, x1, y1, x2, y2, color, scale, value)
    void screenCopyPaste(short, short, short, short, short, short); // copy a portion of screen (xs, ys, xd, yd, width, height)
    void bgColor(short); // change background color
    void outlineColor(short); // change outline color
    void contrast(short); // change display contrast
    bool frameDelay(short); // set inter frame delay for media video command
    void linePatern(short); // set the line draw pattern for line drawing
    void screenMode(char); // set desired orientation of a screen
    void transparency(short); // turn on or off transparency
    void transparentColor(short); // change transparent to color
    void setGraphics(short, short); // set graphics parameters
    short getGraphics(short); // get graphics parameters
    
// Media Commands
    short media_Init(); // initialise memory card
    bool media_SetAdd(int); // set byte address
    bool media_SetSector(int); // set sector address
    bool media_ReadSector(); // read sector
    bool media_WriteSector(char *); // write sector

// 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)

    
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
};