forked from brewtrollergit/brewtroller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI_LCD.h
549 lines (475 loc) · 15.8 KB
/
UI_LCD.h
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
Copyright (C) 2009, 2010 Matt Reba, Jeremiah Dillingham
This file is part of BrewTroller.
BrewTroller is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BrewTroller is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BrewTroller. If not, see <http://www.gnu.org/licenses/>.
BrewTroller - Open Source Brewing Computer
Software Lead: Matt Reba (matt_AT_brewtroller_DOT_com)
Hardware Lead: Jeremiah Dillingham (jeremiah_AT_brewtroller_DOT_com)
Documentation, Forums and more information available at http://www.brewtroller.com
*/
#ifndef UILCD_H
#define UILCD_H
#include "Config.h"
#include "Enum.h"
#include "HWProfile.h"
#include <Wire.h>
#include <LiquidCrystalFP.h>
#include <stdlib.h> // for malloc and free
#include <EEPROM.h>
void* operator new(size_t size) { return malloc(size); }
void operator delete(void* ptr) { free(ptr); }
//*****************************************************************************************************************************
// 4-Bit GPIO LCD Class
//*****************************************************************************************************************************
//**********************************************************************************
// LCD Timing Fix
//**********************************************************************************
// Some LCDs seem to have issues with displaying garbled characters but introducing
// a delay seems to help or resolve completely. You may comment out the following
// lines to remove this delay between a print of each character.
//
//#define LCD_DELAY_CURSOR 60
//#define LCD_DELAY_CHAR 60
//**********************************************************************************
class LCD4Bit
{
public:
#ifndef UI_DISPLAY_SETUP
LCD4Bit(byte rs, byte enable, byte d4, byte d5, byte d6, byte d7) {
_lcd = new LiquidCrystal(rs, enable, d4, d5, d6, d7);
}
#else
LCD4Bit(byte rs, byte enable, byte d4, byte d5, byte d6, byte d7, byte b, byte c) {
_lcd = new LiquidCrystal(rs, enable, d4, d5, d6, d7);
brightPin = b;
contrastPin = c;
}
#endif
void init(){
_lcd->begin(20, 4);
#ifdef UI_DISPLAY_SETUP
TCCR2B = 0x01;
pinMode(brightPin, OUTPUT);
pinMode(contrastPin, OUTPUT);
setBright(loadLCDBright());
setContrast(loadLCDContrast());
#endif
}
void update() { }
void print(byte iRow, byte iCol, char sText[]){
_lcd->setCursor(iCol, iRow);
#ifdef LCD_DELAY_CURSOR
delayMicroseconds(LCD_DELAY_CURSOR);
#endif
int i = 0;
while (sText[i] != 0) {
_lcd->print(sText[i++]);
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
}
//Version of PrintLCD reading from PROGMEM
void print_P(byte iRow, byte iCol, const char *sText){
_lcd->setCursor(iCol, iRow);
while (pgm_read_byte(sText) != 0) {
_lcd->print(pgm_read_byte(sText++));
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
}
void clear(){
_lcd->begin(20, 4);
}
void center(byte iRow, byte iCol, char sText[], byte fieldWidth){
rPad(iRow, iCol, "", fieldWidth, ' ');
if (strlen(sText) < fieldWidth) _lcd->setCursor(iCol + ((fieldWidth - strlen(sText)) / 2), iRow);
else _lcd->setCursor(iCol, iRow);
#ifdef LCD_DELAY_CURSOR
delayMicroseconds(LCD_DELAY_CURSOR);
#endif
int i = 0;
while (sText[i] != 0) {
_lcd->print(sText[i++]);
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
}
char lPad(byte iRow, byte iCol, char sText[], byte length, char pad) {
_lcd->setCursor(iCol, iRow);
#ifdef LCD_DELAY_CURSOR
delayMicroseconds(LCD_DELAY_CURSOR);
#endif
if (strlen(sText) < length) {
for (byte i=0; i < length-strlen(sText); i++) {
_lcd->print(pad);
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
}
int i = 0;
while (sText[i] != 0) {
_lcd->print(sText[i++]);
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
}
char rPad(byte iRow, byte iCol, char sText[], byte length, char pad) {
_lcd->setCursor(iCol, iRow);
#ifdef LCD_DELAY_CURSOR
delayMicroseconds(LCD_DELAY_CURSOR);
#endif
int i = 0;
while (sText[i] != 0) {
_lcd->print(sText[i++]);
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
if (strlen(sText) < length) {
for (byte i=0; i < length-strlen(sText) ; i++) {
_lcd->print(pad);
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
}
}
void setCustChar_P(byte slot, const byte *charDef) {
_lcd->command(64 | (slot << 3));
for (byte i = 0; i < 8; i++) {
_lcd->write(pgm_read_byte(charDef++));
#ifdef LCD_DELAY_CHAR
delayMicroseconds(LCD_DELAY_CHAR);
#endif
}
_lcd->command(B10000000);
}
void writeCustChar(byte iRow, byte iCol, byte slot) {
_lcd->setCursor(iCol, iRow);
#ifdef LCD_DELAY_CURSOR
delayMicroseconds(LCD_DELAY_CURSOR);
#endif
_lcd->write(slot);
}
#ifdef UI_DISPLAY_SETUP
void setBright(byte val) {
analogWrite(brightPin, 255 - val);
bright = val;
}
void setContrast(byte val) {
analogWrite(contrastPin, val);
contrast = val;
}
void saveConfig(void) {
saveLCDBright(bright);
saveLCDContrast(contrast);
}
byte getBright(void) {
return bright;
}
byte getContrast(void) {
return contrast;
}
#endif
private:
LiquidCrystal * _lcd;
#ifdef UI_DISPLAY_SETUP
byte brightPin, contrastPin;
byte bright, contrast;
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__)
void saveLCDBright(byte val) { EEPROM.write(2048, val); }
void saveLCDContrast(byte val) { EEPROM.write(2049, val); }
byte loadLCDBright() { return EEPROM.read(2048); }
byte loadLCDContrast() { return EEPROM.read(2049); }
#else
//Fake It: 644P Only Supports 0-2047
void saveLCDBright(byte val) { }
void saveLCDContrast(byte val) { }
byte loadLCDBright() { }
byte loadLCDContrast() { }
#endif
#endif
};
//*****************************************************************************************************************************
// I2C LCD Class
//*****************************************************************************************************************************
class LCDI2C
{
public:
LCDI2C(byte addr) {
i2cLCDAddr = addr;
}
void init(){
delay(1000);
Wire.begin();
fastWrite = (i2cLcdGetVersion() > 968 ? 1 : 0);
}
void print(byte iRow, byte iCol, char sText[]){
byte pos = iRow * 20 + iCol;
memcpy((byte*)&screen[pos], sText, min(strlen(sText), 80-pos));
}
//Version of PrintLCD reading from PROGMEM
void print_P(byte iRow, byte iCol, const char *sText){
byte pos = iRow * 20 + iCol;
memcpy_P((byte*)&screen[pos], sText, min(strlen_P(sText), 80-pos));
}
void clear() {
memset(screen, ' ', 80);
i2cLcdClear();
}
void center(byte iRow, byte iCol, char sText[], byte fieldWidth){
byte sLen = strlen(sText);
byte textStart = (fieldWidth - sLen) / 2;
char s[21];
memset(s, ' ', fieldWidth);
memcpy(s + textStart, sText, sLen);
s[fieldWidth] = '\0';
print(iRow, iCol, s);
}
char lPad(byte iRow, byte iCol, char sText[], byte length, char pad) {
char s[21];
byte sLen = strlen(sText);
byte textStart = length - sLen;
memset(s, pad, textStart);
memcpy(s + textStart, sText, sLen);
s[length] = 0;
print(iRow, iCol, s);
}
char rPad(byte iRow, byte iCol, char sText[], byte length, char pad) {
char s[21];
byte sLen = strlen(sText);
memcpy(s, sText, sLen);
memset(s + sLen, pad, length - sLen);
s[length] = 0;
print(iRow, iCol, s);
}
void setCustChar_P(byte slot, const byte *charDef) {
i2cLcdSetCustChar_P(slot, charDef);
}
void writeCustChar(byte iRow, byte iCol, byte slot) {
screen[iRow * 20 + iCol] = slot;
}
void update() {
for (byte row = 0; row < 4; row++) {
if (fastWrite) i2cLcdSetCursor(0, row);
for (byte col = 0; col < 20; col++) {
if (fastWrite) i2cLcdWriteByte(screen[row * 20 + col]);
else i2cLcdWriteCustChar(col, row, screen[row * 20 + col]);
}
}
}
void setBright(byte val) {
i2cSetBright(val);
}
void setContrast(byte val) {
i2cSetContrast(val);
}
void saveConfig(void) {
i2cSaveConfig();
}
byte getBright(void) {
return i2cGetBright();
}
byte getContrast(void) {
return i2cGetContrast();
}
private:
byte screen[80];
uint8_t i2cLCDAddr;
boolean fastWrite;
uint8_t i2cLcdBegin(byte iCols, byte iRows) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x01);
Wire.send(iCols);
Wire.send(iRows);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(5);
#endif
return retValue;
}
uint8_t i2cLcdClear() {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x02);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(3);
#endif
return retValue;
}
uint8_t i2cLcdSetCursor(byte iCol, byte iRow) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x03);
Wire.send(iCol);
Wire.send(iRow);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(1);
#endif
return retValue;
}
uint8_t i2cLcdPrint(byte iCol, byte iRow, char s[]) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x04);
Wire.send(iCol);
Wire.send(iRow);
char *p = s;
while (*p) {
Wire.send(*p++);
}
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(3);
#endif
return retValue;
}
uint8_t i2cLcdWrite(byte iCol, byte iRow, byte len, char s[]) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x14);
Wire.send(iCol);
Wire.send(iRow);
Wire.send(len);
for (byte i = 0; i < len; i++) Wire.send(s[i]);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(3);
#endif
return retValue;
}
uint8_t i2cLcdWriteByte(char s) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x15);
Wire.send(s);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(1);
#endif
return retValue;
}
uint8_t i2cLcdSetCustChar_P(byte slot, const byte *charDef) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x05);
Wire.send(slot);
for (byte i = 0; i < 8; i++) {
Wire.send(pgm_read_byte(charDef++));
}
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(5);
#endif
return retValue;
}
uint8_t i2cLcdWriteCustChar(byte iCol, byte iRow, byte c) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x06);
Wire.send(iCol);
Wire.send(iRow);
Wire.send(c);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(1);
#endif
return retValue;
}
uint8_t i2cSetBright(byte val) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x07);
Wire.send(val);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(3);
#endif
return retValue;
}
uint8_t i2cSetContrast(byte val) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x08);
Wire.send(val);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(3);
#endif
return retValue;
}
uint8_t i2cGetBright(void) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x09);
Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(10);
#endif
Wire.requestFrom(i2cLCDAddr, (uint8_t) 1);
while(Wire.available())
{
return Wire.receive();
}
}
uint8_t i2cGetContrast(void) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x0A);
Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(10);
#endif
Wire.requestFrom(i2cLCDAddr, (uint8_t) 1);
while(Wire.available())
{
return Wire.receive();
}
}
uint8_t i2cSaveConfig(void) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x0B);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(10);
#endif
return retValue;
}
uint8_t i2cLoadConfig(void) {
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x0C);
uint8_t retValue = Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(10);
#endif
return retValue;
}
int i2cLcdGetVersion(void) {
//This command may not be implemented in which case bogus values are returned
//Executing the command twice after other differing requests will validate the result
int retValue[2];
for (byte pass = 0; pass < 2; pass++) {
if (pass) i2cGetBright();
else i2cGetContrast();
uint8_t * p = (uint8_t *) &retValue[pass];
Wire.beginTransmission(i2cLCDAddr);
Wire.send(0x16);
Wire.endTransmission();
#ifdef UI_LCD_I2CDELAYS
delay(10);
#endif
Wire.requestFrom(i2cLCDAddr, (uint8_t) 2);
while (Wire.available()) {
*(p++) = Wire.receive();
}
}
if (retValue[0] == retValue[1]) return retValue[0];
else return 0;
}
};
#endif