-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathled.js
48 lines (41 loc) · 964 Bytes
/
led.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
// @ts-nocheck
'use strict'
const Bluebird = require('bluebird')
function Led(gpio) {
Bluebird.promisifyAll(gpio)
this.gpio = gpio
this.on = false
this.blinkInterval = false
}
Led.prototype.enable = function () {
this.on = true
if (this.blinkInterval) {
clearInterval(this.blinkInterval)
this.blinkInterval = null
}
return this.gpio.writeAsync(1)
}
Led.prototype.disable = function () {
this.on = false
if (this.blinkInterval) {
clearInterval(this.blinkInterval)
this.blinkInterval = null
}
return this.gpio.writeAsync(0)
}
Led.prototype.toggle = function () {
this.on = !this.on
return this.gpio.writeAsync(this.on ? 1 : 0)
}
Led.prototype.blink = function (delay) {
const Led = this
if (!this.blinkInterval) {
this.blinkInterval = setInterval(function () {
Led.toggle()
}, delay)
this.on = true
return this.gpio.writeAsync(1)
}
return Bluebird.resolve()
}
module.exports.Led = Led