-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (56 loc) · 2.2 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
const gpio = require('pigpio').Gpio;
const hap = require("hap-nodejs");
const Accessory = hap.Accessory;
const Characteristic = hap.Characteristic;
const CharacteristicEventTypes = hap.CharacteristicEventTypes;
const Service = hap.Service;
const accessoryUuid = hap.uuid.generate("basementmaker.projects.ledstrip");
const accessory = new Accessory("LED Strip Accesssory", accessoryUuid);
const lightService = new Service.Lightbulb("LED Strip");
const onCharacteristic = lightService.getCharacteristic(Characteristic.On);
const brightnessCharacteristic = lightService.getCharacteristic(Characteristic.Brightness);
var showLogging = false;
var LEDstripStatusIsOn = false;
var currentLEDbrightness = 0;
var ledStripGPIOpin = new gpio(17, {mode: gpio.OUTPUT});
ledStripGPIOpin.pwmWrite(0);
onCharacteristic.on(CharacteristicEventTypes.GET, callback => {
if (showLogging) { console.log("Is LED Strip On?: " + LEDstripStatusIsOn); }
callback(undefined, LEDstripStatusIsOn);
});
onCharacteristic.on(CharacteristicEventTypes.SET, (value, callback) => {
if (showLogging) { console.log("Setting LED Strip On: " + value); }
if ( value == true && LEDstripStatusIsOn == false) {
if ( currentLEDbrightness == 0 ) {
ledStripGPIOpin.pwmWrite(255);
} else {
ledStripGPIOpin.pwmWrite(currentLEDbrightness);
}
} else if ( value == false ) {
ledStripGPIOpin.pwmWrite(0);
} else {
// do nothing
}
LEDstripStatusIsOn = value;
callback();
});
brightnessCharacteristic.on(CharacteristicEventTypes.GET, (callback) => {
if (showLogging) { console.log("Current Strip brightness level?: " + currentLEDbrightness); }
callback(undefined, currentLEDbrightness);
});
brightnessCharacteristic.on(CharacteristicEventTypes.SET, (value, callback) => {
if (showLogging) { console.log("Setting Strip brightness level to: " + value); }
var val = parseInt(255*(value/100), 10);
ledStripGPIOpin.pwmWrite(val);
currentLEDbrightness = Math.ceil((val/255)*100);
callback();
});
accessory.addService(lightService);
accessory.publish({
username: "BB:00:00:00:00:01",
pincode: "000-00-123",
port: 47129,
category: hap.Categories.LIGHTBULB
});
console.log("Running!");
console.log("Device Pin Code: 000-00-123");