/
LcdMenu.cpp
497 lines (411 loc) · 13.1 KB
/
LcdMenu.cpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
*
* Copyright 2018 - BT-Soft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* LcdMenu.cpp
*
* Created on: 2018. ápr. 26.
* Author: bt-soft
*/
#include "LcdMenu.h"
#include "Buzzer.h"
/**
* Konstruktor
*/
LcdMenu::LcdMenu(void) {
//Software SPI
// D9 - BackLight LED
// D8 - Serial clock out (SCLK)
// D7 - Serial data out (DIN)
// D6 - Data/Command select (D/C)
// D5 - LCD chip select (CS)
// D4 - LCD reset (RST)
nokia5110Display = new Nokia5110DisplayWrapper(PIN_LCD_SCLK, PIN_LCD_DIN, PIN_LCD_DC, PIN_LCD_CS, PIN_LCD_RST, PIN_LCD_BLACKLIGHT);
//--- Háttérvilágítás kezelése
nokia5110Display->setBlackLightState(pConfig->configVars.blackLightState); //háttérvilágítás beállítása a konfig szerint
//Menüelemek inicializálása
initMenuItems();
resetMenu();
}
/**
* Splash képernyõ kirajzolása
*/
void LcdMenu::drawSplashScreen(void) {
nokia5110Display->clearDisplay();
nokia5110Display->setTextSize(1);
nokia5110Display->setTextColor(BLACK);
nokia5110Display->println(" Arduino Nano");
nokia5110Display->println(" Spot Welder");
nokia5110Display->setCursor(8, 25);
nokia5110Display->print("v");
nokia5110Display->setTextSize(2);
sprintf(tempBuff, "%s", &pConfig->configVars.version);
nokia5110Display->setCursor(14, 18);
nokia5110Display->print(tempBuff);
nokia5110Display->setTextSize(1);
nokia5110Display->setCursor(0, 40);
nokia5110Display->println(" BT-Soft 2018");
nokia5110Display->display();
}
/**
* Menü felhúzása
*/
void LcdMenu::initMenuItems(void) {
menuItems[0] = {"Weld mode", WELD, &pConfig->configVars.pulseCountWeldMode, 0, 1, NULL};
menuItems[1] = {"PreWeld pulse", PULSE, &pConfig->configVars.preWeldPulseCnt, 0, 255, NULL};
menuItems[2] = {"Pause pulse", PULSE, &pConfig->configVars.pausePulseCnt, 0, 255, NULL};
menuItems[3] = {"Weld pulse", PULSE, &pConfig->configVars.weldPulseCnt, 1, 255, NULL};
menuItems[4] = {"Packets cnt", BYTE, &pConfig->configVars.packetCnt, 1, 99, NULL};
menuItems[5] = {"Packets pause", PULSE, &pConfig->configVars.packetPauseCnt, 1, 255, NULL};
menuItems[6] = {"MOT T.Alrm", TEMP, &pConfig->configVars.motTempAlarm, 50, 90, NULL};
menuItems[7] = {"LCD contrast", BYTE, &pConfig->configVars.contrast, 0, 127, &LcdMenu::lcdContrastCallBack};
//menuItems[8] = {"LCD bias", BYTE, &pConfig->configVars.bias, 0, 7, &LcdMenu::lcdBiasCallBack};
menuItems[8] = {"LCD light", BOOL, &pConfig->configVars.blackLightState, 0, 1, &LcdMenu::lcdBackLightCallBack};
menuItems[9] = {"Beep", BOOL, &pConfig->configVars.beepState, 0, 1, &LcdMenu::beepStateCallBack};
menuItems[10] = {"Fctry reset", FUNCT, NULL, 0, 0, &LcdMenu::factoryResetCallBack};
menuItems[11] = {"Exit menu", FUNCT, NULL, 0, 0, &LcdMenu::exitCallBack};
}
/**
* Menü alapállapotba
*/
void LcdMenu::resetMenu(void) {
//viewPort beállítás
menuViewport.firstItem = 0;
menuViewport.lastItem = MENU_VIEWPORT_SIZE - 1;
menuViewport.selectedItem = 0;
}
/**
* Hõmérséklet kiírása a main- és az alarm screen-nél
*/
void LcdMenu::drawTempValue(float *pCurrentMotTemp) {
nokia5110Display->setTextSize(2);
nokia5110Display->setCursor(abs(*pCurrentMotTemp) > 99.9 ? 0 : 10, 34);
dtostrf(*pCurrentMotTemp, 1, 1, tempBuff);
nokia5110Display->print(tempBuff);
nokia5110Display->setTextSize(1);
nokia5110Display->setCursor(72, 40);
sprintf(tempBuff, "%cC", DEGREE_SYMBOL_CODE);
nokia5110Display->print(tempBuff);
}
/**
* Main screen
*/
void LcdMenu::drawMainDisplay(float *pCurrentMotTemp) {
nokia5110Display->clearDisplay();
nokia5110Display->setTextColor(BLACK);
nokia5110Display->setTextSize(1);
//Pulzusszámláló mód
if (pConfig->configVars.pulseCountWeldMode) {
if (pConfig->configVars.packetCnt > 1) {
sprintf(tempBuff, "Pc %-2d Pp %-3d ", pConfig->configVars.packetCnt, pConfig->configVars.packetPauseCnt);
} else {
sprintf(tempBuff, "Pc %-2d Pp - ", pConfig->configVars.packetCnt);
}
nokia5110Display->setTextColor(WHITE, BLACK);
nokia5110Display->println(tempBuff);
nokia5110Display->setTextColor(BLACK, WHITE);
nokia5110Display->println("Pwld Paus Wld");
sprintf(tempBuff, "%-3d %-3d %-3d", pConfig->configVars.preWeldPulseCnt, pConfig->configVars.pausePulseCnt, pConfig->configVars.weldPulseCnt);
nokia5110Display->println(tempBuff);
} else { //kézi hegesztés
nokia5110Display->setTextColor(WHITE, BLACK);
nokia5110Display->println("Manual welding");
nokia5110Display->setTextColor(BLACK, WHITE);
}
//Hõmérséklet kiírása
nokia5110Display->setCursor(0, 26);
nokia5110Display->print("MOT Temp:");
this->drawTempValue(pCurrentMotTemp);
nokia5110Display->display();
}
/**
* Magas hõmérséklet riasztás
*/
void LcdMenu::drawWarningDisplay(float *pCurrentMotTemp) {
nokia5110Display->clearDisplay();
nokia5110Display->setTextColor(BLACK);
nokia5110Display->setTextSize(1);
nokia5110Display->println("!!!!!!!!!!!!!!");
nokia5110Display->println(" MOT Temp is");
nokia5110Display->println(" too high!");
nokia5110Display->println("!!!!!!!!!!!!!!");
//Hõmérséklet kiírása
this->drawTempValue(pCurrentMotTemp);
nokia5110Display->display();
}
/**
* MainMenu kirajzolása
* - Menüfelirat
* - viewportban látható elemek megjeelnítése
* - kiválasztott elem hátterének megváltoztatása
*/
void LcdMenu::drawMainMenu(void) {
nokia5110Display->clearDisplay();
nokia5110Display->setTextSize(1);
nokia5110Display->setTextColor(BLACK, WHITE);
nokia5110Display->setCursor(15, 0);
nokia5110Display->print("MAIN MENU");
nokia5110Display->drawFastHLine(0, 10, 83, BLACK);
for (byte i = 0; i < MENU_VIEWPORT_SIZE; i++) {
byte itemNdx = menuViewport.firstItem + i;
nokia5110Display->setCursor(0, MENU_VIEVPORT_LINEPOS[i]);
//selected?
if (itemNdx == menuViewport.selectedItem) {
nokia5110Display->setTextColor(WHITE, BLACK);
nokia5110Display->print(">");
} else {
nokia5110Display->setTextColor(BLACK, WHITE);
nokia5110Display->print(" ");
}
nokia5110Display->print(menuItems[itemNdx].title);
}
nokia5110Display->display();
}
/**
* menüelem beállítõ képernyõ
*/
void LcdMenu::drawMenuItemValue() {
MenuItemT p = menuItems[menuViewport.selectedItem];
nokia5110Display->clearDisplay();
nokia5110Display->setTextSize(1);
nokia5110Display->setTextColor(BLACK, WHITE);
nokia5110Display->setCursor(5, 0);
nokia5110Display->print(p.title);
nokia5110Display->drawFastHLine(0, 10, 83, BLACK);
nokia5110Display->setCursor(5, 15);
//Prompt
switch (p.valueType) {
case TEMP:
case BYTE:
case BOOL:
nokia5110Display->print("Value");
break;
case WELD:
nokia5110Display->print("Mode");
break;
case PULSE:
nokia5110Display->print("Pulse");
break;
}
nokia5110Display->setTextSize(2);
nokia5110Display->setCursor(5, 25);
//Típus szerinti kiírás
String dspValue = "unknown";
switch (p.valueType) {
case BOOL:
dspValue = *(bool *) p.valuePtr ? "ON" : "OFF";
break;
case WELD:
dspValue = *(bool *) p.valuePtr ? "Pulse" : "Manual";
break;
case PULSE:
case TEMP:
case BYTE:
dspValue = String(*(byte *) p.valuePtr);
break;
}
nokia5110Display->print(dspValue);
//Mértékegység
nokia5110Display->setTextSize(1);
switch (p.valueType) {
case TEMP: //°C kiírás
nokia5110Display->setCursor(55, 30);
sprintf(tempBuff, "%cC", DEGREE_SYMBOL_CODE);
nokia5110Display->print(tempBuff);
break;
case PULSE: //msec kiírás
if (SYSTEM_PERIOD_TIME > 0.0) {
nokia5110Display->setCursor(35, 40);
nokia5110Display->print(msecToStr(*(byte *) p.valuePtr));
}
break;
default:
break;
}
nokia5110Display->display();
}
/**
* Menüben lefelé lépkedés
*/
void LcdMenu::stepDown(void) {
//Az utolsó elem a kiválasztott? Ha igen, akkor nem megyünk tovább
if (menuViewport.selectedItem == LAST_MENUITEM_NDX) {
return;
}
//A következõ menüelem lesz a kiválasztott
menuViewport.selectedItem++;
//A viewport aljánál túljutottunk? Ha igen, akkor scrollozunk egyet lefelé
if (menuViewport.selectedItem > menuViewport.lastItem) {
menuViewport.firstItem++;
menuViewport.lastItem++;
}
}
/**
* Menüben felfelé lépkedés
*/
void LcdMenu::stepUp(void) {
//Az elsõ elem a kiválasztott? Ha igen, akkor nem megyünk tovább
if (menuViewport.selectedItem == 0) {
return;
}
//Az elõzõ menüelem lesz a kiválasztott
menuViewport.selectedItem--;
//A viewport tetejénél túljutottunk? Ha igen, akkor scrollozunk egyet felfelé
if (menuViewport.selectedItem < menuViewport.firstItem) {
menuViewport.firstItem--;
menuViewport.lastItem--;
}
}
/**
* A kiválasztott menüelem értékének növelése
*/
void LcdMenu::incSelectedValue(void) {
MenuItemT *pSelectedMenuItem = this->getSelectedItemPtr();
switch (pSelectedMenuItem->valueType) {
case LcdMenu::BYTE:
case LcdMenu::PULSE:
case LcdMenu::TEMP:
if (*(byte *) pSelectedMenuItem->valuePtr < pSelectedMenuItem->maxValue) {
(*(byte *) pSelectedMenuItem->valuePtr)++;
}
break;
case LcdMenu::WELD:
case LcdMenu::BOOL:
if (!*(bool *) pSelectedMenuItem->valuePtr) { //ha most false, akkor true-t csinálunk belõle
*(bool *) pSelectedMenuItem->valuePtr = true;
}
break;
}
}
/**
* A kiválasztott menüelem értékének csökkentése
*/
void LcdMenu::decSelectedValue(void) {
MenuItemT *pSelectedMenuItem = this->getSelectedItemPtr();
switch (pSelectedMenuItem->valueType) {
case LcdMenu::BYTE:
case LcdMenu::PULSE:
case LcdMenu::TEMP:
if (*(byte *) pSelectedMenuItem->valuePtr > pSelectedMenuItem->minValue) {
(*(byte *) pSelectedMenuItem->valuePtr)--;
}
break;
case LcdMenu::WELD:
case LcdMenu::BOOL:
if (*(bool *) pSelectedMenuItem->valuePtr) { //ha most true, akkor false-t csinálunk belõle
*(bool *) pSelectedMenuItem->valuePtr = false;
}
break;
}
}
/**
* Menüelemhez tartozó segédfüggvény meghívása - ha van
*/
void LcdMenu::invokeMenuItemCallBackFunct(void) {
MenuItemT *pSelectedMenuItem = this->getSelectedItemPtr();
if (pSelectedMenuItem->callbackFunct != NULL) {
(this->*(pSelectedMenuItem->callbackFunct))();
}
}
//------------------------------------------------------------------- Menüelemek callback függvényei
/**
* LCD Kontraszt kezelése
*/
void LcdMenu::lcdContrastCallBack(void) {
nokia5110Display->setContrast(pConfig->configVars.contrast);
pConfig->wantSaveConfig = true;
}
/**
* LCD Elõfeszítés (bias) kezelése
*/
void LcdMenu::lcdBiasCallBack(void) {
nokia5110Display->setBias(pConfig->configVars.bias);
pConfig->wantSaveConfig = true;
}
/**
* LCD Háttérvilágítás kezelése
*/
void LcdMenu::lcdBackLightCallBack(void) {
nokia5110Display->setBlackLightState(pConfig->configVars.blackLightState);
pConfig->wantSaveConfig = true;
}
/**
* Beep kezelése
*/
void LcdMenu::beepStateCallBack(void) {
if (pConfig->configVars.beepState) {
pBuzzer->buzzerMenu();
}
pConfig->wantSaveConfig = true;
}
/**
* Gyári beállítások visszaállítása
*/
void LcdMenu::factoryResetCallBack(void) {
nokia5110Display->clearDisplay();
nokia5110Display->setTextSize(1);
nokia5110Display->setTextColor(BLACK, WHITE);
nokia5110Display->setCursor(15, 0);
nokia5110Display->print("RESET");
nokia5110Display->drawFastHLine(0, 10, 83, BLACK);
nokia5110Display->setTextSize(2);
nokia5110Display->setCursor(5, 25);
nokia5110Display->print("Ok...");
nokia5110Display->display();
//Default konfig létrehozása
pConfig->createDefaultConfig();
pConfig->wantSaveConfig = true;
//Konfig beállítások érvényesítése
lcdContrastCallBack();
lcdBackLightCallBack();
beepStateCallBack();
//menü alapállapotba
resetMenu();
menuState = FORCE_MAIN_DISPLAY;
}
/**
* Kilépés a menübõl
*/
void LcdMenu::exitCallBack(void) {
nokia5110Display->clearDisplay();
nokia5110Display->setTextColor(BLACK, WHITE);
nokia5110Display->setTextSize(1);
nokia5110Display->setCursor(0, 20);
nokia5110Display->print("Return...");
nokia5110Display->display();
//menü alapállapotba
resetMenu();
menuState = FORCE_MAIN_DISPLAY; //Kilépünk a menübõl
}
// ------------------------------------------------------ utils
/**
* msec -> String konverter
*/
String LcdMenu::msecToStr(long pulseCnt) {
//A ZCD miatt SYSTEM_FREQUENCY-n (100Hz vagy 120Hz-en) vagyunk
long fullPulseTime = (SYSTEM_PERIOD_TIME / 2) * 1000 * pulseCnt;
byte sec = fullPulseTime / 1000;
int msec = fullPulseTime - (sec * 1000);
String res = "";
if (sec > 0) {
res += sec;
res += "s ";
}
if (msec > 0) {
res += msec;
res += "ms";
}
return res;
}