/*******************************************************************************
Start of Arduino_GFX setting
******************************************************************************/
#include <Arduino_GFX_Library.h>
/*******************************************************************************
End of Arduino_GFX setting
******************************************************************************/
#define TFT_CS 15 // GFX_NOT_DEFINED for display without CS pin
#define TFT_DC 2 // GFX_NOT_DEFINED for display without DC pin (9-bit SPI)
#define TFT_RST -1
Arduino_DataBus *bus = new Arduino_ESP32SPI(TFT_DC, TFT_CS, 18 /* SCK */, 23 /* MOSI */, GFX_NOT_DEFINED /* MISO */, VSPI /* spi_num */);
Arduino_GFX *tft = new Arduino_ILI9341(bus, TFT_RST, 1 /* rotation */, false /* IPS */);
int16_t CY = 120;
int16_t CX = 160;
uint8_t R = 100;
int8_t now_min = 10;
int8_t now_hour = 1;
int8_t seconds = 0;
uint16_t c_analog_back = NAVY;
uint16_t c_analog_minute = WHITE;
uint16_t c_analog_hour = YELLOW;
void do_draw_h_m(int16_t &CX, int16_t &CY, uint8_t &R);
//SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
void setup(void) {
Serial.begin(115200);
// Init Display
if (!tft->begin()) {
Serial.println("tft->begin() failed!");
}
tft->fillScreen(BLACK);
do_draw_clock(CX, CY, R);
}
//LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
void loop() {
seconds += 1;
if (seconds == 60) {
seconds = seconds - 60;
now_min += 1;
}
if (now_min == 60) {
now_min = 0;
now_hour += 1;
}
if (now_hour == 24) {
now_hour = 1;
}
do_draw_h_m(CX, CY, R);
delay(100); //test
}
//=============================================================
// do_show_min()
//=============================================================
void do_show_min(int16_t &CX, int16_t &CY, uint8_t &R, byte num, bool show) {
uint16_t color;
if (show)
color = c_analog_minute;
else
color = c_analog_back;
float x, y, x1, y1, x3, y3, x4, y4, Am;
float alpha = (6 * num + 270) * 0.0175;
Am = (now_min * 6);
/*
x3 = 10 * sin((Am + 8) * 0.0175);
y3 = 10 * -cos((Am + 8) * 0.0175);
x4 = 10 * sin((Am - 8) * 0.0175);
y4 = 10 * -cos((Am - 8) * 0.0175);
*/
x = CX + (R * 0.1) * cos(alpha);
y = CY + (R * 0.1) * sin(alpha);
x1 = CX + (R * 0.9) * cos(alpha);
y1 = CY + (R * 0.9) * sin(alpha);
y3 = CX * sin((Am + 8) * 0.0175);
x3 = CY * -cos((Am + 8) * 0.0175);
y4 = CX * sin((Am - 8) * 0.0175);
x4 = CY * -cos((Am - 8) * 0.0175);
tft->drawLine(x, y, x3, y3, color);
tft->drawLine(x3, y3, x1, y1, color);
tft->drawLine(x, y, x4, y4, color);
tft->drawLine(x4, y4, x1, y1, color);
/*
tft->drawLine(x, y, x1, y1, color);
tft->drawLine(x + 1, y, x1, y1, color);
tft->drawLine(x - 1, y, x1, y1, color);
*/
}
//=============================================================
// do_show_hour()
//=============================================================
void do_show_hour(int16_t &CX, int16_t &CY, uint8_t &R, byte h, byte m, bool show) {
float x, y;
uint16_t color;
if (show)
color = c_analog_hour;
else
color = c_analog_back;
float alpha = (30 * h + (m / 2) + 270) * 0.0175;
x = CX + (R * 0.5) * cos(alpha);
y = CY + (R * 0.5) * sin(alpha);
//hhhhhhhhh
tft->drawLine(CX, CY, x, y, color);
tft->drawLine(CX + 1, CY, x, y, color);
tft->drawLine(CX - 1, CY, x, y, color);
}
//=============================================================
// do_draw_clock()
//=============================================================
void do_draw_clock(int16_t &CX, int16_t &CY, uint8_t &R) {
float alpha = 0;
float x, y;
int color;
byte rr;
//fill circ
tft->fillCircle(CX, CY, R, c_analog_back);
for (int i = 1; i < 3; i++) tft->drawCircle(CX, CY, R + i, DARKGREY);
//point
for (int i = 0; i < 12; i++) {
alpha = (30 * i + 270) * 0.0175;
x = CX + (R + 3) * cos(alpha);
y = CY + (R + 3) * sin(alpha);
if (i % 3 == 0) {
color = YELLOW;
rr = 3;
} else {
color = CYAN;
rr = 2;
}
tft->fillCircle(x, y, rr, color);
}
}
//---------------------------------------------
void do_draw_h_m(int16_t &CX, int16_t &CY, uint8_t &R) {
//static unsigned long next_mill=0;
static byte last_min = 0;
static byte last_hour = 0;
// static byte a_m, a_ch;
if (last_min == now_min) {
return;
}
do_show_min(CX, CY, R, last_min, false);
do_show_min(CX, CY, R, now_min, true);
//////////////hour
do_show_hour(CX, CY, R, last_hour, last_min, false);
do_show_hour(CX, CY, R, now_hour, now_min, true);
last_min = now_min;
last_hour = now_hour;
}