-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA9535-gpio.js
348 lines (323 loc) · 14.2 KB
/
PCA9535-gpio.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//Сделал опрос всех портоа по прерыванию 1-я версия
module.exports = function (RED) {
const i2c = require('i2c-bus');
const Gpio = require('onoff').Gpio;
const INPUT_PORT_REG = 0x0;
const INPUT_PORT_REG1 = 0x1;
const OUTPUT_PORT_REG = 0x2;
const OUTPUT_PORT_REG1 = 0x3;
const CONFIG_REG = 0x6;
const CONFIG_REG1 = 0x7;
const DI1_ADRESS = 0x20;
const DI2_ADRESS = 0x21;
const DI_IRQ = 11;
const DI_CONFIG_IN = 0xff;
const DI_CONFIG_OUT = 0x00;
let old_state_pin = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false];
function setTrue(node) {
node.status({ fill: "green", shape: "dot", text: "true" });
}
function setFalse(node) {
node.status({ fill: "blue", shape: "dot", text: "false" });
}
function setWrongProperties(node) {
node.status({ fill: "red", shape: "ring", text: "Wrong properties" });
}
function setOk(node) {
node.status({ fill: "yellow", shape: "dot", text: "OK" });
}
function setWrongPayload(node) {
node.status({ fill: "red", shape: "ring", text: "Wrong payload" });
}
function checkStatus(state) {
return state.pin >= 1 && state.pin <= 16 && state.bus >= 0 && state.bus <= 7
}
function setStatus(node) {
if (checkStatus(node)) setOk(node)
else setWrongProperties(node)
}
function setNode(config, node) {
RED.nodes.createNode(node, config);
node.pin = parseInt(config.pin)
node.bus = parseInt(config.bus)
node.address = parseInt(config.address, 16)
node.gpioint = parseInt(config.gpioint)
}
function setNodeDI(config, node) {
RED.nodes.createNode(node, config);
node.pin = parseInt(config.pin)
node.bus = parseInt(config.bus)
node.address = parseInt(config.address, 16)
node.gpioint = parseInt(config.gpioint)
}
function DI_CONTROLLER(config) {
setNodeDI(config, this)
setStatus(this)
//выполнение при старте ноды
// let old_state_pin = false;
let state_pin;
// setTimeout(function() {
if (checkStatus(this)) {
//запись текущей конфигурации пина
//read actual config
const i2cX = i2c.openSync(this.bus);
i2cX.writeByteSync(DI1_ADRESS, CONFIG_REG, DI_CONFIG_IN);
i2cX.writeByteSync(DI1_ADRESS, CONFIG_REG1, DI_CONFIG_IN);
i2cX.writeByteSync(DI2_ADRESS, CONFIG_REG1, DI_CONFIG_IN);
// Функция чтения состояние пинов при старте
let input = i2cX.readByteSync(DI1_ADRESS, INPUT_PORT_REG); // Read date DI1-8
let input1 = i2cX.readByteSync(DI1_ADRESS, INPUT_PORT_REG1); // Read date DI9-16
let input2 = i2cX.readByteSync(DI2_ADRESS, INPUT_PORT_REG); // Read date Output for reset IRQ
input2 = i2cX.readByteSync(DI2_ADRESS, INPUT_PORT_REG1); // Read date DI16-24
//read actual DI 1-24
var pin;
for (pin=0; pin<25; pin++) {
if (pin<=7) {
state_pin = (input & (1 << pin));
var msgs = new Array(24);
if (state_pin > 0) {
msgs [pin] = {payload: false}
} else {
msgs [pin] = {payload: true}
}
old_state_pin [pin] = state_pin;
}
if ((pin>=8) && (pin<=15)) {
state_pin = (input1 & (1 << pin-8));
var msgs = new Array(24);
if (state_pin > 0) {
msgs [pin] = {payload: false}
} else {
msgs [pin] = {payload: true}
old_state_pin [pin] = state_pin;
}
}
if ((pin>=16) && (pin<=24)) {
state_pin = (input2 & (1 << pin-16));
var msgs = new Array(24);
if (state_pin > 0) {
msgs [pin] = {payload: false}
} else {
msgs [pin] = {payload: true}
}
old_state_pin [pin] = state_pin;
}
}
this.send(msgs);
/*
if (this.pin <= 8) {
//read actual DI 1-8
let input = i2cX.readByteSync(this.address, INPUT_PORT_REG);
state_pin = (input & (1 << (this.pin - 1)));
if (state_pin > 0) {
var msg = {payload: false}
setFalse(this)
} else {
msg = {payload: true}
setTrue(this)
}
this.send(msg);
old_state_pin = state_pin;
} else {
//read actual input 8-15
let input1 = i2cX.readByteSync(this.address, INPUT_PORT_REG1);
state_pin1 = (input1 & (1 << (this.pin - 9)));
if (state_pin1 > 0) {
msg = {payload: false}
setFalse(this)
} else {
msg = {payload: true}
setTrue(this)
}
old_state_pin1 = state_pin1;
this.send(msg);
}
*/
} else {
setWrongProperties(this)
}
// }, 3000);
// выполнение чтения пинов при прерывании gpio
const button = new Gpio(DI_IRQ, 'in', 'falling');
button.watch((err, value) => {
if (err) {
throw err;
this.status({fill:"red",shape:"ring",text:"error irq gpio"});
} else {
const i2cX = i2c.openSync(this.bus);
let input = i2cX.readByteSync(DI1_ADRESS, INPUT_PORT_REG); // Read dtate DI1-8
let input1 = i2cX.readByteSync(DI1_ADRESS, INPUT_PORT_REG1); // Read dtate DI9-16
let input2 = i2cX.readByteSync(DI2_ADRESS, INPUT_PORT_REG1); // Read dtate DI16-24
//read actual DI 1-24
var pin;
for (pin=0; pin<25; pin++){
if (pin<=7) {
state_pin = (input & (1 << pin));
if (state_pin !== old_state_pin[pin]) { // выводить только когда поменялось состояние
var msgs = new Array(24);
if (state_pin > 0) {
msgs [pin] = {payload: false}
setFalse(this)
} else {
msgs [pin] = {payload: true}
setTrue(this)
}
this.send(msgs);
old_state_pin [pin] = state_pin;
}
}
if ((pin>=8) && (pin<=15)) {
state_pin = (input1 & (1 << pin-8));
if (state_pin !== old_state_pin[pin]) { // выводить только когда поменялось состояние
var msgs = new Array(24);
if (state_pin > 0) {
msgs [pin] = {payload: false}
setFalse(this)
} else {
msgs [pin] = {payload: true}
setTrue(this)
}
this.send(msgs);
old_state_pin [pin] = state_pin;
}
}
if ((pin>=16) && (pin<=24)) {
state_pin = (input2 & (1 << pin-16));
if (state_pin !== old_state_pin[pin]) { // выводить только когда поменялось состояние
var msgs = new Array(24);
if (state_pin > 0) {
msgs [pin] = {payload: false}
setFalse(this)
} else {
msgs [pin] = {payload: true}
setTrue(this)
}
this.send(msgs);
old_state_pin [pin] = state_pin;
}
}
/*//read actual input
if (this.pin <= 8) {
//read actual input 0-7
let input = i2cX.readByteSync(this.address, INPUT_PORT_REG);
state_pin = (input & (1 << (this.pin - 1)));
if (state_pin !== old_state_pin) { // выводить только когда поменялось состояние
if (state_pin > 0) {
var msg = { payload: false}
setFalse(this)
} else {
msg = {payload: true}
setTrue(this)
}
this.send(msg);
old_state_pin=state_pin;
}else{}
}
else {
//read actual input 8-15
let input1 = i2cX.readByteSync(this.address, INPUT_PORT_REG1);
state_pin1 = (input1 & (1 << (this.pin - 9)));
if (state_pin1 !== old_state_pin1) { // выводить только когда поменялось состояние
if (state_pin1 > 0) {
msg = { payload: false}
setFalse(this)
} else {
msg = { payload: true}
setTrue(this)
}
this.send(msg);
old_state_pin1=state_pin1;
}else{}
}*/
}
}
});
process.on('SIGINT', _ => {
button.unexport();
});
}
RED.nodes.registerType("DI CONTROLLER", DI_CONTROLLER);
function DOU_TCONTROLLER(config) {
setNode(config, this)
setStatus(this)
if (checkStatus(this)) {
const i2cX = i2c.openSync(this.bus);
// read actual config 0-7
if (this.pin <= 8) {
let output = i2cX.readByteSync(this.address, OUTPUT_PORT_REG);
output &= ~(1 << (9-this.pin-1));
i2cX.writeByteSync(this.address, OUTPUT_PORT_REG, output);
config = i2cX.readByteSync(this.address, CONFIG_REG);
config &= ~(1 << (9-this.pin-1));
i2cX.writeByteSync(this.address, CONFIG_REG, config);
} else {
// read actual config 8-15
let output = i2cX.readByteSync(this.address, OUTPUT_PORT_REG1);
output &= ~(1 << (this.pin-9));
i2cX.writeByteSync(this.address, OUTPUT_PORT_REG1, output);
config = i2cX.readByteSync(this.address, CONFIG_REG1);
config &= ~(1 << (this.pin-9));
i2cX.writeByteSync(this.address, CONFIG_REG1, config);
}
} else {
setWrongProperties(this)
}
this.on('input', function (msg) {
if (checkStatus(this)) {
if (msg.payload === "true") msg.payload = true;
if (msg.payload === "false") msg.payload = false;
const i2cX = i2c.openSync(this.bus);
//read actual output 0-7
if (this.pin <= 8) {
let output = i2cX.readByteSync(this.address, OUTPUT_PORT_REG);
if (msg.payload === true || msg.payload === 1) {
output |= (1 << (9-this.pin-1));
setTrue(this)
} else if (msg.payload === false || msg.payload === 0) {
output &= ~(1 << (9-this.pin-1));
setFalse(this)
} else {
setWrongPayload(this)
}
//write new output
i2cX.writeByteSync(this.address, OUTPUT_PORT_REG, output);
} else {
//read actual output 8-17
let output = i2cX.readByteSync(this.address, OUTPUT_PORT_REG1);
if (msg.payload === true || msg.payload === 1) {
output |= (1 << (this.pin-9));
setTrue(this)
} else if (msg.payload === false || msg.payload === 0) {
output &= ~(1 << (this.pin-9));
setFalse(this)
} else {
setWrongPayload(this)
}
//write new output
i2cX.writeByteSync(this.address, OUTPUT_PORT_REG1, output);
}
} else {
setWrongProperties(this)
}
});
this.on('close', function() { //Функция закрытия ноды - выключить за собой!
if (checkStatus(this)) {
const i2cX = i2c.openSync(this.bus);
//read actual output 0-7
if (this.pin <= 8) {
let output = i2cX.readByteSync(this.address, OUTPUT_PORT_REG);
output &= ~(1 << (9 - this.pin - 1));
i2cX.writeByteSync(this.address, OUTPUT_PORT_REG, output);
} else {
//read actual output 8-17
let output = i2cX.readByteSync(this.address, OUTPUT_PORT_REG1);
output &= ~(1 << (this.pin - 9));
i2cX.writeByteSync(this.address, OUTPUT_PORT_REG1, output);
}
} else {
setWrongProperties(this)
}
});
}
RED.nodes.registerType("DOUT CONTROLLER", DOU_TCONTROLLER);
}