#include "Adafruit_ILI9341.h"
#include "Adafruit_GFX.h"
#include "SPI.h"
//#include "lib/draw.h"
void drawCircleInShells(Adafruit_ILI9341 &tft, int16_t x, int16_t y, uint16_t color, int16_t r, int16_t ri);
#define TFT_SCLK 5 // Clock line, "SCK" or "CLK"
#define TFT_MOSI 6 // Master In Slave Out, "SDI", "SDA", "SI", "DIN"
#define TFT_RST 7 // RESET
#define TFT_DC 8 // Data/Command
#define TFT_CS 9 // Chip Select
#define TFT_BL 10 // Backlight
// Cyan
// #define LIGHT GC9A01A_CYAN
// #define DARK GC9A01A_DARKCYAN
// Pink
#define GC9A01A_LIGHT_PINK 0xFC18
#define GC9A01A_PINK 0xFDB8
#define LIGHT GC9A01A_PINK
#define DARK GC9A01A_LIGHT_PINK
// Green
// #define LIGHT GC9A01A_GREEN
// #define DARK GC9A01A_DARKGREEN
// Grey
// #define LIGHT GC9A01A_LIGHTGREY
// #define DARK GC9A01A_DARKGREY
#define WHITE 0xFFFF
#define BLACK 0x0000
//Adafruit_GC9A01A tft(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
// Screen
#define SCREEN_DC 9
#define SCREEN_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(SCREEN_CS, SCREEN_DC);
void setup() {
// Turn on backlight.
// pinMode(TFT_BL, OUTPUT);
// digitalWrite(TFT_BL, HIGH);
// Initialize the display.
tft.begin();
// Get size.
int w = tft.width(), h = tft.height();
// Draw eye color.
drawCircleDownwardsFast(tft, w / 2, h / 2, w * 0.5, DARK);
drawCircleDownwardsFast(tft, w / 2, h / 2, w * 0.4, LIGHT);
// Cover bottom third with black.
tft.fillRect(0, 0.9 * h, w, h, BLACK);
// Draw a white circle in the upper left corner.
drawCircleInShells(tft, w / 3, h / 3, WHITE, w / 16, 0);
// Draws a big pupil in the center.
drawCircleDownwardsFast(tft, w / 2, h / 2, w / 8, WHITE);
// Draw a white circle in the upper left corner.
drawCircleInShells(tft, w * 2 / 3, h * 2 / 3, WHITE, w / 24, 0);
}
void loop(void) {
// Get size.
int w = tft.width(), h = tft.height();
// Wait.
delay(random(2000, 10000));
// Take a random action.
int action = random(3);
if (action == 0) {
// Blink upper circle.
drawCircleInShells(tft, w / 3, h / 3, LIGHT, w / 32, w / 16);
drawCircleInShells(tft, w / 3, h / 3, WHITE, w / 16, w / 32);
} else if (action == 1) {
// Blink lower circle.
drawCircleInShells(tft, w * 2 / 3, h * 2 / 3, LIGHT, 0, w / 24);
delay(random(50, 150));
drawCircleInShells(tft, w * 2 / 3, h * 2 / 3, WHITE, w / 24, 0);
} else {
// Blink center circle.
drawCircleDownwardsFast(tft, w / 2, h / 2, w / 8 + 1, LIGHT);
delay(random(100, 500));
drawCircleUpwardsFast(tft, w / 2, h / 2, w / 8, WHITE);
}
}
void drawCircle(Adafruit_ILI9341 &tft, int16_t x0, int16_t y0, uint16_t color, int16_t r) {
int16_t x = 0;
int16_t y = r;
tft.writePixel(x0, y0 + r, color);
tft.writePixel(x0, y0 - r, color);
tft.writePixel(x0 + r, y0, color);
tft.writePixel(x0 - r, y0, color);
while (x < y) {
if (x * x + y * y >= r * r) {
y--;
} else {
x++;
}
tft.writePixel(x0 + x, y0 + y, color);
tft.writePixel(x0 - x, y0 + y, color);
tft.writePixel(x0 + x, y0 - y, color);
tft.writePixel(x0 - x, y0 - y, color);
tft.writePixel(x0 + y, y0 + x, color);
tft.writePixel(x0 - y, y0 + x, color);
tft.writePixel(x0 + y, y0 - x, color);
tft.writePixel(x0 - y, y0 - x, color);
}
}
void drawCircleInShells(Adafruit_ILI9341 &tft, int16_t x, int16_t y,
uint16_t color, int16_t r, int16_t ri = 0) {
// Draws a circle with a radius of `r`, centered at `(x, y)`, in a spiral
// pattern, starting from radius `ri`. If `ri` is bigger than `r`, then
// the circle will be drawn inward, resulting in an outer circle with the
// specified color and an inner circle with the background color.
tft.startWrite();
if (ri > r) {
while (ri > r) {
drawCircle(tft, x, y, color, ri);
ri--;
}
} else {
while (ri < r) {
drawCircle(tft, x, y, color, ri);
ri++;
}
}
tft.endWrite();
}
void drawCircleDownwardsFast(Adafruit_ILI9341 &tft, int16_t x, int16_t y,
int16_t r, uint16_t color) {
tft.startWrite();
int16_t x0 = 1;
for (int16_t y0 = -r; y0 < 0; y0++) {
while (x0 * x0 + y0 * y0 <= r * r) {
x0++;
}
tft.writeFastHLine(x - x0, y + y0, 2 * x0, color);
}
for (int16_t y0 = 0; y0 < r; y0++) {
while (x0 * x0 + y0 * y0 > r * r) {
x0--;
}
tft.writeFastHLine(x - x0, y + y0, 2 * x0, color);
}
tft.endWrite();
}
void drawCircleUpwardsFast(Adafruit_ILI9341 &tft, int16_t x, int16_t y,
int16_t r, uint16_t color) {
tft.startWrite();
int16_t x0 = 1;
for (int16_t y0 = r; y0 > 0; y0--) {
while (x0 * x0 + y0 * y0 <= r * r) {
x0++;
}
tft.writeFastHLine(x - x0, y + y0, 2 * x0, color);
}
for (int16_t y0 = 0; y0 >= -r; y0--) {
while (x0 * x0 + y0 * y0 > r * r) {
x0--;
}
tft.writeFastHLine(x - x0, y + y0, 2 * x0, color);
}
tft.endWrite();
}