forked from tnys/homebridge-gpio
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
97 lines (77 loc) · 2.88 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var wpi = require("wiring-pi");
var pkginfo = require("./package.json");
var gpioExports = require("./readExports.js")();
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-gpio-wpi', 'GPIO', GPIOAccessory);
}
function GPIOAccessory(log, config) {
this.log = log;
this.name = config['name'];
this.pin = config['pin'];
this.duration = config['duration'];
//Config option to invert behaviour of GPIO output - i.e. 0 = On, 1 = Off.
this.inverted = ( config['inverted'] === "true" );
this.service = new Service.Switch(this.name);
this.informationService = new Service.AccessoryInformation();
this.log("Configuring accessory %s (homebridge-gpio-wpi version %s)", this.name, pkginfo.version);
if (!this.pin) throw new Error('Pin not configured.');
var currentPin = this.pin;
var currentPinStatus = gpioExports.find( p => p.pin === currentPin );
if(currentPinStatus.error) {
throw new Error('Pin ' + this.pin + ' is not readable (' + currentPinStatus.error.code + '). Did you run gpio export as the right user?');
} else {
if (currentPinStatus.direction != 'out') {
throw new Error('Pin ' + this.pin + ' is not configured for OUTPUT. Run gpio mode -g ' + this.pin + ' out');
}
}
//Use pin numbering based on /sys/class/gpio exports (non-root)
wpi.setup('sys');
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
}
GPIOAccessory.prototype.getServices = function() {
return [this.service];
}
GPIOAccessory.prototype.getOn = function(callback) {
// inverted XOR pin_value
var on = ( this.inverted != wpi.digitalRead(this.pin) );
callback(null, on);
}
GPIOAccessory.prototype.setOn = function(on, callback) {
if (on) {
this.pinAction(!this.inverted * 1);
if (is_defined(this.duration) && is_int(this.duration)) {
this.pinTimer()
}
callback(null);
} else {
this.pinAction(this.inverted * 1);
callback(null);
}
}
GPIOAccessory.prototype.pinAction = function(action) {
this.log('Turning ' + (action == (!this.inverted * 1) ? 'on' : 'off') + ' pin #' + this.pin);
var self = this;
wpi.digitalWrite(self.pin, action);
var success = (wpi.digitalRead(self.pin) == action);
return success;
}
GPIOAccessory.prototype.pinTimer = function() {
var self = this;
setTimeout(function() {
self.log('Timer expired ' + self.duration + 'ms');
self.pinAction(self.inverted * 1);
}, self.duration);
}
// Check value is a +ve integer
var is_int = function(n) {
return (n > 0) && (n % 1 === 0);
}
var is_defined = function(v) {
return typeof v !== 'undefined';
}