Library for 1.8 inch 160*180 TFT Display. Nice fonts in different formats.

Committer:
rolo644u
Date:
Sat Aug 22 11:25:19 2015 +0000
Revision:
2:291ac9fb4d64
Parent:
1:6a6d8d2b19f1
Updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rolo644u 0:3015b613b20d 1 #ifndef ST7735_H
rolo644u 0:3015b613b20d 2 #define ST7735_H
rolo644u 0:3015b613b20d 3
rolo644u 0:3015b613b20d 4 #include "mbed.h"
rolo644u 0:3015b613b20d 5
rolo644u 0:3015b613b20d 6 // Comment out the #defines below with // to stop that font being loaded
rolo644u 0:3015b613b20d 7 // If all fonts are loaded the total space required is ablout 17890 bytes
rolo644u 0:3015b613b20d 8
rolo644u 0:3015b613b20d 9 #define LOAD_FONT2 // Small font, needs ~3092 bytes in FLASH
rolo644u 0:3015b613b20d 10 #define LOAD_FONT4 // Medium font, needs ~8126 bytes in FLASH
rolo644u 0:3015b613b20d 11 #define LOAD_FONT6 // Large font, needs ~4404 bytes in FLASH
rolo644u 0:3015b613b20d 12 #define LOAD_FONT7 // 7 segment font, needs ~3652 bytes in FLASH
rolo644u 0:3015b613b20d 13 #define LOAD_FONT8 // Large font needs ~10kbytes, only 1234567890:.
rolo644u 0:3015b613b20d 14
rolo644u 0:3015b613b20d 15 #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
rolo644u 0:3015b613b20d 16 #define pgm_read_word(addr) (*(const unsigned short *)(addr))
rolo644u 0:3015b613b20d 17
rolo644u 0:3015b613b20d 18 #define swap(a, b) { int16_t t = a; a = b; b = t; }
rolo644u 0:3015b613b20d 19
rolo644u 0:3015b613b20d 20 #define INITR_GREENTAB 0x0
rolo644u 0:3015b613b20d 21 #define INITR_REDTAB 0x1
rolo644u 0:3015b613b20d 22 #define INITR_BLACKTAB 0x2
rolo644u 0:3015b613b20d 23
rolo644u 0:3015b613b20d 24 #define ST7735_TFTWIDTH 128
rolo644u 0:3015b613b20d 25 #define ST7735_TFTHEIGHT 160
rolo644u 0:3015b613b20d 26
rolo644u 0:3015b613b20d 27 #define ST7735_NOP 0x00
rolo644u 0:3015b613b20d 28 #define ST7735_SWRESET 0x01
rolo644u 0:3015b613b20d 29 #define ST7735_RDDID 0x04
rolo644u 0:3015b613b20d 30 #define ST7735_RDDST 0x09
rolo644u 0:3015b613b20d 31
rolo644u 0:3015b613b20d 32 #define ST7735_SLPIN 0x10
rolo644u 0:3015b613b20d 33 #define ST7735_SLPOUT 0x11
rolo644u 0:3015b613b20d 34 #define ST7735_PTLON 0x12
rolo644u 0:3015b613b20d 35 #define ST7735_NORON 0x13
rolo644u 0:3015b613b20d 36
rolo644u 0:3015b613b20d 37 #define ST7735_INVOFF 0x20
rolo644u 0:3015b613b20d 38 #define ST7735_INVON 0x21
rolo644u 0:3015b613b20d 39 #define ST7735_DISPOFF 0x28
rolo644u 0:3015b613b20d 40 #define ST7735_DISPON 0x29
rolo644u 0:3015b613b20d 41 #define ST7735_CASET 0x2A
rolo644u 0:3015b613b20d 42 #define ST7735_RASET 0x2B
rolo644u 0:3015b613b20d 43 #define ST7735_RAMWR 0x2C
rolo644u 0:3015b613b20d 44 #define ST7735_RAMRD 0x2E
rolo644u 0:3015b613b20d 45
rolo644u 0:3015b613b20d 46 #define ST7735_PTLAR 0x30
rolo644u 0:3015b613b20d 47 #define ST7735_COLMOD 0x3A
rolo644u 0:3015b613b20d 48 #define ST7735_MADCTL 0x36
rolo644u 0:3015b613b20d 49
rolo644u 0:3015b613b20d 50 #define ST7735_FRMCTR1 0xB1
rolo644u 0:3015b613b20d 51 #define ST7735_FRMCTR2 0xB2
rolo644u 0:3015b613b20d 52 #define ST7735_FRMCTR3 0xB3
rolo644u 0:3015b613b20d 53 #define ST7735_INVCTR 0xB4
rolo644u 0:3015b613b20d 54 #define ST7735_DISSET5 0xB6
rolo644u 0:3015b613b20d 55
rolo644u 0:3015b613b20d 56 #define ST7735_PWCTR1 0xC0
rolo644u 0:3015b613b20d 57 #define ST7735_PWCTR2 0xC1
rolo644u 0:3015b613b20d 58 #define ST7735_PWCTR3 0xC2
rolo644u 0:3015b613b20d 59 #define ST7735_PWCTR4 0xC3
rolo644u 0:3015b613b20d 60 #define ST7735_PWCTR5 0xC4
rolo644u 0:3015b613b20d 61 #define ST7735_VMCTR1 0xC5
rolo644u 0:3015b613b20d 62
rolo644u 0:3015b613b20d 63 #define ST7735_RDID1 0xDA
rolo644u 0:3015b613b20d 64 #define ST7735_RDID2 0xDB
rolo644u 0:3015b613b20d 65 #define ST7735_RDID3 0xDC
rolo644u 0:3015b613b20d 66 #define ST7735_RDID4 0xDD
rolo644u 0:3015b613b20d 67
rolo644u 0:3015b613b20d 68 #define ST7735_PWCTR6 0xFC
rolo644u 0:3015b613b20d 69
rolo644u 0:3015b613b20d 70 #define ST7735_GMCTRP1 0xE0
rolo644u 0:3015b613b20d 71 #define ST7735_GMCTRN1 0xE1
rolo644u 0:3015b613b20d 72
rolo644u 0:3015b613b20d 73 // Color definitions
rolo644u 0:3015b613b20d 74 #define ST7735_BLACK 0x0000
rolo644u 0:3015b613b20d 75 #define ST7735_BLUE 0x001F
rolo644u 0:3015b613b20d 76 #define ST7735_RED 0xF800
rolo644u 0:3015b613b20d 77 #define ST7735_GREEN 0x07E0
rolo644u 0:3015b613b20d 78 #define ST7735_CYAN 0x07FF
rolo644u 0:3015b613b20d 79 #define ST7735_MAGENTA 0xF81F
rolo644u 0:3015b613b20d 80 #define ST7735_YELLOW 0xFFE0
rolo644u 0:3015b613b20d 81 #define ST7735_WHITE 0xFFFF
rolo644u 0:3015b613b20d 82 #define ST7735_GREY 0x39C4
rolo644u 0:3015b613b20d 83 #define ST7735_NAVY 0x000F
rolo644u 0:3015b613b20d 84 #define ST7735_DARKGREEN 0x03E0
rolo644u 0:3015b613b20d 85 #define ST7735_MAROON 0x7800
rolo644u 0:3015b613b20d 86 #define ST7735_PURPLE 0x780F
rolo644u 0:3015b613b20d 87 #define ST7735_OLIVE 0x7BE0
rolo644u 0:3015b613b20d 88 #define ST7735_LIGHTGREY 0xC618
rolo644u 0:3015b613b20d 89 #define ST7735_DARKGREY 0x7BEF
rolo644u 0:3015b613b20d 90 #define ST7735_BROWN 0xFD20
rolo644u 0:3015b613b20d 91 #define ST7735_PINK 0xF81F
rolo644u 0:3015b613b20d 92
rolo644u 0:3015b613b20d 93 #define MADCTL_MY 0x80
rolo644u 0:3015b613b20d 94 #define MADCTL_MX 0x40
rolo644u 0:3015b613b20d 95 #define MADCTL_MV 0x20
rolo644u 0:3015b613b20d 96 #define MADCTL_ML 0x10
rolo644u 0:3015b613b20d 97 #define MADCTL_RGB 0x00
rolo644u 0:3015b613b20d 98 #define MADCTL_BGR 0x08
rolo644u 0:3015b613b20d 99 #define MADCTL_MH 0x04
rolo644u 0:3015b613b20d 100
rolo644u 0:3015b613b20d 101 class ST7735
rolo644u 0:3015b613b20d 102 {
rolo644u 0:3015b613b20d 103
rolo644u 0:3015b613b20d 104 public:
rolo644u 0:3015b613b20d 105 /** Create a ST7735 object connected to the specified pins
rolo644u 0:3015b613b20d 106 *
rolo644u 0:3015b613b20d 107 * @param ce Pin connected to chip enable
rolo644u 0:3015b613b20d 108 * @param dc Pin connected to data/command select
rolo644u 0:3015b613b20d 109 * @param mosi Pin connected to data input
rolo644u 0:3015b613b20d 110 * @param sclk Pin connected to serial clock
rolo644u 0:3015b613b20d 111 *
rolo644u 2:291ac9fb4d64 112 * Reset pin from LCD module is connected to reset pin microcontroller
rolo644u 0:3015b613b20d 113 */
rolo644u 2:291ac9fb4d64 114
rolo644u 0:3015b613b20d 115 ST7735(PinName cePin, PinName dcPin, PinName mosiPin, PinName sclkPin);
rolo644u 2:291ac9fb4d64 116
rolo644u 0:3015b613b20d 117 private:
rolo644u 0:3015b613b20d 118 void writecommand(char c);
rolo644u 0:3015b613b20d 119 void writedata(char c);
rolo644u 0:3015b613b20d 120 void initSPI();
rolo644u 0:3015b613b20d 121 void commonInit(const uint8_t *cmdList);
rolo644u 0:3015b613b20d 122 void commandList(const uint8_t *addr);
rolo644u 2:291ac9fb4d64 123 void pushColor(uint16_t color);
rolo644u 2:291ac9fb4d64 124
rolo644u 0:3015b613b20d 125 public:
rolo644u 0:3015b613b20d 126 uint8_t tabcolor;
rolo644u 0:3015b613b20d 127 uint8_t colstart, rowstart; // some displays need this changed
rolo644u 0:3015b613b20d 128 void initB();
rolo644u 0:3015b613b20d 129 void initR(uint8_t options);
rolo644u 0:3015b613b20d 130 void setRotation(uint8_t m);
rolo644u 0:3015b613b20d 131 void fillScreen(uint16_t color);
rolo644u 0:3015b613b20d 132 void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
rolo644u 0:3015b613b20d 133 void setAddrWindow(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1);
rolo644u 0:3015b613b20d 134 void invertDisplay(bool i);
rolo644u 0:3015b613b20d 135 void drawPixel(int16_t x, int16_t y, uint16_t color);
rolo644u 0:3015b613b20d 136 void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
rolo644u 0:3015b613b20d 137 void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
rolo644u 0:3015b613b20d 138 uint16_t Color565(uint8_t r, uint8_t g, uint8_t b);
rolo644u 0:3015b613b20d 139 void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
rolo644u 0:3015b613b20d 140 void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
rolo644u 0:3015b613b20d 141 void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
rolo644u 0:3015b613b20d 142 void drawCircleHelper( int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color);
rolo644u 0:3015b613b20d 143 void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
rolo644u 0:3015b613b20d 144 void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color);
rolo644u 0:3015b613b20d 145 void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
rolo644u 0:3015b613b20d 146 void fillTriangle ( int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
rolo644u 0:3015b613b20d 147 void drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color);
rolo644u 0:3015b613b20d 148 void fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color);
rolo644u 0:3015b613b20d 149 void setCursor(int16_t x, int16_t y);
rolo644u 0:3015b613b20d 150 void setTextSize(uint8_t s);
rolo644u 0:3015b613b20d 151 void setTextColor(uint16_t c);
rolo644u 0:3015b613b20d 152 void setTextColor(uint16_t c, uint16_t b);
rolo644u 0:3015b613b20d 153 void setTextWrap(bool w);
rolo644u 0:3015b613b20d 154 uint8_t getRotation(void);
rolo644u 0:3015b613b20d 155 void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
rolo644u 0:3015b613b20d 156 int drawUnicode(unsigned int uniCode, int x, int y, int font);
rolo644u 0:3015b613b20d 157 int drawNumber(long long_num,int poX, int poY, int font);
rolo644u 0:3015b613b20d 158 int drawChar(char c, int x, int y, int font);
rolo644u 0:3015b613b20d 159 int drawString(char *string, int poX, int poY, int font);
rolo644u 0:3015b613b20d 160 int drawCentreString(char *string, int dX, int poY, int font);
rolo644u 0:3015b613b20d 161 int drawRightString(char *string, int dX, int poY, int font);
rolo644u 0:3015b613b20d 162 int drawFloat(float floatNumber, int decimal, int poX, int poY, int font);
rolo644u 0:3015b613b20d 163
rolo644u 0:3015b613b20d 164
rolo644u 0:3015b613b20d 165
rolo644u 0:3015b613b20d 166 private:
rolo644u 0:3015b613b20d 167 SPI* spi;
rolo644u 0:3015b613b20d 168 DigitalOut* ce;
rolo644u 0:3015b613b20d 169 DigitalOut* rst;
rolo644u 0:3015b613b20d 170 DigitalOut* dc;
rolo644u 0:3015b613b20d 171
rolo644u 0:3015b613b20d 172 protected:
rolo644u 0:3015b613b20d 173 static const int16_t WIDTH = ST7735_TFTWIDTH; // This is the 'raw' display w/h - never changes
rolo644u 0:3015b613b20d 174 static const int16_t HEIGHT = ST7735_TFTHEIGHT;
rolo644u 0:3015b613b20d 175 int16_t _width, _height; // Display w/h as modified by current rotation
rolo644u 0:3015b613b20d 176 uint16_t textcolor, textbgcolor;
rolo644u 1:6a6d8d2b19f1 177 uint8_t textsize;
rolo644u 1:6a6d8d2b19f1 178 uint8_t rotation;
rolo644u 0:3015b613b20d 179 bool wrap; // If set, 'wrap' text at right edge of display
rolo644u 0:3015b613b20d 180 int16_t cursor_x;
rolo644u 0:3015b613b20d 181 int16_t cursor_y;
rolo644u 0:3015b613b20d 182 };
rolo644u 0:3015b613b20d 183
rolo644u 0:3015b613b20d 184 #endif