-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
59 lines (51 loc) · 1.38 KB
/
test.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
const comms = require('ncd-red-comm');
const PCA9685 = require('./index.js');
/*
* Allows use of a USB to I2C converter form ncd.io
*/
// var port = '/dev/tty.usbserial-DN04EUP8';
let port = 'COM6';
let serial = new comms.NcdSerial(port, 115200);
let comm = new comms.NcdSerialI2C(serial, 0);
/*
* Use the native I2C port on the Raspberry Pi
*/
//var comm = new comms.NcdI2C(1);
let config = {
frequency: 55,
channels: 8,
correctionFactor: 1,
comm: comm
};
let address = 64;
let driver = new PCA9685(address, comm, config);
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function testAllChannels(pulseValue = 4096, runCount = 0){
try {
while(driver.status.initialized === false) {
//Check for initialization every 10ms
await sleep(10);
}
if (runCount === 20) {
return true;
}
console.log(`Setting Pulse Value to ${pulseValue}`);
for (let i = 0; i < config.channels; i++) {
console.log(`CH: ${i}`);
await driver.setPwm(i, pulseValue, 0);
}
setTimeout(testAllChannels, 100, getRandomInt(4000), (runCount + 1))
} catch (err) {
console.error(err.message || JSON.stringify(err));
throw new Error(err);
}
}
console.log(`Test Started`);
testAllChannels().then(r => {
console.log(`Test Completed`);
});