Original TFT project

Dependencies:   mbed Adafruit_ST7735 Adafruit_GFX

Committer:
tgrosch
Date:
Thu Oct 14 01:52:19 2021 +0000
Revision:
0:476e21f8f23a
Original;

Who changed what in which revision?

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