graphicstest.ino
1 // SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 /*************************************************** 6 Example graphics test code specifically for Hallowing 7 8 Adafruit invests time and resources providing this open source code, 9 please support Adafruit and open-source hardware by purchasing 10 products from Adafruit! 11 12 Written by Limor Fried/Ladyada for Adafruit Industries. 13 MIT license, all text above must be included in any redistribution 14 ****************************************************/ 15 16 #include <Adafruit_GFX.h> // Core graphics library 17 #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735 18 #include <SPI.h> 19 20 // These are 'hard wired' 21 #define TFT_CS 39 22 #define TFT_RST 37 23 #define TFT_DC 38 24 #define TFT_BACKLIGHT 7 25 26 Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); 27 28 29 float p = 3.1415926; 30 31 void setup(void) { 32 pinMode(TFT_BACKLIGHT, OUTPUT); 33 digitalWrite(TFT_BACKLIGHT, HIGH); 34 35 Serial.begin(9600); 36 Serial.print("Hello! ST77xx TFT Test"); 37 38 // Use this initializer (uncomment) if you're using a 1.44" TFT 39 tft.initR(INITR_144GREENTAB); // initialize a ST7735S chip, black tab 40 41 tft.setRotation(2); 42 43 Serial.println("Initialized"); 44 45 uint16_t time = millis(); 46 tft.fillScreen(ST77XX_BLACK); 47 time = millis() - time; 48 49 Serial.println(time, DEC); 50 delay(500); 51 52 // large block of text 53 tft.fillScreen(ST77XX_BLACK); 54 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); 55 delay(1000); 56 57 // tft print function! 58 tftPrintTest(); 59 delay(4000); 60 61 // a single pixel 62 tft.drawPixel(tft.width()/2, tft.height()/2, ST77XX_GREEN); 63 delay(500); 64 65 // line draw test 66 testlines(ST77XX_YELLOW); 67 delay(500); 68 69 // optimized lines 70 testfastlines(ST77XX_RED, ST77XX_BLUE); 71 delay(500); 72 73 testdrawrects(ST77XX_GREEN); 74 delay(500); 75 76 testfillrects(ST77XX_YELLOW, ST77XX_MAGENTA); 77 delay(500); 78 79 tft.fillScreen(ST77XX_BLACK); 80 testfillcircles(10, ST77XX_BLUE); 81 testdrawcircles(10, ST77XX_WHITE); 82 delay(500); 83 84 testroundrects(); 85 delay(500); 86 87 testtriangles(); 88 delay(500); 89 90 mediabuttons(); 91 delay(500); 92 93 Serial.println("done"); 94 delay(1000); 95 } 96 97 void loop() { 98 tft.invertDisplay(true); 99 delay(500); 100 tft.invertDisplay(false); 101 delay(500); 102 } 103 104 void testlines(uint16_t color) { 105 tft.fillScreen(ST77XX_BLACK); 106 for (int16_t x=0; x < tft.width(); x+=6) { 107 tft.drawLine(0, 0, x, tft.height()-1, color); 108 } 109 for (int16_t y=0; y < tft.height(); y+=6) { 110 tft.drawLine(0, 0, tft.width()-1, y, color); 111 } 112 113 tft.fillScreen(ST77XX_BLACK); 114 for (int16_t x=0; x < tft.width(); x+=6) { 115 tft.drawLine(tft.width()-1, 0, x, tft.height()-1, color); 116 } 117 for (int16_t y=0; y < tft.height(); y+=6) { 118 tft.drawLine(tft.width()-1, 0, 0, y, color); 119 } 120 121 tft.fillScreen(ST77XX_BLACK); 122 for (int16_t x=0; x < tft.width(); x+=6) { 123 tft.drawLine(0, tft.height()-1, x, 0, color); 124 } 125 for (int16_t y=0; y < tft.height(); y+=6) { 126 tft.drawLine(0, tft.height()-1, tft.width()-1, y, color); 127 } 128 129 tft.fillScreen(ST77XX_BLACK); 130 for (int16_t x=0; x < tft.width(); x+=6) { 131 tft.drawLine(tft.width()-1, tft.height()-1, x, 0, color); 132 } 133 for (int16_t y=0; y < tft.height(); y+=6) { 134 tft.drawLine(tft.width()-1, tft.height()-1, 0, y, color); 135 } 136 } 137 138 void testdrawtext(char *text, uint16_t color) { 139 tft.setCursor(0, 0); 140 tft.setTextColor(color); 141 tft.setTextWrap(true); 142 tft.print(text); 143 } 144 145 void testfastlines(uint16_t color1, uint16_t color2) { 146 tft.fillScreen(ST77XX_BLACK); 147 for (int16_t y=0; y < tft.height(); y+=5) { 148 tft.drawFastHLine(0, y, tft.width(), color1); 149 } 150 for (int16_t x=0; x < tft.width(); x+=5) { 151 tft.drawFastVLine(x, 0, tft.height(), color2); 152 } 153 } 154 155 void testdrawrects(uint16_t color) { 156 tft.fillScreen(ST77XX_BLACK); 157 for (int16_t x=0; x < tft.width(); x+=6) { 158 tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color); 159 } 160 } 161 162 void testfillrects(uint16_t color1, uint16_t color2) { 163 tft.fillScreen(ST77XX_BLACK); 164 for (int16_t x=tft.width()-1; x > 6; x-=6) { 165 tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1); 166 tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2); 167 } 168 } 169 170 void testfillcircles(uint8_t radius, uint16_t color) { 171 for (int16_t x=radius; x < tft.width(); x+=radius*2) { 172 for (int16_t y=radius; y < tft.height(); y+=radius*2) { 173 tft.fillCircle(x, y, radius, color); 174 } 175 } 176 } 177 178 void testdrawcircles(uint8_t radius, uint16_t color) { 179 for (int16_t x=0; x < tft.width()+radius; x+=radius*2) { 180 for (int16_t y=0; y < tft.height()+radius; y+=radius*2) { 181 tft.drawCircle(x, y, radius, color); 182 } 183 } 184 } 185 186 void testtriangles() { 187 tft.fillScreen(ST77XX_BLACK); 188 int color = 0xF800; 189 int t; 190 int w = tft.width()/2; 191 int x = tft.height()-1; 192 int y = 0; 193 int z = tft.width(); 194 for(t = 0 ; t <= 15; t++) { 195 tft.drawTriangle(w, y, y, x, z, x, color); 196 x-=4; 197 y+=4; 198 z-=4; 199 color+=100; 200 } 201 } 202 203 void testroundrects() { 204 tft.fillScreen(ST77XX_BLACK); 205 int color = 100; 206 int i; 207 int t; 208 for(t = 0 ; t <= 4; t+=1) { 209 int x = 0; 210 int y = 0; 211 int w = tft.width()-2; 212 int h = tft.height()-2; 213 for(i = 0 ; i <= 16; i+=1) { 214 tft.drawRoundRect(x, y, w, h, 5, color); 215 x+=2; 216 y+=3; 217 w-=4; 218 h-=6; 219 color+=1100; 220 } 221 color+=100; 222 } 223 } 224 225 void tftPrintTest() { 226 tft.setTextWrap(false); 227 tft.fillScreen(ST77XX_BLACK); 228 tft.setCursor(0, 30); 229 tft.setTextColor(ST77XX_RED); 230 tft.setTextSize(1); 231 tft.println("Hello World!"); 232 tft.setTextColor(ST77XX_YELLOW); 233 tft.setTextSize(2); 234 tft.println("Hello World!"); 235 tft.setTextColor(ST77XX_GREEN); 236 tft.setTextSize(3); 237 tft.println("Hello World!"); 238 tft.setTextColor(ST77XX_BLUE); 239 tft.setTextSize(4); 240 tft.print(1234.567); 241 delay(1500); 242 tft.setCursor(0, 0); 243 tft.fillScreen(ST77XX_BLACK); 244 tft.setTextColor(ST77XX_WHITE); 245 tft.setTextSize(0); 246 tft.println("Hello World!"); 247 tft.setTextSize(1); 248 tft.setTextColor(ST77XX_GREEN); 249 tft.print(p, 6); 250 tft.println(" Want pi?"); 251 tft.println(" "); 252 tft.print(8675309, HEX); // print 8,675,309 out in HEX! 253 tft.println(" Print HEX!"); 254 tft.println(" "); 255 tft.setTextColor(ST77XX_WHITE); 256 tft.println("Sketch has been"); 257 tft.println("running for: "); 258 tft.setTextColor(ST77XX_MAGENTA); 259 tft.print(millis() / 1000); 260 tft.setTextColor(ST77XX_WHITE); 261 tft.print(" seconds."); 262 } 263 264 void mediabuttons() { 265 // play 266 tft.fillScreen(ST77XX_BLACK); 267 tft.fillRoundRect(25, 10, 78, 60, 8, ST77XX_WHITE); 268 tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_RED); 269 delay(500); 270 // pause 271 tft.fillRoundRect(25, 90, 78, 60, 8, ST77XX_WHITE); 272 tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_GREEN); 273 tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_GREEN); 274 delay(500); 275 // play color 276 tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_BLUE); 277 delay(50); 278 // pause color 279 tft.fillRoundRect(39, 98, 20, 45, 5, ST77XX_RED); 280 tft.fillRoundRect(69, 98, 20, 45, 5, ST77XX_RED); 281 // play color 282 tft.fillTriangle(42, 20, 42, 60, 90, 40, ST77XX_GREEN); 283 }