-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
190 lines (169 loc) · 5.76 KB
/
index.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
const TWENTY_FIVE_MEGAHERTZ = 25000000,
TWELVE_BITS = 4096,
NANOSECONDS_IN_SECOND = 1000000,
REGISTERS = {
"MODE": 0x00,
"PRESCALE": 0xFE,
"PULSE_ON_L": 0x06,
"PULSE_ON_H": 0x07,
"PULSE_OFF_L": 0x08,
"PULSE_OFF_H": 0x09,
"ALL_ON_L": 0xFA,
"ALL_ON_H": 0xFB,
"ALL_OFF_L": 0xFC,
"ALL_OFF_H": 0xFD
},
VALIDATE = {
"channel": function (channel) {
return (channel > -1 && channel < 16);
},
"pwm": function (pwmValues) {
if (typeof pwmValues === 'number') {
pwmValues = [pwmValues]
}
//Validate each passed in value
for (let pwmValue of pwmValues) {
if (pwmValue < 0 || pwmValue > 4096) {
return false;
}
}
return true;
},
"pulse": function (pulse, frequency) {
return (pulse > -1 && pulse < 1000000 / frequency);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = class PCA9685{
constructor(addr, comm, config) {
this.config = config || {};
this.comm = comm;
this.addr = addr;
this.settable = ['all'];
this.status = {
channels: {}
};
//Initialize Channels
if (this.config.channels) {
for (let i = 0; i < this.config.channels; i++) {
let channelName = `channel_${i}`;
this.settable.push(channelName);
this.status.channels[i] = {
pulseOn: 0,
pulseOff: 0
};
}
}
this.status.initialized = false;
this.init().then(r => {
console.debug(`PCA9685 Initialized`);
});
}
async init(){
try {
//Set chip to standby
console.debug(`Setting chip to standby`);
await this.comm.writeBytes(this.addr, REGISTERS.MODE, 0x00);
console.debug(`Set chip to standby`);
//Set chip pre-scale value
console.debug(`Setting chip prescale value`);
await this.setPwmFrequency(this.config.frequency, this.config.correctionFactor);
//Stop all PWM
console.debug(`Stopping all PWM`);
await this.stop();
//Mark chip as initialized
this.status.initialized = true;
console.debug(`PCA9685 Chip Initialized`);
} catch (err) {
let errorMessage = `Error initializing PCA9685`;
console.error(err.message || errorMessage);
throw new Error(err);
}
}
async getStatus() {
return this.status;
}
async setPwmFrequency(frequency, correctionFactor) {
try {
if (frequency < 40 || frequency > 4096) {
throw new Error("Frequency is invalid. Valid Input 40 - 1000 Hz");
}
correctionFactor = correctionFactor || 1;
let prescaleValue = (((TWENTY_FIVE_MEGAHERTZ / TWELVE_BITS) / frequency) - 1);
let prescale = Math.floor(prescaleValue * correctionFactor + 0.5);
//Save currentMode of chip
let originalMode = await this.comm.readBytes(this.addr, REGISTERS.MODE, 1);
//Set chip mode to standby (Turn off oscillator to prescale value)
let newMode = (originalMode & 0x7F) | 0x10;
await this.comm.writeBytes(this.addr, REGISTERS.MODE, newMode);
//Set prescale value
await this.comm.writeBytes(this.addr, REGISTERS.PRESCALE, prescale);
//Set board back to original mode
try {
await this.comm.writeBytes(this.addr, REGISTERS.MODE, originalMode);
} catch (err) {
//Ignore error. Board reboots and does not send ACK
}
//Wait for chip restart
await sleep(1000);
//Clear restart bit
await this.comm.writeBytes(this.addr, REGISTERS.MODE, originalMode | 0x80);
this.status.pwmFrequency = frequency;
this.status.correctionFactor = correctionFactor;
} catch (err) {
console.error(`Error setting PWM pre-scale frequency`);
throw new Error(err);
}
}
async setPwm(channel, pulseOn, pulseOff) {
try {
if (VALIDATE.channel(channel) === false) {
throw new Error("Invalid channel");
}
if (VALIDATE.pwm([pulseOn, pulseOff]) === false) {
throw new Error("Invalid PWM value. Accepted values 0-4096")
}
//Set bytes of pulse on and off each separated into 2 registers.
await this.comm.writeBytes(this.addr, REGISTERS.PULSE_ON_L + 4 * channel, pulseOn & 0xFF);
await this.comm.writeBytes(this.addr, REGISTERS.PULSE_ON_H + 4 * channel, pulseOn >> 8);
await this.comm.writeBytes(this.addr, REGISTERS.PULSE_OFF_L + 4 * channel, pulseOff & 0xFF);
await this.comm.writeBytes(this.addr, REGISTERS.PULSE_OFF_H + 4 * channel, pulseOff >> 8);
this.status.channels[channel] = {
pulseOn: pulseOn,
pulseOff: pulseOff
};
//Buffer for command to complete
} catch (err) {
let errorMessage = `Error setting PWM for channel ${channel} [${pulseOn} - ${pulseOff}]. Err: ${err.message}`;
console.error(errorMessage);
throw new Error(errorMessage);
}
}
async setPulse(channel, pulse) {
try {
if (VALIDATE.channel(channel) === false) {
throw new Error("Invalid channel");
}
if (VALIDATE.pulse(pulse, this.config.frequency) === false) {
throw new Error("Invalid pulse. Pulse must be > 0 and less than PWM period");
}
let pulseLength = (NANOSECONDS_IN_SECOND / this.config.frequency) / TWELVE_BITS;
await this.setPwm(channel, 0, Math.floor(pulse / pulseLength));
} catch (err) {
let errorMessage = `Error setting pulse on CH: ${channel}, Pulse: ${pulse}. Err: ${err.message}`;
console.error(errorMessage);
throw new Error(errorMessage);
}
}
async stop() {
try {
await this.comm.writeBytes(this.addr, REGISTERS.ALL_OFF_H, 0x10);
} catch (err) {
let errorMessage = `Error stopping pulse on ALL channels. Err: ${err.message}`
console.error(errorMessage);
throw new Error(errorMessage);
}
}
};