for 4180 final project

Fork of 4DGL-uLCD-SE by jim hamblen

Revision:
1:8b656995301f
Child:
2:edae99e4abe7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uLCD_4DGL_Text.cpp	Mon Nov 11 01:22:41 2013 +0000
@@ -0,0 +1,293 @@
+//
+// uLCD_4DGL is a class to drive 4D Systems TFT touch screens
+//
+// Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr>
+//
+// uLCD_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.
+//
+// uLCD_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 uLCD_4DGL.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+
+//****************************************************************************************************
+void uLCD_4DGL :: set_font(char mode)     // set font size
+{
+    char command[3]= "";
+
+    int w, h, fx = 8, fy = 8;
+
+    command[0] = SETFONT;
+    command[1] = 0;
+    command[2] = mode;
+
+    current_font = mode;
+
+    if (current_orientation == IS_PORTRAIT) {
+        w = SIZE_X;
+        h = SIZE_Y;
+    } else {
+        w = SIZE_Y;
+        h = SIZE_X;
+    }
+
+    switch (mode) {
+        case FONT_5X7 :
+            fx = 6;
+            fy = 8;
+            break;
+        case FONT_8X8 :
+            fx = 8;
+            fy = 8;
+            break;
+        case FONT_8X12 :
+            fx = 8;
+            fy = 12;
+            break;
+        case FONT_12X16 :
+            fx = 12;
+            fy = 16;
+            break;
+    }
+
+    max_col = w / fx;
+    max_row = h / fy;
+
+    writeCOMMAND(command, 3);
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: text_mode(char mode)     // set text mode
+{
+    char command[3]= "";
+
+    command[0] = TEXTMODE;
+    command[1] = 0;
+    command[2] = mode;
+
+    writeCOMMAND(command, 3);
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: text_char(char c, char col, char row, int color)     // draw a text char
+{
+    char command[6]= "";
+    command[0] = 0xE4; //move cursor
+    command[1] = 0;
+    command[2] = row;
+    command[3] = 0;
+    command[4] = col;
+    writeCOMMAND(command, 5);
+
+    command[0] = 0x7F;  //set color
+
+    int red5   = (color >> (16 + 3)) & 0x1F;              // get red on 5 bits
+    int green6 = (color >> (8 + 2))  & 0x3F;              // get green on 6 bits
+    int blue5  = (color >> (0 + 3))  & 0x1F;              // get blue on 5 bits
+
+    command[1] = ((red5 << 3)   + (green6 >> 3)) & 0xFF;  // first part of 16 bits color
+    command[2] = ((green6 << 5) + (blue5 >>  0)) & 0xFF;  // second part of 16 bits color
+    writeCOMMAND(command, 3);
+
+    command[0] = TEXTCHAR;  //print char
+    command[1] = 0;
+    command[2] = c;
+    writeCOMMAND(command, 3);
+
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: graphic_char(char c, int x, int y, int color, char width, char height)     // draw a graphic char
+{
+    char command[10]= "";
+
+    command[0] = GRAPHCHAR;
+
+    command[1] = c;
+
+    command[2] = (x >> 8) & 0xFF;
+    command[3] = x & 0xFF;
+
+    command[4] = (y >> 8) & 0xFF;
+    command[5] = y & 0xFF;
+
+    int red5   = (color >> (16 + 3)) & 0x1F;              // get red on 5 bits
+    int green6 = (color >> (8 + 2))  & 0x3F;              // get green on 6 bits
+    int blue5  = (color >> (0 + 3))  & 0x1F;              // get blue on 5 bits
+
+    command[6] = ((red5 << 3)   + (green6 >> 3)) & 0xFF;  // first part of 16 bits color
+    command[7] = ((green6 << 5) + (blue5 >>  0)) & 0xFF;  // second part of 16 bits color
+
+    command[8] = width;
+
+    command[9] = height;
+
+    writeCOMMAND(command, 10);
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: text_string(char *s, char col, char row, char font, int color)     // draw a text string
+{
+
+    char command[1000]= "";
+    int size = strlen(s);
+    int i = 0;
+
+    set_font(font);
+
+    command[0] = 0xE4; //move cursor
+    command[1] = 0;
+    command[2] = row;
+    command[3] = 0;
+    command[4] = col;
+    writeCOMMAND(command, 5);
+
+    command[0] = 0x7F;  //set color
+    int red5   = (color >> (16 + 3)) & 0x1F;              // get red on 5 bits
+    int green6 = (color >> (8 + 2))  & 0x3F;              // get green on 6 bits
+    int blue5  = (color >> (0 + 3))  & 0x1F;              // get blue on 5 bits
+
+    command[1] = ((red5 << 3)   + (green6 >> 3)) & 0xFF;  // first part of 16 bits color
+    command[2] = ((green6 << 5) + (blue5 >>  0)) & 0xFF;  // second part of 16 bits color
+    writeCOMMAND(command, 3);
+
+    command[0] = TEXTSTRING;
+    for (i=0; i<size; i++) command[1+i] = s[i];
+    command[1+size] = 0;
+    writeCOMMANDnull(command, 2 + size);
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: graphic_string(char *s, int x, int y, char font, int color, char width, char height)     // draw a text string
+{
+
+    char command[1000]= "";
+    int size = strlen(s);
+    int i = 0;
+
+    command[0] = GRAPHSTRING;
+
+    command[1] = (x >> 8) & 0xFF;
+    command[2] = x & 0xFF;
+
+    command[3] = (y >> 8) & 0xFF;
+    command[4] = y & 0xFF;
+
+    command[5] = font;
+
+    int red5   = (color >> (16 + 3)) & 0x1F;              // get red on 5 bits
+    int green6 = (color >> (8 + 2))  & 0x3F;              // get green on 6 bits
+    int blue5  = (color >> (0 + 3))  & 0x1F;              // get blue on 5 bits
+
+    command[6] = ((red5 << 3)   + (green6 >> 3)) & 0xFF;  // first part of 16 bits color
+    command[7] = ((green6 << 5) + (blue5 >>  0)) & 0xFF;  // second part of 16 bits color
+
+    command[8] = width;
+
+    command[9] = height;
+
+    for (i=0; i<size; i++) command[10+i] = s[i];
+
+    command[10+size] = 0;
+
+    writeCOMMAND(command, 11 + size);
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: text_button(char *s, char mode, int x, int y, int button_color, char font, int text_color, char width, char height)     // draw a text string
+{
+
+    char command[1000]= "";
+    int size = strlen(s);
+    int i = 0, red5, green6, blue5;
+
+    command[0] = TEXTBUTTON;
+
+    command[1] = mode;
+
+    command[2] = (x >> 8) & 0xFF;
+    command[3] = x & 0xFF;
+
+    command[4] = (y >> 8) & 0xFF;
+    command[5] = y & 0xFF;
+
+    red5   = (button_color >> (16 + 3)) & 0x1F;              // get red on 5 bits
+    green6 = (button_color >> (8 + 2))  & 0x3F;              // get green on 6 bits
+    blue5  = (button_color >> (0 + 3))  & 0x1F;              // get blue on 5 bits
+
+    command[6] = ((red5 << 3)   + (green6 >> 3)) & 0xFF;  // first part of 16 bits color
+    command[7] = ((green6 << 5) + (blue5 >>  0)) & 0xFF;  // second part of 16 bits color
+
+    command[8] = font;
+
+    red5   = (text_color >> (16 + 3)) & 0x1F;              // get red on 5 bits
+    green6 = (text_color >> (8 + 2))  & 0x3F;              // get green on 6 bits
+    blue5  = (text_color >> (0 + 3))  & 0x1F;              // get blue on 5 bits
+
+    command[9] = ((red5 << 3)   + (green6 >> 3)) & 0xFF;  // first part of 16 bits color
+    command[10] = ((green6 << 5) + (blue5 >>  0)) & 0xFF;  // second part of 16 bits color
+
+    command[11] = width;
+
+    command[12] = height;
+
+    for (i=0; i<size; i++) command[13+i] = s[i];
+
+    command[13+size] = 0;
+
+    writeCOMMAND(command, 14 + size);
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: locate(char col, char row)     // place text curssor at col, row
+{
+    current_col = col;
+    current_row = row;
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: color(int color)     // set text color
+{
+    current_color = color;
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: putc(char c)     // place char at current cursor position
+{
+
+    text_char(c, current_col++, current_row, current_color);
+
+    if (current_col == max_col) {
+        current_col = 0;
+        current_row++;
+    }
+    if (current_row == max_row) {
+        current_row = 0;
+    }
+}
+
+//****************************************************************************************************
+void uLCD_4DGL :: puts(char *s)     // place string at current cursor position
+{
+
+    text_string(s, current_col, current_row, current_font, current_color);
+
+    current_col += strlen(s);
+
+    if (current_col >= max_col) {
+        current_row += current_col / max_col;
+        current_col %= max_col;
+    }
+    if (current_row >= max_row) {
+        current_row %= max_row;
+    }
+}