-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCA9685.js
122 lines (98 loc) · 3.07 KB
/
PCA9685.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
"use strict";
const PCA9685 = require("./index.js");
module.exports = function(RED){
let driver_pool = {};
function NcdI2cDeviceNode(config){
RED.nodes.createNode(this, config);
//set the address from config
config.address = parseInt(config.address);
this.addr = config.address;
//set the channel count from config
this.channelCount = parseInt(config.channelCount);
//remove sensor reference if it exists
if(typeof driver_pool[this.id] != 'undefined'){
//Redeployment
driver_pool[this.id].sensor.stop();
delete(driver_pool[this.id]);
}
this.warn(RED.nodes.getNode(config.connection).i2c);
this.warn(config);
//create new sensor reference
this.sensor = new PCA9685(config.address, RED.nodes.getNode(config.connection).i2c, config);
let node = this;
driver_pool[this.id] = {
sensor: this.sensor,
node: this
};
//Display device status on node
function device_status(){
if(node.sensor.status.initialized === false){
node.status({fill:"red",shape:"ring",text:"disconnected"});
return false;
}
node.status({fill:"green",shape:"dot",text:"connected"});
return true;
}
//send telemetry data out the nodes output
function send_payload(_status){
let msg = [
{topic: 'chip_status', payload: node.sensor.status.initialized}
];
//Figure out how outputs are linked
for (let sensor of node.sensor.status.channels) {
msg.push({
topic: `channel_1_pwm`,
payload: sensor
})
}
node.send(msg);
}
function getStatus() {
return node.sensor.status;
}
async function emitStatus() {
let status = await getStatus();
let msg = [
{topic: 'status', payload: JSON.stringify(node.sensor.status)}
];
node.send(msg);
}
node.on('input', async (msg) => {
//if status is requested, fetch it
if(msg.topic === `get_status`){
return emitStatus();
}
//Set channel PWM range
if(msg.topic === `channel_pwm`) {
await node.sensor.setPwm(msg.payload.channel, msg.payload.pulseOn, msg.payload.pulseOff);
return emitStatus();
}
//Set channel PWM pulse
if(msg.topic.indexOf(`channel_pulse`) === 0) {
await node.sensor.setPulse(msg.payload.channel, msg.payload.pulse);
return emitStatus();
}
//Set PWM Chip Pre-Scale Value
if(msg.topic === `set_pwm_frequency`) {
await node.sensor.setPwmFrequency(msg.payload);
return emitStatus();
}
//Set PWM Chip Pre-Scale Value
if(msg.topic === `stop`) {
await node.sensor.stop();
return emitStatus();
}
});
//if node is removed, kill the sensor object
node.on('close', async (removed, done) => {
if(removed){
await driver_pool[this.id].sensor.stop();
delete(driver_pool[node.id]);
}
done();
});
emitStatus();
}
//register the node with Node-RED
RED.nodes.registerType("ncd-pca9685", NcdI2cDeviceNode);
};