forked from Rich-Nelson/day-cycle-clock-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayOutput.cpp
More file actions
330 lines (279 loc) · 7.86 KB
/
DisplayOutput.cpp
File metadata and controls
330 lines (279 loc) · 7.86 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "DisplayOutput.h"
DisplayOutput::DisplayOutput(){
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
}
void DisplayOutput::begin(){
tft.begin();
tft.setRotation(2);
}
void DisplayOutput::servoMoveTo(uint8_t degree, uint16_t s_delay){
#ifdef DEBUG
Serial.print("Servo Move To: ");
Serial.println(degree);
#endif
servo.attach(SERVO_PIN);
servo.write(degree);
delay(s_delay);
servo.detach();
}
void DisplayOutput::servoAttach(){
servo.attach(SERVO_PIN);
}
void DisplayOutput::servoDetach(){
servo.attach(SERVO_PIN);
}
void DisplayOutput::stripRGBRow(int r, int g, int b, int row) {
#ifdef DEBUG
Serial.print(r);
Serial.print(" , ");
Serial.print(g);
Serial.print(" , ");
Serial.print(b);
Serial.print(" , ");
Serial.println(row);
#endif
int first_led_in_row = NUM_LEDS / NUM_ROWS * row;
int last_led_in_row = NUM_LEDS / NUM_ROWS * (row + 1);
for (int x = first_led_in_row; x < last_led_in_row; x++) {
leds[x] = CRGB(r, g, b);
}
FastLED.show();
}
void DisplayOutput::stripRGB(int colors[NUM_ROWS][3]){
#ifdef DEBUG
Serial.print("{");
#endif
for (int row; row < NUM_ROWS; row++){
#ifdef DEBUG
Serial.print("{");
Serial.print(colors[row][0]);
Serial.print(", ");
Serial.print(colors[row][1]);
Serial.print(", ");
Serial.print(colors[row][2]);
Serial.print("}, ");
#endif
stripRGBRow(colors[row][0], colors[row][1], colors[row][2], row);
}
#ifdef DEBUG
Serial.println("}");
#endif
}
void DisplayOutput::drawSun() {
tft.fillScreen();
fillCircle(YELLOW);
}
void DisplayOutput::fillArc(int32_t x0, int32_t y0, int32_t r1, bool side, int8_t percent_transition, uint8_t phase, int16_t color){
int32_t y1;
int32_t y2;
int32_t width = r1*percent_transition/100;
int32_t r2 = width/2+sq(r1)/(2*width);
int32_t x02 = x0 - (r2 - width);
#ifdef DEBUG
Serial.print("percent_transition: ");
Serial.print(percent_transition);
Serial.print("width: ");
Serial.print(width);
Serial.print(" r2: ");
Serial.print(r2);
Serial.print(" x02: ");
Serial.println(x02);
#endif
if(x0+width < tft_width && x0+width > 0){
for(int16_t x = x0; x < x0+r1; x++ ){
y1 = sqrt(sq(r1)-sq(x-x0));
y2 = sqrt(sq(r2)-sq(x-x02));
#ifdef DEBUG
Serial.print("x: ");
Serial.print(x);
Serial.print(" y1: ");
Serial.print(y1);
Serial.print(" y2: ");
Serial.print(y2);
Serial.print(" h: ");
Serial.println(y1*2);
#endif
if (phase == WAXING_CRESCENT || phase == WANING_CRESCENT){
if(side == 0){
tft.drawFastVLine(2*x0-x, y0-y1, y1-y2, color);
tft.drawFastVLine(2*x0-x, y0+y2, y1-y2, color);
}
if(side == 1){
tft.drawFastVLine(x, y0-y1, y1-y2, color);
tft.drawFastVLine(x, y0+y2, y1-y2, color);
}
}else{
if(side == 0){
tft.drawFastVLine(2*x0-x, y0-y2, y2*2, color);
}
if(side == 1){
tft.drawFastVLine(x, y0-y2, y2*2, color);
}
}
}
}
}
void DisplayOutput::updateMoon( uint8_t moon_phase_precentage){
uint8_t quot = moon_phase_precentage / 25;
uint8_t rem = moon_phase_precentage % 25;
int8_t moon_progress;
if (rem <= phase_buffer){
moon_phase = quot * 2;
}else if( rem < 25 - phase_buffer){
moon_phase = quot * 2 + 1;
}else{
moon_phase = quot * 2 + 2;
}
tft.fillScreen();
//Draw Moon shadow gradient
if (MOON_SHADOW && moon_phase_precentage % 50 > phase_buffer*2 && moon_phase_precentage % 50 < 50 - phase_buffer*2){
if (moon_phase <= FULL_MOON){
moon_progress = -1;
}else{
moon_progress = 1;
}
drawMoon(moon_phase, moon_phase_precentage, tft_width/2+(9*moon_progress), 0x4208);
drawMoon(moon_phase, moon_phase_precentage, tft_width/2+(6*moon_progress), 0x8410);
drawMoon(moon_phase, moon_phase_precentage, tft_width/2+(3*moon_progress), 0xDEDB);
}
drawMoon(moon_phase, moon_phase_precentage, tft_width/2, WHITE);
}
void DisplayOutput::drawMoon( uint8_t moon_phase, uint8_t moon_phase_precentage, int16_t x0, int16_t color){
uint8_t percent_transition;
#ifdef DEBUG
Serial.print("Moon Phase Test: ");
Serial.println(moon_phase);
#endif
switch (moon_phase) {
case NEW_MOON:
break;
case WAXING_CRESCENT:
percent_transition = 100-moon_phase_precentage*100/25;
fillArc(x0, tft_y_center, 62, 1, percent_transition, moon_phase, color);
break;
case FIRST_QUARTER:
fillArc(x0, tft_y_center, 62, 1, 100, moon_phase, color);
break;
case WAXING_GIBBOUS:
percent_transition = (moon_phase_precentage-25)*100/25;
fillArc(x0, tft_y_center, 62, 1, 100, moon_phase, color);
fillArc(x0, tft_y_center, 62, 0, percent_transition, moon_phase, color);
break;
case FULL_MOON:
fillArc(x0, tft_y_center, 62, 0, 100, moon_phase, color);
fillArc(x0, tft_y_center, 62, 1, 100, moon_phase, color);
break;
case WANING_GIBBOUS:
percent_transition = 100-(moon_phase_precentage-50)*100/25;
fillArc(x0, tft_y_center, 62, 0, 100, moon_phase, color);
fillArc(x0, tft_y_center, 62, 1, percent_transition, moon_phase, color);
break;
case THIRD_QUARTER:
fillArc(x0, tft_y_center, 62, 0, 100, moon_phase, color);
break;
case WANING_CRESCENT:
percent_transition = (moon_phase_precentage-75)*100/25;
fillArc(x0, tft_y_center, 62, 0, percent_transition, moon_phase, color);
break;
}
#ifdef DEBUG
Serial.print("Moon Phase: ");
Serial.println(moon_phase_precentage);
#endif
}
void DisplayOutput::printMenuTitle(String titleString) {
fillMenuTitle();
int x_offset = (4 - titleString.length()) * 8;
tft.setCursor(32 + x_offset, 16);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.print(titleString);
}
bool DisplayOutput::amCheck(int time_hour){
if( time_hour < 12){
return true;
}else{
return false;
}
}
int DisplayOutput::convert12Hr(int time_hour){
if (time_hour == 0){
return 12;
}else if( time_hour > 12){
return time_hour - 12;
}else{
return time_hour;
}
}
void DisplayOutput::printAMPM(bool AM){
fillAMPM();
tft.setCursor(48, 84);
tft.setTextColor(BLACK);
tft.setTextSize(3);
if(AM){
tft.print("AM");
}else{
tft.print("PM");
}
}
void DisplayOutput::printTime(int time_hour, int time_min) {
bool AM = amCheck(time_hour);
time_hour = convert12Hr(time_hour);
printAMPM(AM);
fillValue();
tft.setCursor(7, 48);
tft.setTextColor(BLACK);
tft.setTextSize(4);
if(time_hour < 10){
tft.print(0, DEC);
}
tft.print(time_hour, DEC);
tft.print(':');
if(time_min < 10){
tft.print(0, DEC);
}
tft.println(time_min, DEC);
}
void DisplayOutput::printDate(uint8_t month, uint8_t day, uint16_t year) {
fillValue();
tft.setCursor(16, 42);
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.print(daysOfTheMonth[month]);
tft.print(' ');
tft.println(day, DEC);
tft.setCursor(28, 72);
tft.print(year, DEC);
}
void DisplayOutput::printValue(int value, bool hr = 0) {
if(hr){
bool AM = amCheck(value);
value = convert12Hr(value);
printAMPM(AM);
}
int val_length = String(value).length();
int cursor_x;
tft.setTextColor(BLACK);
if (val_length <= 5) {
tft.setTextSize(4);
cursor_x = 6 + (5 - val_length) * 12;
} else {
tft.setTextSize(3);
cursor_x = 6 + (5 - val_length) * 10;
}
fillValue();
tft.setCursor(cursor_x, 48);
tft.print(value);
}
void DisplayOutput::fillMenuTitle() {
tft.fillRect(32, 16, 70, 24, YELLOW);
}
void DisplayOutput::fillValue() {
tft.fillRect(7, 48, 118, 30, YELLOW);
}
void DisplayOutput::fillAMPM() {
tft.fillRect(48, 80, 28, 24, YELLOW);
}
void DisplayOutput::fillCircle(uint16_t color) {
tft.fillCircle(tft_width/2, tft_y_center, tft_width/2-2, color);
}