-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollphat.js
275 lines (249 loc) · 9.45 KB
/
scrollphat.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// http://www.issi.com/WW/pdf/31FL3730.pdf for LED driver chip spec
module.exports = function (RED) {
var i2c = require("i2c-bus");
//Bus address of the I2C bus
var BUS_ADDRESS = 1;
// I2C Address of the ScrollPhat
var SP_ADDRESS = 0x60;
// Starting address for the matrix 1 data register
var SP_COMMAND = 0x01;
// Address for configuration register
var SP_MODE_COMMAND = 0x00;
// Address for brightness register
var SP_BRIGHTNESS_COMMAND = 0x19;
// Configuration register set to 5x11 LED matrix
var SP_MODE_5X11 = Buffer.alloc(1, 3);
//buffer for pixel data which will be written the matrix 1 data register
var buffer = Buffer.alloc(12, 0);
//After the 11 columns, we need to send 0xFF to finish the message to the I2C slave
buffer.writeUInt8(0xff, 11);
// Buffer for brightness level
var brightness = Buffer.alloc(1);
var initok = (function() {
try {
var scrollphat = i2c.openSync(BUS_ADDRESS);
scrollphat.writeI2cBlockSync(SP_ADDRESS, SP_MODE_COMMAND, 1, SP_MODE_5X11);
scrollphat.closeSync();
console.log("Init OK");
} catch (e) {
throw ("Failed to initialise scrollphat: " + e);
return false;
}
})();
function spSetPixelNode(config) {
RED.nodes.createNode(this,config);
var node = this;
try {
var scrollphat = i2c.openSync(BUS_ADDRESS);
if (scrollphat.hasOwnProperty("_forceAccess")) {
node.status({fill: "green", shape: "dot", text: "connected"});
} else
{
node.status({fill: "red", shape: "ring", text: "error"});
throw "I2C connection error";
}
} catch (e) {
node.error("There was a problem: " + e);
}
node.on("input", function(msg) {
// Preflight check on msg.payload - must have a row, a column and a value (true/false for on/off)
if (typeof msg.payload.x === "number" && typeof msg.payload.y === "number" && typeof msg.payload.value === "boolean") {
if ( 0 > msg.payload.x || msg.payload.x > 10) { node.error("Column is out of bounds", msg); }
if ( 0 > msg.payload.y || msg.payload.y > 31) { node.error("Row is out of bounds", msg); }
node.xPos = msg.payload.x || 0;
node.yPos = msg.payload.y || 0;
node.ledValue = msg.payload.value || false;
node.additive = msg.payload.additive && true;
//Add LEDs to existing pattern by default or reset to just new pixel if user specifically sets msg.payload.additive to false
if (node.additive === false){ buffer.fill(0x00,0,11); }
//This is transcribed from set_pixel in https://github.com/pimoroni/scroll-phat/blob/master/library/scrollphat/IS31FL3730.py
//Lots of bitwise shifting to set the right pixel in column x ( buffer elements 0-10) and row y (bits 0-4).
if (node.ledValue) { buffer[node.xPos] |= (1 << node.yPos); } else { buffer[node.xPos] &= ~(1 << node.yPos); }
}
//Write the entire buffer to the Scroll Phat
scrollphat.writeI2cBlockSync(SP_ADDRESS, SP_COMMAND, 12, buffer);
});
node.on("close", function() {
try {
scrollphat.closeSync();
if (scrollphat._peripherals.length === 0) {
//node.warn("Scroll Phat i2c connection closed.");
} else {throw "Failed to close scrollphat connection";}
} catch (e) {
node.error("Error: " + e);
}
});
}
function spClearNode(config) {
RED.nodes.createNode(this,config);
var node = this;
try {
var scrollphat = i2c.openSync(BUS_ADDRESS);
if (scrollphat.hasOwnProperty("_forceAccess")) {
node.status({fill: "green", shape: "dot", text: "connected"});
} else {
node.status({fill: "red", shape: "ring", text: "error"});
throw "I2C connection error";
}
} catch (e) {
node.error("There was a problem: " + e);
}
node.on("input", function(msg) {
// Preflight check on msg.payload
if (msg.payload === true || msg.payload.toString().toLowerCase() === "on" || msg.payload === 1) {
buffer.fill(0x1F,0,11);
} else if (msg.payload === false || msg.payload.toString().toLowerCase() === "off" || msg.payload === 0) {
buffer.fill(0x00,0,11);
} else {
node.warn("Invalid Scroll pHAT clear msg.payload");
}
//Write the entire buffer to the Scroll Phat
scrollphat.writeI2cBlockSync(SP_ADDRESS, SP_COMMAND, 12, buffer);
});
node.on("close", function() {
try {
scrollphat.closeSync();
if (scrollphat._peripherals.length === 0) {
// node.warn("Scroll Phat i2c connection closed.");
} else {throw "Failed to close scrollphat connection";}
} catch (e) {
node.error("Error: " + e);
}
});
}
function spBrightnessNode(config) {
RED.nodes.createNode(this,config);
var node = this;
try {
var scrollphat = i2c.openSync(BUS_ADDRESS);
if (scrollphat.hasOwnProperty("_forceAccess")) {
node.status({fill: "green", shape: "dot", text: "connected"});
} else {
node.status({fill: "red", shape: "ring", text: "error"});
throw "I2C connection error";
}
} catch (e) {
node.error("There was a problem: " + e);
}
node.on("input", function(msg) {
// Preflight check on msg.payload
if (msg.payload >= 0 && msg.payload <= 100) {
//Need an integer that's definitely between 0 and 128
brightness[0] = Math.max(0,(Math.min(128,(Math.round((128*msg.payload) / 100)))));
} else {
node.warn("Invalid Scroll pHAT brightness level");
}
//Write the entire buffer to the Scroll Phat
scrollphat.writeI2cBlockSync(SP_ADDRESS, SP_BRIGHTNESS_COMMAND, 1, brightness);
});
node.on("close", function() {
try {
scrollphat.closeSync();
if (scrollphat._peripherals.length === 0) {
// node.warn("Scroll Phat i2c connection closed.");
} else {throw "Failed to close scrollphat connection";}
} catch (e) {
node.error("Error: " + e);
}
});
}
function spColumnNode(config) {
RED.nodes.createNode(this,config);
var node = this;
try {
var scrollphat = i2c.openSync(BUS_ADDRESS);
if (scrollphat.hasOwnProperty("_forceAccess")) {
node.status({fill: "green", shape: "dot", text: "connected"});
} else {
node.status({fill: "red", shape: "ring", text: "error"});
throw "I2C connection error";
}
} catch (e) {
node.error("There was a problem: " + e);
}
node.on("input", function(msg) {
// Preflight check on msg.payload
if (msg.payload.x >= 0 && msg.payload.x <= 10) {
//set percent if not set; correct invalid percentage
var spPercent = Math.max(0,Math.min(100,msg.payload.percent)) || 100;
buffer[msg.payload.x] = 0x00;
switch (true) {
case spPercent == 100:
buffer[msg.payload.x] |= (1 << 0);
case spPercent >= 80:
buffer[msg.payload.x] |= (1 << 1);
case spPercent >= 60:
buffer[msg.payload.x] |= (1 << 2);
case spPercent >= 40:
buffer[msg.payload.x] |= (1 << 3);
case spPercent >= 20:
buffer[msg.payload.x] |= (1 << 4);
break;
default:
}
} else {
node.warn("Invalid column specified");
}
//Write the entire buffer to the Scroll Phat
scrollphat.writeI2cBlockSync(SP_ADDRESS, SP_COMMAND, 12, buffer);
});
node.on("close", function() {
try {
scrollphat.closeSync();
if (scrollphat._peripherals.length === 0) {
// node.warn("Scroll Phat i2c connection closed.");
} else {throw "Failed to close scrollphat connection";}
} catch (e) {
node.error("Error: " + e);
}
});
}
function spRowNode(config) {
RED.nodes.createNode(this,config);
var node = this;
try {
var scrollphat = i2c.openSync(BUS_ADDRESS);
if (scrollphat.hasOwnProperty("_forceAccess")) {
node.status({fill: "green", shape: "dot", text: "connected"});
} else {
node.status({fill: "red", shape: "ring", text: "error"});
throw "I2C connection error";
}
} catch (e) {
node.error("There was a problem: " + e);
}
node.on("input", function(msg) {
// Preflight check on msg.payload
if (msg.payload.y >= 0 && msg.payload.y <= 4) {
//set percent if not set; correct invalid percentage
var spPercent = Math.max(0,Math.min(100,msg.payload.percent)) || 100;
for (var ix = 0; ix < 11; ix++){
if (spPercent >= 100*((ix+1)/11) ) {
buffer[ix] |= (1 << msg.payload.y);
} else {
buffer[ix] &= ~(1 << msg.payload.y);
}
}
} else {
node.warn("Invalid row specified");
}
//Write the entire buffer to the Scroll Phat
scrollphat.writeI2cBlockSync(SP_ADDRESS, SP_COMMAND, 12, buffer);
});
node.on("close", function() {
try {
scrollphat.closeSync();
if (scrollphat._peripherals.length === 0) {
// node.warn("Scroll Phat i2c connection closed.");
} else {throw "Failed to close scrollphat connection";}
} catch (e) {
node.error("Error: " + e);
}
});
}
RED.nodes.registerType("spSetPixel", spSetPixelNode);
RED.nodes.registerType("spClear", spClearNode);
RED.nodes.registerType("spBrightness", spBrightnessNode);
RED.nodes.registerType("spColumn", spColumnNode);
RED.nodes.registerType("spRow", spRowNode);
};