initial
This commit is contained in:
18
node_modules/onoff/test/blink-led.js
generated
vendored
Normal file
18
node_modules/onoff/test/blink-led.js
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const led = new Gpio(17, 'out');
|
||||
|
||||
const iv = setInterval(() => {
|
||||
led.writeSync(led.readSync() ^ 1);
|
||||
}, 100);
|
||||
|
||||
setTimeout(() => {
|
||||
clearInterval(iv);
|
||||
|
||||
led.writeSync(0);
|
||||
led.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
}, 2000);
|
||||
|
59
node_modules/onoff/test/change-configuration.js
generated
vendored
Normal file
59
node_modules/onoff/test/change-configuration.js
generated
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
"use strict";
|
||||
|
||||
const assert = require('assert');
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
|
||||
let output = new Gpio(8, 'out');
|
||||
let input = new Gpio(7, 'in', 'both');
|
||||
|
||||
function watchWithSecondConfiguration() {
|
||||
input.watch((err, value) => {
|
||||
assert(!err, 'error during interrupt detection');
|
||||
assert(value === 1, 'expected interrupt on rising edge');
|
||||
|
||||
setTimeout(() => {
|
||||
input.unexport();
|
||||
output.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
}, 10);
|
||||
});
|
||||
|
||||
output.writeSync(1);
|
||||
}
|
||||
|
||||
function changeConfiguration() {
|
||||
input.unwatchAll();
|
||||
|
||||
let temp = output;
|
||||
temp.setDirection('in');
|
||||
output = input;
|
||||
input = temp;
|
||||
|
||||
output.setEdge('none');
|
||||
output.setDirection('out');
|
||||
output.writeSync(0);
|
||||
assert(output.direction() === 'out', 'expected direction to be out');
|
||||
assert(output.edge() === 'none', 'expected edge to be none');
|
||||
assert(output.readSync() === 0, 'expected value to be 0');
|
||||
|
||||
input.setEdge('rising');
|
||||
assert(input.direction() === 'in', 'expected direction to be in');
|
||||
assert(input.edge() === 'rising', 'expected edge to be rising');
|
||||
assert(input.readSync() === 0, 'expected value to be 0');
|
||||
|
||||
watchWithSecondConfiguration();
|
||||
}
|
||||
|
||||
function watchWithFirstConfiguration() {
|
||||
input.watch((err, value) => {
|
||||
assert(!err, 'error during interrupt detection');
|
||||
assert(value === 1, 'expected interrupt on rising edge');
|
||||
|
||||
setTimeout(changeConfiguration, 10);
|
||||
});
|
||||
|
||||
output.writeSync(1);
|
||||
}
|
||||
|
||||
watchWithFirstConfiguration();
|
39
node_modules/onoff/test/configure-and-check-active-low.js
generated
vendored
Normal file
39
node_modules/onoff/test/configure-and-check-active-low.js
generated
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
* In this test, GPIO7 is connected to one end of a 1kΩ current limiting
|
||||
* resistor and GPIO8 is connected to the other end of the resistor.
|
||||
*/
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const assert = require('assert');
|
||||
const input = new Gpio(7, 'in');
|
||||
const output = new Gpio(8, 'out', {activeLow: true});
|
||||
|
||||
assert(input.activeLow() === false);
|
||||
assert(output.activeLow() === true);
|
||||
output.writeSync(0);
|
||||
assert(input.readSync() === 1);
|
||||
output.writeSync(1);
|
||||
assert(input.readSync() === 0);
|
||||
|
||||
output.setActiveLow(false);
|
||||
assert(input.activeLow() === false);
|
||||
assert(output.activeLow() === false);
|
||||
output.writeSync(0);
|
||||
assert(input.readSync() === 0);
|
||||
output.writeSync(1);
|
||||
assert(input.readSync() === 1);
|
||||
|
||||
input.setActiveLow(true);
|
||||
assert(input.activeLow() === true);
|
||||
assert(output.activeLow() === false);
|
||||
output.writeSync(0);
|
||||
assert(input.readSync() === 1);
|
||||
output.writeSync(1);
|
||||
assert(input.readSync() === 0);
|
||||
|
||||
input.unexport();
|
||||
output.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
|
13
node_modules/onoff/test/configure-and-check-input.js
generated
vendored
Normal file
13
node_modules/onoff/test/configure-and-check-input.js
generated
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const assert = require('assert');
|
||||
const input = new Gpio(4, 'in', 'rising');
|
||||
|
||||
assert(input.direction() === 'in');
|
||||
assert(input.edge() === 'rising');
|
||||
|
||||
input.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
|
35
node_modules/onoff/test/configure-and-check-output.js
generated
vendored
Normal file
35
node_modules/onoff/test/configure-and-check-output.js
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const assert = require('assert');
|
||||
const output = new Gpio(17, 'out');
|
||||
|
||||
assert(output.direction() === 'out');
|
||||
|
||||
output.writeSync(1);
|
||||
assert(output.readSync() === 1);
|
||||
|
||||
output.writeSync(0);
|
||||
assert(output.readSync() === 0);
|
||||
|
||||
output.write(1, (err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
output.read((err, value) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
assert(value === 1);
|
||||
|
||||
output.writeSync(0);
|
||||
assert(output.readSync() === 0);
|
||||
|
||||
output.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
});
|
||||
});
|
||||
|
56
node_modules/onoff/test/debounce.js
generated
vendored
Normal file
56
node_modules/onoff/test/debounce.js
generated
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
const assert = require('assert');
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const output = new Gpio(8, 'out');
|
||||
const button = new Gpio(7, 'in', 'both', {debounceTimeout: 10});
|
||||
|
||||
let buttonPressedCount = 0;
|
||||
let buttonReleasedCount = 0;
|
||||
|
||||
function simulateToggleButtonStateWithBounce(cb) {
|
||||
let toggleCount = 0;
|
||||
|
||||
const iv = setInterval(() => {
|
||||
if (toggleCount === 19) {
|
||||
clearInterval(iv);
|
||||
return cb();
|
||||
}
|
||||
|
||||
output.writeSync(output.readSync() ^ 1);
|
||||
toggleCount += 1;
|
||||
}, 2);
|
||||
}
|
||||
|
||||
function simulatePressAndReleaseButtonWithBounce() {
|
||||
simulateToggleButtonStateWithBounce(() => {
|
||||
setTimeout(() => {
|
||||
simulateToggleButtonStateWithBounce(() => {
|
||||
setTimeout(() => {
|
||||
assert(buttonPressedCount === 1);
|
||||
assert(buttonReleasedCount === 1);
|
||||
|
||||
button.unexport();
|
||||
output.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
}, 20);
|
||||
});
|
||||
}, 50);
|
||||
});
|
||||
}
|
||||
|
||||
button.watch((err, value) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (value === 1) {
|
||||
buttonPressedCount += 1;
|
||||
} else if (value === 0) {
|
||||
buttonReleasedCount += 1;
|
||||
}
|
||||
});
|
||||
|
||||
simulatePressAndReleaseButtonWithBounce();
|
||||
|
15
node_modules/onoff/test/export-many-times.js
generated
vendored
Normal file
15
node_modules/onoff/test/export-many-times.js
generated
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
|
||||
for (let i = 1; i <= 1000000; i += 1) {
|
||||
const led = new Gpio(17, 'out');
|
||||
led.writeSync(led.readSync() ^ 1);
|
||||
led.unexport();
|
||||
if (i % 10 === 0) {
|
||||
console.log(i);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
|
47
node_modules/onoff/test/many-interrupts.js
generated
vendored
Normal file
47
node_modules/onoff/test/many-interrupts.js
generated
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
const assert = require('assert');
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const input = new Gpio(7, 'in', 'both');
|
||||
const output = new Gpio(8, 'out');
|
||||
|
||||
let toggleCount = 0;
|
||||
let falling = 0;
|
||||
let rising = 0;
|
||||
|
||||
function toggleOutput() {
|
||||
output.writeSync(output.readSync() ^ 1);
|
||||
toggleCount += 1;
|
||||
}
|
||||
|
||||
function interrupt(err, value) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (value === 1) {
|
||||
rising += 1;
|
||||
} else if (value === 0) {
|
||||
falling += 1;
|
||||
}
|
||||
|
||||
assert(output.readSync() === value);
|
||||
|
||||
if (rising + falling < 2000) {
|
||||
toggleOutput();
|
||||
} else {
|
||||
assert(toggleCount === 2000);
|
||||
assert(rising === falling);
|
||||
assert(rising + falling === toggleCount);
|
||||
|
||||
input.unexport();
|
||||
output.writeSync(0);
|
||||
output.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
}
|
||||
}
|
||||
|
||||
input.watch(interrupt);
|
||||
toggleOutput();
|
||||
|
37
node_modules/onoff/test/output-with-edge-bug.js
generated
vendored
Normal file
37
node_modules/onoff/test/output-with-edge-bug.js
generated
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
"use strict";
|
||||
|
||||
// Test for https://github.com/fivdi/onoff/issues/87
|
||||
//
|
||||
// If a Gpio is instantiated for an output GPIO and the edge parameter is
|
||||
// specified then the edge parameter should be ignored. Attempting to write
|
||||
// the sysfs edge file for an output GPIO results in an
|
||||
// "EIO: i/o error, write"
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const assert = require('assert');
|
||||
|
||||
function ensureGpio17Unexported(cb) {
|
||||
let led = new Gpio(17, 'out');
|
||||
|
||||
led.unexport();
|
||||
|
||||
setTimeout(() => {
|
||||
cb();
|
||||
}, 100);
|
||||
}
|
||||
|
||||
ensureGpio17Unexported(() => {
|
||||
let led;
|
||||
|
||||
assert.doesNotThrow(
|
||||
() => {
|
||||
led = new Gpio(17, 'out', 'both');
|
||||
},
|
||||
'can\'t instantiate a Gpio for an output with edge option specified'
|
||||
);
|
||||
|
||||
led.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
});
|
||||
|
73
node_modules/onoff/test/performance-async.js
generated
vendored
Normal file
73
node_modules/onoff/test/performance-async.js
generated
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
|
||||
const pulseLed = (led, pulseCount, cb) => {
|
||||
let time = process.hrtime();
|
||||
|
||||
const loop = (count) => {
|
||||
if (count === 0) {
|
||||
time = process.hrtime(time);
|
||||
const writesPerSecond = pulseCount * 2 / (time[0] + time[1] / 1E9);
|
||||
return cb(null, writesPerSecond);
|
||||
}
|
||||
|
||||
led.write(1, (err) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
led.write(0, (err) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
loop(count - 1);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
loop(pulseCount);
|
||||
};
|
||||
|
||||
const asyncWritesPerSecond = (cb) => {
|
||||
const led = new Gpio(17, 'out');
|
||||
let writes = 0;
|
||||
|
||||
const loop = (count) => {
|
||||
if (count === 0) {
|
||||
led.unexport();
|
||||
return cb(null, writes / 10);
|
||||
}
|
||||
|
||||
pulseLed(led, 10000, (err, writesPerSecond) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
writes += writesPerSecond;
|
||||
|
||||
loop(count - 1);
|
||||
});
|
||||
};
|
||||
|
||||
// Do a dry run first to get the runtime primed
|
||||
pulseLed(led, 5000, (err, writesPerSecond) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
loop(10);
|
||||
});
|
||||
};
|
||||
|
||||
asyncWritesPerSecond((err, averageWritesPerSecond) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
console.log(
|
||||
' ' + Math.floor(averageWritesPerSecond) + ' async writes per second'
|
||||
);
|
||||
});
|
||||
|
46
node_modules/onoff/test/performance-interrupt.js
generated
vendored
Normal file
46
node_modules/onoff/test/performance-interrupt.js
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
* In this test, GPIO7 is connected to one end of a 1kΩ current limiting
|
||||
* resistor and GPIO8 is connected to the other end of the resistor. GPIO7 is
|
||||
* an interrupt generating input and GPIO8 is an output. By toggling the state
|
||||
* of the output an interrupt is generated. The output is toggled as often as
|
||||
* possible to determine the maximum rate at which interrupts can be handled.
|
||||
*/
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const input = new Gpio(7, 'in', 'both');
|
||||
const output = new Gpio(8, 'out');
|
||||
|
||||
let irqCount = 0;
|
||||
let iv;
|
||||
|
||||
// Exit handler
|
||||
function exit() {
|
||||
input.unexport();
|
||||
output.unexport();
|
||||
|
||||
clearInterval(iv);
|
||||
}
|
||||
process.on('SIGINT', exit);
|
||||
|
||||
// Interrupt handler
|
||||
input.watch((err, value) => {
|
||||
if (err) {
|
||||
exit();
|
||||
}
|
||||
|
||||
irqCount += 1;
|
||||
|
||||
// Trigger next interrupt by toggling output.
|
||||
output.writeSync(value === 0 ? 1 : 0);
|
||||
});
|
||||
|
||||
// Print number of interrupts once a second.
|
||||
iv = setInterval(() => {
|
||||
console.log(irqCount);
|
||||
irqCount = 0;
|
||||
}, 1000);
|
||||
|
||||
// Trigger first interrupt by toggling output.
|
||||
output.writeSync(output.readSync() === 0 ? 1 : 0);
|
||||
|
40
node_modules/onoff/test/performance-sync.js
generated
vendored
Normal file
40
node_modules/onoff/test/performance-sync.js
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
|
||||
const pulseLed = (led, pulseCount) => {
|
||||
let time = process.hrtime();
|
||||
|
||||
for (let i = 0; i !== pulseCount; i += 1) {
|
||||
led.writeSync(1);
|
||||
led.writeSync(0);
|
||||
}
|
||||
|
||||
time = process.hrtime(time);
|
||||
|
||||
const writesPerSecond = pulseCount * 2 / (time[0] + time[1] / 1E9);
|
||||
|
||||
return writesPerSecond;
|
||||
}
|
||||
|
||||
const syncWritesPerSecond = () => {
|
||||
const led = new Gpio(17, 'out');
|
||||
let writes = 0;
|
||||
|
||||
// Do a dry run first to get the runtime primed
|
||||
pulseLed(led, 50000);
|
||||
|
||||
for (let i = 0; i !== 10; i += 1) {
|
||||
writes += pulseLed(led, 100000);
|
||||
}
|
||||
|
||||
led.unexport();
|
||||
|
||||
return writes / 10;
|
||||
}
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
console.log(
|
||||
' ' + Math.floor(syncWritesPerSecond()) + ' sync writes per second'
|
||||
);
|
||||
|
5
node_modules/onoff/test/run-performance-tests
generated
vendored
Executable file
5
node_modules/onoff/test/run-performance-tests
generated
vendored
Executable file
@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
node performance-async
|
||||
node performance-sync
|
||||
node performance-interrupt
|
||||
|
12
node_modules/onoff/test/run-tests
generated
vendored
Executable file
12
node_modules/onoff/test/run-tests
generated
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
node blink-led
|
||||
node change-configuration
|
||||
node configure-and-check-active-low
|
||||
node configure-and-check-input
|
||||
node configure-and-check-output
|
||||
node debounce
|
||||
node many-interrupts
|
||||
node output-with-edge-bug
|
||||
node wait-for-interrupt
|
||||
node wait-for-many-interrupts
|
||||
|
24
node_modules/onoff/test/wait-for-interrupt.js
generated
vendored
Normal file
24
node_modules/onoff/test/wait-for-interrupt.js
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const assert = require('assert');
|
||||
const button = new Gpio(4, 'in', 'both');
|
||||
|
||||
assert(button.direction() === 'in');
|
||||
assert(button.edge() === 'both');
|
||||
|
||||
console.info('Please press button connected to GPIO #4...');
|
||||
|
||||
button.watch((err, value) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
assert(value === 0 || value === 1);
|
||||
|
||||
button.unexport();
|
||||
|
||||
console.log('ok - ' + __filename);
|
||||
console.log(' button pressed, value was ' + value);
|
||||
});
|
||||
|
29
node_modules/onoff/test/wait-for-many-interrupts.js
generated
vendored
Normal file
29
node_modules/onoff/test/wait-for-many-interrupts.js
generated
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
|
||||
const Gpio = require('../onoff').Gpio;
|
||||
const assert = require('assert');
|
||||
const button = new Gpio(4, 'in', 'rising', {
|
||||
debounceTimeout : 10
|
||||
});
|
||||
let count = 0;
|
||||
|
||||
assert(button.direction() === 'in');
|
||||
assert(button.edge() === 'rising');
|
||||
|
||||
console.info('Please press button connected to GPIO4 5 times...');
|
||||
|
||||
button.watch((err, value) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
|
||||
console.log('button pressed ' + count + ' times, value was ' + value);
|
||||
|
||||
if (count === 5) {
|
||||
button.unexport();
|
||||
console.log('ok - ' + __filename);
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user