|
| 1 | +/************************************************************************** |
| 2 | + This is a library for several Adafruit displays based on ST77* drivers. |
| 3 | +
|
| 4 | + This example works with the 1.14" TFT breakout |
| 5 | + ----> https://www.adafruit.com/product/4383 |
| 6 | + The 1.3" TFT breakout |
| 7 | + ----> https://www.adafruit.com/product/4313 |
| 8 | + The 1.47" TFT breakout |
| 9 | + ----> https://www.adafruit.com/product/5393 |
| 10 | + The 1.54" TFT breakout |
| 11 | + ----> https://www.adafruit.com/product/3787 |
| 12 | + The 1.69" TFT breakout |
| 13 | + ----> https://www.adafruit.com/product/5206 |
| 14 | + The 1.9" TFT breakout |
| 15 | + ----> https://www.adafruit.com/product/5394 |
| 16 | + The 2.0" TFT breakout |
| 17 | + ----> https://www.adafruit.com/product/4311 |
| 18 | +
|
| 19 | +
|
| 20 | + Check out the links above for our tutorials and wiring diagrams. |
| 21 | + These displays use SPI to communicate, 4 or 5 pins are required to |
| 22 | + interface (RST is optional). |
| 23 | +
|
| 24 | + Adafruit invests time and resources providing this open source code, |
| 25 | + please support Adafruit and open-source hardware by purchasing |
| 26 | + products from Adafruit! |
| 27 | +
|
| 28 | + Written by Limor Fried/Ladyada for Adafruit Industries. |
| 29 | + MIT license, all text above must be included in any redistribution |
| 30 | + **************************************************************************/ |
| 31 | + |
| 32 | +#include <Adafruit_GFX.h> // Core graphics library |
| 33 | +#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 |
| 34 | +#include <SPI.h> |
| 35 | + |
| 36 | +#if defined(ARDUINO_FEATHER_ESP32) // Feather Huzzah32 |
| 37 | + #define TFT_CS 14 |
| 38 | + #define TFT_RST 15 |
| 39 | + #define TFT_DC 32 |
| 40 | + |
| 41 | +#elif defined(ESP8266) |
| 42 | + #define TFT_CS 4 |
| 43 | + #define TFT_RST 16 |
| 44 | + #define TFT_DC 5 |
| 45 | + |
| 46 | +#else |
| 47 | + // For the breakout board, you can use any 2 or 3 pins. |
| 48 | + // These pins will also work for the 1.8" TFT shield. |
| 49 | + #define TFT_CS 10 |
| 50 | + #define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin |
| 51 | + #define TFT_DC 8 |
| 52 | +#endif |
| 53 | + |
| 54 | +// OPTION 1 (recommended) is to use the HARDWARE SPI pins, which are unique |
| 55 | +// to each board and not reassignable. For Arduino Uno: MOSI = pin 11 and |
| 56 | +// SCLK = pin 13. This is the fastest mode of operation and is required if |
| 57 | +// using the breakout board's microSD card. |
| 58 | + |
| 59 | +Fri3dBadge_TFT tft = Fri3dBadge_TFT(); |
| 60 | + |
| 61 | +// OPTION 2 lets you interface the display using ANY TWO or THREE PINS, |
| 62 | +// tradeoff being that performance is not as fast as hardware SPI above. |
| 63 | +//#define TFT_MOSI 11 // Data out |
| 64 | +//#define TFT_SCLK 13 // Clock out |
| 65 | + |
| 66 | +//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); |
| 67 | + |
| 68 | + |
| 69 | +float p = 3.1415926; |
| 70 | + |
| 71 | +void setup(void) { |
| 72 | + Serial.begin(9600); |
| 73 | + Serial.print(F("Hello! ST77xx TFT Test")); |
| 74 | + |
| 75 | + // Use this initializer (uncomment) if using a 1.3" or 1.54" 240x240 TFT: |
| 76 | + tft.init(240, 240); // Init ST7789 240x240 |
| 77 | + |
| 78 | + // OR use this initializer (uncomment) if using a 1.69" 280x240 TFT: |
| 79 | + //tft.init(240, 280); // Init ST7789 280x240 |
| 80 | + |
| 81 | + // OR use this initializer (uncomment) if using a 2.0" 320x240 TFT: |
| 82 | + //tft.init(240, 320); // Init ST7789 320x240 |
| 83 | + |
| 84 | + // OR use this initializer (uncomment) if using a 1.14" 240x135 TFT: |
| 85 | + //tft.init(135, 240); // Init ST7789 240x135 |
| 86 | + |
| 87 | + // OR use this initializer (uncomment) if using a 1.47" 172x320 TFT: |
| 88 | + //tft.init(172, 320); // Init ST7789 172x320 |
| 89 | + |
| 90 | + // OR use this initializer (uncomment) if using a 1.9" 170x320 TFT: |
| 91 | + //tft.init(170, 320); // Init ST7789 170x320 |
| 92 | + |
| 93 | + // SPI speed defaults to SPI_DEFAULT_FREQ defined in the library, you can override it here |
| 94 | + // Note that speed allowable depends on chip and quality of wiring, if you go too fast, you |
| 95 | + // may end up with a black screen some times, or all the time. |
| 96 | + //tft.setSPISpeed(40000000); |
| 97 | + Serial.println(F("Initialized")); |
| 98 | + |
| 99 | + uint16_t time = millis(); |
| 100 | + tft.fillScreen(ST77XX_BLACK); |
| 101 | + time = millis() - time; |
| 102 | + |
| 103 | + Serial.println(time, DEC); |
| 104 | + delay(500); |
| 105 | + |
| 106 | + // large block of text |
| 107 | + tft.fillScreen(ST77XX_BLACK); |
| 108 | + 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. ", ST77XX_WHITE); |
| 109 | + delay(1000); |
| 110 | + |
| 111 | + // tft print function! |
| 112 | + tftPrintTest(); |
| 113 | + delay(4000); |
| 114 | + |
| 115 | + // a single pixel |
| 116 | + tft.drawPixel(tft.width()/2, tft.height()/2, ST77XX_GREEN); |
| 117 | + delay(500); |
| 118 | + |
| 119 | + // line draw test |
| 120 | + testlines(ST77XX_YELLOW); |
| 121 | + delay(500); |
| 122 | + |
| 123 | + // optimized lines |
| 124 | + testfastlines(ST77XX_RED, ST77XX_BLUE); |
| 125 | + delay(500); |
| 126 | + |
| 127 | + testdrawrects(ST77XX_GREEN); |
| 128 | + delay(500); |
| 129 | + |
| 130 | + testfillrects(ST77XX_YELLOW, ST77XX_MAGENTA); |
| 131 | + delay(500); |
| 132 | + |
| 133 | + tft.fillScreen(ST77XX_BLACK); |
| 134 | + testfillcircles(10, ST77XX_BLUE); |
| 135 | + testdrawcircles(10, ST77XX_WHITE); |
| 136 | + delay(500); |
| 137 | + |
| 138 | + testroundrects(); |
| 139 | + delay(500); |
| 140 | + |
| 141 | + testtriangles(); |
| 142 | + delay(500); |
| 143 | + |
| 144 | + mediabuttons(); |
| 145 | + delay(500); |
| 146 | + |
| 147 | + Serial.println("done"); |
| 148 | + delay(1000); |
| 149 | +} |
| 150 | + |
| 151 | +void loop() { |
| 152 | + tft.invertDisplay(true); |
| 153 | + delay(500); |
| 154 | + tft.invertDisplay(false); |
| 155 | + delay(500); |
| 156 | +} |
| 157 | + |
| 158 | +void testlines(uint16_t color) { |
| 159 | + tft.fillScreen(ST77XX_BLACK); |
| 160 | + for (int16_t x=0; x < tft.width(); x+=6) { |
| 161 | + tft.drawLine(0, 0, x, tft.height()-1, color); |
| 162 | + delay(0); |
| 163 | + } |
| 164 | + for (int16_t y=0; y < tft.height(); y+=6) { |
| 165 | + tft.drawLine(0, 0, tft.width()-1, y, color); |
| 166 | + delay(0); |
| 167 | + } |
| 168 | + |
| 169 | + tft.fillScreen(ST77XX_BLACK); |
| 170 | + for (int16_t x=0; x < tft.width(); x+=6) { |
| 171 | + tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color); |
| 172 | + delay(0); |
| 173 | + } |
| 174 | + for (int16_t y=0; y < tft.height(); y+=6) { |
| 175 | + tft.drawLine(tft.width()-1, 0, 0, y, color); |
| 176 | + delay(0); |
| 177 | + } |
| 178 | + |
| 179 | + tft.fillScreen(ST77XX_BLACK); |
| 180 | + for (int16_t x=0; x < tft.width(); x+=6) { |
| 181 | + tft.drawLine(0, tft.height()-1, x, 0, color); |
| 182 | + delay(0); |
| 183 | + } |
| 184 | + for (int16_t y=0; y < tft.height(); y+=6) { |
| 185 | + tft.drawLine(0, tft.height()-1, tft.width()-1, y, color); |
| 186 | + delay(0); |
| 187 | + } |
| 188 | + |
| 189 | + tft.fillScreen(ST77XX_BLACK); |
| 190 | + for (int16_t x=0; x < tft.width(); x+=6) { |
| 191 | + tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color); |
| 192 | + delay(0); |
| 193 | + } |
| 194 | + for (int16_t y=0; y < tft.height(); y+=6) { |
| 195 | + tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color); |
| 196 | + delay(0); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +void testdrawtext(char *text, uint16_t color) { |
| 201 | + tft.setCursor(0, 0); |
| 202 | + tft.setTextColor(color); |
| 203 | + tft.setTextWrap(true); |
| 204 | + tft.print(text); |
| 205 | +} |
| 206 | + |
| 207 | +void testfastlines(uint16_t color1, uint16_t color2) { |
| 208 | + tft.fillScreen(ST77XX_BLACK); |
| 209 | + for (int16_t y=0; y < tft.height(); y+=5) { |
| 210 | + tft.drawFastHLine(0, y, tft.width(), color1); |
| 211 | + } |
| 212 | + for (int16_t x=0; x < tft.width(); x+=5) { |
| 213 | + tft.drawFastVLine(x, 0, tft.height(), color2); |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +void testdrawrects(uint16_t color) { |
| 218 | + tft.fillScreen(ST77XX_BLACK); |
| 219 | + for (int16_t x=0; x < tft.width(); x+=6) { |
| 220 | + tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +void testfillrects(uint16_t color1, uint16_t color2) { |
| 225 | + tft.fillScreen(ST77XX_BLACK); |
| 226 | + for (int16_t x=tft.width()-1; x > 6; x-=6) { |
| 227 | + tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1); |
| 228 | + tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +void testfillcircles(uint8_t radius, uint16_t color) { |
| 233 | + for (int16_t x=radius; x < tft.width(); x+=radius*2) { |
| 234 | + for (int16_t y=radius; y < tft.height(); y+=radius*2) { |
| 235 | + tft.fillCircle(x, y, radius, color); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +void testdrawcircles(uint8_t radius, uint16_t color) { |
| 241 | + for (int16_t x=0; x < tft.width()+radius; x+=radius*2) { |
| 242 | + for (int16_t y=0; y < tft.height()+radius; y+=radius*2) { |
| 243 | + tft.drawCircle(x, y, radius, color); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +void testtriangles() { |
| 249 | + tft.fillScreen(ST77XX_BLACK); |
| 250 | + uint16_t color = 0xF800; |
| 251 | + int t; |
| 252 | + int w = tft.width()/2; |
| 253 | + int x = tft.height()-1; |
| 254 | + int y = 0; |
| 255 | + int z = tft.width(); |
| 256 | + for(t = 0 ; t <= 15; t++) { |
| 257 | + tft.drawTriangle(w, y, y, x, z, x, color); |
| 258 | + x-=4; |
| 259 | + y+=4; |
| 260 | + z-=4; |
| 261 | + color+=100; |
| 262 | + } |
| 263 | +} |
| 264 | + |
| 265 | +void testroundrects() { |
| 266 | + tft.fillScreen(ST77XX_BLACK); |
| 267 | + uint16_t color = 100; |
| 268 | + int i; |
| 269 | + int t; |
| 270 | + for(t = 0 ; t <= 4; t+=1) { |
| 271 | + int x = 0; |
| 272 | + int y = 0; |
| 273 | + int w = tft.width()-2; |
| 274 | + int h = tft.height()-2; |
| 275 | + for(i = 0 ; i <= 16; i+=1) { |
| 276 | + tft.drawRoundRect(x, y, w, h, 5, color); |
| 277 | + x+=2; |
| 278 | + y+=3; |
| 279 | + w-=4; |
| 280 | + h-=6; |
| 281 | + color+=1100; |
| 282 | + } |
| 283 | + color+=100; |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +void tftPrintTest() { |
| 288 | + tft.setTextWrap(false); |
| 289 | + tft.fillScreen(ST77XX_BLACK); |
| 290 | + tft.setCursor(0, 30); |
| 291 | + tft.setTextColor(ST77XX_RED); |
| 292 | + tft.setTextSize(1); |
| 293 | + tft.println("Hello World!"); |
| 294 | + tft.setTextColor(ST77XX_YELLOW); |
| 295 | + tft.setTextSize(2); |
| 296 | + tft.println("Hello World!"); |
| 297 | + tft.setTextColor(ST77XX_GREEN); |
| 298 | + tft.setTextSize(3); |
| 299 | + tft.println("Hello World!"); |
| 300 | + tft.setTextColor(ST77XX_BLUE); |
| 301 | + tft.setTextSize(4); |
| 302 | + tft.print(1234.567); |
| 303 | + delay(1500); |
| 304 | + tft.setCursor(0, 0); |
| 305 | + tft.fillScreen(ST77XX_BLACK); |
| 306 | + tft.setTextColor(ST77XX_WHITE); |
| 307 | + tft.setTextSize(0); |
| 308 | + tft.println("Hello World!"); |
| 309 | + tft.setTextSize(1); |
| 310 | + tft.setTextColor(ST77XX_GREEN); |
| 311 | + tft.print(p, 6); |
| 312 | + tft.println(" Want pi?"); |
| 313 | + tft.println(" "); |
| 314 | + tft.print(8675309, HEX); // print 8,675,309 out in HEX! |
| 315 | + tft.println(" Print HEX!"); |
| 316 | + tft.println(" "); |
| 317 | + tft.setTextColor(ST77XX_WHITE); |
| 318 | + tft.println("Sketch has been"); |
| 319 | + tft.println("running for: "); |
| 320 | + tft.setTextColor(ST77XX_MAGENTA); |
| 321 | + tft.print(millis() / 1000); |
| 322 | + tft.setTextColor(ST77XX_WHITE); |
| 323 | + tft.print(" seconds."); |
| 324 | +} |
| 325 | + |
| 326 | +void mediabuttons() { |
| 327 | + // play |
| 328 | + tft.fillScreen(ST77XX_BLACK); |
| 329 | + tft.fillRoundRect(25, 10, 78, 60, 8, ST77XX_WHITE); |
| 330 | + tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_RED); |
| 331 | + delay(500); |
| 332 | + // pause |
| 333 | + tft.fillRoundRect(25, 90, 78, 60, 8, ST77XX_WHITE); |
| 334 | + tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_GREEN); |
| 335 | + tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_GREEN); |
| 336 | + delay(500); |
| 337 | + // play color |
| 338 | + tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_BLUE); |
| 339 | + delay(50); |
| 340 | + // pause color |
| 341 | + tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_RED); |
| 342 | + tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_RED); |
| 343 | + // play color |
| 344 | + tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_GREEN); |
| 345 | +} |
0 commit comments