-
Notifications
You must be signed in to change notification settings - Fork 3
/
LCDi2c_PCF2119x.cpp
executable file
·540 lines (465 loc) · 17 KB
/
LCDi2c_PCF2119x.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
Copyright © 2014 José Luis Zabalza License LGPLv3+: GNU
LGPL version 3 or later <http://www.gnu.org/copyleft/lgpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
*/
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
// []
// [] Implement LCD LiquidCrystall API Version
// [] for PCF2119x controller LCD based
// [] LCD API 1.0 4-3-2009 by dale@wentztech.com compliant
// []
// [] This library is based on a TontonJules work
// [] http://forum.arduino.cc/index.php?topic=33427.0
// []
// [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
#include <Wire.h>
#include <string.h> //needed for strlen()
#include <inttypes.h>
#include <Arduino.h> //all things wiring / arduino
#include "LCDi2c_PCF2119x.h"
//----------------------------------------------------------------------------------
LCDi2c_PCF2119x::LCDi2c_PCF2119x(uint8_t i2c_addr, uint8_t num_col, uint8_t num_lines, char charset)
{
Wire.begin();
m_num_lines = num_lines;
m_num_col = num_col;
m_i2caddress = i2c_addr;
m_charset = charset;
m_actual_blink = blkoff;
m_actual_cursor = crsroff;
m_actual_active = actvron;
m_actual_horizontal_orientation = horizontal_normal;
m_actual_vertical_orientation = vertical_normal;
m_actual_scroll = scrolloff;
m_actual_addr_move = addr_inc;
}
//----------------------------------------------------------------------------------------
void LCDi2c_PCF2119x::init()
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x30); // command: functions (1x32, basic set)
Wire.write(0x04 | m_actual_addr_move | m_actual_scroll ); // command: entry mode (increment, no shift)
Wire.write(0x14); // command: Curs_disp_shift (move cursor, right)
Wire.write(0x31); // command: functions (1x32, extended instruction set)
Wire.write(0x04 | m_actual_horizontal_orientation | m_actual_vertical_orientation ); // command: Disp_conf
Wire.write(0x10); // command: Temp_ctl (TC1=0, TC2=0) Display depended
Wire.write(0x42); // command: HV_gen (HV stage 3) Display depended
Wire.write(0x9F); // command: VLCDset (set VLCD, store VA) Display depended
Wire.write(0x30); // command: functions (1x32, basic set)
Wire.write(0x80); // command: DDRAM address = 0x00
Wire.write(0x02); // command: return home
Wire.endTransmission();
waitBusy();
clear();
setDisplayControl();
}
//----------------------------------------------------------------------------------------
void LCDi2c_PCF2119x::begin(int cols,int rows)
{
m_num_col = cols;
m_num_lines = rows;
init();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::command(uint8_t value)
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(value); // command:
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
size_t LCDi2c_PCF2119x::write(uint8_t value)
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x40);
Wire.write(ASCIItoLCD(value));
Wire.endTransmission();
waitBusy();
return(1);
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::clear()
{
if(m_charset == 'R')
{
/* ==================[From datasheet]====================================================
When using character set ‘R’, the following alternative instruction set has to be used:
1. Switch display off (Display_ctl, bit D = 0).
2. Write a blank pattern into all DDRAM addresses (Write_data).
3. Switch display on (Display_ctl, bit D = 1).
====================================================================================== */
active_mode last_active_mode = m_actual_active;
noDisplay();
// Now we erase all 80 characters area because the no visible area
// is used on autoscroll mode
// Maximun bytes on a transmision = 32 so we trunk on 3 blocks to write 80 bytes
Wire.beginTransmission(m_i2caddress);
Wire.write(0x80); // control byte. A control byte after next instruction
Wire.write(0x80); // Set_DDRAM = 0x00
Wire.write(0x40); // control byte Select Data Register
for(int i=0;i<0x1B;i++)
Wire.write(0x91); // White character
Wire.endTransmission();
waitBusy();
Wire.beginTransmission(m_i2caddress);
Wire.write(0x80); // control byte. A control byte after next instruction
Wire.write(0x9B); // Set_DDRAM = 0x1B
Wire.write(0x40); // control byte Select Data Register
for(int i=0;i<0x1B;i++)
Wire.write(0x91); // White character
Wire.endTransmission();
waitBusy();
Wire.beginTransmission(m_i2caddress);
Wire.write(0x80); // control byte. A control byte after next instruction
Wire.write(0xB6); // Set_DDRAM = 0x26
Wire.write(0x40); // control byte Select Data Register
for(int i=0;i<0x1A;i++)
Wire.write(0x91); // White character
Wire.endTransmission();
waitBusy();
home();
if(last_active_mode == actvron)
display();
}
else
command(1); // Clear_display
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::home()
{
command(2); // Return_home
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::display()
{
m_actual_active = actvron;
setDisplayControl();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::noDisplay()
{
m_actual_active = actvoff;
setDisplayControl();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::cursor()
{
m_actual_cursor = crsron;
setDisplayControl();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::noCursor()
{
m_actual_cursor = crsroff;
setDisplayControl();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::blink()
{
m_actual_blink = blkon;
setDisplayControl();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::noBlink()
{
m_actual_blink = blkoff;
setDisplayControl();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::setCursor(uint8_t col, uint8_t row)
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00);
Wire.write(0x80+16*row+col);
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::autoscroll()
{
// scroll area is all 80 character buffer not only visible area
m_actual_scroll = scrollon;
setEntryMode();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::noAutoscroll()
{
m_actual_scroll = scrolloff;
setEntryMode();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::leftToRight()
{
m_actual_addr_move = addr_inc;
setEntryMode();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::rightToLeft()
{
m_actual_addr_move = addr_dec;
setEntryMode();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::scrollDisplayLeft()
{
// Curs_disp_shift on shift operation don't modify either address pointer or data memory
// only change LCD internal index visible area.
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x18); // command: Curs_disp_shift shift to left
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::scrollDisplayRight()
{
// Curs_disp_shift on shift operation don't modify either address pointer or data memory
// only change internal index to visible area.
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x1C); // command: Curs_disp_shift shift to right
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::createChar(uint8_t char_num, uint8_t *rows)
{
char_num &=0x0F;
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
/* ============= From Datasheet ============================
* Only bit 0 to bit 5 of the CGRAM address are set by the Set_CGRAM command.
* Bit 6 can be set using the Set_DDRAM command in the valid address range or
* by using the auto-increment feature during CGRAM write.
===========================================================*/
if(char_num >= 8)
Wire.write(0x80 | (char_num * 8)); // command: set bit6 CGRAM Address via set set_DDRAM command
Wire.write(0x40 | ((char_num * 8) & B00111111)); // command: set CGRAM Address
Wire.endTransmission();
Wire.beginTransmission(m_i2caddress);
Wire.write(0x40); // control byte select data register
for(int i=0;i<8;i++)
Wire.write(rows[i]);
Wire.endTransmission();
waitBusy();
}
// Only for debug
#ifdef DEBUG
//----------------------------------------------------------------------------------------
void LCDi2c_PCF2119x::dump()
{
// Backup address point
uint8_t addpnt = getAddressPoint();
waitBusy();
Wire.beginTransmission(m_i2caddress);
Wire.write(0x80); // control byte. A control byte after next instruction
Wire.write(0x80); // command: DDRAM address = 0x00
Wire.write(0x40); // control byte Select Data Register
Wire.endTransmission();
Wire.requestFrom(m_i2caddress, uint8_t( 32)); // maximun transmission 32 bytes
for(int i=1;Wire.available();i++)
{
char c = Wire.read();
Serial.print(uint8_t(c),HEX);
if(i%16 == 0)
Serial.println("");
else
Serial.print(" ");
}
waitBusy();
Wire.beginTransmission(m_i2caddress);
Wire.write(0x80); // control byte. A control byte after next instruction
Wire.write(0xA0); // command: DDRAM address = 0x20
Wire.write(0x40); // control byte Select Data Register
Wire.endTransmission();
Wire.requestFrom(m_i2caddress, uint8_t( 32));
for(int i=1;Wire.available();i++)
{
char c = Wire.read();
Serial.print(uint8_t(c),HEX);
if(i%16 == 0)
Serial.println("");
else
Serial.print(" ");
}
waitBusy();
Wire.beginTransmission(m_i2caddress);
Wire.write(0x80); // control byte. A control byte after next instruction
Wire.write(0xC0); // command: DDRAM address = 0x40
Wire.write(0x40); // control byte Select Data Register
Wire.endTransmission();
Wire.requestFrom(m_i2caddress, uint8_t( 16));
for(int i=1;Wire.available();i++) // display turn around 80 bytes DDRAM
{ // so we only read 16 bytes
char c = Wire.read();
Serial.print(uint8_t(c),HEX);
if(i%16 == 0)
Serial.println("");
else
Serial.print(" ");
}
// Restore address point
setAddressPoint(addpnt);
}
#endif
//----------------------------------------------------------------------------------
// Specific controller API
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::setAddressPoint(uint8_t newAddr)
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x80 | (newAddr & 0x7F)); // command: DDRAM address = newAddr
Wire.endTransmission();
}
//----------------------------------------------------------------------------------------
uint8_t LCDi2c_PCF2119x::getAddressPoint()
{
uint8_t Result=0xFF;
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte Select Instruction Register
Wire.endTransmission();
Wire.requestFrom(m_i2caddress, uint8_t( 1));
if(Wire.available())
{
Result = Wire.read() & 0x7F; // receive a byte as character
}
return(Result);
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::cursorLeft()
{
// Curs_disp_shift on move cursor operation modify address pointer to correspond to the
// character under cursor.
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x10); // command: Curs_disp_shift move cursor to left
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::cursorRight()
{
// Curs_disp_shift on move cursor operation modify address pointer to correspond to the
// character under cursor.
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x14); // command: Curs_disp_shift move cursor to right
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::normalHorizontalOrientation()
{
m_actual_horizontal_orientation = horizontal_normal;
setDisplayConfig();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::reverseHorizontalOrientation()
{
m_actual_horizontal_orientation = horizontal_reverse;
setDisplayConfig();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::normalVerticalOrientation()
{
m_actual_vertical_orientation = vertical_normal;
setDisplayConfig();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::reverseVerticalOrientation()
{
m_actual_vertical_orientation = vertical_reverse;
setDisplayConfig();
}
//----------------------------------------------------------------------------------
// Private API
//----------------------------------------------------------------------------------------
uint8_t LCDi2c_PCF2119x::isBusy()
{
uint8_t Result = 0x80;
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte select Instruction Register
Wire.endTransmission();
Wire.requestFrom(m_i2caddress, uint8_t( 1));
if(Wire.available())
{
Result = Wire.read() & 0x80;
}
return(Result);
}
//----------------------------------------------------------------------------------------
uint8_t LCDi2c_PCF2119x::waitBusy()
{
#define MAXRETRY 10
int i;
uint8_t Result;
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte select Instruction Register
Wire.endTransmission();
for(i = 0,Result = 0x80 ;
Result && (i < MAXRETRY) ;
i++)
{
Wire.requestFrom(m_i2caddress, uint8_t( 1));
if(Wire.available())
{
Result = Wire.read() & 0x80;
#ifdef DEBUG
if( Result & 0x80)
Serial.println("Busy");
#endif
}
}
return(Result);
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::setDisplayControl()
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x08 | m_actual_active | m_actual_cursor | m_actual_blink); // command: display control (display on, cursor off, blink off)
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::setDisplayConfig()
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x31); // command: functions (1x32, extended instruction set)
Wire.write(0x04 | m_actual_horizontal_orientation | m_actual_vertical_orientation ); // command: Disp_conf
Wire.write(0x30); // command: functions (1x32, extended instruction set)
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
void LCDi2c_PCF2119x::setEntryMode()
{
Wire.beginTransmission(m_i2caddress);
Wire.write(0x00); // control byte
Wire.write(0x04 | m_actual_scroll | m_actual_addr_move); // command: entry mode (increment, no shift)
Wire.endTransmission();
waitBusy();
}
//----------------------------------------------------------------------------------
unsigned char LCDi2c_PCF2119x::ASCIItoLCD(unsigned char ch)
{
/// @todo revise for others charsets
unsigned char c=ch;
if(m_charset == 'R' || m_charset == 'F')
{
if ( ((ch >= 0x20) && (ch <= 0x3F)) || ((ch >= 0x41) && (ch <= 0x5A)) || ((ch >= 0x61) && (ch <= 0x7A)) )
c = 0x80 + ch;
}
return c;
}