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