-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·112 lines (85 loc) · 3.05 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var exec = require('child_process').exec;
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-pwm-light", "PWM-Light", PWMLightAccessory);
}
function PWMLightAccessory(log, config) {
// global vars
this.log = log;
// configuration vars
this.name = config["name"];
this.pin = config["pin"];
log(`${this.name} on pin #${this.pin}`);
// init pwm
exec(`gpio -g mode ${this.pin} pwm`);
exec(`gpio -g pwm ${this.pin} 0`, (error, stdout, stderr) => {});
// state vars
this.on = 0;
this.brightness = 100;
this.currentPWM = 0;
this.stepPWM = 64;
this.interval = undefined;
// register the service and provide the functions
this.service = new Service.Lightbulb(this.name);
// /~https://github.com/KhaosT/HAP-NodeJS/blob/master/lib/gen/HomeKitTypes.js#L493
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.service
.getCharacteristic(Characteristic.Brightness)
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
}
PWMLightAccessory.prototype.control = function(brightness, on) {
var targetPWM = on && Math.round(brightness * 1024 / 100);
this.stepPWM = Math.abs(targetPWM - this.currentPWM) / (400 / 25);
if(!this.interval) {
this.interval = setInterval(this.tick.bind(this), 25);
}
//exec(`gpio -g pwm ${this.pin} ${targetPWM}`, (error, stdout, stderr) => {});
}
PWMLightAccessory.prototype.tick = function() {
var targetPWM = this.on && Math.round(this.brightness * 1024 / 100);
if(this.currentPWM > targetPWM) {
this.currentPWM -= this.stepPWM;
if(this.currentPWM < targetPWM) {
this.currentPWM = targetPWM;
}
} else {
this.currentPWM += this.stepPWM;
if(this.currentPWM > targetPWM) {
this.currentPWM = targetPWM;
}
}
exec(`gpio -g pwm ${this.pin} ${this.currentPWM}`, (error, stdout, stderr) => {});
if(this.currentPWM == targetPWM) {
clearInterval(this.interval);
this.interval = undefined;
}
}
PWMLightAccessory.prototype.getOn = function(callback) {
this.log("Requested On: %s", this.on);
callback(null, this.on);
}
PWMLightAccessory.prototype.getBrightness = function(callback) {
this.log("Requested Brightness: %s", this.brightness);
callback(null, this.brightness);
}
PWMLightAccessory.prototype.setOn = function(on, callback) {
this.log("Set On: %s", on);
this.on = on;
this.control(this.brightness, this.on);
callback(null);
}
PWMLightAccessory.prototype.setBrightness = function(brightness, callback) {
this.log("Set Brightness: %s", brightness);
this.brightness = brightness;
this.control(this.brightness, this.on);
callback(null);
}
PWMLightAccessory.prototype.getServices = function() {
return [this.service];
}