-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c-lcd.js
71 lines (68 loc) · 2.39 KB
/
i2c-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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Source: https://www.switch-science.com/catalog/1407/
var Lcd = function(i2cPort, slaveAddress) {
this.i2cPort = i2cPort;
this.i2cSlave = null;
this.slaveAddress = slaveAddress;
this.contrast = 35;
};
Lcd.prototype = {
init: function() {
return new Promise((resolve, reject) => {
this.i2cPort.open(this.slaveAddress).then((i2cSlave) => {
this.i2cSlave = i2cSlave;
this.i2cSlave.write8(0x00, 0b00111000); // function set
this.i2cSlave.write8(0x00, 0b00111001); // function set
this.i2cSlave.write8(0x00, 0b00000100); // EntryModeSet
this.i2cSlave.write8(0x00, 0b00010100); // interval osc
this.i2cSlave.write8(0x00, 0b01110000 | (this.contrast & 0xF)); // contrast Low
this.i2cSlave.write8(0x00, 0b01011100 | ((this.contrast >> 4) & 0x3)); // contast High/icon/power
this.i2cSlave.write8(0x00, 0b01101100); // follower control
this.i2cSlave.write8(0x00, 0b00111000); // function set
this.i2cSlave.write8(0x00, 0b00001100); // Display On
this.i2cSlave.write8(0x00, 0b00000001); // Clear Display
console.log("init ok:"+this.i2cSlave);
resolve();
}, (err) => {
console.log("Lcd.init() Error: " + error.message);
reject(err);
});
});
},
clearDisplay: function() {
return new Promise(async (resolve, reject) => {
if (this.i2cSlave === null) {
reject("i2cSlave Address des'nt yet open!");
} else {
await this.i2cSlave.write8(0x00, 0x01);
}
});
},
drawString: function(str) {
return new Promise(async (resolve, reject) => {
if (this.i2cSlave === null) {
reject("i2cSlave Address does'nt yet open!");
} else if (str.length > 16) {
reject("string must be shorter than 16 characters!");
} else {
this.clearDisplay().catch(e => {
reject(e);
});
await this.i2cSlave.write8(0x00, 0x80);
for (var i = 0; i < str.length; i++) {
if (i === 8) {
await this.i2cSlave.write8(0x00, 0x80 | 0x40);
}
if (i === str.length - 1) {
await this.i2cSlave.write8(0b01000000, str.charAt(i).charCodeAt());
} else {
await this.i2cSlave.write8(0b11000000, str.charAt(i).charCodeAt());
}
}
}
});
},
setContrast: function(contrast) {
this.contrast = contrast;
this.init();
}
};