-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathi2clcd.js
69 lines (61 loc) · 2.12 KB
/
i2clcd.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
module.exports = function(RED) {
LCD = require('./node_modules/i2c-lcd/lib/lcd');
var lcd;
function initLCD() {
lcd.init().then(function() {
return lcd.createChar(0, [0x1b, 0x15, 0x0e, 0x1b, 0x15, 0x1b, 0x15, 0x0e]);
}).then(function() {
return lcd.createChar(1, [0x0c, 0x12, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00]);
}).then(function() {
return lcd.home();
}).then(function() {
return lcd.print("Raspberry Pi " + (String.fromCharCode(0)));
}).then(function() {
return lcd.setCursor(0, 1);
}).then(function() {
return lcd.print("lcd-node");
}).delay(1500);
};
function LcdNode(config) {
console.log("creating LCD node");
RED.nodes.createNode(this,config);
var node = this;
this.LCD_ADDR = parseInt(config.addr);
console.log("LCD node init @ i2c addr:" + this.LCD_ADDR);
lcd = new LCD("/dev/i2c-1",this.LCD_ADDR);
initLCD();
this.on('input', function(msg) {
console.log("LCD input "+msg.topic);
if (msg.topic.localeCompare("init") == 0) {
lcd.init();
}
if (msg.topic.localeCompare("clear") == 0) {
lcd.clear();
}
if (msg.topic.localeCompare("on") == 0) {
lcd.on();
}
if (msg.topic.localeCompare("off") == 0) {
lcd.off();
}
if (msg.topic.localeCompare("line1") == 0) {
lcd.setCursor(0,0).then(function() {
lcd.print(msg.payload); });
}
if (msg.topic.localeCompare("line2") == 0) {
lcd.setCursor(0,1).then(function() {
lcd.print(msg.payload); });
}
if (msg.topic.localeCompare("line3") == 0) {
lcd.setCursor(0,2).then(function() {
lcd.print(msg.payload); });
}
if (msg.topic.localeCompare("line4") == 0) {
lcd.setCursor(0,3).then(function() {
lcd.print(msg.payload); });
}
node.send(msg); //pass message through
});
}
RED.nodes.registerType("i2clcd",LcdNode);
}