#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
float x = 100;
float y = 100;
float direction = random(1,278);
int speed = 1;
int size = 10;
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
void drawPolygon(uint8_t x, uint8_t y, uint8_t radius, uint8_t sides, double angle, int color)
{
uint8_t x1, y1, x2, y2;
double th, startAngle;
if (sides < 3) { // polygon must be at least 3 sides!
return;
}
if (sides > 31) { // too many sides is slow and just makes a circle anyway...
tft.drawCircle (x, y, radius, color);
}
// we work in radians here
startAngle = PI / 180 * (angle - 90.0); // make 0 degrees straight up
th = startAngle;
x2 = x + round (cos (th) * radius); // get first vertex
y2 = y + round (sin (th) * radius);
for (uint8_t i = 1; i <= sides; i++)
{
x1 = x2; // old vertex is...
y1 = y2; // ...the new startpoint
th = startAngle + (i * TWO_PI) / sides ; // angle of next vertex in radians
x2 = x + round (cos(th) * radius); // get next vertex
y2 = y + round (sin(th) * radius);
tft.drawLine(x1, y1, x2, y2, color); // draw side
}
}
void draw() {
drawPolygon(x, y, size, 5, direction, TFT_RED);
drawPolygon(x, y, size+1, 5, direction, TFT_BLACK);
// tft.drawCircle(x,y,size,TFT_RED);
// tft.drawLine(x, y, 0, 0, TFT_RED);
}
void updateLoc() {
if (x < size+2) {
direction = (180 - direction);
}
if (x > (240 - size+2)) {
direction = (180 - direction);
}
if (y < size+2) {
direction = -direction;
}
if (y > (320 - size+2)) {
direction = -direction;
}
x += cos(direction * 3.1415 / 180) * speed;
y += sin(direction * 3.1415 / 180) * speed;
}
void setup(void) {
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
}
void loop() {
updateLoc();
draw();
}