4D systems Picaso uLCD 32PTU touch display library

Picaso_4DGL-32PTU_graphics.cpp

Committer:
CaptainR
Date:
2016-09-12
Revision:
9:32eb75c01e9d
Parent:
8:b634ac9c92f8
Child:
10:b959bb206e6b

File content as of revision 9:32eb75c01e9d:

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

//**************************************************************************
// The Clear Screen command clears the screen using the current background color.
// This command brings some of the settings back to default; such as,
//  Transparency turned OFF
//  Outline color set to BLACK
//  Opacity set to OPAQUE
//  Pen set to OUTLINE
//  Line patterns set to OFF
//  Right text margin set to full width
//  Text magnifications set to 1
//  All origins set to 0:0
// The alternative to maintain settings and clear screen is 
// to draw a filled rectangle with the required background color.
//**************************************************************************
void PICASO_4DGL :: cls() {  // clear screen

    char command[2] = "";
    
    command[0] = (CLS >> (8*1)) & 0xff;
    command[1] = (CLS >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 2);
    getResponse(1);
}

//**************************************************************************
// The Change Color command changes all oldColor pixels to newColor 
// within the clipping window area.
//**************************************************************************
void PICASO_4DGL :: changeColor(short oldColor, short newColor) {
    
    char command[6] = "";
    
    command[0] = (CHANGE_COLOR >> (8*1)) & 0xff;
    command[1] = (CHANGE_COLOR >> (8*0)) & 0xff;
    command[2] = (oldColor >> (8*1)) & 0xff;
    command[3] = (oldColor >> (8*0)) & 0xff;
    command[4] = (newColor >> (8*1)) & 0xff;
    command[5] = (newColor >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 6);
    getResponse(1);
}

//**************************************************************************
// The Draw Circle command draws a circle with centre point x, y 
// with radius r using the specified color.
//**************************************************************************
void PICASO_4DGL :: drawCircle(short x, short y, short r, short color) {
    
    char command[10] = "";
    
    command[0] = (DRAW_CIRCLE >> (8*1)) & 0xff;
    command[1] = (DRAW_CIRCLE >> (8*0)) & 0xff;
    command[2] = (x >> (8*1)) & 0xff;
    command[3] = (x >> (8*0)) & 0xff;
    command[4] = (y >> (8*1)) & 0xff;
    command[5] = (y >> (8*0)) & 0xff;
    command[6] = (r >> (8*1)) & 0xff;
    command[7] = (r >> (8*0)) & 0xff;
    command[8] = (color >> (8*1)) & 0xff;
    command[9] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 10);
    getResponse(1);
}

//**************************************************************************
// The Draw Circle command draws a solid circle with centre point x1, y1
// with radius r using the specified color.
// The outline color can be specified with the “Outline Color” command.
// If “Outline Color” is set to 0, no outline is drawn.
//**************************************************************************
void PICASO_4DGL :: drawFilledCircle(short x, short y, short r, short color) {
    
    char command[10] = "";
    
    command[0] = (CIRCLE_FILLED >> (8*1)) & 0xff;
    command[1] = (CIRCLE_FILLED >> (8*0)) & 0xff;
    command[2] = (x >> (8*1)) & 0xff;
    command[3] = (x >> (8*0)) & 0xff;
    command[4] = (y >> (8*1)) & 0xff;
    command[5] = (y >> (8*0)) & 0xff;
    command[6] = (r >> (8*1)) & 0xff;
    command[7] = (r >> (8*0)) & 0xff;
    command[8] = (color >> (8*1)) & 0xff;
    command[9] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 10);
    getResponse(1);
}

//**************************************************************************
// The Draw Line command draws a line from x1, y1 to x2, y2 
// using the specified color.
//**************************************************************************
void PICASO_4DGL :: drawLine(short x1, short y1, short x2, short y2, short color) {
    
    char command[12] = "";
    
    command[0] = (DRAW_LINE >> (8*1)) & 0xff;
    command[1] = (DRAW_LINE >> (8*0)) & 0xff;
    command[2] = (x1 >> (8*1)) & 0xff;
    command[3] = (x1 >> (8*0)) & 0xff;
    command[4] = (y1 >> (8*1)) & 0xff;
    command[5] = (y1 >> (8*0)) & 0xff;
    command[6] = (x2 >> (8*1)) & 0xff;
    command[7] = (x2 >> (8*0)) & 0xff;
    command[8] = (y2 >> (8*1)) & 0xff;
    command[9] = (y2 >> (8*0)) & 0xff;
    command[10] = (color >> (8*1)) & 0xff;
    command[11] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 12);
    getResponse(1);
}

//**************************************************************************
// The Draw Rectangle command draws a rectangle from x1, y1 to x2, y2
// using the specified color. 
// The line may be tessellated with the “Line Pattern” command.
//**************************************************************************
void PICASO_4DGL :: drawRectangle(short x1, short y1, short x2, short y2, short color) {
    
    char command[12] = "";
    
    command[0] = (DRAW_RECTANGLE >> (8*1)) & 0xff;
    command[1] = (DRAW_RECTANGLE >> (8*0)) & 0xff;
    command[2] = (x1 >> (8*1)) & 0xff;
    command[3] = (x1 >> (8*0)) & 0xff;
    command[4] = (y1 >> (8*1)) & 0xff;
    command[5] = (y1 >> (8*0)) & 0xff;
    command[6] = (x2 >> (8*1)) & 0xff;
    command[7] = (x2 >> (8*0)) & 0xff;
    command[8] = (y2 >> (8*1)) & 0xff;
    command[9] = (y2 >> (8*0)) & 0xff;
    command[10] = (color >> (8*1)) & 0xff;
    command[11] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 12);
    getResponse(1);
}

//**************************************************************************
// The Draw Filled Rectangle command draws a solid rectangle from x1, y1 to x2, y2
// using the specified color. 
// The line may be tessellated with the “Line Pattern” command.
// The outline color can be specified with the “Outline Color” command.
// If “Outline Color” is set to 0, no outline is drawn.
//**************************************************************************
void PICASO_4DGL :: drawFilledRectangle(short x1, short y1, short x2, short y2, short color) {
    
    char command[12] = "";
    
    command[0] = (RECTANGLE_FILLED >> (8*1)) & 0xff;
    command[1] = (RECTANGLE_FILLED >> (8*0)) & 0xff;
    command[2] = (x1 >> (8*1)) & 0xff;
    command[3] = (x1 >> (8*0)) & 0xff;
    command[4] = (y1 >> (8*1)) & 0xff;
    command[5] = (y1 >> (8*0)) & 0xff;
    command[6] = (x2 >> (8*1)) & 0xff;
    command[7] = (x2 >> (8*0)) & 0xff;
    command[8] = (y2 >> (8*1)) & 0xff;
    command[9] = (y2 >> (8*0)) & 0xff;
    command[10] = (color >> (8*1)) & 0xff;
    command[11] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 12);
    getResponse(1);
}


//**************************************************************************
// The Draw Polyline command plots lines between points specified by a pair of arrays
// using the specified color. 
// The lines may be tessellated with the “Line Pattern” command.
// The “Draw Polyline” command can be used to create complex raster graphics
// by loading the arrays from serial input or from MEDIA with very little code requirement.
// 
// n - Specifies the number of elements in the x and y arrays specifying the vertices for the polyline.
// vx, vy - Specifies the array of elements for the x/y coordinates of the vertices.
// Vx1, vx2, …, vxN, vy1, vy2, …, vyN
//**************************************************************************
void PICASO_4DGL :: drawPolyline(short n, short *vx, short *vy, short color) {
    
    int size = 6 + (n*4);
    int i, j = 4;
    char command[size];
    for(i = 0; i < size; i++) command[i] = 0;
    
    command[0] = (DRAW_POLYLINE >> (8*1)) & 0xff;
    command[1] = (DRAW_POLYLINE >> (8*0)) & 0xff;
    command[2] = (n >> (8*1)) & 0xff;
    command[3] = (n >> (8*0)) & 0xff;
    for (i = 0; i < n; i++) {
        command[j] = (vx[i] >> (8*1)) & 0xff;
        j++;
        command[j] = (vx[i] >> (8*0)) & 0xff;
        j++;
    }
    for (i = 0; i < n; i++) {
        command[j] = (vy[i] >> (8*1)) & 0xff;
        j++;
        command[j] = (vy[i] >> (8*0)) & 0xff;
        j++;
    }
    command[j] = (color >> (8*1)) & 0xff;
    command[j+1] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, size);
    getResponse(1);
}

//**************************************************************************
// The Draw Polygon command plots lines between points specified by a pair of arrays
// using the specified color. 
// The last point is drawn back to the first point, completing the polygon.
// The lines may be tessellated with the “Line Pattern” command.
// The Draw Polygon command can be used to create complex raster graphics
// by loading the arrays from serial input or from MEDIA with very little code requirement.
// 
// n - Specifies the number of elements in the x and y arrays specifying the vertices for the polygon.
// vx, vy - Specifies the array of elements for the x/y coordinates of the vertices.
// Vx1, vx2, …, vxN, vy1, vy2, …, vyN
//**************************************************************************
void PICASO_4DGL :: drawPolygon(short n, short *vx, short *vy, short color) {
    
    int size = 6 + (n*4);
    int i, j = 4;
    char command[size];
    for(i = 0; i < size; i++) command[i] = 0;
    
    command[0] = (DRAW_POLYGON >> (8*1)) & 0xff;
    command[1] = (DRAW_POLYGON >> (8*0)) & 0xff;
    command[2] = (n >> (8*1)) & 0xff;
    command[3] = (n >> (8*0)) & 0xff;
    for (i = 0; i < n; i++) {
        command[j] = (vx[i] >> (8*1)) & 0xff;
        j++;
        command[j] = (vx[i] >> (8*0)) & 0xff;
        j++;
    }
    for (i = 0; i < n; i++) {
        command[j] = (vy[i] >> (8*1)) & 0xff;
        j++;
        command[j] = (vy[i] >> (8*0)) & 0xff;
        j++;
    }
    command[j] = (color >> (8*1)) & 0xff;
    command[j+1] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, size);
    getResponse(1);
}

//**************************************************************************
// The Draw Filled Polygon command draws a solid Polygon between specified vertices:
// x1, y1 x2, y2, .... , xn, yn using the specified color.
// The last point is drawn back to the first point, completing the polygon.
// Vertices must be a minimum of 3 and can be specified in any fashion.
// 
// n - Specifies the number of elements in the x and y arrays specifying the vertices for the polygon.
// vx, vy - Specifies the array of elements for the x/y coordinates of the vertices.
// Vx1, vx2, …, vxN, vy1, vy2, …, vyN
//**************************************************************************
void PICASO_4DGL :: drawFilledPolygon(short n, short *vx, short *vy, short color) {
    
    if (n >= 3) {
        int size = 6 + (n*4);
        int i, j = 4;
        char command[size];
        for(i = 0; i < size; i++) command[i] = 0;
        
        command[0] = (POLYGON_FILLED >> (8*1)) & 0xff;
        command[1] = (POLYGON_FILLED >> (8*0)) & 0xff;
        command[2] = (n >> (8*1)) & 0xff;
        command[3] = (n >> (8*0)) & 0xff;
        for (i = 0; i < n; i++) {
            command[j] = (vx[i] >> (8*1)) & 0xff;
            j++;
            command[j] = (vx[i] >> (8*0)) & 0xff;
            j++;
        }
        for (i = 0; i < n; i++) {
            command[j] = (vy[i] >> (8*1)) & 0xff;
            j++;
            command[j] = (vy[i] >> (8*0)) & 0xff;
            j++;
        }
        command[j] = (color >> (8*1)) & 0xff;
        command[j+1] = (color >> (8*0)) & 0xff;
        
        writeCOMMAND(command, size);
        getResponse(1);
    }
}

//**************************************************************************
// The Draw Triangle command draws a triangle outline between vertices
// x1,y1 , x2,y2 and x3,y3 using the specified color.
// The line may be tessellated with the “Line Pattern” command.
//**************************************************************************
void PICASO_4DGL :: drawTriangle(short x1, short y1, short x2, short y2, short x3, short y3, short color) {
    
    char command[16] = "";
    
    command[0] = (DRAW_TRIANGLE >> (8*1)) & 0xff;
    command[1] = (DRAW_TRIANGLE >> (8*0)) & 0xff;
    command[2] = (x1 >> (8*1)) & 0xff;
    command[3] = (x1 >> (8*0)) & 0xff;
    command[4] = (y1 >> (8*1)) & 0xff;
    command[5] = (y1 >> (8*0)) & 0xff;
    command[6] = (x2 >> (8*1)) & 0xff;
    command[7] = (x2 >> (8*0)) & 0xff;
    command[8] = (y2 >> (8*1)) & 0xff;
    command[9] = (y2 >> (8*0)) & 0xff;
    command[10] = (x3 >> (8*1)) & 0xff;
    command[11] = (x3 >> (8*0)) & 0xff;
    command[12] = (y3 >> (8*1)) & 0xff;
    command[13] = (y3 >> (8*0)) & 0xff;
    command[14] = (color >> (8*1)) & 0xff;
    command[15] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 16);
    getResponse(1);
}

//**************************************************************************
// The Draw Filled Triangle command draws a solid triangle between vertices
// x1, y1, x2, y2 and x3, y3 using the specified color.
//**************************************************************************
void PICASO_4DGL :: drawFilledTriangle(short x1, short y1, short x2, short y2, short x3, short y3, short color) {
    
    char command[16] = "";
    
    command[0] = (TRIANGLE_FILLED >> (8*1)) & 0xff;
    command[1] = (TRIANGLE_FILLED >> (8*0)) & 0xff;
    command[2] = (x1 >> (8*1)) & 0xff;
    command[3] = (x1 >> (8*0)) & 0xff;
    command[4] = (y1 >> (8*1)) & 0xff;
    command[5] = (y1 >> (8*0)) & 0xff;
    command[6] = (x2 >> (8*1)) & 0xff;
    command[7] = (x2 >> (8*0)) & 0xff;
    command[8] = (y2 >> (8*1)) & 0xff;
    command[9] = (y2 >> (8*0)) & 0xff;
    command[10] = (x3 >> (8*1)) & 0xff;
    command[11] = (x3 >> (8*0)) & 0xff;
    command[12] = (y3 >> (8*1)) & 0xff;
    command[13] = (y3 >> (8*0)) & 0xff;
    command[14] = (color >> (8*1)) & 0xff;
    command[15] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 16);
    getResponse(1);
}

//**************************************************************************
// The Calculate Orbit command calculates the x, y coordinates of a distant point relative
// to the current origin, where the only known parameters are the angle and the distance
// from the current origin. The new coordinates are calculated and then placed in the
// destination variables Xdest and Ydest.
//**************************************************************************
void PICASO_4DGL :: calculateOrbit(short angle, short distance) {
    
    char command[6] = "";
    
    command[0] = (CALCULATE_ORBIT >> (8*1)) & 0xff;
    command[1] = (CALCULATE_ORBIT >> (8*0)) & 0xff;
    command[2] = (angle >> (8*1)) & 0xff;
    command[3] = (angle >> (8*0)) & 0xff;
    command[4] = (distance >> (8*1)) & 0xff;
    command[5] = (distance >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 6);
    calculateOrbitResponse();
}

//**************************************************************************
// The Put Pixel command draws a pixel at position x, y using the specified color.
//**************************************************************************
void PICASO_4DGL :: putPixel(short x, short y, short color) {
    
    char command[8] = "";
    
    command[0] = (PUT_PIXEL >> (8*1)) & 0xff;
    command[1] = (PUT_PIXEL >> (8*0)) & 0xff;
    command[2] = (x >> (8*1)) & 0xff;
    command[3] = (x >> (8*0)) & 0xff;
    command[4] = (y >> (8*1)) & 0xff;
    command[5] = (y >> (8*0)) & 0xff;
    command[6] = (color >> (8*1)) & 0xff;
    command[7] = (color >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 8);
    getResponse(1);
}

//**************************************************************************
// The Move Origin command moves the origin to a new position, which is suitable for
// specifying the location for both graphics and text.
//**************************************************************************
void PICASO_4DGL :: moveOrigin(short x, short y) {
    
    char command[6] = "";
    
    command[0] = (MOVE_ORIGIN >> (8*1)) & 0xff;
    command[1] = (MOVE_ORIGIN >> (8*0)) & 0xff;
    command[2] = (x >> (8*1)) & 0xff;
    command[3] = (x >> (8*0)) & 0xff;
    command[4] = (y >> (8*1)) & 0xff;
    command[5] = (y >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 6);
    getResponse(1);
}

//**************************************************************************
// The Draw Line & Move Origin command draws a line from the current origin 
// to a new position. The Origin is then set to the new position. 
// The line is drawn using the current object colour, using the 
// “Set Graphics Parameters” – “Object Colour” command. The line may be 
// tessellated with the “Line Pattern” command.
// 
// Note: this command is mostly useful with the “Calculate Orbit” command, 
// and usually the “Draw Line” command would be used
//**************************************************************************
void PICASO_4DGL :: lineTo(short x, short y) {
    
    char command[6] = "";
    
    command[0] = (LINE_TO >> (8*1)) & 0xff;
    command[1] = (LINE_TO >> (8*0)) & 0xff;
    command[2] = (x >> (8*1)) & 0xff;
    command[3] = (x >> (8*0)) & 0xff;
    command[4] = (y >> (8*1)) & 0xff;
    command[5] = (y >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 6);
    getResponse(1);
}

//**************************************************************************
// The Set Clip Window command specifies a clipping window region on the screen such
// that any objects and text placed onto the screen will be clipped and displayed only
// within that region. For the clipping window to take effect, the clipping setting must be
// enabled separately using the “Clipping” command
//**************************************************************************
void PICASO_4DGL :: setClipWindow(short x1, short y1, short x2, short y2) {
    
    char command[10] = "";
    
    command[0] = (SET_CLIP_WINDOW >> (8*1)) & 0xff;
    command[1] = (SET_CLIP_WINDOW >> (8*0)) & 0xff;
    command[2] = (x1 >> (8*1)) & 0xff;
    command[3] = (x1 >> (8*0)) & 0xff;
    command[4] = (y1 >> (8*1)) & 0xff;
    command[5] = (y1 >> (8*0)) & 0xff;
    command[6] = (x2 >> (8*1)) & 0xff;
    command[7] = (x2 >> (8*0)) & 0xff;
    command[8] = (y2 >> (8*1)) & 0xff;
    command[9] = (y2 >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 10);
    getResponse(1);
}

//**************************************************************************
// The Clipping command Enables or Disables the ability for Clipping to be used. 
// The clipping points are set with “Set Clip Window” and must be set first.
//**************************************************************************
void PICASO_4DGL :: clipping(short value) {
    
    char command[4] = "";
    
    command[0] = (CLIPPING >> (8*1)) & 0xff;
    command[1] = (CLIPPING >> (8*0)) & 0xff;
    command[2] = (value >> (8*1)) & 0xff;
    command[3] = (value >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 4);
    getResponse(1);
}

//**************************************************************************
// The Extend Clip Region command forces the clip region to the extent 
// of the last text that was printed, or the last image that was shown.
//**************************************************************************
void PICASO_4DGL :: extendClipRegion() {
    
    char command[2] = "";
    
    command[0] = (EXTEND_CLIP >> (8*1)) & 0xff;
    command[1] = (EXTEND_CLIP >> (8*0)) & 0xff;
    
    writeCOMMAND(command, 2);
    getResponse(1);
}