/*
Clock, date, temperature board use ILI9341 LCD for Arduino-NANO
(c) 2023 by Ralf Nordhausen
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "RTClib.h"
#include "DHT.h"
//
// DHT const and variables
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
String LastHum = " "; // 5
String LastTemp = " "; // 6
//
// clock variables
RTC_DS1307 rtc;
// LCD const and variables
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int16_t lcd_back = 0x7c2f;
int16_t lcd_draw = 0x0;
int16_t DrawColor = 0x0;
int16_t lcd_width = 240;
int16_t lcd_height = 320;
// clock display variables
int16_t SY = 0; // Zifferngröße --> Höhe
int16_t SX = 0; // Zifferngröße --> Breite
int16_t PX = 0; // Kann zum zentrieren erhöht werden
int16_t PY = 0; // Kann zum zentrieren erhöht werden
int16_t WI = 0; // Linienbreite = 1/10 der Höhe
int16_t DI = 0; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand)
int DayLength = 12;
String WeekDay = " "; // 12
String LastDay = " "; // 12
String MainTime = "02:54:56";
String LastDate = " "; // 10
String LastTime = " "; // 8
// messure variable
int16_t Runtime_0 = 0; // system run time
int16_t Runtime_1 = 0; // time to draw MainTime
int16_t Runtime_2 = 0; // time to draw DayOfWeek
int16_t Runtime_3 = 0; // time to draw Huminity
int16_t Runtime_4 = 0; // time to draw Date
int16_t Runtime_5 = 0; // time to draw Temperature
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// clear the screen
//
void clr_scr() {
for (int16_t y = 0; y < lcd_height;) {
tft.drawFastHLine(0, y, lcd_width, lcd_back);
y++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// set the draw color
//
void SetColor(byte ShowDigit) {
if (ShowDigit == 1)
DrawColor = lcd_draw;
else
DrawColor = lcd_back;
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw a horizontal line
//
void DrawHLine(int16_t XA, int16_t YA, int16_t XB) {
/* for (int16_t x = XA; x <= XB; x++) {
tft.drawPixel(x, YA, DrawColor);
}*/
tft.drawFastHLine(XA, YA, XB - XA + 1, DrawColor);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw a vertical line
//
void DrawVLine(int16_t XA, int16_t YA, int16_t YB) {
/* for (int16_t y = YA; y <= YB; y++) {
tft.drawPixel(XA, y, DrawColor);
}*/
tft.drawFastVLine(XA, YA, YB - YA + 1, DrawColor);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw a line
//
void DrawLine(int16_t X1, int16_t Y1, int16_t X2, int16_t Y2) {
int16_t A;
int16_t X;
int16_t Y;
int16_t DX;
int16_t DY;
float M;
if (Y1 == Y2)
DrawHLine(X1, Y1, X2);
else if (X1 == X2)
DrawVLine(X1, Y1, Y2);
else {
DX = abs(X2 - X1);
DY = abs(Y2 - Y1);
if (DX > DY) {
if (X1 > X2) {
A = X1;
X1 = X2;
X2 = A;
A = Y1;
Y1 = Y2;
Y2 = A;
}
M = float(DY) / float(DX);
if (Y2 < Y1)
M = -M;
for (X = X1; X <= X2; X++) {
Y = round(M * (X - X2) + Y2);
tft.drawPixel(X, Y, DrawColor);
}
}
else {
if (Y1 > Y2) {
A = Y1;
Y1 = Y2;
Y2 = A;
A = X1;
X1 = X2;
X2 = A;
}
M = float(DX) / float(DY);
if (X2 < X1)
M = -M;
for (Y = Y1; Y <= Y2; Y++) {
X = round(M * (Y - Y2) + X2);
tft.drawPixel(X, Y, DrawColor);
}
}
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw a circle
//
void Draw_Circle(int16_t XM, int16_t YM, int16_t Radius) { // Bresenham_Circle
int16_t DY;
int16_t Error;
if (XM < 1)
return;
if (YM < 1)
return;
DY = Radius;
Error = 1 - Radius;
for (int16_t DX = 0; DX <= DY; DX++) {
DrawHLine(XM - DY, YM - DX, XM + DY); // OCT 8-1
DrawHLine(XM - DX, YM - DY, XM + DX); // OCT 7-2
DrawHLine(XM - DX, YM + DY, XM + DX); // OCT 6-3
DrawHLine(XM - DY, YM + DX, XM + DY); // OCT 5-4
if (Error < 0)
Error += DX << 1 + 1;
else {
Error += ((DX - DY) << 1);
DY--;
}
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw digits a to g
//
void Draw_DA() {
for (int16_t T = 0; T < WI;) {
DrawHLine(PX + 1 + T, PY + T, PX + SX - 2 - T);
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
void Draw_DB() {
for (int16_t T = 0; T < WI;) {
DrawVLine(PX + SX - 1 - T, PY + 1 + T, PY + (SY >> 1) - 1 - (T >> 1));
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
void Draw_DC() {
for (int16_t T = 0; T < WI;) {
DrawVLine(PX + SX - 1 - T, PY + 1 + (T >> 1) + (SY >> 1), PY + SY - 2 - T);
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
void Draw_DD() {
for (int16_t T = 0; T < WI;) {
DrawHLine(PX + 1 + T, PY + SY - 1 - T, PX + SX - 2 - T);
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
void Draw_DE() {
for (int16_t T = 0; T < WI;) {
DrawVLine(PX + T, PY + 1 + (T >> 1) + (SY >> 1), PY + SY - 2 - T);
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
void Draw_DF() {
for (int16_t T = 0; T < WI;) {
DrawVLine(PX + T, PY + 1 + T, PY + (SY >> 1) - 1 - (T >> 1));
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
void Draw_DG() {
DrawHLine(PX + 2, PY + (SY >> 1), PX + SX - 3);
for (int16_t T = 1; T < (WI >> 1);) {
DrawHLine(PX + 2 + (T << 1), PY - T + (SY >> 1), PX + SX - (T << 1) - 3);
DrawHLine(PX + 2 + (T << 1), PY + T + (SY >> 1), PX + SX - (T << 1) - 3);
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
void Draw_DP() {
SetColor(1);
Draw_Circle(PX + (WI>>1), PY + SY - WI + 1, WI>>1);
/* for (int16_t Y = 0; Y <= WI;) {
DrawHLine(PX, PY + SY - WI + Y, PX + WI);
Y++;
}*/
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw digits g1 to m
//
void Draw_DG1() {
DrawHLine(PX + 2, PY + (SY >> 1), PX + (SX >> 1));
for (int16_t T = 1; T < (WI >> 1);) {
DrawHLine(PX + 2 + (T << 1), PY - T + (SY >> 1), PX + (SX >> 1));
DrawHLine(PX + 2 + (T << 1), PY + T + (SY >> 1), PX + (SX >> 1));
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
void Draw_DG2() {
DrawHLine(PX + 2 + (SX >> 1), PY + (SY >> 1), PX + SX - 3);
for (int16_t T = 1; T < (WI >> 1);) {
DrawHLine(PX + 2 + (SX >> 1), PY - T + (SY >> 1), PX + SX - (T << 1) - 3);
DrawHLine(PX + 2 + (SX >> 1), PY + T + (SY >> 1), PX + SX - (T << 1) - 3);
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
// Links oben \ = $0400
void Draw_DH() {
int16_t XA;
int16_t YA;
int16_t XB;
int16_t YB;
int16_t T;
XA = PX + WI + 1;
YA = PY + WI + 1;
XB = PX + (SX >> 1) - (WI >> 1) + 2; // Normal = + 1
YB = PY + (SY >> 1) - (WI >> 1) - 1;
// Mittlere Linie
DrawLine(XA, YA, XB, YB);
for (int16_t T = 1; T < (WI >> 1);) {
// Y-Schleife oben
DrawLine(XA + T, YA, XB, YB - T);
// Y-Schleife unten
DrawLine(XA, YA + T, XB - T, YB);
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
// Oben │ = $0800 }
void Draw_DI() {
DrawVLine(PX + (SX >> 1), PY + WI + 1, PY + (SY >> 1) - 2);
for (int16_t T = 1; T < (WI >> 1);) {
DrawVLine(PX + (SX >> 1) + T, PY + WI + 1, PY + (SY >> 1) - (T << 2));
DrawVLine(PX + (SX >> 1) - T, PY + WI + 1, PY + (SY >> 1) - (T << 2));
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
// Rechts oben /= $1000
void Draw_DJ() {
int16_t XA;
int16_t YA;
int16_t XB;
int16_t YB;
int16_t T;
XA = PX + SX - WI - 2;
YA = PY + WI + 1;
XB = PX + (SX >> 1) + (WI >> 1) - 2; // Normal = - 1
YB = PY + (SY >> 1) - (WI >> 1) - 1;
// Mittlere Linie
DrawLine(XA, YA, XB, YB);
for (int16_t T = 1; T < (WI >> 1);) {
DrawLine(XA - T, YA, XB, YB - T); // Y-Schleife oben
DrawLine(XA, YA + T, XB + T, YB); // Y-Schleife unten
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
// Links unten / = $2000
void Draw_DK() {
int16_t XA;
int16_t YA;
int16_t XB;
int16_t YB;
int16_t T;
XA = PX + WI + 1;
YA = PY + SY - WI - 2;
XB = PX + (SX >> 1) - (WI >> 1) + 2; // Normal = + 1
YB = PY + (SY >> 1) + (WI >> 1) + 1;
// Mittlere Linie
DrawLine(XA, YA, XB, YB);
for (int16_t T = 1; T < (WI >> 1);) {
DrawLine(XA + T, YA, XB, YB + T); // Y-Schleife oben
DrawLine(XA, YA - T, XB - T, YB); // Y-Schleife unten
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
// Unten │ = $4000
void Draw_DL() {
DrawVLine(PX + (SX >> 1), PY + (SY >> 1) + 2, PY + SY - WI - 2);
for (int16_t T = 1; T < (WI >> 1);) {
DrawVLine(PX + (SX >> 1) + T, PY + (SY >> 1) + 2 + (T << 1), PY + SY - WI - 2);
DrawVLine(PX + (SX >> 1) - T, PY + (SY >> 1) + 2 + (T << 1), PY + SY - WI - 2);
T++;
}
}
/// ─────────────────────────────────────────────────────────────────────────────────────────
// Rechts unten \ = $8000
void Draw_DM() {
int16_t XA;
int16_t YA;
int16_t XB;
int16_t YB;
int16_t T;
XA = PX + SX - WI - 2;
YA = PY + SY - WI - 2;
XB = PX + (SX >> 1) + (WI >> 1) - 2; // Normal = - 1
YB = PY + (SY >> 1) + (WI >> 1) + 1;
//Mittlere Linie
DrawLine(XA, YA, XB, YB);
for (int16_t T = 1; T < (WI >> 1);) {
DrawLine(XA - T, YA, XB, YB + T); // Y-Schleife oben
DrawLine(XA, YA - T, XB + T, YB); // Y-Schleife unten
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw "."
//
void Draw_Point() {
SetColor(1);
Draw_Circle(PX + 2, PY + SY - 1 - WI >> 1, WI >> 1);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw ":"
//
void Draw_Colon() {
SetColor(1);
Draw_Circle(PX + (WI >> 1), PY + (SY >> 1) - (SY >> 3) - WI, WI >> 1);
Draw_Circle(PX + (WI >> 1), PY + (SY >> 1) + (SY >> 3) + WI, WI >> 1);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw "°"
//
void Draw_Celsius() {
int16_t CL;
CL = DI + DI >> 1;
SetColor(1);
Draw_Circle(PX + CL, PY + CL, CL);
SetColor(0);
Draw_Circle(PX + CL, PY + CL, CL >> 1);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw percentage symbol %
//
void Draw_Percent() {
int16_t XA;
int16_t YA;
int16_t XB;
int16_t YB;
int16_t CL;
int16_t T;
CL = DI + DI >> 1;
SetColor(1);
Draw_Circle(PX + CL, PY + CL, CL);
Draw_Circle(PX + SX - CL - 2, PY + SY - 1 - CL, CL);
SetColor(0);
Draw_Circle(PX + CL, PY + CL, CL >> 1);
Draw_Circle(PX + SX - CL - 2, PY + SY - 1 - CL, CL >> 1);
SetColor(1);
XB = PX;
YA = PY;
XA = PX + SX - 2;
YB = PY + SY - 2;
// Mittlere Linie
DrawLine(XA, YA, XB, YB);
for (T = 1; T < WI >> 1;) {
DrawLine(XA - T, YA, XB, YB - T); // Y-Schleife oben
DrawLine(XA, YA + T, XB + T, YB); // Y-Schleife unten
T++;
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw a "c"
//
void Draw_Celsius_Char() {
SetColor(1);
PX = PX - (SX>>2);
SX = SX - WI + 2;
WI = WI - 1;
Draw_DD();
Draw_DE();
WI = WI + 1;
Draw_DG();
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw a 7 segment char (0..9, A..Z) ".", ":", "%"
//
void Draw7Seg(char C, char LC) {
word SevenSeg[37] = {
// 0 1 2 3 4 5 6 7 8 9 .
0x003F, 0x0006, 0x005B, 0x004F, 0x0066, 0x006D, 0x007D, 0x0007, 0x007F, 0x006F, 0x0080,
// A B C D E F G H I J K L M
0x0077, 0x007F, 0x0039, 0x480F, 0x0079, 0x0071, 0x023D, 0x0076, 0x4809, 0x000E, 0x9130, 0x0038, 0x5436,
// N O P Q R S T U V W X Y Z
0x8436, 0x003F, 0x0073, 0x803F, 0x8073, 0x006D, 0x4801, 0x003E, 0x3030, 0xA836, 0xB400, 0x5400, 0x3009
};
word N = 0;
word O = 0;
if ((C >= '0') && (C <= '9')) {
if (LC != ' ')
O = SevenSeg[LC - 48]; // Last char
N = SevenSeg[C - 48]; // Current char
}
else if ((C >= 'A') && (C <= 'Z')) {
if (LC != ' ')
O = SevenSeg[LC - 65 + 11]; // Last char
N = SevenSeg[C - 65 + 11]; // Current char
}
else
switch (C) {
case ':':
if (LC == ' ')
Draw_Colon();
break;
case '^':
if (LC == ' ')
Draw_Celsius();
break;
case '%':
if (LC == ' ')
Draw_Percent();
break;
case 'c':
if (LC == ' ')
Draw_Celsius_Char();
break;
case '.':
if (LC == ' ') {
O = SevenSeg[LC - 46 + 10]; // Last char
N = SevenSeg[C - 46 + 10]; // Current char
}
break;
case ' ':
if (LC != ' ') {
if ((LC >= '0') && (LC <= '9'))
O = SevenSeg[LC - 48]; // Last char
else if ((LC >= 'A') && (LC <= 'Z'))
O = SevenSeg[LC - 65 + 11]; // Last char
}
}
//
// Segmente nur Ausgeben, wenn sie sich geändert haben
//
if (O != N) {
SetColor(0);
if (((0x0001 & N) == 0) && ((0x0001 & O) != 0)) Draw_DA();
if (((0x0002 & N) == 0) && ((0x0002 & O) != 0)) Draw_DB();
if (((0x0004 & N) == 0) && ((0x0004 & O) != 0)) Draw_DC();
if (((0x0008 & N) == 0) && ((0x0008 & O) != 0)) Draw_DD();
if (((0x0010 & N) == 0) && ((0x0010 & O) != 0)) Draw_DE();
if (((0x0020 & N) == 0) && ((0x0020 & O) != 0)) Draw_DF();
if (((0x0040 & N) == 0) && ((0x0040 & O) != 0)) Draw_DG();
if (((0x0080 & N) == 0) && ((0x0080 & O) != 0)) Draw_DP();
if (((0x0100 & N) == 0) && ((0x0100 & O) != 0)) Draw_DG1();
if (((0x0200 & N) == 0) && ((0x0200 & O) != 0)) Draw_DG2();
if (((0x0400 & N) == 0) && ((0x0400 & O) != 0)) Draw_DH();
if (((0x0800 & N) == 0) && ((0x0800 & O) != 0)) Draw_DI();
if (((0x1000 & N) == 0) && ((0x1000 & O) != 0)) Draw_DJ();
if (((0x2000 & N) == 0) && ((0x2000 & O) != 0)) Draw_DK();
if (((0x4000 & N) == 0) && ((0x4000 & O) != 0)) Draw_DL();
if (((0x8000 & N) == 0) && ((0x8000 & O) != 0)) Draw_DM();
SetColor(1);
if (((0x0001 & N) != 0) && ((0x0001 & O) == 0)) Draw_DA();
if (((0x0002 & N) != 0) && ((0x0002 & O) == 0)) Draw_DB();
if (((0x0004 & N) != 0) && ((0x0004 & O) == 0)) Draw_DC();
if (((0x0008 & N) != 0) && ((0x0008 & O) == 0)) Draw_DD();
if (((0x0010 & N) != 0) && ((0x0010 & O) == 0)) Draw_DE();
if (((0x0020 & N) != 0) && ((0x0020 & O) == 0)) Draw_DF();
if (((0x0040 & N) != 0) && ((0x0040 & O) == 0)) Draw_DG();
if (((0x0080 & N) != 0) && ((0x0080 & O) == 0)) Draw_DP();
if (((0x0100 & N) != 0) && ((0x0100 & O) == 0)) Draw_DG1();
if (((0x0200 & N) != 0) && ((0x0200 & O) == 0)) Draw_DG2();
if (((0x0400 & N) != 0) && ((0x0400 & O) == 0)) Draw_DH();
if (((0x0800 & N) != 0) && ((0x0800 & O) == 0)) Draw_DI();
if (((0x1000 & N) != 0) && ((0x1000 & O) == 0)) Draw_DJ();
if (((0x2000 & N) != 0) && ((0x2000 & O) == 0)) Draw_DK();
if (((0x4000 & N) != 0) && ((0x4000 & O) == 0)) Draw_DL();
if (((0x8000 & N) != 0) && ((0x8000 & O) == 0)) Draw_DM();
}
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw the main time with seconds
//
// Die Ziffern werden bei jedem Aufruf neu Ausgegeben, jedoch nur, wenn sie
// sich geändert haben. Alle Sonderzeichen werden nur einmal Ausgegeben.
//
void DrawMainTime() {
PX = 2; // Kann zum zentrieren erhöht werden
PY = 2; // Kann zum zentrieren erhöht werden
SY = 115; // Zifferngröße --> Höhe 235
SX = 49; // Zifferngröße --> Breite 127
WI = 9; // Linienbreite = 1/10 der Höhe 23
DI = 9; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand) 24
DateTime now = rtc.now();
String Hour = " ";
String Minute = " ";
String Second = " ";
Hour = String(now.hour(), DEC);
if (Hour.length() == 1) Hour = "0" + Hour;
Minute = String(now.minute(), DEC);
if (Minute.length() == 1) Minute = "0" + Minute;
Second = String(now.second(), DEC);
if (Second.length() == 1) Second = "0" + Second;
MainTime = Hour + ":" + Minute + " " + Second;
Draw7Seg(MainTime[0], LastTime[0]); PX = PX + SX + DI;
Draw7Seg(MainTime[1], LastTime[1]); PX = PX + SX + DI;
Draw7Seg(MainTime[2], LastTime[2]); PX = PX + DI + DI;
Draw7Seg(MainTime[3], LastTime[3]); PX = PX + SX + DI;
Draw7Seg(MainTime[4], LastTime[4]); PX = PX + SX + DI;
PX = PX+2; // Kann zum zentrieren erhöht werden
PY = 64; // Kann zum zentrieren geändert werden
SY = 53; // Zifferngröße --> Höhe 119
SX = 27; // Zifferngröße --> Breite 63
WI = 5; // Linienbreite = 1/10 der Höhe 11
DI = 7; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand) 12
Draw7Seg(MainTime[6], LastTime[6]); PX = PX + SX + DI;
Draw7Seg(MainTime[7], LastTime[7]); PX = PX + SX;
LastTime = MainTime;
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw the day of week
//
void DrawDayOfWeek() {
PX = 4; // Notwendige Buchstaben(15) ACDEFGHIMNORSTW
PY = 126; // Y-Position
SY = 49; // Segmentgröße --> Breite
SX = 21; // Segmentgröße --> doppelte Höhe
WI = 4; // Linienbreite
DI = 3; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand)
DateTime now = rtc.now();
switch (now.dayOfTheWeek()) {
case 0: { WeekDay = "SONNTAG "; break; }
case 1: { WeekDay = "MONTAG "; break; }
case 2: { WeekDay = "DIENSTAG "; break; }
case 3: { WeekDay = "MITTWOCH "; break; }
case 4: { WeekDay = "DONNERSTAG"; break; }
case 5: { WeekDay = "FREITAG "; break; }
case 6: { WeekDay = "SAMSTAG "; break; }
}
SetColor(1);
for (int16_t T = 0; T < DayLength; T++) {
Draw7Seg(WeekDay[T], LastDay[T]);
PX = PX + SX + DI;
}
LastDay = WeekDay;
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw the huminity
//
void DrawHumidity() {
String Hum = "46.8%"; // 5
String Humidity = "00.0";
float HUM = dht.readHumidity();
Hum = String(HUM, 1) + "%";
int POS = 0;
PX = 228;
PY = 142;
SY = 31; // Segmentgröße --> Breite
SX = 17; // Segmentgröße --> doppelte Höhe
WI = 4; // Linienbreite
DI = 3; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand)
Draw7Seg(Hum[POS], LastHum[POS]); PX = PX + SX + WI; POS++;
Draw7Seg(Hum[POS], LastHum[POS]); PX = PX + SX + WI; POS++;
Draw7Seg(Hum[POS], LastHum[POS]); PX = PX + DI + DI + DI; POS++;
Draw7Seg(Hum[POS], LastHum[POS]); PX = PX + SX + WI; POS++;
Draw7Seg(Hum[POS], LastHum[POS]); PX = PX + SX + WI;
LastHum = Hum;
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw the full date
//
void DrawTheDate() {
String Date = " "; // 10
String Year = " ";
String Month = " ";
String Day = " ";
int POS = 0;
PX = 2;
PY = 184;
SY = 49; // Segmentgröße --> Breite
SX = 21; // Segmentgröße --> doppelte Höhe
WI = 4; // Linienbreite
DI = 3; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand)
Date = "06.02.2022";
DateTime now = rtc.now();
Year = String(now.year(), DEC);
Month = String(now.month(), DEC);
if (Month.length() == 1) Month = "0" + Month;
Day = String(now.day(), DEC);
if (Day.length() == 1) Day = "0" + Day;
Date = Day + "." + Month + "." + Year;
SetColor(1);
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + DI + DI + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + DI + DI + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI; POS++;
Draw7Seg(Date[POS], LastDate[POS]); PX = PX + SX + DI;
LastDate = Date;
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// draw the temperature
//
void DrawTemperature() {
String Temp = "22.3^c"; // 6 °
String Temperature = "00.0";
float Tmp = dht.readTemperature();
Temp = String(Tmp, 1) + "^c";
int POS = 0;
PX = 228;
PY = 202;
SY = 31; // Segmentgröße --> Breite
SX = 17; // Segmentgröße --> doppelte Höhe
WI = 4; // Linienbreite
DI = 3; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand)
Draw7Seg(Temp[POS], LastTemp[POS]); PX = PX + SX + WI; POS++;
Draw7Seg(Temp[POS], LastTemp[POS]); PX = PX + SX + WI; POS++;
Draw7Seg(Temp[POS], LastTemp[POS]); PX = PX + DI + DI; POS++;
Draw7Seg(Temp[POS], LastTemp[POS]); PX = PX + SX + WI; POS++;
Draw7Seg(Temp[POS], LastTemp[POS]); PX = PX + DI + DI + DI; POS++;
Draw7Seg(Temp[POS], LastTemp[POS]); PX = PX + SX + WI;
LastTemp = Temp;
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// this is a test only:
// draw single char
//
void SingleCharTestDraw() {
PX = 4; // Kann zum zentrieren erhöht werden
PY = 4; // Kann zum zentrieren erhöht werden
SY = 99; // Zifferngröße --> Höhe 119
SX = 49; // Zifferngröße --> Breite 63
WI = 5; // Linienbreite = 1/10 der Höhe 11
DI = 8; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand) 12
Draw7Seg('X', ' ');
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// this is a test only:
// draw any numbers 0..9 as a 7-segment char
//
void NumericTestDraw() {
PX = 4; // Kann zum zentrieren erhöht werden
PY = 4; // Kann zum zentrieren erhöht werden
SY = 99; // Zifferngröße --> Höhe 119
SX = 49; // Zifferngröße --> Breite 63
WI = 5; // Linienbreite = 1/10 der Höhe 11
DI = 8; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand) 12
Draw7Seg('0', ' '); delay(1000);
Draw7Seg('1', '0'); delay(1000);
Draw7Seg('2', '1'); delay(1000);
Draw7Seg('3', '2'); delay(1000);
Draw7Seg('4', '3'); delay(1000);
Draw7Seg('5', '4'); delay(1000);
Draw7Seg('6', '5'); delay(1000);
Draw7Seg('7', '6'); delay(1000);
Draw7Seg('8', '7'); delay(1000);
Draw7Seg('9', '8'); delay(1000);
Draw7Seg('0', '9'); delay(1000);
Draw7Seg(' ', '0'); delay(1000);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// this is a test only:
// draw any chars A..Z as a 7-segment char
//
void AlphaNumericTestDraw() {
PX = 4; // Kann zum zentrieren erhöht werden
PY = 4; // Kann zum zentrieren erhöht werden
SY = 99; // Zifferngröße --> Höhe 119
SX = 59; // Zifferngröße --> Breite 63
WI = 5; // Linienbreite = 1/10 der Höhe 11
DI = 8; // Abstand (10x = 7 Abstände + 2 Doppelpunkte + Rand) 12
Draw7Seg('A', ' '); delay(1000);
Draw7Seg('B', 'A'); delay(1000);
Draw7Seg('C', 'B'); delay(1000);
Draw7Seg('D', 'C'); delay(1000);
Draw7Seg('E', 'D'); delay(1000);
Draw7Seg('F', 'E'); delay(1000);
Draw7Seg('G', 'F'); delay(1000);
Draw7Seg('H', 'G'); delay(1000);
Draw7Seg('I', 'H'); delay(1000);
Draw7Seg('J', 'I'); delay(1000);
Draw7Seg('K', 'J'); delay(1000);
Draw7Seg('L', 'K'); delay(1000);
Draw7Seg('M', 'L'); delay(1000);
Draw7Seg('N', 'M'); delay(1000);
Draw7Seg('O', 'N'); delay(1000);
Draw7Seg('P', 'O'); delay(1000);
Draw7Seg('Q', 'P'); delay(1000);
Draw7Seg('R', 'Q'); delay(1000);
Draw7Seg('S', 'R'); delay(1000);
Draw7Seg('T', 'S'); delay(1000);
Draw7Seg('U', 'T'); delay(1000);
Draw7Seg('V', 'U'); delay(1000);
Draw7Seg('W', 'V'); delay(1000);
Draw7Seg('X', 'W'); delay(1000);
Draw7Seg('Y', 'X'); delay(1000);
Draw7Seg('Z', 'Y'); delay(1000);
Draw7Seg(' ', 'Z'); delay(1000);
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// this is a test only:
// test the speed of drawing all 7-segment chars
//
/*
int16_t Runtime_0 = 0; // system run time
int16_t Runtime_1 = 0; // time to draw MainTime
int16_t Runtime_2 = 0; // time to draw DayOfWeek
int16_t Runtime_3 = 0; // time to draw Huminity
int16_t Runtime_4 = 0; // time to draw Date
int16_t Runtime_5 = 0; // time to draw Temperature
*/
void speedtest() {
Runtime_0 = millis();
DrawMainTime(); Runtime_1 = millis();
DrawDayOfWeek(); Runtime_2 = millis();
DrawHumidity(); Runtime_3 = millis();
DrawTheDate(); Runtime_4 = millis();
DrawTemperature(); Runtime_5 = millis();
tft.print ("Arduino = "); tft.println(Runtime_0);
tft.print ("MainTime = "); tft.println(Runtime_1 - Runtime_0);
tft.print ("DayWeek = "); tft.println(Runtime_2 - Runtime_1);
tft.print ("Huminity = "); tft.println(Runtime_3 - Runtime_2);
tft.print ("Date = "); tft.println(Runtime_4 - Runtime_3);
tft.print ("Temperature = "); tft.println(Runtime_5 - Runtime_4);
tft.print ("Total = "); tft.println(Runtime_5 - Runtime_0);
tft.println();
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// setup
//
void setup() {
if (! rtc.begin()) {
Serial.begin(115200);
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
dht.begin();
tft.begin();
clr_scr();
tft.setRotation(3); // use the lcd in vertical direction
tft.setCursor(20, 165);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(01, 01);
return;
DrawMainTime();
DrawDayOfWeek();
DrawHumidity();
DrawTheDate();
DrawTemperature();
return;
speedtest();
speedtest();
/* Norm Fast Norm Fast
DrawMainTime(); 347 46 397 48
DrawDayOfWeek(); 101 33 139 47
DrawHuminity(); 0 0 70 37
DrawTheDate(); 124 19 164 57
DrawTemperature(); 0 0 55 20
TotalTime 572 98 825 209
*/
}
// ─────────────────────────────────────────────────────────────────────────────────────────
//
// main loop
//
void loop() {
// SingleCharTestDraw(); return;
// NumericTestDraw(); return;
// AlphaNumericTestDraw(); return;
DrawMainTime();
DrawDayOfWeek();
DrawHumidity();
DrawTheDate();
DrawTemperature();
}