-
Notifications
You must be signed in to change notification settings - Fork 126
/
i2c-lcd.js
52 lines (43 loc) · 1.19 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
var rpio = require('../lib/rpio');
/*
* Magic numbers to initialise the i2c display device and write output,
* cribbed from various python drivers.
*/
var init = new Buffer([0x03, 0x03, 0x03, 0x02, 0x28, 0x0c, 0x01, 0x06]);
var LCD_LINE1 = 0x80, LCD_LINE2 = 0xc0;
var LCD_ENABLE = 0x04, LCD_BACKLIGHT = 0x08;
/*
* Data is written 4 bits at a time with the lower 4 bits containing the mode.
*/
function lcdwrite4(data)
{
rpio.i2cWrite(Buffer([(data | LCD_BACKLIGHT)]));
rpio.i2cWrite(Buffer([(data | LCD_ENABLE | LCD_BACKLIGHT)]));
rpio.i2cWrite(Buffer([((data & ~LCD_ENABLE) | LCD_BACKLIGHT)]));
}
function lcdwrite(data, mode)
{
lcdwrite4(mode | (data & 0xF0));
lcdwrite4(mode | ((data << 4) & 0xF0));
}
/*
* Write a string to the specified LCD line.
*/
function lineout(str, addr)
{
lcdwrite(addr, 0);
str.split('').forEach(function (c) {
lcdwrite(c.charCodeAt(0), 1);
});
}
/*
* We can now start the program, talking to the i2c LCD at address 0x27.
*/
rpio.i2cBegin();
rpio.i2cSetSlaveAddress(0x27);
rpio.i2cSetBaudRate(10000);
for (var i = 0; i < init.length; i++)
lcdwrite(init[i], 0);
lineout('node.js i2c LCD!', LCD_LINE1);
lineout('npm install rpio', LCD_LINE2);
rpio.i2cEnd();