Skip to content

Commit 6fc3eb1

Browse files
committed
input.js: simplified
Now that I'm spawning another thread for the main loop, I can go ahead and use delay(). Also, I'm not using up the processor because misc.cpp will call the sleep() function.
1 parent 6034b49 commit 6fc3eb1

2 files changed

Lines changed: 13 additions & 27 deletions

File tree

input.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,22 @@ var bb = require('bonescript');
33
var outputPin = bone.P8_3;
44
var inputPin = bone.P8_5;
55

6-
var timeEvent = 0;
7-
var nextState = HIGH;
8-
var toggleGPIO = function() {
9-
timeEvent++;
10-
console.time(''+timeEvent);
11-
digitalWrite(outputPin, nextState);
12-
nextState = (nextState == HIGH) ? LOW : HIGH;
13-
};
14-
var handler = function(pin, value) {
15-
console.timeEnd(''+timeEvent);
16-
console.log(pin.key + ' changed to ' + ((value == HIGH) ? 'HIGH' : 'LOW'));
17-
};
18-
var startToggle = function() {
19-
// The loop() function would block the running of the event handler.
20-
// setInterval() is a way to get a function to run at an interval that
21-
// does not block while it is waiting. The challenge is you must provide
22-
// a function that can be called, rather than just continue where you
23-
// left off. This can add some complexity for beginners, but it has some
24-
// benefits for writing embedded software.
25-
setInterval(toggleGPIO, 500);
26-
};
27-
286
setup = function() {
297
console.log('Please connect ' + inputPin.key + ' to ' + outputPin.key +
308
' with a 1kohm resistor');
319
pinMode(inputPin, INPUT);
3210
pinMode(outputPin, OUTPUT);
3311
digitalWrite(outputPin, LOW);
34-
attachInterrupt(inputPin, CHANGE, handler);
35-
setTimeout(startToggle, 500); // give some time for the handler to spawn
12+
attachInterrupt(inputPin, CHANGE, function(pin, value) {
13+
console.log(pin.key + ' changed to ' + ((value == HIGH) ? 'HIGH' : 'LOW'));
14+
});
15+
};
16+
17+
loop = function() {
18+
digitalWrite(outputPin, HIGH);
19+
delay(500);
20+
digitalWrite(outputPin, LOW);
21+
delay(500);
3622
};
3723

3824
bb.run();

node_modules/bonescript/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)