-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswitch.js
64 lines (53 loc) · 1.04 KB
/
switch.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
"use strict";
// Can be used for a led, a relay... etc.
/**
* For the onoff doc,
* see https://github.com/fivdi/onoff
*/
const State = {
HIGH: 1,
LOW: 0
};
const Direction = {
IN: 'in',
OUT: 'out'
};
const Event = {
NONE: 'none',
RISING: 'rising',
FAILING: 'failing',
BOTH: 'both'
};
const Gpio = require('onoff').Gpio; // Constructor function for Gpio objects.
Gpio.prototype.high = function() {
this.writeSync(State.HIGH);
};
Gpio.prototype.low = function() {
this.writeSync(State.LOW);
};
Gpio.prototype.isHigh = function() {
return this.readSync() === State.HIGH;
};
const defaultPin = 18; // GPIO_18, Wiring/PI4J 1
let Switch = function(pin) {
if (pin === undefined) {
pin = defaultPin;
}
console.log("Switch on pin:", pin);
let PIN;
try {
PIN = new Gpio(pin, Direction.OUT);
} catch (err) {
throw err;
}
this.on = function() {
PIN.high();
};
this.off = function() {
PIN.low();
};
this.shutdown = function() {
PIN.unexport();
};
};
exports.Switch = Switch; // Made public.