-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.ino
185 lines (155 loc) · 5.11 KB
/
functions.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Interval for rotating display
unsigned long displayRotateInterval = 10000;
unsigned long lastDisplayChange = 0;
byte displayMode = 0;
void updateDisplay() {
unsigned long currentMillis = millis();
if (currentMillis - lastDisplayChange >= displayRotateInterval) {
lastDisplayChange = currentMillis;
displayMode = (displayMode + 1) % 4; // Rotate between 3 display modes
}
fill_solid(leds, NUM_LEDS, CRGB::Black); // Clear the display
switch (displayMode) {
case 0:
drawTime(1, 0, CRGB(255, 0, 0), true, false); // Display time
break;
case 1:
drawDate(1, 0, CRGB(255, 0, 0) ); // Display date
break;
case 2:
drawTempHum(0, 0, CRGB::Green, true);
break;
case 3:
drawTempHum(0, 0, CRGB::Blue, false);
break;
}
}
void drawLetter(int posx, int posy, char letter, CRGB color) {
if ((posx > -FontWidth) && (posx < kMatrixWidth)) {
for (int x = 0; x < FontWidth; x++) {
for (int y = 0; y < FontHeight; y++) {
if (bitRead(pgm_read_byte(&(Font[letter][FontWidth - 1 - x])), y) == 1) {
leds[XYsafe(posx + x, posy + y)] = color;
}
}
}
}
}
void drawTempHum(int x, int y, CRGB color, bool isTemperature)
{
char tmpStr[10]; // Buffer to hold the converted string
// Adjust initial x position
x = 4;
if (isTemperature) {
dtostrf(t, 3, 0, tmpStr);
Serial.println("Temperature string: " + String(tmpStr));
// Add "C" at y position 1 and x position -2
drawLetter(x, 1, 'C', CRGB(255, 0, 0));
x += FontWidth - 3; // Move to the next position
// Add "." at y position 0 and x position -2
drawLetter(x, -5, '.', CRGB(255, 0, 0));
x += 6; // Move to the next position
} else {
dtostrf(h, 3, 0, tmpStr);
Serial.println("Humidity string: " + String(tmpStr));
// Add "%" at y position 0 and x position -2
drawLetter(x, 1, '%', CRGB(0, 0, 255));
x += FontWidth + 1; // Move to the next position
}
int length = strlen(tmpStr); // Get the length of the character array
for (int i = 0; i < length; i++) {
char letter = tmpStr[length - 1 - i]; // Reverse order
drawLetter(x, y, letter, color); // Draw the letter
x += FontWidth + 1; // Move to the next position
}
}
void drawTime(int x, int y, CRGB color, bool colon, bool seconds) {
// Get current time components
int hours = timeClient.getHours();
int minutes = timeClient.getMinutes();
int secs = timeClient.getSeconds();
// Clear the LEDs
fill_solid(leds, NUM_LEDS, CRGB::Black); // Clear the display
// Display time on LED matrix with swapped positions
x -= 0;
drawLetter(x, y, minutes % 10 + '0', color);
x += FontWidth + 1;
drawLetter(x, y, minutes / 10 + '0', color);
x += FontWidth;
if (colon) {
if (secs % 2 == 0) {
drawLetter(x - 1, y, ':', color);
}
x += 4;
}
drawLetter(x, y, hours % 10 + '0', color);
x += FontWidth + 1;
if (hours / 10 > 0) {
drawLetter(x, y, hours / 10 + '0', color);
}
if (seconds)
leds[XYsafe(secs * kMatrixWidth / 60, kMatrixHeight - 1)] = color;
}
void drawDate(int x, int y, CRGB color) {
// Get current time
time_t epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime((time_t*)&epochTime);
// Get current date components
int day = ptm->tm_mday;
int month = ptm->tm_mon + 1; // tm_mon is 0-11, so we add 1
// Clear the display
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Display date on LED matrix
x -= 0;
drawLetter(x, y, month % 10 + '0', color);
x += FontWidth + 1;
drawLetter(x, y, month / 10 + '0', color);
x += FontWidth + 1;
drawLetter(x-2, y+1, '.', color);
x += 4;
drawLetter(x, y, day % 10 + '0', color);
x += FontWidth + 1;
drawLetter(x, y, day / 10 + '0', color);
}
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if( kMatrixSerpentineLayout == false)
{
if (kMatrixVertical == false)
i = (y * kMatrixWidth) + x;
else
i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
}
if( kMatrixSerpentineLayout == true)
{
if (kMatrixVertical == false)
{
if( y & 0x01)
{
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
}
else
{
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
else
{ // vertical positioning
if ( x & 0x01)
i = kMatrixHeight * (kMatrixWidth - (x+1))+y;
else
i = kMatrixHeight * (kMatrixWidth - x) - (y+1);
}
}
return i;
}
uint16_t XYsafe( uint8_t x, uint8_t y)
{
if( x >= kMatrixWidth) return -1;
if( y >= kMatrixHeight) return -1;
return XY(x,y);
}