SPI based library for the ST7735 LCD controller.

Dependents:   RayCastingEngine RETRO_LCD_PerformanceTest RETRO_loop_test RETRO_RickGame ... more

Files at this revision

API Documentation at this revision

Comitter:
taylorza
Date:
Sun Feb 01 05:09:49 2015 +0000
Parent:
12:146a55dccb22
Child:
14:c94d0a2c2ba0
Commit message:
Improved font handling and added an OEM font. Added a means to measure the width and height of a string rendered with a font

Changed in this revision

LCD_ST7735.cpp Show annotated file Show diff for this revision Revisions of this file
LCD_ST7735.h Show annotated file Show diff for this revision Revisions of this file
font_IBM.h Show annotated file Show diff for this revision Revisions of this file
font_OEM.h Show annotated file Show diff for this revision Revisions of this file
--- a/LCD_ST7735.cpp	Sun Jan 25 06:53:17 2015 +0000
+++ b/LCD_ST7735.cpp	Sun Feb 01 05:09:49 2015 +0000
@@ -385,14 +385,35 @@
         
 void LCD_ST7735::drawString(const uint8_t *pFont, int x, int y, const char *pString)
 {
+    uint8_t w = *pFont;
+    uint8_t h = *(pFont + 1);
+    uint8_t offset = *(pFont + 2);
+    uint8_t leftPad = *(pFont + 3);
+    uint8_t rightPad = *(pFont + 4);
+    uint8_t topPad = *(pFont + 5);
+    uint8_t bottomPad = *(pFont + 6);
+    
     char *p = (char*)pString;
     while(*p != 0)
     {
-        drawChar(pFont, x, y, *p++);
-        x += 8;
+        drawChar(pFont, x, y, *p++, w, h, offset, leftPad, rightPad, topPad, bottomPad);
+        x += (w + leftPad + rightPad);
     }
 }
 
+void measureString(const uint8_t *pFont, const char *pString, uint8_t &width, uint8_t &height)
+{
+    uint8_t w = *pFont;
+    uint8_t h = *(pFont + 1);
+    uint8_t leftPad = *(pFont + 3);
+    uint8_t rightPad = *(pFont + 4);
+    uint8_t topPad = *(pFont + 5);
+    uint8_t bottomPad = *(pFont + 6);
+    
+    width = (w + leftPad + rightPad) * strlen(pString);
+    height = (h + topPad + bottomPad);
+}
+
 void LCD_ST7735::selectDevice()
 {
     _spi.prepareFastSPI();
@@ -426,16 +447,36 @@
     endBatchCommand();
 }
 
-void LCD_ST7735::drawChar(const uint8_t *pFont, int x, int y, char c)
+void LCD_ST7735::drawChar(const uint8_t *pFont, int x, int y, char c, uint8_t w, uint8_t h, uint8_t offset, uint8_t leftPad, uint8_t rightPad, uint8_t topPad, uint8_t bottomPad)
 {
-    const uint8_t *pChar = pFont + (c * 8);
+    const uint8_t *pChar = (pFont + 7) + ((c - offset) * h);
+    
+    clip(x, y, w + leftPad + rightPad, h + topPad + bottomPad);
+    
+    beginBatchCommand(CMD_RAMWR);
     
-    clip(x, y, 8, 8);
-    beginBatchCommand(CMD_RAMWR);
-    for(int r = 0; r < 8; ++r)
+    // Render top spacing
+    for (int r = 0; r < topPad; ++r)
+    {
+        for (int c = 0; c < w + leftPad + rightPad; ++c)
+        {
+            writeBatchData(_backgroundColorHigh);
+            writeBatchData(_backgroundColorLow);
+        }
+    }
+    
+    // Render character
+    for(int r = 0; r < h; ++r)
     {
         uint8_t b = pChar[r];
-        for(int c = 0; c < 8; ++c)
+        
+        // Render left spacing
+        for (int c = 0; c < leftPad; ++c)
+        {
+            writeBatchData(_backgroundColorHigh);
+            writeBatchData(_backgroundColorLow);
+        }        
+        for(int c = 0; c < w; ++c)
         {
             if (b & 0x80)
             {
@@ -450,6 +491,22 @@
                 
             b <<= 1;
         }
+        
+        for (int c = 0; c < rightPad; ++c)
+        {
+            writeBatchData(_backgroundColorHigh);
+            writeBatchData(_backgroundColorLow);
+        }        
+    }
+    
+    // Render bottom spacing
+    for (int r = 0; r < bottomPad; ++r)
+    {
+        for (int c = 0; c < w + leftPad + rightPad; ++c)
+        {
+            writeBatchData(_backgroundColorHigh);
+            writeBatchData(_backgroundColorLow);
+        }
     }
     endBatchCommand();
 }
--- a/LCD_ST7735.h	Sun Jan 25 06:53:17 2015 +0000
+++ b/LCD_ST7735.h	Sun Feb 01 05:09:49 2015 +0000
@@ -195,10 +195,17 @@
          * @param pFont Pointer to the font used to render the string to the display
          * @param x The X coordinate location to draw the string.
          * @param y The Y coordinate location to draw the string.
-         * @param pString ASCIIZ string to draw to the display.
-         * @note The font is currently limited to an 8x8 font. See the font_IBM.h file for an example font.
+         * @param pString ASCIIZ string to draw to the display.         
         */
-        void drawString(const uint8_t *pFont, int x, int y, const char *pString); 
+        void drawString(const uint8_t *pFont, int x, int y, const char *pString);
+        
+        /** Measure the width and height of the string when rendered with the specified font
+         * @param pFont Pointer to the font used to measure the string         
+         * @param pString ASCIIZ string to measure.
+         * @param width Reference to the variable that will contain the width
+         * @param height Reference to the variable that will contain the height         
+        */
+        void measureString(const uint8_t *pFont, const char *pString, uint8_t &width, uint8_t &height);
         
         /** Select the device on the SPI bus.
         selectDevice needs to be called before accessing the screen if there are multiple devices on the SPI bus.
@@ -222,7 +229,7 @@
     private:
         void drawVertLine(int x1, int y1, int y2, uint16_t color);
         void drawHorizLine(int x1, int y1, int x2, uint16_t color);
-        void drawChar(const uint8_t *pFont, int x, int y, char c);
+        void drawChar(const uint8_t *pFont, int x, int y, char c, uint8_t w, uint8_t h, uint8_t offset, uint8_t leftPad, uint8_t rightPad, uint8_t topPad, uint8_t bottomPad);
         
     private:
         void swap(int &a, int &b);
--- a/font_IBM.h	Sun Jan 25 06:53:17 2015 +0000
+++ b/font_IBM.h	Sun Feb 01 05:09:49 2015 +0000
@@ -5,8 +5,9 @@
 #ifndef __FONT_IBM_H__
 #define __FONT_IBM_H__
 
-const uint8_t font_ibm[2048] =
-{
+const uint8_t font_ibm[] =
+{    
+    0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,       // Width, Height, FirstChar, left padding, right padding, top padding, bottom padding
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Char 000 (.)
     0x7E, 0x81, 0xA5, 0x81, 0xBD, 0x99, 0x81, 0x7E, // Char 001 (.)
     0x7E, 0xFF, 0xDB, 0xFF, 0xC3, 0xE7, 0xFF, 0x7E, // Char 002 (.)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/font_OEM.h	Sun Feb 01 05:09:49 2015 +0000
@@ -0,0 +1,108 @@
+///////////////////////////////////////////////////////////////////////////////
+// LCD_ST7735 - Driver for ST7735 LCD display controller
+// Author: Chris Taylor (taylorza)
+// Font: Standard LCD font by GHI Electronics
+
+#ifndef __FONT_OEM_H__
+#define __FONT_OEM_H__
+
+const uint8_t font_oem[] =
+{
+    0x06, 0x07, 0x20, 0x00, 0x00, 0x00, 0x01, // Width, Height, FirstChar, left padding, right padding, top padding, bottom padding
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Space */
+    0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x20, /* ! */
+    0x50, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00, /* " */
+    0x50, 0x50, 0xf8, 0x50, 0xf8, 0x50, 0x50, /* # */
+    0x20, 0x78, 0xa0, 0x70, 0x28, 0xf0, 0x20, /* $ */
+    0xc0, 0xc8, 0x10, 0x20, 0x40, 0x98, 0x18, /* % */
+    0x60, 0x90, 0xa0, 0x40, 0xa0, 0x98, 0x60, /* & */
+    0x60, 0x20, 0x40, 0x00, 0x00, 0x00, 0x00, /* ' */
+    0x10, 0x20, 0x40, 0x40, 0x40, 0x20, 0x10, /* ( */
+    0x40, 0x20, 0x10, 0x10, 0x10, 0x20, 0x40, /* ) */
+    0x00, 0x20, 0xa8, 0x70, 0xa8, 0x20, 0x00, /* * */
+    0x00, 0x20, 0x20, 0xf8, 0x20, 0x20, 0x00, /* + */
+    0x00, 0x00, 0x00, 0x00, 0xc0, 0x40, 0x80, /* , */
+    0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, /* - */
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60, /* . */
+    0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, /* / */
+    0x70, 0x88, 0x98, 0xa8, 0xc8, 0x88, 0x70, /* 0 */
+    0x20, 0x60, 0x20, 0x20, 0x20, 0x20, 0x70, /* 1 */
+    0x70, 0x88, 0x08, 0x10, 0x20, 0x40, 0xf8, /* 2 */
+    0xf8, 0x10, 0x20, 0x10, 0x08, 0x88, 0x70, /* 3 */
+    0x10, 0x30, 0x50, 0x90, 0xf8, 0x10, 0x10, /* 4 */
+    0xf8, 0x80, 0xf0, 0x08, 0x08, 0x88, 0x70, /* 5 */
+    0x30, 0x40, 0x80, 0xf0, 0x88, 0x88, 0x70, /* 6 */
+    0xf8, 0x08, 0x10, 0x20, 0x40, 0x40, 0x40, /* 7 */
+    0x70, 0x88, 0x88, 0x70, 0x88, 0x88, 0x70, /* 8 */
+    0x70, 0x88, 0x88, 0x78, 0x08, 0x10, 0x60, /* 9 */
+    0x00, 0x60, 0x60, 0x00, 0x60, 0x60, 0x00, /* : */
+    0x00, 0x60, 0x60, 0x00, 0x60, 0x20, 0x40, /* ; */
+    0x10, 0x20, 0x40, 0x80, 0x40, 0x20, 0x10, /* < */
+    0x00, 0x00, 0xf8, 0x00, 0xf8, 0x00, 0x00, /* = */
+    0x40, 0x20, 0x10, 0x08, 0x10, 0x20, 0x40, /* > */
+    0x70, 0x88, 0x08, 0x10, 0x20, 0x00, 0x20, /* ? */
+    0x70, 0x88, 0xb8, 0xa8, 0xb8, 0x80, 0x70, /* @ */
+    0x70, 0x88, 0x88, 0x88, 0xf8, 0x88, 0x88, /* A */
+    0xf0, 0x88, 0x88, 0xf0, 0x88, 0x88, 0xf0, /* B */
+    0x70, 0x88, 0x80, 0x80, 0x80, 0x88, 0x70, /* C */
+    0xe0, 0x90, 0x88, 0x88, 0x88, 0x90, 0xe0, /* D */
+    0xf8, 0x80, 0x80, 0xf0, 0x80, 0x80, 0xf8, /* E */
+    0xf8, 0x80, 0x80, 0xf0, 0x80, 0x80, 0x80, /* F */
+    0x70, 0x88, 0x80, 0xb8, 0x88, 0x88, 0x78, /* G */
+    0x88, 0x88, 0x88, 0xf8, 0x88, 0x88, 0x88, /* H */
+    0x70, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70, /* I */
+    0x38, 0x10, 0x10, 0x10, 0x10, 0x90, 0x60, /* J */
+    0x88, 0x90, 0xa0, 0xc0, 0xa0, 0x90, 0x88, /* K */
+    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xf8, /* L */
+    0x88, 0xd8, 0xa8, 0xa8, 0x88, 0x88, 0x88, /* M */
+    0x88, 0x88, 0xc8, 0xa8, 0x98, 0x88, 0x88, /* N */
+    0x70, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70, /* O */
+    0xf0, 0x88, 0x88, 0xf0, 0x80, 0x80, 0x80, /* P */
+    0x70, 0x88, 0x88, 0x88, 0xa8, 0x90, 0x68, /* Q */
+    0xf0, 0x88, 0x88, 0xf0, 0xa0, 0x90, 0x88, /* R */
+    0x70, 0x88, 0x80, 0x70, 0x08, 0x88, 0x70, /* S */
+    0xf8, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, /* T */
+    0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70, /* U */
+    0x88, 0x88, 0x88, 0x88, 0x88, 0x50, 0x20, /* V */
+    0x88, 0x88, 0x88, 0xa8, 0xa8, 0xa8, 0x50, /* W */
+    0x88, 0x88, 0x50, 0x20, 0x50, 0x88, 0x88, /* X */
+    0x88, 0x88, 0x88, 0x50, 0x20, 0x20, 0x20, /* Y */
+    0xf8, 0x08, 0x10, 0x20, 0x40, 0x80, 0xf8, /* Z */
+    0x70, 0x40, 0x40, 0x40, 0x40, 0x40, 0x70, /* [ */
+    0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00, /* \ */
+    0x70, 0x10, 0x10, 0x10, 0x10, 0x10, 0x70, /* ] */
+    0x20, 0x50, 0x88, 0x00, 0x00, 0x00, 0x00, /* ^ */
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, /* _ */
+    0x30, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, /* ` */
+    0x00, 0x00, 0x70, 0x08, 0x78, 0x88, 0x78, /* a */
+    0x80, 0x80, 0xf0, 0x88, 0x88, 0x88, 0xf0, /* b */
+    0x00, 0x00, 0x78, 0x80, 0x80, 0x80, 0x78, /* c */
+    0x08, 0x08, 0x78, 0x88, 0x88, 0x88, 0x78, /* d */
+    0x00, 0x00, 0x70, 0x88, 0xf8, 0x80, 0x70, /* e */
+    0x18, 0x20, 0xf8, 0x20, 0x20, 0x20, 0x20, /* f */
+    0x00, 0x00, 0x78, 0x88, 0x78, 0x08, 0x70, /* g */
+    0x80, 0x80, 0xb0, 0xc8, 0x88, 0x88, 0x88, /* h */
+    0x20, 0x00, 0x60, 0x20, 0x20, 0x20, 0x70, /* i */
+    0x10, 0x00, 0x30, 0x10, 0x10, 0x90, 0x60, /* j */
+    0x80, 0x80, 0x90, 0xa0, 0xc0, 0xa0, 0x90, /* k */
+    0x60, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70, /* l */
+    0x00, 0x00, 0xf0, 0xa8, 0xa8, 0xa8, 0xa8, /* m */
+    0x00, 0x00, 0xb0, 0xc8, 0x88, 0x88, 0x88, /* n */
+    0x00, 0x00, 0x70, 0x88, 0x88, 0x88, 0x70, /* o */
+    0x00, 0x00, 0xf0, 0x88, 0xf0, 0x80, 0x80, /* p */
+    0x00, 0x00, 0x78, 0x88, 0x78, 0x08, 0x08, /* q */
+    0x00, 0x00, 0xb0, 0xc0, 0x80, 0x80, 0x80, /* r */
+    0x00, 0x00, 0x78, 0x80, 0x70, 0x08, 0xf0, /* s */
+    0x20, 0x20, 0xf8, 0x20, 0x20, 0x20, 0x18, /* t */
+    0x00, 0x00, 0x88, 0x88, 0x88, 0x98, 0x68, /* u */
+    0x00, 0x00, 0x88, 0x88, 0x88, 0x50, 0x20, /* v */
+    0x00, 0x00, 0x88, 0x88, 0xa8, 0xa8, 0x50, /* w */
+    0x00, 0x00, 0x88, 0x50, 0x20, 0x50, 0x88, /* x */
+    0x00, 0x00, 0x88, 0x88, 0x78, 0x08, 0x70, /* y */
+    0x00, 0x00, 0xf8, 0x10, 0x20, 0x40, 0xf8, /* z */
+    0x30, 0x40, 0x40, 0x80, 0x40, 0x40, 0x30, /* { */
+    0x20, 0x20, 0x20, 0x00, 0x20, 0x20, 0x20, /* | */
+    0x60, 0x10, 0x10, 0x08, 0x10, 0x10, 0x60, /* } */
+    0x00, 0x20, 0x10, 0xf8, 0x10, 0x20, 0x00, /* ~ */
+};
+#endif // __FONT_OEM_H__
\ No newline at end of file