-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
129 lines (99 loc) · 3.55 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";
var Service, Characteristic;
const piblaster = require('pi-blaster.js');
const converter = require('color-convert');
const fs = require('fs');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-gpio-rgb-ledstrip', 'SmartLedStrip', SmartLedStripAccessory);
}
function SmartLedStripAccessory(log, config) {
this.log = log;
this.name = config['name'];
this.rPin = config['rPin'];
this.gPin = config['gPin'];
this.bPin = config['bPin'];
this.enabled = true ;
try {
if (!this.rPin)
throw new Error("rPin not set!")
if (!this.gPin)
throw new Error("gPin not set!")
if (!this.bPin)
throw new Error("bPin not set!")
if (!fs.existsSync('/dev/pi-blaster'))
throw new Error("/dev/pi-blaster does not exist!")
} catch (err) {
this.log("An error has been thrown! " + err);
this.log("homebridge-gpio-rgb-ledstrip won't work until you fix this problem");
this.enabled = false;
}
}
SmartLedStripAccessory.prototype = {
getServices : function(){
if(this.enabled){
let informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Manfredi Pistone')
.setCharacteristic(Characteristic.Model, 'GPIO-RGB-LedStrip')
.setCharacteristic(Characteristic.SerialNumber, '06-06-00');
let smartLedStripService = new Service.Lightbulb(this.name);
smartLedStripService
.getCharacteristic(Characteristic.On)
.on('change',this.toggleState.bind(this));
smartLedStripService
.addCharacteristic(new Characteristic.Brightness())
.on('change',this.toggleState.bind(this));
smartLedStripService
.addCharacteristic(new Characteristic.Hue())
.on('change',this.toggleState.bind(this));
smartLedStripService
.addCharacteristic(new Characteristic.Saturation())
.on('change',this.toggleState.bind(this));
this.informationService = informationService;
this.smartLedStripService = smartLedStripService;
this.log("SmartLedStrip has been successfully initialized!");
return [informationService, smartLedStripService];
}else{
this.log("SmartLedStrip has not been initialized, please check your logs..");
return [];
}
},
isOn : function() {
return this.smartLedStripService.getCharacteristic(Characteristic.On).value;
},
getBrightness : function(){
return this.smartLedStripService.getCharacteristic(Characteristic.Brightness).value;
},
getHue : function(){
return this.smartLedStripService.getCharacteristic(Characteristic.Hue).value;
},
getSaturation : function(){
return this.smartLedStripService.getCharacteristic(Characteristic.Saturation).value;
},
toggleState : function()
{
if(this.enabled){
if(!this.isOn())
{
this.updateRGB(0,0,0);
return;
}
var brightness = this.getBrightness();
if(brightness!=0){
var rgb = converter.hsv.rgb([this.getHue(), this.getSaturation(), brightness]);
this.updateRGB(rgb[0], rgb[1], rgb[2]);
}else{
this.updateRGB(0,0,0);
}
}
},
updateRGB : function(red, green, blue)
{
this.log("Setting rgb values to: Red: "+red + " Green: "+green+ " Blue: "+blue);
piblaster.setPwm(this.rPin, red/255);
piblaster.setPwm(this.gPin, green/255);
piblaster.setPwm(this.bPin, blue/255);
}
}