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:
4:2eb7d188ba43
Parent:
3:38c4a70421f0
Child:
5:57e689b2af4f
--- a/Adafruit_ST7735.cpp	Thu Jan 15 21:00:58 2015 +0000
+++ b/Adafruit_ST7735.cpp	Sat Jan 24 21:32:09 2015 +0000
@@ -21,8 +21,8 @@
 #include "board.h"
 
 // Constructor 
-Adafruit_ST7735::Adafruit_ST7735(PinName cs, PinName rs, PinName rst,PinName LITE) 
-        : _cs(cs), _rs(rs), _rst(rst), Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT) 
+Adafruit_ST7735::Adafruit_ST7735(PinName cs, PinName rs, PinName rst, PinName bl ) 
+        : _cs(cs), _rs(rs), _rst(rst), _bl(bl), Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT) 
 { }
 
 
@@ -236,7 +236,7 @@
 
     // use default SPI format
     spi_port.format(8,3);
-    spi_port.frequency(32000000);     // Lets try 48MHz
+    spi_port.frequency(24000000);     // Lets try 48MHz
 
     // toggle RST low to reset; CS low so it'll listen to us
     _cs = 0;
@@ -277,6 +277,36 @@
     commandList(Rcmd3);
 }
 
+void Adafruit_ST7735::backlight( uint8_t setting )
+{
+    _bl = setting;
+}
+
+void Adafruit_ST7735::writeString( int x, int row, char * str, int flag ){
+if( flag == NORMAL ){
+        setCursor( x, row * 10 );
+        setTextColor( BLACK, WHITE );
+        printf("%s",str);
+    } else {
+        setCursor( x, row * 10 );
+        setTextColor( WHITE, BLACK );
+        printf("%s",str);
+    }
+}
+
+void Adafruit_ST7735::writeString( int x, int row, char * str, int flag, char size ){
+    setTextSize(size);
+    if( flag == NORMAL ){
+        setCursor( x, row*8 );
+        setTextColor( BLACK, WHITE );
+        printf("%s",str);
+    } else {
+        setCursor( x, row*8 );
+        setTextColor( WHITE, RED );
+        printf("%s",str);
+    }
+}
+
 
 void Adafruit_ST7735::setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1,
                                     uint8_t y1)
@@ -415,7 +445,7 @@
 
 // fill a rectangle
 void Adafruit_ST7735::bitmap(int16_t x, int16_t y, int16_t w, int16_t h,
-                               const unsigned char *bitmap )
+                               const unsigned char *map )
 {
 
     // rudimentary clipping (drawChar w/big text requires this)
@@ -429,14 +459,64 @@
     _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] );
+            spi_port.write( map[ y*w*2 + x*2 ] );
+            spi_port.write( map[ y*w*2 + x*2 + 1] );
         }
     }
 
     _cs = 1;
 }
 
+// fill a rectangle
+void Adafruit_ST7735::bitmapBW(int16_t x, int16_t y, const unsigned char *map, int16_t w, int16_t h,
+                                int16_t foreground, int16_t background )
+{
+    char dw, dh, xx, yy;
+    
+    char fgA = foreground >> 8;
+    char fgB = foreground & 0xFF;
+    
+    char bgA = background >> 8;
+    char bgB = background & 0xFF;
+    
+     dw = w;
+     dh = h;
+    
+    // rudimentary clipping (drawChar w/big text requires this)
+    // out of screen
+    if((x >= _width) || (y >= _height)) return;
+    
+    
+    if((x + w - 1) >= _width){      // positive clipping
+        dw = _width  - x;
+        setAddrWindow(x, y, _width-1, y+h-1);
+    } else if((y + h - 1) >= _height){ 
+        dh = _height - y;
+        setAddrWindow(x, y, x+w-1, _height-1);   
+    } else if( x < 0 ){             // negative clipping
+        setAddrWindow(0, y, x+w-1, y+h-1);
+    } else {
+        setAddrWindow(x, y, x+w-1, y+h-1);   
+    }
+
+    _rs = 1;
+    _cs = 0;
+    for(yy=0; yy<dh; yy++) {
+        for(xx=0; xx<dw; xx++) {       
+            if( xx+x >= 0){
+                if( map[ (yy*w +xx)/8 ] & ( 1<<(xx%8) ) ){
+                    spi_port.write( fgA ); 
+                    spi_port.write( fgB ); 
+                } else {
+                    spi_port.write( bgA ); 
+                    spi_port.write( bgB );     
+                }
+            }
+        }
+    }
+    _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)
 {
@@ -485,5 +565,3 @@
 {
     writecommand(i ? ST7735_INVON : ST7735_INVOFF);
 }
-
-