forked from espruino/EspruinoDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroveLCDRGB.js
99 lines (91 loc) · 2.61 KB
/
GroveLCDRGB.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
/* Copyright (c) 2015 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. */
var LCD = {
// commands
CLEARDISPLAY : 0x01,
RETURNHOME : 0x02,
ENTRYMODESET : 0x04,
DISPLAYCONTROL : 0x08,
CURSORSHIFT : 0x10,
FUNCTIONSET : 0x20,
SETCGRAMADDR : 0x40,
SETDDRAMADDR : 0x80,
// flags for display entry mode
ENTRYRIGHT : 0x00,
ENTRYLEFT : 0x02,
ENTRYSHIFTINCREMENT : 0x01,
ENTRYSHIFTDECREMENT : 0x00,
// flags for display on/off control
DISPLAYON : 0x04,
DISPLAYOFF : 0x00,
CURSORON : 0x02,
CURSOROFF : 0x00,
BLINKON : 0x01,
BLINKOFF : 0x00,
// flags for display/cursor shift
DISPLAYMOVE : 0x08,
CURSORMOVE : 0x00,
MOVERIGHT : 0x04,
MOVELEFT : 0x00,
// flags for function set
_8BITMODE : 0x10,
_4BITMODE : 0x00,
_2LINE : 0x08,
_1LINE : 0x00,
_5x10DOTS : 0x04,
_5x8DOTS : 0x00
};
/*Text LCD screen with an RGB backlight
--------------------------------
```
var grove = require("GrovePico");
var g = new (require("GroveLCDRGB"))(grove.I2C2);
g.setColor(64,128,255);
g.clear();
g.write("Hello");
g.setCursor(5,1);
g.write("World");
```
*/
function GroveLCDRGB(pins) {
// find the I2C device
this.i2c = I2C.find(pins[0]);
if (!this.i2c) throw "Pins not capable of I2C";
this.i2c.setup({scl:pins[0], sda:pins[1]});
// Init LCD
var lcdType = LCD._2LINE;
this.cmd(LCD.FUNCTIONSET | lcdType);
this.mode = LCD.DISPLAYON | LCD.CURSOROFF | LCD.BLINKOFF;
this.cmd(LCD.DISPLAYCONTROL | this.mode);
this.clear();
// Initialize to default text direction (for romance languages)
this.cmd(LCD.ENTRYMODESET | LCD.ENTRYLEFT | LCD.ENTRYSHIFTDECREMENT);
// Init backlight controller
this.setReg(0,0);
this.setReg(1,0);
this.setReg(8,0xAA);
// white
this.setColor(255,255,255);
}
exports = GroveLCDRGB;
/** Access the LCD PWM driver */
GroveLCDRGB.prototype.setReg = function(a,d) { this.i2c.writeTo(0x62, [a,d]); };
/** Send a command to the LCD */
GroveLCDRGB.prototype.cmd = function(c) { this.i2c.writeTo(0x3E, [0x80,c]); };
/** Send characters to the LCD */
GroveLCDRGB.prototype.write = function(s) {
s = ""+s;
for (var i in s)
this.i2c.writeTo(0x3E, [0x40,s.charCodeAt(i)]);
};
/** Set the LCD backlight color */
GroveLCDRGB.prototype.setColor = function(r,g,b) {
this.setReg(4,r);
this.setReg(3,g);
this.setReg(2,b);
};
// Clear the display
GroveLCDRGB.prototype.clear = function() { this.cmd(LCD.CLEARDISPLAY); };
// Return to home position
GroveLCDRGB.prototype.home = function() { this.cmd(LCD.CLEARDISPLAY); };
// Set cursor position (0-based)
GroveLCDRGB.prototype.setCursor = function(col, row) { this.cmd(row === 0 ? col|0x80 : col|0xc0); };