TFT graphic test program (ST7735) Using 1.8" Adafruit TFT Shield with Joystick and microSD v1.0 Verified by WIZwiki-W7500 platform

Dependencies:   Adafruit_GFX Adafruit_ST7735 mbed

Dependents:   IHM_Grafico_PetFinder

Committer:
justinkim
Date:
Thu Jul 30 22:58:37 2015 +0000
Revision:
1:8742c832cb43
Parent:
0:b0981712db9b
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:b0981712db9b 1 #include "mbed.h"
justinkim 0:b0981712db9b 2 #include "Adafruit_ST7735.h"
justinkim 0:b0981712db9b 3
justinkim 0:b0981712db9b 4 Adafruit_ST7735 tft(D11, D12, D13, D10, D8, D9); // MOSI, MISO, SCLK, SSEL, TFT_DC, TFT_RST
justinkim 0:b0981712db9b 5
justinkim 0:b0981712db9b 6 void testlines(uint16_t color);
justinkim 0:b0981712db9b 7 void testfastlines(uint16_t color1, uint16_t color2);
justinkim 0:b0981712db9b 8 void testdrawrects(uint16_t color);
justinkim 0:b0981712db9b 9 void testfillrects(uint16_t color1, uint16_t color2);
justinkim 0:b0981712db9b 10 void testfillcircles(uint8_t radius, uint16_t color);
justinkim 0:b0981712db9b 11 void testdrawcircles(uint8_t radius, uint16_t color);
justinkim 0:b0981712db9b 12 void testtriangles(void);
justinkim 0:b0981712db9b 13 void testroundrects(void);
justinkim 0:b0981712db9b 14 void mediabuttons(void);
justinkim 0:b0981712db9b 15 void testdrawtext(char *text, uint16_t color);
justinkim 0:b0981712db9b 16 void tftPrintTest(void);
justinkim 0:b0981712db9b 17
justinkim 0:b0981712db9b 18 int main(void)
justinkim 0:b0981712db9b 19 {
justinkim 0:b0981712db9b 20 // Use this initializer if you're using a 1.8" TFT
justinkim 0:b0981712db9b 21 tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab
justinkim 0:b0981712db9b 22
justinkim 0:b0981712db9b 23 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 24 testdrawtext("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla sed malesuada et, malesuada sit amet turpis. Sed porttitor neque ut ante pretium vitae malesuada nunc bibendum. Nullam aliquet ultrices massa eu hendrerit. Ut sed nisi lorem. In vestibulum purus a tortor imperdiet posuere. ", ST7735_WHITE);
justinkim 0:b0981712db9b 25 wait_ms(1000);
justinkim 0:b0981712db9b 26
justinkim 0:b0981712db9b 27 // tft print function!
justinkim 0:b0981712db9b 28 tftPrintTest();
justinkim 0:b0981712db9b 29 wait_ms(1000);
justinkim 0:b0981712db9b 30
justinkim 0:b0981712db9b 31 tft.drawPixel(tft.width()/2, tft.height()/2, ST7735_GREEN);
justinkim 0:b0981712db9b 32 wait_ms(500);
justinkim 0:b0981712db9b 33
justinkim 0:b0981712db9b 34 testlines(ST7735_YELLOW);
justinkim 0:b0981712db9b 35 wait_ms(500);
justinkim 0:b0981712db9b 36
justinkim 0:b0981712db9b 37 testfastlines(ST7735_RED, ST7735_BLUE);
justinkim 0:b0981712db9b 38 wait_ms(500);
justinkim 0:b0981712db9b 39
justinkim 0:b0981712db9b 40 testdrawrects(ST7735_GREEN);
justinkim 0:b0981712db9b 41 wait_ms(500);
justinkim 0:b0981712db9b 42
justinkim 0:b0981712db9b 43 testfillrects(ST7735_YELLOW, ST7735_MAGENTA);
justinkim 0:b0981712db9b 44 wait_ms(500);
justinkim 0:b0981712db9b 45
justinkim 0:b0981712db9b 46 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 47 testfillcircles(10, ST7735_BLUE);
justinkim 0:b0981712db9b 48 testdrawcircles(10, ST7735_WHITE);
justinkim 0:b0981712db9b 49 wait_ms(500);
justinkim 0:b0981712db9b 50
justinkim 0:b0981712db9b 51 testroundrects();
justinkim 0:b0981712db9b 52 wait_ms(500);
justinkim 0:b0981712db9b 53
justinkim 0:b0981712db9b 54 testtriangles();
justinkim 0:b0981712db9b 55 wait_ms(500);
justinkim 0:b0981712db9b 56
justinkim 0:b0981712db9b 57 mediabuttons();
justinkim 0:b0981712db9b 58 wait_ms(500);
justinkim 0:b0981712db9b 59
justinkim 0:b0981712db9b 60 while(1)
justinkim 0:b0981712db9b 61 {
justinkim 0:b0981712db9b 62 tft.invertDisplay(true);
justinkim 0:b0981712db9b 63 wait_ms(500);
justinkim 0:b0981712db9b 64 tft.invertDisplay(false);
justinkim 0:b0981712db9b 65 wait_ms(500);
justinkim 0:b0981712db9b 66 }
justinkim 0:b0981712db9b 67 }
justinkim 0:b0981712db9b 68
justinkim 0:b0981712db9b 69 void testlines(uint16_t color) {
justinkim 0:b0981712db9b 70 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 71 for (int16_t x=0; x < tft.width(); x+=6) {
justinkim 0:b0981712db9b 72 tft.drawLine(0, 0, x, tft.height()-1, color);
justinkim 0:b0981712db9b 73 }
justinkim 0:b0981712db9b 74 for (int16_t y=0; y < tft.height(); y+=6) {
justinkim 0:b0981712db9b 75 tft.drawLine(0, 0, tft.width()-1, y, color);
justinkim 0:b0981712db9b 76 }
justinkim 0:b0981712db9b 77
justinkim 0:b0981712db9b 78 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 79 for (int16_t x=0; x < tft.width(); x+=6) {
justinkim 0:b0981712db9b 80 tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color);
justinkim 0:b0981712db9b 81 }
justinkim 0:b0981712db9b 82 for (int16_t y=0; y < tft.height(); y+=6) {
justinkim 0:b0981712db9b 83 tft.drawLine(tft.width()-1, 0, 0, y, color);
justinkim 0:b0981712db9b 84 }
justinkim 0:b0981712db9b 85
justinkim 0:b0981712db9b 86 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 87 for (int16_t x=0; x < tft.width(); x+=6) {
justinkim 0:b0981712db9b 88 tft.drawLine(0, tft.height()-1, x, 0, color);
justinkim 0:b0981712db9b 89 }
justinkim 0:b0981712db9b 90 for (int16_t y=0; y < tft.height(); y+=6) {
justinkim 0:b0981712db9b 91 tft.drawLine(0, tft.height()-1, tft.width()-1, y, color);
justinkim 0:b0981712db9b 92 }
justinkim 0:b0981712db9b 93
justinkim 0:b0981712db9b 94 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 95 for (int16_t x=0; x < tft.width(); x+=6) {
justinkim 0:b0981712db9b 96 tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color);
justinkim 0:b0981712db9b 97 }
justinkim 0:b0981712db9b 98 for (int16_t y=0; y < tft.height(); y+=6) {
justinkim 0:b0981712db9b 99 tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color);
justinkim 0:b0981712db9b 100 }
justinkim 0:b0981712db9b 101 }
justinkim 0:b0981712db9b 102
justinkim 0:b0981712db9b 103 void testfastlines(uint16_t color1, uint16_t color2) {
justinkim 0:b0981712db9b 104 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 105 for (int16_t y=0; y < tft.height(); y+=5) {
justinkim 0:b0981712db9b 106 tft.drawFastHLine(0, y, tft.width(), color1);
justinkim 0:b0981712db9b 107 }
justinkim 0:b0981712db9b 108 for (int16_t x=0; x < tft.width(); x+=5) {
justinkim 0:b0981712db9b 109 tft.drawFastVLine(x, 0, tft.height(), color2);
justinkim 0:b0981712db9b 110 }
justinkim 0:b0981712db9b 111 }
justinkim 0:b0981712db9b 112
justinkim 0:b0981712db9b 113 void testdrawrects(uint16_t color) {
justinkim 0:b0981712db9b 114 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 115 for (int16_t x=0; x < tft.width(); x+=6) {
justinkim 0:b0981712db9b 116 tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color);
justinkim 0:b0981712db9b 117 }
justinkim 0:b0981712db9b 118 }
justinkim 0:b0981712db9b 119
justinkim 0:b0981712db9b 120 void testfillrects(uint16_t color1, uint16_t color2) {
justinkim 0:b0981712db9b 121 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 122 for (int16_t x=tft.width()-1; x > 6; x-=6) {
justinkim 0:b0981712db9b 123 tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
justinkim 0:b0981712db9b 124 tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
justinkim 0:b0981712db9b 125 }
justinkim 0:b0981712db9b 126 }
justinkim 0:b0981712db9b 127
justinkim 0:b0981712db9b 128 void testfillcircles(uint8_t radius, uint16_t color) {
justinkim 0:b0981712db9b 129 for (int16_t x=radius; x < tft.width(); x+=radius*2) {
justinkim 0:b0981712db9b 130 for (int16_t y=radius; y < tft.height(); y+=radius*2) {
justinkim 0:b0981712db9b 131 tft.fillCircle(x, y, radius, color);
justinkim 0:b0981712db9b 132 }
justinkim 0:b0981712db9b 133 }
justinkim 0:b0981712db9b 134 }
justinkim 0:b0981712db9b 135
justinkim 0:b0981712db9b 136 void testdrawcircles(uint8_t radius, uint16_t color) {
justinkim 0:b0981712db9b 137 for (int16_t x=0; x < tft.width()+radius; x+=radius*2) {
justinkim 0:b0981712db9b 138 for (int16_t y=0; y < tft.height()+radius; y+=radius*2) {
justinkim 0:b0981712db9b 139 tft.drawCircle(x, y, radius, color);
justinkim 0:b0981712db9b 140 }
justinkim 0:b0981712db9b 141 }
justinkim 0:b0981712db9b 142 }
justinkim 0:b0981712db9b 143
justinkim 0:b0981712db9b 144 void testtriangles() {
justinkim 0:b0981712db9b 145 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 146 int color = 0xF800;
justinkim 0:b0981712db9b 147 int t;
justinkim 0:b0981712db9b 148 int w = tft.width()/2;
justinkim 0:b0981712db9b 149 int x = tft.height()-1;
justinkim 0:b0981712db9b 150 int y = 0;
justinkim 0:b0981712db9b 151 int z = tft.width();
justinkim 0:b0981712db9b 152 for(t = 0 ; t <= 15; t+=1) {
justinkim 0:b0981712db9b 153 tft.drawTriangle(w, y, y, x, z, x, color);
justinkim 0:b0981712db9b 154 x-=4;
justinkim 0:b0981712db9b 155 y+=4;
justinkim 0:b0981712db9b 156 z-=4;
justinkim 0:b0981712db9b 157 color+=100;
justinkim 0:b0981712db9b 158 }
justinkim 0:b0981712db9b 159 }
justinkim 0:b0981712db9b 160
justinkim 0:b0981712db9b 161 void testroundrects() {
justinkim 0:b0981712db9b 162 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 163 int color = 100;
justinkim 0:b0981712db9b 164 int i;
justinkim 0:b0981712db9b 165 int t;
justinkim 0:b0981712db9b 166 for(t = 0 ; t <= 4; t+=1) {
justinkim 0:b0981712db9b 167 int x = 0;
justinkim 0:b0981712db9b 168 int y = 0;
justinkim 0:b0981712db9b 169 int w = tft.width()-2;
justinkim 0:b0981712db9b 170 int h = tft.height()-2;
justinkim 0:b0981712db9b 171 for(i = 0 ; i <= 16; i+=1) {
justinkim 0:b0981712db9b 172 tft.drawRoundRect(x, y, w, h, 5, color);
justinkim 0:b0981712db9b 173 x+=2;
justinkim 0:b0981712db9b 174 y+=3;
justinkim 0:b0981712db9b 175 w-=4;
justinkim 0:b0981712db9b 176 h-=6;
justinkim 0:b0981712db9b 177 color+=1100;
justinkim 0:b0981712db9b 178 }
justinkim 0:b0981712db9b 179 color+=100;
justinkim 0:b0981712db9b 180 }
justinkim 0:b0981712db9b 181 }
justinkim 0:b0981712db9b 182
justinkim 0:b0981712db9b 183 void mediabuttons() {
justinkim 0:b0981712db9b 184 // play
justinkim 0:b0981712db9b 185 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 186 tft.fillRoundRect(25, 10, 78, 60, 8, ST7735_WHITE);
justinkim 0:b0981712db9b 187 tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_RED);
justinkim 0:b0981712db9b 188 wait_ms(500);
justinkim 0:b0981712db9b 189 // pause
justinkim 0:b0981712db9b 190 tft.fillRoundRect(25, 90, 78, 60, 8, ST7735_WHITE);
justinkim 0:b0981712db9b 191 tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_GREEN);
justinkim 0:b0981712db9b 192 tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_GREEN);
justinkim 0:b0981712db9b 193 wait_ms(500);
justinkim 0:b0981712db9b 194 // play color
justinkim 0:b0981712db9b 195 tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_BLUE);
justinkim 0:b0981712db9b 196 wait_ms(500);
justinkim 0:b0981712db9b 197 // pause color
justinkim 0:b0981712db9b 198 tft.fillRoundRect(39, 98, 20, 45, 5, ST7735_RED);
justinkim 0:b0981712db9b 199 tft.fillRoundRect(69, 98, 20, 45, 5, ST7735_RED);
justinkim 0:b0981712db9b 200 wait_ms(500);
justinkim 0:b0981712db9b 201 // play color
justinkim 0:b0981712db9b 202 tft.fillTriangle(42, 20, 42, 60, 90, 40, ST7735_GREEN);
justinkim 0:b0981712db9b 203 }
justinkim 0:b0981712db9b 204
justinkim 0:b0981712db9b 205 void testdrawtext(char *text, uint16_t color) {
justinkim 0:b0981712db9b 206 tft.setCursor(0, 0);
justinkim 0:b0981712db9b 207 tft.setTextColor(color);
justinkim 0:b0981712db9b 208 tft.setTextWrap(true);
justinkim 0:b0981712db9b 209 tft.printf("%s",text);
justinkim 0:b0981712db9b 210 }
justinkim 0:b0981712db9b 211
justinkim 0:b0981712db9b 212 void tftPrintTest() {
justinkim 0:b0981712db9b 213 tft.setTextWrap(false);
justinkim 0:b0981712db9b 214 tft.fillScreen(ST7735_BLACK);
justinkim 0:b0981712db9b 215 tft.setCursor(0, 30);
justinkim 0:b0981712db9b 216 tft.setTextColor(ST7735_RED);
justinkim 0:b0981712db9b 217 tft.setTextSize(1);
justinkim 0:b0981712db9b 218 tft.printf("Hello World!\r\n");
justinkim 0:b0981712db9b 219 tft.setTextColor(ST7735_YELLOW);
justinkim 0:b0981712db9b 220 tft.setTextSize(2);
justinkim 0:b0981712db9b 221 tft.printf("Hello World!\r\n");
justinkim 0:b0981712db9b 222 tft.setTextColor(ST7735_GREEN);
justinkim 0:b0981712db9b 223 tft.setTextSize(3);
justinkim 0:b0981712db9b 224 tft.printf("Hello World!\r\n");
justinkim 0:b0981712db9b 225 }