forked from dniklaus/arduino-display-lcdkeypad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LcdKeypad.cpp
522 lines (462 loc) · 9.76 KB
/
LcdKeypad.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
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
/*
* LcdKeypad.cpp
*
* Created on: 15.10.2013
* Author: niklausd
*/
#include "Arduino.h"
#include "Wire.h"
#include "SpinTimer.h"
#include "LiquidTWI2.h"
#include "LiquidCrystal.h"
#include "LcdKeypad.h"
//=========================================================
class KeyPollTimerAction : public SpinTimerAction
{
public:
KeyPollTimerAction(LcdKeypad* lcdKeypad)
: m_lcdKeypad(lcdKeypad)
{ }
void timeExpired()
{
if (0 != m_lcdKeypad)
{
m_lcdKeypad->handleButtons();
}
}
private:
LcdKeypad* m_lcdKeypad;
};
//=========================================================
const int LcdKeypad::s_defaultLcdRSPin = 8;
const int LcdKeypad::s_defaultLcdEnPin = 9;
const int LcdKeypad::s_defaultLcdD4Pin = 4;
const int LcdKeypad::s_defaultLcdD5Pin = 5;
const int LcdKeypad::s_defaultLcdD6Pin = 6;
const int LcdKeypad::s_defaultLcdD7Pin = 7;
const int LcdKeypad::s_defaultLcdBackLightCtrlPin = 10;
const bool LcdKeypad::s_defaultIsLcdBackLightOn = false;
const int LcdKeypad::s_defaultKeyAdcPin = A0;
const int LcdKeypad::s_defaultKeyPollTime = 50;
const int LcdKeypad::s_rightKeyLimit = 65;
const int LcdKeypad::s_upKeyLimit = 221;
const int LcdKeypad::s_downKeyLimit = 395;
const int LcdKeypad::s_leftKeyLimit = 602;
const int LcdKeypad::s_selectKeyLimit = 873;
LcdKeypad::LcdKeypad(MCPType mcptype, uint8_t i2cAddr, uint8_t detectDevice, uint8_t backlightInverted,
int lcdRSPin, int lcdEnPin, int lcdD4Pin, int lcdD5Pin, int lcdD6Pin, int lcdD7Pin,
int lcdBackLightCtrlPin, bool isLcdBackLightOn)
: m_lcdBackLightCtrlPin(lcdBackLightCtrlPin)
, m_backlightColor(LCDBL_OFF)
, m_currentKey(NO_KEY)
, m_keyPollTimer(new SpinTimer(s_defaultKeyPollTime, new KeyPollTimerAction(this), SpinTimer::IS_RECURRING, SpinTimer::IS_AUTOSTART))
, m_adapter(0)
, m_liquidTwi2(0)
, m_liquidCrystal(0)
{
Wire.begin();
// first try to reach the LiquidTWI2 through I2C
Wire.beginTransmission(i2cAddr);
int error = Wire.endTransmission();
if (0 == error)
{
// the device is reachable so now create LiquidTWI2 object
m_liquidTwi2 = new LiquidTWI2(i2cAddr, detectDevice, backlightInverted);
setMCPType(mcptype);
// scan for the device & set up the LCD's number of columns and rows:
begin(16, 2);
if (0 == m_liquidTwi2->LcdDetected())
{
// device not detected (might be another item on this I2C address)
delete m_liquidTwi2;
m_liquidTwi2 = 0;
}
else
{
setBackLightControl();
}
}
if (0 == m_liquidTwi2)
{
// no success with LiquidTWI2, try the LiquidCrystal type
m_liquidCrystal = new LiquidCrystal(lcdRSPin, lcdEnPin, lcdD4Pin, lcdD5Pin, lcdD6Pin, lcdD7Pin);
setBackLightControl();
// button adc input
pinMode(s_defaultKeyAdcPin, INPUT); // ensure Key ADC pin is an input
digitalWrite(s_defaultKeyAdcPin, LOW); // ensure pull-up is off on Key ADC pin
// set up the LCD's number of columns and rows:
begin(16, 2);
}
}
LcdKeypad::~LcdKeypad()
{
delete m_liquidCrystal;
m_liquidCrystal = 0;
delete m_liquidTwi2;
m_liquidTwi2 = 0;
delete m_keyPollTimer->action();
m_keyPollTimer->attachAction(0);
delete m_keyPollTimer;
m_keyPollTimer = 0;
}
void LcdKeypad::attachAdapter(LcdKeypadAdapter* adapter)
{
m_adapter = adapter;
}
LcdKeypadAdapter* LcdKeypad::adapter()
{
return m_adapter;
}
void LcdKeypad::setBacklight(LcdBacklightColor lcdBacklightColor)
{
m_backlightColor = lcdBacklightColor;
setBackLightControl();
}
void LcdKeypad::setBackLightOn(bool isLcdBackLightOn)
{
m_backlightColor = isLcdBackLightOn ? LCDBL_WHITE : LCDBL_OFF;
setBackLightControl();
}
void LcdKeypad::setBackLightControl()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->setBacklight(m_backlightColor);
}
else if (0 != m_liquidCrystal)
{
bool isLcdBackLightOn = (LCDBL_OFF != m_backlightColor);
if (isLcdBackLightOn)
{
Serial.println("LcdKeypad::setBackLightControl, isLcdBackLightOn=1");
pinMode(m_lcdBackLightCtrlPin, INPUT);
digitalWrite(m_lcdBackLightCtrlPin, LOW);
}
else
{
Serial.println("LcdKeypad::setBackLightControl, isLcdBackLightOn=0");
pinMode(m_lcdBackLightCtrlPin, OUTPUT);
digitalWrite(m_lcdBackLightCtrlPin, LOW);
}
}
}
void LcdKeypad::handleButtons()
{
Key polledKey = NO_KEY;
if (0 != m_liquidTwi2)
{
uint8_t buttons = m_liquidTwi2->readButtons();
if (buttons)
{
if (buttons & BUTTON_RIGHT)
{
polledKey = RIGHT_KEY;
}
else if (buttons & BUTTON_UP)
{
polledKey = UP_KEY;
}
else if (buttons & BUTTON_DOWN)
{
polledKey = DOWN_KEY;
}
else if (buttons & BUTTON_LEFT)
{
polledKey = LEFT_KEY;
}
else if (buttons & BUTTON_SELECT)
{
polledKey = SELECT_KEY;
}
}
}
else if (0 != m_liquidCrystal)
{
int keyPress = analogRead(s_defaultKeyAdcPin);
if (keyPress < s_rightKeyLimit)
{
polledKey = RIGHT_KEY;
}
else if (keyPress < s_upKeyLimit)
{
polledKey = UP_KEY;
}
else if (keyPress < s_downKeyLimit)
{
polledKey = DOWN_KEY;
}
else if (keyPress < s_leftKeyLimit)
{
polledKey = LEFT_KEY;
}
else if (keyPress < s_selectKeyLimit)
{
polledKey = SELECT_KEY;
}
}
if (polledKey != m_currentKey)
{
m_currentKey = polledKey;
if (0 != m_adapter)
{
m_adapter->handleKeyChanged(polledKey);
}
}
}
LcdKeypad::Key LcdKeypad::getCurrentKey()
{
return m_currentKey;
}
bool LcdKeypad::isNoKey()
{
return (NO_KEY == m_currentKey);
}
bool LcdKeypad::isUpKey()
{
return (UP_KEY == m_currentKey);
}
bool LcdKeypad::isDownKey()
{
return (DOWN_KEY == m_currentKey);
}
bool LcdKeypad::isSelectKey()
{
return (SELECT_KEY == m_currentKey);
}
bool LcdKeypad::isLeftKey()
{
return (LEFT_KEY == m_currentKey);
}
bool LcdKeypad::isRightKey()
{
return (RIGHT_KEY == m_currentKey);
}
void LcdKeypad::begin(uint8_t cols, uint8_t rows, uint8_t dotsize)
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->begin(cols, rows, dotsize);
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->begin(cols, rows, dotsize);
}
}
void LcdKeypad::clear()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->clear();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->clear();
}
}
void LcdKeypad::home()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->home();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->home();
}
}
void LcdKeypad::noDisplay()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->noDisplay();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->noDisplay();
}
}
void LcdKeypad::display()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->display();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->display();
}
}
void LcdKeypad::noBlink()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->noBlink();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->noBlink();
}
}
void LcdKeypad::blink()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->blink();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->blink();
}
}
void LcdKeypad::noCursor()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->noCursor();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->noCursor();
}
}
void LcdKeypad::cursor()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->cursor();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->cursor();
}
}
void LcdKeypad::scrollDisplayLeft()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->scrollDisplayLeft();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->scrollDisplayLeft();
}
}
void LcdKeypad::scrollDisplayRight()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->scrollDisplayRight();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->scrollDisplayRight();
}
}
void LcdKeypad::leftToRight()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->leftToRight();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->leftToRight();
}
}
void LcdKeypad::rightToLeft()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->rightToLeft();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->rightToLeft();
}
}
void LcdKeypad::autoscroll()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->autoscroll();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->autoscroll();
}
}
void LcdKeypad::noAutoscroll()
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->noAutoscroll();
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->noAutoscroll();
}
}
void LcdKeypad::createChar(uint8_t location, uint8_t charmap[])
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->createChar(location, charmap);
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->createChar(location, charmap);
}
}
void LcdKeypad::setCursor(uint8_t col, uint8_t row)
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->setCursor(col, row);
}
else if (0 != m_liquidCrystal)
{
m_liquidCrystal->setCursor(col, row);
}
}
size_t LcdKeypad::write(uint8_t value)
{
#if defined(ARDUINO) && (ARDUINO >= 100) //scl
#endif
size_t ret = 0;
if (0 != m_liquidTwi2)
{
ret = m_liquidTwi2->write(value);
}
else if (0 != m_liquidCrystal)
{
ret = m_liquidCrystal->write(value);
}
return ret;
}
void LcdKeypad::command(uint8_t value)
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->command(value);
}
else if (0 != m_liquidCrystal)
{
//m_liquidCrystal->command(value);
}
}
void LcdKeypad::setMCPType(MCPType mcptype)
{
if (0 != m_liquidTwi2)
{
m_liquidTwi2->setMCPType(mcptype);
}
}
uint8_t LcdKeypad::readButtons()
{
uint8_t buttons = 0;
if (0 != m_liquidTwi2)
{
buttons = m_liquidTwi2->readButtons();
}
return buttons;
}