Skip to content

Commit

Permalink
use arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fivdi committed Jul 27, 2018
1 parent 57257a0 commit 3dcba09
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 55 deletions.
32 changes: 17 additions & 15 deletions README.md
Expand Up @@ -72,7 +72,7 @@ const Gpio = require('onoff').Gpio;
const led = new Gpio(17, 'out');
const button = new Gpio(4, 'in', 'both');

button.watch(function (err, value) {
button.watch((err, value) => {
led.writeSync(value);
});
```
Expand Down Expand Up @@ -101,15 +101,15 @@ const Gpio = require('onoff').Gpio;
const led = new Gpio(17, 'out');
const button = new Gpio(4, 'in', 'both');

button.watch(function (err, value) {
button.watch((err, value) => {
if (err) {
throw err;
}

led.writeSync(value);
});

process.on('SIGINT', function () {
process.on('SIGINT', () => {
led.unexport();
button.unexport();
});
Expand Down Expand Up @@ -137,15 +137,15 @@ const Gpio = require('onoff').Gpio;
const led = new Gpio(17, 'out');
const button = new Gpio(4, 'in', 'rising', {debounceTimeout: 10});

button.watch(function (err, value) {
button.watch((err, value) => {
if (err) {
throw err;
}

led.writeSync(led.readSync() ^ 1);
});

process.on('SIGINT', function () {
process.on('SIGINT', () => {
led.unexport();
button.unexport();
});
Expand All @@ -160,7 +160,7 @@ can be used to achieve this.
```js
const Gpio = require('onoff').Gpio;

const useLed = function (led, value) {
const useLed = (led, value) => {
led.writeSync(value);
}

Expand All @@ -171,7 +171,7 @@ if (Gpio.accessible) {
// more real code here
} else {
led = {
writeSync: function (value) {
writeSync: (value) => {
console.log('virtual led now uses value: ' + value);
}
};
Expand Down Expand Up @@ -373,12 +373,12 @@ const led = new Gpio(17, 'out'); // Export GPIO17 as an output

// Toggle the state of the LED connected to GPIO17 every 200ms.
// Here synchronous methods are used. Asynchronous methods are also available.
const iv = setInterval(function () {
const iv = setInterval(() => {
led.writeSync(led.readSync() ^ 1); // 1 = on, 0 = off :)
}, 200);

// Stop blinking the LED and turn it off after 5 seconds
setTimeout(function () {
setTimeout(() => {
clearInterval(iv); // Stop blinking
led.writeSync(0); // Turn LED off
led.unexport(); // Unexport GPIO and free resources
Expand All @@ -395,27 +395,29 @@ const led = new Gpio(17, 'out'); // Export GPIO17 as an output

// Toggle the state of the LED connected to GPIO17 every 200ms 'count' times.
// Here asynchronous methods are used. Synchronous methods are also available.
(function blink(count) {
const blinkLed = (count) => {
if (count <= 0) {
return led.unexport();
}

led.read(function (err, value) { // Asynchronous read
led.read((err, value) => { // Asynchronous read
if (err) {
throw err;
}

led.write(value ^ 1, function (err) { // Asynchronous write
led.write(value ^ 1, (err) => { // Asynchronous write
if (err) {
throw err;
}
});
});

setTimeout(function () {
blink(count - 1);
setTimeout(() => {
blinkLed(count - 1);
}, 200);
}(25));
};

blinkLed(25);
```

## How Does onoff Work?
Expand Down
14 changes: 8 additions & 6 deletions examples/blink-led-async.js
Expand Up @@ -5,25 +5,27 @@ const led = new Gpio(17, 'out'); // Export GPIO17 as an output

// Toggle the state of the LED connected to GPIO17 every 200ms 'count' times.
// Here asynchronous methods are used. Synchronous methods are also available.
(function blink(count) {
const blinkLed = (count) => {
if (count <= 0) {
return led.unexport();
}

led.read(function (err, value) { // Asynchronous read
led.read((err, value) => { // Asynchronous read
if (err) {
throw err;
}

led.write(value ^ 1, function (err) { // Asynchronous write
led.write(value ^ 1, (err) => { // Asynchronous write
if (err) {
throw err;
}
});
});

setTimeout(function () {
blink(count - 1);
setTimeout(() => {
blinkLed(count - 1);
}, 200);
}(25));
};

blinkLed(25);

4 changes: 2 additions & 2 deletions examples/blink-led.js
Expand Up @@ -5,12 +5,12 @@ const led = new Gpio(17, 'out'); // Export GPIO17 as an output

// Toggle the state of the LED connected to GPIO17 every 200ms.
// Here synchronous methods are used. Asynchronous methods are also available.
const iv = setInterval(function () {
const iv = setInterval(() => {
led.writeSync(led.readSync() ^ 1); // 1 = on, 0 = off :)
}, 200);

// Stop blinking the LED and turn it off after 5 seconds
setTimeout(function () {
setTimeout(() => {
clearInterval(iv); // Stop blinking
led.writeSync(0); // Turn LED off
led.unexport(); // Unexport GPIO and free resources
Expand Down
4 changes: 2 additions & 2 deletions examples/debounce-button.js
Expand Up @@ -4,15 +4,15 @@ const Gpio = require('../onoff').Gpio;
const led = new Gpio(17, 'out');
const button = new Gpio(4, 'in', 'rising', {debounceTimeout: 10});

button.watch(function (err, value) {
button.watch((err, value) => {
if (err) {
throw err;
}

led.writeSync(led.readSync() ^ 1);
});

process.on('SIGINT', function () {
process.on('SIGINT', () => {
led.unexport();
button.unexport();
});
Expand Down
4 changes: 2 additions & 2 deletions examples/light-switch.js
Expand Up @@ -4,15 +4,15 @@ const Gpio = require('../onoff').Gpio;
const led = new Gpio(17, 'out');
const button = new Gpio(4, 'in', 'both');

button.watch(function (err, value) {
button.watch((err, value) => {
if (err) {
throw err;
}

led.writeSync(value);
});

process.on('SIGINT', function () {
process.on('SIGINT', () => {
led.unexport();
button.unexport();
});
Expand Down
2 changes: 1 addition & 1 deletion examples/wait-for-interrupt.js
Expand Up @@ -10,7 +10,7 @@ console.log('Please press the button on GPIO4...');

// The callback passed to watch will be invoked when the button connected to
// GPIO4 is pressed
button.watch(function (err, value) {
button.watch((err, value) => {
if (err) {
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion onoff.js
Expand Up @@ -14,7 +14,7 @@ const LOW_BUF = Buffer.from('0');
const HIGH = 1;
const LOW = 0;

const waitForAccessPermission = function (paths) {
const waitForAccessPermission = (paths) => {
paths.forEach((path) => {
let tries = 0;

Expand Down
6 changes: 3 additions & 3 deletions test/change-configuration.js
Expand Up @@ -6,7 +6,7 @@ const Gpio = require('../onoff').Gpio;
let output = new Gpio(8, 'out');
let input = new Gpio(7, 'in', 'both');

function watchWithSecondConfiguration() {
const watchWithSecondConfiguration = () => {
input.watch((err, value) => {
assert(!err, 'error during interrupt detection');
assert(value === 1, 'expected interrupt on rising edge');
Expand All @@ -22,7 +22,7 @@ function watchWithSecondConfiguration() {
output.writeSync(1);
}

function changeConfiguration() {
const changeConfiguration = () => {
input.unwatchAll();

let temp = output;
Expand All @@ -45,7 +45,7 @@ function changeConfiguration() {
watchWithSecondConfiguration();
}

function watchWithFirstConfiguration() {
const watchWithFirstConfiguration = () => {
input.watch((err, value) => {
assert(!err, 'error during interrupt detection');
assert(value === 1, 'expected interrupt on rising edge');
Expand Down
4 changes: 2 additions & 2 deletions test/debounce.js
Expand Up @@ -8,7 +8,7 @@ const button = new Gpio(7, 'in', 'both', {debounceTimeout: 10});
let buttonPressedCount = 0;
let buttonReleasedCount = 0;

function simulateToggleButtonStateWithBounce(cb) {
const simulateToggleButtonStateWithBounce = (cb) => {
let toggleCount = 0;

const iv = setInterval(() => {
Expand All @@ -22,7 +22,7 @@ function simulateToggleButtonStateWithBounce(cb) {
}, 2);
}

function simulatePressAndReleaseButtonWithBounce() {
const simulatePressAndReleaseButtonWithBounce = () => {
simulateToggleButtonStateWithBounce(() => {
setTimeout(() => {
simulateToggleButtonStateWithBounce(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/many-interrupts.js
Expand Up @@ -9,12 +9,12 @@ let toggleCount = 0;
let falling = 0;
let rising = 0;

function toggleOutput() {
const toggleOutput = () => {
output.writeSync(output.readSync() ^ 1);
toggleCount += 1;
}

function interrupt(err, value) {
const interrupt = (err, value) => {
if (err) {
throw err;
}
Expand Down
2 changes: 1 addition & 1 deletion test/output-with-edge-bug.js
Expand Up @@ -10,7 +10,7 @@
const Gpio = require('../onoff').Gpio;
const assert = require('assert');

function ensureGpio17Unexported(cb) {
const ensureGpio17Unexported = (cb) => {
let led = new Gpio(17, 'out');

led.unexport();
Expand Down
2 changes: 1 addition & 1 deletion test/performance-interrupt.js
Expand Up @@ -15,7 +15,7 @@ let irqCount = 0;
let iv;

// Exit handler
function exit() {
const exit = () => {
input.unexport();
output.unexport();

Expand Down
1 change: 1 addition & 0 deletions unittest/mocks/epoll.js
@@ -1,3 +1,4 @@
'use strict';

class Epoll {

Expand Down
33 changes: 16 additions & 17 deletions unittest/watching.js
@@ -1,4 +1,4 @@
"use strict";
'use strict';

const assert = require('assert');
const mockFs = require('mock-fs');
Expand All @@ -12,6 +12,20 @@ const Gpio = require('../onoff').Gpio;
describe('watching', () => {
let pin;

const listener1 = (err, value) => {
if (err) {
throw err;
}
console.log(`listener1: value is ${value}`);
}

const listener2 = (err, value) => {
if (err) {
throw err;
}
console.log(`listener2: value is ${value}`);
}

beforeEach(() => {
mockFs({
'/sys/class/gpio': {
Expand Down Expand Up @@ -106,19 +120,4 @@ describe('watching', () => {
pin.unexport();
mockFs.restore();
});

function listener1(err, value) {
if (err) {
throw err;
}
console.log(`listener1: value is ${value}`);
}

function listener2(err, value) {
if (err) {
throw err;
}
console.log(`listener2: value is ${value}`);
}

});
});

0 comments on commit 3dcba09

Please sign in to comment.