This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd.js
56 lines (48 loc) · 1.44 KB
/
lcd.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
var i2c = require('i2c-bus');
var sleep = require('sleep');
var DISPLAY_RGB_ADDR = 0x62;
var DISPLAY_TEXT_ADDR = 0x3e;
//Public
module.exports = LCD;
function LCD(port) {
this.port = port;
}
LCD.prototype.openSync = function() {
this.i2c1 = i2c.openSync(this.port);
}
LCD.prototype.closeSync = function() {
this.i2c1.closeSync();
}
LCD.prototype.setRGB = function(r, g, b) {
this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,0,0)
this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,1,0)
this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,0x08,0xaa)
this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,4,r)
this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,3,g)
this.i2c1.writeByteSync(DISPLAY_RGB_ADDR,2,b)
}
LCD.prototype.textCommand = function(cmd) {
this.i2c1.writeByteSync(DISPLAY_TEXT_ADDR, 0x80, cmd);
}
LCD.prototype.setText = function(text) {
this.textCommand(0x01) // clear display
sleep.usleep(50000);
this.textCommand(0x08 | 0x04) // display on, no cursor
this.textCommand(0x28) // 2 lines
sleep.usleep(50000);
var count = 0;
var row = 0;
for(var i = 0, len = text.length; i < len; i++) {
if(text[i] === '\n' || count === 16) {
count = 0;
row ++;
if(row === 2)
break;
this.textCommand(0xc0)
if(text[i] === '\n')
continue;
}
count++;
this.i2c1.writeByteSync(DISPLAY_TEXT_ADDR, 0x40, text[i].charCodeAt(0));
}
}