-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled.js
106 lines (88 loc) · 1.87 KB
/
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
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
module.exports = ledMgr;
function ledMgr(opt) {
//ext. libs
const fs = require('fs');
const path = require('path');
const PROD = !fs.existsSync(path.join(__dirname, 'debug'));
const Gpio = require('onoff').Gpio;
let led = {};
if (PROD) {
led = {
red: new Gpio(2, 'low'),
green: new Gpio(3, 'low')
}
}
//consts
const led_flash_interval = 1000;
// options
let logger = opt.logger;
// API/data for end-user
return {
goBusy: goBusy,
goBusyGreen: goBusyGreen,
goBusyFlashing: goBusyFlashing,
exitBusy: exitBusy
}
// private functionns
var ledFlashIntervalId = 0;
function ledRedOn() {
if (led.red) {
led.red.writeSync(1);
}
ledGreenOff();
logger.debug('[led] red ON');
}
function ledRedOff() {
if (led.red) {
led.red.writeSync(0);
}
logger.debug('[led] red OFF');
}
function ledGreenOn() {
if (led.green) {
led.green.writeSync(1);
}
ledRedOff();
logger.debug('[led] green ON');
}
function ledGreenOff() {
if (led.green) {
led.green.writeSync(0);
}
logger.debug('[led] green OFF');
}
function goBusy() {
if (ledFlashIntervalId) {
clearInterval(ledFlashIntervalId);
}
ledRedOn();
}
function goBusyGreen() {
if (ledFlashIntervalId) {
clearInterval(ledFlashIntervalId);
}
ledGreenOn();
}
function goBusyFlashing() {
if (ledFlashIntervalId) {
clearInterval(ledFlashIntervalId);
}
logger.info('red led flashing');
let lighton = true;
ledFlashIntervalId = setInterval(function() {
if (lighton) {
ledRedOn();
} else {
ledRedOff();
}
lighton = !lighton;
}, led_flash_interval);
}
function exitBusy() {
if (ledFlashIntervalId) {
clearInterval(ledFlashIntervalId);
}
ledRedOff();
ledGreenOff();
}
}