-
Notifications
You must be signed in to change notification settings - Fork 10
/
lcdi2c.js
454 lines (390 loc) · 13.9 KB
/
lcdi2c.js
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
/**
* lcdi2c.js - Add I2C LCD character display using PCF8574 I2C port expander.
* https://github.com/wilberforce/lcd-pcf8574 but replaced calls to i2c library with
* calls to i2c-bus. Currently, only works in synchronous output mode. Asynch mode does not work.
* LCD i2c interface via PCF8574P
* http://dx.com/p/lcd1602-adapter-board-w-iic-i2c-interface-black-works-with-official-arduino-boards-216865
* https://gist.github.com/chrisnew/6725633
* http://www.espruino.com/HD44780
* http://www.espruino.com/LCD1602
*/
const i2c = require('i2c-bus');
const sleep = require('sleep');
let LCD = class LCD {
constructor(device, address, cols, rows) {
this.displayPorts = {
RS: 0x01,
E: 0x04,
D4: 0x10,
D5: 0x20,
D6: 0x40,
D7: 0x80,
CHR: 1,
CMD: 0,
backlight: 0x08,
RW: 0x20 // not used
};
// <<<<<<< alexfederlin-patch-1
var buffer = new Buffer(3); //Required for printlnBuffer.
//LCD() - Initialize LCD object.
// device: I2C bus number; 0 for rev. 1 boards, 1 for rev. 2+ boards.
// address: Address of device (use i2cdetect to determine this)
// cols: columns supported by display (e.g. 16 or 20)
// rows: rows supported by display (e.g. 2 or 4 )
var LCD = function (device, address, cols, rows ) {
//this.i2c = new i2c(address, {
// device : device
// });
this.device = device;
this.address = address;
this.cols = cols;
this.rows = rows;
this.error = null;
this.i2c = i2c.open( device, function( err ) {
if ( err ) {
console.log( 'Unable to open I2C port on device ' + device + ' ERROR: ' + err );
console.log( this );
this.error = err;
return this
};
});
//console.log( 'Opened I2C port on bus ' + device + ' for LCD at address 0x' + address.toString( 16 ) + '.' );
this._sleep(1000);
this.init();
return this;
};
// commands
LCD.CLEARDISPLAY = 0x01;
LCD.RETURNHOME = 0x02;
LCD.ENTRYMODESET = 0x04;
LCD.DISPLAYCONTROL = 0x08;
LCD.CURSORSHIFT = 0x10;
LCD.FUNCTIONSET = 0x20;
LCD.SETCGRAMADDR = 0x40;
LCD.SETDDRAMADDR = 0x80;
//# flags for display entry mode
LCD.ENTRYRIGHT = 0x00;
LCD.ENTRYLEFT = 0x02;
LCD.ENTRYSHIFTINCREMENT = 0x01;
LCD.ENTRYSHIFTDECREMENT = 0x00;
//# flags for display on/off control
LCD.DISPLAYON = 0x04;
LCD.DISPLAYOFF = 0x00;
LCD.CURSORON = 0x02;
LCD.CURSOROFF = 0x00;
LCD.BLINKON = 0x01;
LCD.BLINKOFF = 0x00;
//# flags for display/cursor shift
LCD.DISPLAYMOVE = 0x08;
LCD.CURSORMOVE = 0x00;
LCD.MOVERIGHT = 0x04;
LCD.MOVELEFT = 0x00;
//# flags for function set
LCD._8BITMODE = 0x10;
LCD._4BITMODE = 0x00;
LCD._2LINE = 0x08;
LCD._1LINE = 0x00;
LCD._5x10DOTS = 0x04;
LCD._5x8DOTS = 0x00;
//Line addresses.
LCD.LINEADDRESS = [];
LCD.LINEADDRESS[1] = 0x80;
LCD.LINEADDRESS[2] = 0xC0;
LCD.LINEADDRESS[3] = 0x94;
LCD.LINEADDRESS[4] = 0xD4;
LCD.prototype.init = function (){
this.write4( 0x33, displayPorts.CMD); //initialization
this._sleep(200);
this.write4(0x32, displayPorts.CMD); //initialization
this._sleep(100);
this.write4( 0x06, displayPorts.CMD); //initialization
this._sleep(100);
this.write4( 0x28, displayPorts.CMD); //initialization
this._sleep(100);
this.write4( 0x01, displayPorts.CMD); //initialization
this._sleep(100);
this.write4(LCD.FUNCTIONSET | LCD._4BITMODE | LCD._2LINE | LCD._5x10DOTS, displayPorts.CMD); //4 bit - 2 line 5x7 matrix
this._sleep(10);
this.write( LCD.DISPLAYCONTROL | LCD.DISPLAYON, displayPorts.CMD); //turn cursor off 0x0E to enable cursor
this._sleep(10);
this.write( LCD.ENTRYMODESET | LCD.ENTRYLEFT, displayPorts.CMD); //shift cursor right
this._sleep(10);
this.write( LCD.CLEARDISPLAY, displayPorts.CMD); // LCD clear
this.write( displayPorts.backlight, displayPorts.CHR ); //Turn on backlight.
}
LCD.prototype._sleep = function (milli) {
sleep.usleep(milli * 1000);
};
// =======
this.buffer = new Buffer(3); //Required for printlnBuffer.
// commands
this.CLEARDISPLAY = 0x01;
this.RETURNHOME = 0x02;
this.ENTRYMODESET = 0x04;
this.DISPLAYCONTROL = 0x08;
this.CURSORSHIFT = 0x10;
this.FUNCTIONSET = 0x20;
this.SETCGRAMADDR = 0x40;
this.SETDDRAMADDR = 0x80;
//# flags for display entry mode
this.ENTRYRIGHT = 0x00;
this.ENTRYLEFT = 0x02;
this.ENTRYSHIFTINCREMENT = 0x01;
this.ENTRYSHIFTDECREMENT = 0x00;
//# flags for display on/off control
this.DISPLAYON = 0x04;
this.DISPLAYOFF = 0x00;
this.CURSORON = 0x02;
this.CURSOROFF = 0x00;
this.BLINKON = 0x01;
this.BLINKOFF = 0x00;
//# flags for display/cursor shift
this.DISPLAYMOVE = 0x08;
this.CURSORMOVE = 0x00;
this.MOVERIGHT = 0x04;
this.MOVELEFT = 0x00;
//# flags for function set
this._8BITMODE = 0x10;
this._4BITMODE = 0x00;
this._2LINE = 0x08;
this._1LINE = 0x00;
this._5x10DOTS = 0x04;
this._5x8DOTS = 0x00;
//Line addresses.
this.LINEADDRESS = [0x80, 0xC0, 0x94, 0xD4];
this.device = device;
this.address = address;
this.cols = cols;
this.rows = rows;
this.error = null;
this.i2c = null;
this._init();
};
_init() {
this.i2c = i2c.open(this.device, function (err) {
if (err) {
console.log('Unable to open I2C port on device ' + device + ' ERROR: ' + err);
console.log(this);
this.error = err;
return this
}
});
this._sleep(1000);
this.write4(0x33, this.displayPorts.CMD); //initialization
this._sleep(200);
this.write4(0x32, this.displayPorts.CMD); //initialization
this._sleep(100);
this.write4(0x06, this.displayPorts.CMD); //initialization
this._sleep(100);
this.write4(0x28, this.displayPorts.CMD); //initialization
this._sleep(100);
this.write4(0x01, this.displayPorts.CMD); //initialization
this._sleep(100);
this.write4(this.FUNCTIONSET | this._4BITMODE | this._2LINE | this._5x10DOTS, this.displayPorts.CMD); //4 bit - 2 line 5x7 matrix
this._sleep(10);
this.write(this.DISPLAYCONTROL | this.DISPLAYON, this.displayPorts.CMD); //turn cursor off 0x0E to enable cursor
this._sleep(10);
this.write(this.ENTRYMODESET | this.ENTRYLEFT, this.displayPorts.CMD); //shift cursor right
this._sleep(10);
this.write(this.CLEARDISPLAY, this.displayPorts.CMD); // LCD clear
this.write(this.displayPorts.backlight, this.displayPorts.CHR); //Turn on backlight.
return this;
};
// >>>>>>> master
_sleep(milli) {
sleep.usleep(milli * 1000);
};
write4(x, c) {
try {
let a = (x & 0xF0); // Use upper 4 bit nibble
this.i2c.sendByteSync(this.address, a | this.displayPorts.backlight | c);
this.i2c.sendByteSync(this.address, a | this.displayPorts.E | this.displayPorts.backlight | c);
this.i2c.sendByteSync(this.address, a | this.displayPorts.backlight | c);
} catch (err) {
this.error = err;
}
this._sleep(2);
};
write4Async(x, c) {
let a = (x & 0xF0); // Use upper 4 bit nibble
this.i2c.sendByte(this.address, a | this.displayPorts.backlight | c, (err) => {
if (err) {
this.error = err;
}
});
this.i2c.sendByte(this.address, a | this.displayPorts.E | this.displayPorts.backlight | c, (err) => {
if (err) {
this.error = err;
}
});
this.i2c.sendByte(this.address, a | this.displayPorts.backlight | c, (err) => {
if (err) {
this.error = err;
}
});
//Had to add this as it fixes a weird bug where the display was showing garbled text after a few minutes
//Found this solution by accident though...
this.i2c.sendByte(this.address, a | this.displayPorts.backlight | c, (err) => {
if (err) {
this.error = err;
}
});
};
write4Block(x, c) {
let a = (x & 0xF0 );
this.buffer[0] = a | this.displayPorts.backlight | c;
this.buffer[1] = a | this.displayPorts.E | this.displayPorts.backlight | c;
this.buffer[2] = a | this.displayPorts.backlight | c;
this.i2c.writeI2cBlockSync(this.address, 1, this.buffer.length, this.buffer);
this._sleep(2);
};
write(x, c) {
this.write4(x, c);
this.write4(x << 4, c);
return this;
};
writeAsync(x, c) {
this.write4Async(x, c);
this.write4Async(x << 4, c);
return this;
};
// <<<<<<< alexfederlin-patch-1
//Set cursor to correct line.
if ( line > 0 && line <= this.rows ) {
this.write( LCD.LINEADDRESS[line], displayPorts.CMD );
this._sleep(2);
};
// =======
writeBlock(x, c) {
this.write4Block(x, c);
this.write4Block(x << 4, c);
return this;
};
//>>>>>>> master
clear() {
return this.write(this.CLEARDISPLAY, this.displayPorts.CMD);
};
print(str) {
if (typeof str === 'string') {
for (let i = 0; i < str.length; i++) {
let c = str[i].charCodeAt(0);
this.write(c, this.displayPorts.CHR);
this._sleep(2);
}
}
return this;
};
printAsync(str) {
if (typeof str === 'string') {
for (let i = 0; i < str.length; i++) {
let c = str[i].charCodeAt(0);
this.writeAsync(c, this.displayPorts.CHR);
//this._sleep(2);
}
}
return this;
};
printBlock(str) {
if (typeof str === 'string') {
for (let i = 0; i < str.length; i++) {
let c = str[i].charCodeAt(0);
this.writeBlock(c, this.displayPorts.CHR);
this._sleep(2);
}
}
};
println(str, line) {
if (typeof str === 'string') {
//Set cursor to correct line.
if (line > 0 && line <= this.rows) {
this.write(this.LINEADDRESS[line - 1], this.displayPorts.CMD);
}
this.print(str.substring(0, this.cols));
}
return this;
};
printlnAsync(str, line) {
if (typeof str === 'string') {
//Set cursor to correct line.
if (line > 0 && line <= this.rows) {
this.writeAsync(this.LINEADDRESS[line - 1], this.displayPorts.CMD);
}
this.printAsync(str.substring(0, this.cols));
}
return this;
};
printlnBlock(str, line) {
if (typeof str === 'string') {
if (line > 0) {
this.write(this.LINEADDRESS[line - 1], this.displayPorts.CMD);
}
//Now, write block to i2c.
this.printBlock(str.substring(0, this.cols));
}
return this;
};
/** flashing block for the current cursor */
cursorFull() {
return this.write(this.DISPLAYCONTROL | this.DISPLAYON | this.CURSORON | this.BLINKON, this.displayPorts.CMD);
};
/** small line under the current cursor */
cursorUnder() {
return this.write(this.DISPLAYCONTROL | this.DISPLAYON | this.CURSORON | this.BLINKOFF, this.displayPorts.CMD);
}
/** set cursor pos, top left = 0,0 */
setCursor(x, y) {
let l = [0x00, 0x40, 0x14, 0x54];
return this.write(this.SETDDRAMADDR | (l[y] + x), this.displayPorts.CMD);
}
/** set cursor to 0,0 */
home() {
return this.write(this.SETDDRAMADDR | 0x00, this.displayPorts.CMD);
}
/** Turn underline cursor off */
blinkOff() {
return this.write(this.DISPLAYCONTROL | this.DISPLAYON | this.CURSOROFF | this.BLINKOFF, this.displayPorts.CMD);
}
/** Turn underline cursor on */
blinkOn() {
return this.write(this.DISPLAYCONTROL | this.DISPLAYON | this.CURSORON | this.BLINKOFF, this.displayPorts.CMD);
}
/** Turn block cursor off */
cursorOff() {
return this.write(this.DISPLAYCONTROL | this.DISPLAYON | this.CURSOROFF | this.BLINKON, this.displayPorts.CMD);
}
/** Turn block cursor on */
cursorOn() {
return this.write(this.DISPLAYCONTROL | this.DISPLAYON | this.CURSORON | this.BLINKON, this.displayPorts.CMD);
}
/** setBacklight */
setBacklight(val) {
if (val > 0) {
this.displayPorts.backlight = 0x08;
} else {
this.displayPorts.backlight = 0x00;
}
return this.write(this.DISPLAYCONTROL, this.displayPorts.CMD);
}
/** setContrast stub */
// setContrast(val) {
// return this.write(this.DISPLAYCONTROL, this.displayPorts.CMD);
// }
/** Turn display off */
off() {
this.displayPorts.backlight = 0x00;
return this.write(this.DISPLAYCONTROL | this.DISPLAYOFF, this.displayPorts.CMD);
}
/** Turn display on */
on() {
this.displayPorts.backlight = 0x08;
return this.write(this.DISPLAYCONTROL | this.DISPLAYON, this.displayPorts.CMD);
}
/** set special character 0..7, data is an array(8) of bytes, and then return to home addr */
createChar(ch, data) {
this.write(this.SETCGRAMADDR | ((ch & 7) << 3), this.displayPorts.CMD);
for (let i = 0; i < 8; i++)
this.write(data[i], this.displayPorts.CHR);
return this.write(this.SETDDRAMADDR, this.displayPorts.CMD);
}
};
module.exports = LCD;