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
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_
- define BOARD_H_
- include <mbed.h>
- include <rtos.h> extern Mutex spi_mutex; extern SPI spi_port;
- 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, committed 2015-01-15
- Comitter:
- AlfredoER
- Date:
- Thu Jan 15 21:00:58 2015 +0000
- Parent:
- 2:18ecda534e06
- Child:
- 4:2eb7d188ba43
- Commit message:
- -Modified to use common spi object that is shared with other modules.
Changed in this revision
Adafruit_ST7735.cpp | Show annotated file Show diff for this revision Revisions of this file |
Adafruit_ST7735.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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) {
--- a/Adafruit_ST7735.h Mon Mar 03 22:13:53 2014 +0000 +++ b/Adafruit_ST7735.h Thu Jan 15 21:00:58 2015 +0000 @@ -21,15 +21,24 @@ #include "mbed.h" #include "Adafruit_GFX.h" - +#include "board.h" #define boolean bool // some flags for initR() :( #define INITR_GREENTAB 0x0 #define INITR_REDTAB 0x1 +#define INITR_GREENTAB 0x0 +#define INITR_REDTAB 0x1 +#define INITR_BLACKTAB 0x2 + +#define INITR_18GREENTAB INITR_GREENTAB +#define INITR_18REDTAB INITR_REDTAB +#define INITR_18BLACKTAB INITR_BLACKTAB +#define INITR_144GREENTAB 0x1 + #define ST7735_TFTWIDTH 128 -#define ST7735_TFTHEIGHT 160 +#define ST7735_TFTHEIGHT 128 #define ST7735_NOP 0x00 #define ST7735_SWRESET 0x01 @@ -87,12 +96,21 @@ #define ST7735_YELLOW 0xFFE0 #define ST7735_WHITE 0xFFFF +#define BLACK 0x0000 +#define BLUE 0x001F +#define RED 0xF800 +#define GREEN 0x07E0 +#define CYAN 0x07FF +#define MAGENTA 0xF81F +#define YELLOW 0xFFE0 +#define WHITE 0xFFFF + class Adafruit_ST7735 : public Adafruit_GFX { public: - Adafruit_ST7735(PinName mosi, PinName miso, PinName sck, PinName CS, PinName RS, PinName RST); + Adafruit_ST7735( PinName CS, PinName RS, PinName RST, PinName LITE); void initB(void); // for ST7735B displays void initR(uint8_t options = INITR_GREENTAB); // for ST7735R @@ -106,6 +124,7 @@ void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); void invertDisplay(boolean i); + void bitmap(int16_t x, int16_t y, int16_t w, int16_t h, const unsigned char *bitmap ); void setRotation(uint8_t r); uint16_t Color565(uint8_t r, uint8_t g, uint8_t b); @@ -119,7 +138,7 @@ uint8_t colstart, rowstart; // some displays need this changed - SPI lcdPort; // does SPI MOSI, MISO and SCK +// SPI lcdPort; // does SPI MOSI, MISO and SCK DigitalOut _cs; // does SPI CE DigitalOut _rs; // register/date select DigitalOut _rst; // does 3310 LCD_RST