forked from craigmw/lcdi2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcdi2c.js
342 lines (292 loc) · 10.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
/**
* 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
};
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.write4(0x30, this.displayPorts.CMD); //initialization
this._sleep(200);
//this.write4(0x32, this.displayPorts.CMD); //initialization
this.write4(0x30, this.displayPorts.CMD); //initialization
this._sleep(100);
//this.write4(0x06, this.displayPorts.CMD); //initialization
this.write4(0x30, 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.CMD); //Turn on backlight.
return this;
};
_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;
};
writeBlock(x, c) {
this.write4Block(x, c);
this.write4Block(x << 4, c);
return this;
};
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;