/*
Forum: https://forum.arduino.cc/t/linienstarke-in-ili9341-library/1417355
Wokwi: https://wokwi.com/projects/449348276000025601
ec2021
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "PointerBarClass.h"
constexpr byte TFT_DC {2};
constexpr byte TFT_CS {15};
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
constexpr struct clockData {
uint16_t x {120};
uint16_t y {140};
uint16_t innerR {110};
uint16_t outerR {118};
uint16_t innerMark {102};
uint16_t outerMark {114};
uint16_t secondR {100};
uint16_t minuteR {84};
uint16_t hourR {62};
uint16_t backColor {0x5AEB};
} myClock;
constexpr float ZeroAngle {-90.0};
unsigned long lastUpdate = 0;
static uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
// Get H, M, S from compile time
int hours = (conv2d(__TIME__) + 1) % 24, minutes = conv2d(__TIME__ + 3), seconds = conv2d(__TIME__ + 6);
// Function call to draw a line from (x0,y0) to (x1, y1) in color col
void drawLine(int x0, int y0, int x1, int y1, uint16_t col) {
// Insert the library specific function or routines here:
// The example uses tft.drawLine()
tft.drawLine(x0, y0, x1, y1, col);
};
PointerClass sekundenZeiger(&drawLine),
minutenZeiger(&drawLine),
stundenZeiger(&drawLine);
void setup() {
sekundenZeiger.setZeroAngle(ZeroAngle);
minutenZeiger.setZeroAngle(ZeroAngle);
stundenZeiger.setZeroAngle(ZeroAngle);
sekundenZeiger.setXYRM(myClock.x, myClock.y, myClock.secondR, ERASE);
minutenZeiger.setXYRM( myClock.x, myClock.y, myClock.minuteR, ERASE);
stundenZeiger.setXYRM( myClock.x, myClock.y, myClock.hourR, ERASE);
tft.begin();
tft.setRotation(0);
paintClockFace();
}
void loop() {
if (millis() - lastUpdate >= 1000) {
lastUpdate = millis();
handleTime();
}
}
void handleTime() {
seconds = ++seconds % 60; // Advance second
if (seconds == 0) {
minutes = ++minutes % 60; // Advance minute
if (minutes == 0) {
hours = ++hours % 24; // Advance hour
}
}
float sdeg = seconds * 6;
float mdeg = minutes * 6;
float hdeg = hours * 30 + mdeg * 0.0833333 + sdeg * 0.01666667;
stundenZeiger.draw(hdeg, ILI9341_WHITE);
minutenZeiger.draw(mdeg, ILI9341_WHITE);
sekundenZeiger.draw(sdeg, ILI9341_RED);
tft.fillCircle(myClock.x, myClock.y, 3, ILI9341_RED);
tft.setCursor(70, 280);
tft.setTextSize(2);
tftTime( hours, ':');
tftTime(minutes, ':');
tftTime(seconds, ' ');
}
void tftTime(int val, char c) {
if (val < 10 ) tft.print("0");
tft.print(val);
tft.print(c);
}
void paintClockFace() {
PixelCoord dot, ending;
tft.fillScreen(myClock.backColor);
tft.setTextColor(ILI9341_WHITE, myClock.backColor);
tft.fillCircle(myClock.x, myClock.y, myClock.outerR, ILI9341_BLUE);
tft.fillCircle(myClock.x, myClock.y, myClock.innerR, ILI9341_BLACK);
// Zeichne die Stunden-Marken
for (int i = 0; i < 360; i += 30) {
dot = CoordFromAngleRadius(i, myClock.innerMark);
ending = CoordFromAngleRadius(i, myClock.outerMark);
tft.drawLine(dot.x, dot.y, ending.x, ending.y, ILI9341_BLUE);
}
// Zeichne die Sekunden- und die Viertelstunden-Marken
for (int i = 0; i < 360; i += 6) {
dot = CoordFromAngleRadius(i, myClock.innerMark);
tft.drawPixel(dot.x, dot.y, ILI9341_WHITE);
if (i % 90 == 0) tft.fillCircle(dot.x, dot.y, 2, ILI9341_WHITE);
}
}
PixelCoord CoordFromAngleRadius(float Angle, int rad) {
return getPixelCoord(Angle, myClock.x, myClock.y, rad, ZeroAngle);
}