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:

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.



File content as of revision 6:f572731fc65f:

  This is a library for the Adafruit 1.8" SPI display.
  This library works with the Adafruit 1.8" TFT Breakout w/SD card
  as well as Adafruit raw 1.8" TFT display
  Check out the links above for our tutorials and wiring diagrams
  These displays use SPI to communicate, 4 or 5 pins are required to
  interface (RST is optional)
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution

#pragma once

#include "mbed.h"
#include "Adafruit_GFX.h"
#include "N3310LCDDefs.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_144GREENTAB   0x1

#define ST7735_TFTWIDTH  128
#define ST7735_TFTHEIGHT 128

#define ST7735_NOP     0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID   0x04
#define ST7735_RDDST   0x09

#define ST7735_SLPIN   0x10
#define ST7735_SLPOUT  0x11
#define ST7735_PTLON   0x12
#define ST7735_NORON   0x13

#define ST7735_INVOFF  0x20
#define ST7735_INVON   0x21
#define ST7735_DISPOFF 0x28
#define ST7735_DISPON  0x29
#define ST7735_CASET   0x2A
#define ST7735_RASET   0x2B
#define ST7735_RAMWR   0x2C
#define ST7735_RAMRD   0x2E

#define ST7735_PTLAR   0x30
#define ST7735_COLMOD  0x3A
#define ST7735_MADCTL  0x36

#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR  0xB4
#define ST7735_DISSET5 0xB6

#define ST7735_PWCTR1  0xC0
#define ST7735_PWCTR2  0xC1
#define ST7735_PWCTR3  0xC2
#define ST7735_PWCTR4  0xC3
#define ST7735_PWCTR5  0xC4
#define ST7735_VMCTR1  0xC5

#define ST7735_RDID1   0xDA
#define ST7735_RDID2   0xDB
#define ST7735_RDID3   0xDC
#define ST7735_RDID4   0xDD

#define ST7735_PWCTR6  0xFC

#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1

// Color definitions
#define ST7735_BLACK   0x0000
#define ST7735_BLUE    0x001F
#define ST7735_RED     0xF800
#define ST7735_GREEN   0x07E0
#define ST7735_CYAN    0x07FF
#define ST7735_MAGENTA 0xF81F
#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 {


  Adafruit_ST7735( PinName CS, PinName RS, PinName RST, PinName BL);

  void     chipEnable();
  void     chipDisable();
  void     initB(void);                             // for ST7735B displays
  void     initR(uint8_t options = INITR_GREENTAB); // for ST7735R
  void     backlight( uint8_t setting );
  void     writeString( int x, int row, char * str, int flag );
  void     writeString( int x, int row, char * str, int flag, char size );
  void     setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
  void     pushColor(uint16_t color);

  void     fillScreen(uint16_t color);
  void     drawPixel(int16_t x, int16_t y, uint16_t color);
  void     drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
  void     drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  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 *map );
  void     bitmapBW(int16_t x, int16_t y, const unsigned char *map, int16_t w, int16_t h, int16_t foreground, int16_t background );
  void     setRotation( uint8_t r);
  uint16_t Color565( uint8_t r, uint8_t g, uint8_t b );


  void     spiwrite(uint8_t),
           writecommand(uint8_t c),
           writedata(uint8_t d),
           commandList(uint8_t *addr),
           commonInit(uint8_t *cmdList);

  uint8_t  colstart, rowstart; // some displays need this changed

//    SPI lcdPort;            // does SPI MOSI, MISO and SCK
    DigitalOut _cs;         // does SPI CE
    DigitalOut _rs;         // register/date select
    DigitalOut _rst;        // does 3310 LCD_RST
    DigitalOut _bl;        // backlight