// #include <SPI.h> // contained in GFX and ILI9341 files
// https://learn.adafruit.com/adafruit-gfx-graphics-library/graphics-primitives
#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_ILI9341.h> // https://github.com/adafruit/Adafruit_ILI9341
#define TFT_RST 8
#define TFT_DC 9
#define TFT_CS 10
#define TFT_MOSI 11
#define TFT_MISO 12
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define TFT_WIDTH 240
#define TFT_HEIGHT 320
#define TFT_MIDDLE TFT_WIDTH / 2
#define TFT_CENTER TFT_HEIGHT / 2
int rowHeight = 8, colWidth = 33, row = 3, col = 3, chr = 0;
int sensorX = A0, sensorY = A1, traceX, traceY;
unsigned long timer, tracetimer, timeout = 100, tracetimeout = 5; // refresh trace and potentiometers
#define RED 0xF800 // 565 0b 11111 000000 00000 // https://rgbcolorpicker.com/565
#define ORG 0xFC00 // 565 0b 11111 100000 00000
#define YEL 0xFFE0 // 565 0b 11111 111111 00000
#define OLV 0x7BE0 // 565 0b 01111 011111 00000
#define LGN 0x07E0 // 565 0b 00000 111111 00000
#define DGN 0x0780 // 565 0b 00000 011110 00000
#define GRN 0x03E0 // 565 0b 00000 011111 00000
#define CYN 0x07FF // 565 0b 00000 111111 11111
#define DCY 0x03EF // 565 0b 00000 011111 01111
#define BLU 0x001F // 565 0b 00000 000000 11111
#define DBL 0x000F // 565 0b 00000 000000 01111
#define MRN 0x7800 // 565 0b 01111 000000 00000
#define MAG 0xF81F // 565 0b 11111 000000 11111
#define PNK 0xFC18 // 565 0b 11111 100000 11000
#define WHT 0xFFFF // 565 0b 11111 111111 11111
#define LGY 0xC618 // 565 0b 11000 110000 11000
#define GRY 0x8410 // 565 0b 10000 100000 10000
#define DGY 0x4208 // 565 0b 01000 010000 01000
#define GRY 0xC618 // 565 0b 11000 110000 11000
#define BLK 0x0000 // 565 0b 00000 000000 00000
void setup() {
Serial.begin(115200);
tft.begin();
tft.setRotation(0); // 0 = none, 1 = 90CW, 2 = 180, 3 = 270CW
textCharacters();
textDraw();
textSize();
graphics();
SIN();
// tft.invertDisplay(1);
// diagnostic();
// appleiie();
// c64();
// setupDisplay();
// changeTextSize();
// changeTextColor();
// extendedASCII();
}
void loop() {
if (millis() - tracetimer > tracetimeout) {
tracetimer = millis();
updateTrace(); // trace
}
if (millis() - timer > timeout) {
timer = millis();
readSensors();
}
}
void SIN() {
int period = 9;
for (int i = 0 ; i < ILI9341_TFTHEIGHT; i++) {
tft.drawPixel((2 + i), (ILI9341_TFTWIDTH / 2 - (90 * sin(period * i * PI / 180))), ILI9341_YELLOW);
}
}
void updateTrace() {
int color = random(0xffff);
if (traceX > TFT_WIDTH) {
traceX = 0;
traceY = 0;
}
if (traceY > TFT_HEIGHT / 8) {
tft.drawLine(traceX, traceY, traceX, TFT_HEIGHT * 0.25, color);
traceY = 0;
}
tft.drawPixel(traceX, TFT_HEIGHT * 0.25 - traceY, color);
traceX++;
traceY++;
}
void readSensors() {
int potX = map(analogRead(sensorX), 0, 1023, 0, 40);
int potY = map(analogRead(sensorY), 0, 1023, 0, 40);
tft.drawCircle(TFT_WIDTH * .5 - potX, TFT_HEIGHT * 0.9 - potY, 5, WHT);
}
void textCharacters() {
tft.setCursor(col, row);
tft.setTextSize(1);
tft.setTextColor(GRN);
// tft.setTextColor(uint16_t color, uint16_t bgcolor);
// tft.setTextWrap(w); // boolean
for (int i = 0; i < 256; i++) {
if (chr < 10) tft.print(" "); // space for tens
if (chr < 100) tft.print(" "); // space for hundreds
tft.print(chr);
tft.print(" ");
tft.write(chr);
row += rowHeight;
if (row > TFT_HEIGHT - rowHeight) {
row = 3;
col += colWidth;
}
chr++;
tft.setCursor(col, row);
}
}
void textSize() {
tft.setCursor(0, TFT_HEIGHT / 3);
for (int i = 1; i < 6; i++) {
switch (i) {
case 1: tft.setTextColor(ORG); break;
case 2: tft.setTextColor(MAG); break;
case 3: tft.setTextColor(CYN); break;
case 4: tft.setTextColor(GRY); break;
case 5: tft.setTextColor(WHT); break;
}
tft.setTextSize(i);
tft.print("Size");
tft.println(i);
}
}
void textDraw() {
tft.drawChar(205, 195, 'A', BLK, GRY, 5);
tft.drawChar(205, 235, 'B', BLK, GRY, 5);
tft.drawChar(205, 275, 'C', BLK, GRY, 5);
}
void graphics() {
// tft.drawPoint(x, y, c);
tft.drawLine(0, TFT_HEIGHT, TFT_WIDTH, 0, RED);
tft.drawLine(0, 0, TFT_WIDTH, TFT_HEIGHT, ORG);
// tft.drawEllipse(x, y, rx, ry, c);
// tft.fillEllipse(x, y, rx, ry, c);
tft.drawRect(0, 0, TFT_WIDTH, TFT_HEIGHT, YEL);
// tft.fillRect(x0, y0, x1, y1, c);
tft.drawCircle(TFT_MIDDLE, TFT_CENTER - TFT_MIDDLE, TFT_MIDDLE, WHT);
tft.drawCircle(TFT_MIDDLE, TFT_CENTER, TFT_MIDDLE, BLU);
tft.drawCircle(TFT_MIDDLE, TFT_CENTER + TFT_MIDDLE, TFT_MIDDLE, WHT);
// tft.fillCircle(x, y, r, c);
tft.drawRoundRect(20, 20, TFT_WIDTH - 40, TFT_HEIGHT - 40, 20, CYN); // 20 = corner radius
// tft.fillRoundRect(x, y, w, h, r, c);
tft.drawTriangle(TFT_MIDDLE, TFT_CENTER - TFT_MIDDLE, 17, 220, 223, 220, YEL);
tft.drawTriangle(TFT_MIDDLE, TFT_CENTER + TFT_MIDDLE / 2, 67, 133, 173, 133, YEL);
// tft.fillTriangle(x0, y0, x1, y1, x2, y2, color);
}
void extendedASCII() {
int j = 0;
for (int i = 127; i < 288; i++) {
if (i < 100) tft.print(" "); // space padding
if (i < 10) tft.print(" "); // space padding
tft.print(i);
tft.print("=");
if (i == 266) // linefeed
tft.print(" ");
else
tft.print(char(i));
if (j++ == 5) {
j = 0;
tft.print("\n");
} else {
tft.print(" ");
}
}
}
void changeTextColor() {
tft.setTextColor(DGN); // 565 DGN 0x0780
tft.setCursor(0, 0);
tft.setTextSize(2);
tft.print("setTextColor(0x0780)");
tft.setCursor(20, 0);
tft.setTextSize(2);
for (long i = 0; i < 0x1f; i++) {
for (long j = 0; j < 0x3f; j++) {
for (long k = 0; k < 0x1f; k++) {
tft.setCursor(0, 20);
tft.setTextColor(i << 11 & j << 5 && k);
tft.print("setTextColor()");
}
}
}
}
void changeTextSize() {
tft.setCursor(0, 0);
tft.setTextSize(1);
tft.print("setTextSize(1)");
tft.setCursor(0, 25);
tft.setTextSize(2);
tft.print("Size = 2");
tft.setCursor(0, 75);
tft.setTextSize(3);
tft.print("Size = 3");
tft.setCursor(0, 150);
tft.setTextSize(4);
tft.print("Size = 4");
tft.setCursor(0, 250);
tft.setTextSize(5);
tft.print("Size = 5");
}
// void setupDisplay() {
// STARTx.initButton(&tft, 60, 200, 100, 70, WHT, BLU, BLK, "START", 2);
// STARTx.drawButton(true);
// MODE_A.initButton(&tft, 60, 285, 100, 70, WHT, RED, BLK, "A", 2);
// MODE_A.drawButton(true);
// MODE_B.initButton(&tft, 175, 285, 100, 70, WHT, GRN, BLK, "B", 2);
// MODE_B.drawButton(true);
// CUSTOM.initButton(&tft, 175, 200, 100, 70, WHT, YEL, BLK, "CUST", 2);
// CUSTOM.drawButton(true);
// }
void c64() {
tft.fillScreen(BLU);
tft.setTextColor(CYN);
tft.println(" **** COMMODORE 64 BASIC V2 ****");
tft.println(" 64k RAM SYSTEM 38911 BASIC BYTES FREE");
tft.println("\n");
tft.println("10 FOR A = 127 TO 288 STEP 1");
tft.println("20 PRINT A; TAB(2)");
tft.println("30 PRINT \"=\" ");
tft.println("40 PRINT CHR$(A)");
tft.println("50 NEXT");
tft.println("\n");
}
void appleiie() {
tft.fillScreen(0x4208); // dark GRY 0100 0.010 000.0 1000
tft.setTextColor(LGN);
tft.println("\n APPLE ][e\n");
tft.println(" 10 FOR A = 127 TO 288 STEP 1");
tft.println(" 20 PRINT A; TAB(2)");
tft.println(" 30 PRINT \"=\" ");
tft.println(" 40 PRINT CHR$(A)");
tft.println(" 50 NEXT");
tft.println("\n");
}
void diagnostic() { // https://github.com/adafruit/Adafruit_ILI9341/blob/master/Adafruit_ILI9341.h
Serial.begin(115200); delay(500);
int x = tft.readcommand8(ILI9341_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
}