Library to control a QVGA TFT connected to SPI. You can use printf to print text The lib can handle different fonts, draw lines, circles, rect and bmp

Files at this revision

API Documentation at this revision

Comitter:
dreschpe
Date:
Wed Aug 31 20:51:14 2011 +0000
Parent:
5:2db1b8070d94
Child:
7:4781bb8eed45
Commit message:
fix padding bug for bmp arrays

Changed in this revision

SPI_TFT.cpp Show annotated file Show diff for this revision Revisions of this file
SPI_TFT.h Show annotated file Show diff for this revision Revisions of this file
--- a/SPI_TFT.cpp	Sat Jul 30 22:15:35 2011 +0000
+++ b/SPI_TFT.cpp	Wed Aug 31 20:51:14 2011 +0000
@@ -9,6 +9,10 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+ 
+ 
+// fix bmp padding for Bitmap function 
+
 
 #include "SPI_TFT.h"
 #include "mbed.h"
@@ -534,10 +538,9 @@
 
 
 
-void SPI_TFT::locate(int column, int row) {
-    _column = column;
-    char_x = font[1] * column;   // get the horz. size of the actual font
-    _row = row;
+void SPI_TFT::locate(int x, int y) {
+    char_x = x;
+    char_y = y;
 }
 
 
@@ -555,24 +558,22 @@
 
 
 int SPI_TFT::_putc(int value) {
-    if (value == '\n') {
-        _column = 0;
+    if (value == '\n') {    // new line
         char_x = 0;
-        _row++;
-        if (_row >= rows()) {
-            _row = 0;
+        char_y = char_y + font[2];
+        if (char_y >= height() - font[2]) {
+            char_y = 0;
         }
     } else {
-        character(_column, _row, value);
-        _column++;
-    }
+        character(char_x, char_y, value);
+     }
     return value;
 }
 
 
 
 
-void SPI_TFT::character(int col, int row, int c) {
+void SPI_TFT::character(int x, int y, int c) {
     unsigned int hor,vert,offset,bpl,j,i,b;
     unsigned char* zeichen;
     unsigned char z,w;
@@ -587,16 +588,13 @@
 
     if (char_x + hor > width()) {
         char_x = 0;
-        _column = 0;
-        _row ++;
-        row++;
-        if (_row >= rows()) {
-            _row = 0;
-            row=0;
+        char_y = char_y + vert;
+       if (char_y >= height() - font[2]) {
+            char_y = 0;
         }
     }
 
-    window(char_x, row * vert,hor,vert); // char box
+    window(char_x, char_y,hor,vert); // char box
     wr_cmd(0x22);
     wr_dat_start();
     zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
@@ -632,19 +630,26 @@
 
 
 void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) {
-    unsigned int    i,j;
+    unsigned int    i,j,padd;
     unsigned short *bitmap_ptr = (unsigned short *)bitmap;
+    // the lines are padded to multiple of 4 bytes in a bitmap 
+    padd = -1;
+    do {
+        padd ++;
+    } while (2*(w + padd)%4 != 0);
     window(x, y, w, h);
     wr_cmd(0x22);
     wr_dat_start();
     _spi.format(16,3);
-    bitmap_ptr += ((h - 1)*w);
+    bitmap_ptr += ((h - 1)* (w + padd));
+    //bitmap_ptr -= padd;      
     for (j = 0; j < h; j++) {        //Lines
         for (i = 0; i < w; i++) {     // copy pixel data to TFT
             _spi.write(*bitmap_ptr);    // one line
             bitmap_ptr++;
         }
         bitmap_ptr -= 2*w;
+        bitmap_ptr -= padd;
     }
     _spi.format(8,3);
     wr_dat_stop();
--- a/SPI_TFT.h	Sat Jul 30 22:15:35 2011 +0000
+++ b/SPI_TFT.h	Wed Aug 31 20:51:14 2011 +0000
@@ -9,6 +9,10 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+ 
+ /* change the char position handling
+  * use pixel (x,y) instadt of colum row */
+ 
 
 #ifndef MBED_SPI_TFT_H
 #define MBED_SPI_TFT_H
@@ -41,8 +45,11 @@
 #define Cyan            0x07FF      /*   0, 255, 255 */
 #define Red             0xF800      /* 255,   0,   0 */
 #define Magenta         0xF81F      /* 255,   0, 255 */
-#define Yellow          0xFFE0      /* 255, 255, 0   */
+#define Yellow          0xFFE0      /* 255, 255,   0 */
 #define White           0xFFFF      /* 255, 255, 255 */
+#define Orange          0xFD20      /* 255, 165,   0 */
+#define GreenYellow     0xAFE5      /* 173, 255,  47 */
+
 
 /** Display control class, based on GraphicsDisplay and TextDisplay
  *
@@ -73,7 +80,7 @@
  *     TFT.locate(0,0);
  *     printf("  Hello Mbed 0");
  *     TFT.set_font((unsigned char*) Arial24x23);  // select font 2
- *     TFT.locate(2,5);
+ *     TFT.locate(48,115);
  *     TFT.printf("Bigger Font");
  *  }
  * @endcode
@@ -164,11 +171,10 @@
     
   /** setup cursor position
    *
-   * @param column 0 to max
-   * @param row 0 to max 
-   * max depend on font size
+   * @param x x-position (top left)
+   * @param y y-position 
    */   
-  void locate(int column, int row);
+  void locate(int x, int y);
     
   /** Fill the screen with _backgroun color
    *
@@ -201,12 +207,12 @@
     
   /** draw a character on given position out of the active font to the TFT
    *
-   * @param col column
-   * @param row row
+   * @param x x-position of char (top left) 
+   * @param y y-position
    * @param c char to print
    *
    */    
-  virtual void character(int col, int row, int c);
+  virtual void character(int x, int y, int c);
     
   /** paint a bitmap on the TFT 
    *
@@ -222,6 +228,9 @@
    *   use edit -> copy block -> C Source to export C array
    *   paste this array into your program
    * 
+   *   define the array as static const unsigned char to put it into flash memory
+   *   cast the pointer to (unsigned char *) :
+   *   tft.Bitmap(10,40,309,50,(unsigned char *)scala);
    */    
   void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap);
     
@@ -369,6 +378,7 @@
     
   unsigned int orientation;
   unsigned int char_x;
+  unsigned int char_y;
  
     
 };