Fork of the Adafruit ST7735R library targeted to the 1.44" TFT with custom high speed monochrome and color drawing routines. Note that this library includes modifications to use a shared SPI module to simplify projects that use the SPI for several peripherals. Read the WIKI to see how to get this library working in your own project.

Fork of Adafruit_ST7735 by Andrew Lindsay

This library is a modification of Andrew Lindsay's ST7735 Adafruit library. It includes custom bitmap drawing routines that work around 15 times faster when using custom byte arrays generated by my png to char array converter.

For more info look to the detailed post explaining the changes and where you can download the converter binaries as well: http://alfredoer.com/microcontrollers-2/adafruit-image-bitmap-generator/

IMPORTANT: One of the main modifications is that this library does not instantiate an SPI object directly, rather it is meant to use a shared SPI object exported in a "board.h" file elsewhere in your project.

The contents of such a file can be something like:

ifndef BOARD_H_

  1. define BOARD_H_
  1. include <mbed.h>
  2. include <rtos.h> extern Mutex spi_mutex; extern SPI spi_port;
  1. endif

And of course, the objects should be instantiated somewhere (like in board.c) like this (for a KL25z, modify as needed):

mosi, miso, sck Mutex spi_mutex; SPI spi_port( PTD2, PTD3, PTD1);

The rationale is that several modules can use the hardware SPI port on several threads and coexist happily with each other.

Revision:
3:38c4a70421f0
Parent:
2:18ecda534e06
Child:
4:2eb7d188ba43
--- a/Adafruit_ST7735.cpp	Mon Mar 03 22:13:53 2014 +0000
+++ b/Adafruit_ST7735.cpp	Thu Jan 15 21:00:58 2015 +0000
@@ -18,11 +18,11 @@
 
 #include "mbed.h"
 #include "Adafruit_ST7735.h"
-
+#include "board.h"
 
 // Constructor 
-Adafruit_ST7735::Adafruit_ST7735(PinName mosi, PinName miso, PinName sck, PinName cs, PinName rs, PinName rst) 
-        : lcdPort(mosi, miso, sck), _cs(cs), _rs(rs), _rst(rst), Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT) 
+Adafruit_ST7735::Adafruit_ST7735(PinName cs, PinName rs, PinName rst,PinName LITE) 
+        : _cs(cs), _rs(rs), _rst(rst), Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT) 
 { }
 
 
@@ -30,7 +30,7 @@
 {
     _rs = 0;
     _cs = 0;
-    lcdPort.write( c );
+    spi_port.write( c );
     _cs = 1;
 }
 
@@ -39,7 +39,7 @@
 {
     _rs = 1;
     _cs = 0;
-    lcdPort.write( c );
+    spi_port.write( c );
 
     _cs = 1;
 }
@@ -169,6 +169,16 @@
     0x00, 0x9F
 },           //     XEND = 159
 
+Rcmd2green144[] = {           // Init for 7735R, part 2 (green 1.44 tab)
+    2,                        //  2 commands in list:
+    ST7735_CASET  , 4      ,  //  1: Column addr set, 4 args, no delay:
+      0x00, 0x00,             //     XSTART = 0
+      0x00, 0x7F,             //     XEND = 127
+    ST7735_RASET  , 4      ,  //  2: Row addr set, 4 args, no delay:
+      0x00, 0x00,             //     XSTART = 0
+      0x00, 0x7F              //     XEND = 127
+},           
+      
 Rcmd3[] = {                 // Init for 7735R, part 3 (red or green tab)
     4,                        //  4 commands in list:
     ST7735_GMCTRP1, 16      , //  1: Magical unicorn dust, 16 args, no delay:
@@ -225,17 +235,17 @@
     _cs = 1;
 
     // use default SPI format
-    lcdPort.format(8,0);
-    lcdPort.frequency(4000000);     // Lets try 4MHz
+    spi_port.format(8,3);
+    spi_port.frequency(32000000);     // Lets try 48MHz
 
     // toggle RST low to reset; CS low so it'll listen to us
     _cs = 0;
     _rst = 1;
-    wait_ms(500);
+    wait_ms(50);
     _rst = 0;
-    wait_ms(500);
+    wait_ms(50);
     _rst = 1;
-    wait_ms(500);
+    wait_ms(50);
 
     if(cmdList) commandList(cmdList);
 }
@@ -256,7 +266,11 @@
         commandList(Rcmd2green);
         colstart = 2;
         rowstart = 1;
-    } else {
+    } else if(options == INITR_144GREENTAB) {
+        commandList(Rcmd2green144);
+        colstart = 2;
+        rowstart = 1;
+  } else {
         // colstart, rowstart left at default '0' values
         commandList(Rcmd2red);
     }
@@ -296,8 +310,8 @@
 
     for(y=_height; y>0; y--) {
         for(x=_width; x>0; x--) {
-            lcdPort.write( hi );
-            lcdPort.write( lo );
+            spi_port.write( hi );
+            spi_port.write( lo );
         }
     }
 
@@ -310,8 +324,8 @@
     _rs = 1;
     _cs = 0;
 
-    lcdPort.write( color >> 8 );
-    lcdPort.write( color );
+    spi_port.write( color >> 8 );
+    spi_port.write( color );
     _cs = 1;
 }
 
@@ -326,8 +340,8 @@
     _rs = 1;
     _cs = 0;
 
-    lcdPort.write( color >> 8 );
-    lcdPort.write( color );
+    spi_port.write( color >> 8 );
+    spi_port.write( color );
 
     _cs = 1;
 }
@@ -346,8 +360,8 @@
     _rs = 1;
     _cs = 0;
     while (h--) {
-        lcdPort.write( hi );
-        lcdPort.write( lo );
+        spi_port.write( hi );
+        spi_port.write( lo );
     }
     _cs = 1;
 }
@@ -366,8 +380,8 @@
     _rs = 1;
     _cs = 0;
     while (w--) {
-        lcdPort.write( hi );
-        lcdPort.write( lo );
+        spi_port.write( hi );
+        spi_port.write( lo );
     }
     _cs = 1;
 }
@@ -390,8 +404,8 @@
     _cs = 0;
     for(y=h; y>0; y--) {
         for(x=w; x>0; x--) {
-            lcdPort.write( hi );
-            lcdPort.write( lo );
+            spi_port.write( hi );
+            spi_port.write( lo );
         }
     }
 
@@ -399,6 +413,30 @@
 }
 
 
+// fill a rectangle
+void Adafruit_ST7735::bitmap(int16_t x, int16_t y, int16_t w, int16_t h,
+                               const unsigned char *bitmap )
+{
+
+    // rudimentary clipping (drawChar w/big text requires this)
+    if((x >= _width) || (y >= _height)) return;
+    if((x + w - 1) >= _width)  w = _width  - x;
+    if((y + h - 1) >= _height) h = _height - y;
+
+    setAddrWindow(x, y, x+w-1, y+h-1);
+
+    _rs = 1;
+    _cs = 0;
+    for(y=0; y<h; y++) {
+        for(x=0; x<w; x++) {
+            spi_port.write( bitmap[ y*w*2 + x*2 ] );
+            spi_port.write( bitmap[ y*w*2 + x*2 + 1] );
+        }
+    }
+
+    _cs = 1;
+}
+
 // Pass 8-bit (each) R,G,B, get back 16-bit packed color
 uint16_t Adafruit_ST7735::Color565(uint8_t r, uint8_t g, uint8_t b)
 {