-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
142 lines (130 loc) · 3.63 KB
/
start.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
130
131
132
133
134
135
136
137
138
139
140
141
142
const { exec } = require('child_process');
const { Gpio } = require('pigpio');
const buzz = new Gpio(27, { mode: Gpio.OUTPUT });
const pwm = new Gpio(13, { mode: Gpio.OUTPUT });
const tacho = new Gpio(6, {
mode: Gpio.INPUT,
edge: Gpio.FALLING_EDGE
});
const button = new Gpio(3, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_UP,
alert: true
});
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
}, 25);
}, 100);
}, 25);
}, 0);
let increment = 0;
tacho.on('interrupt', () => {
increment++;
});
const hz = 25000; // 25khz
const min = 60000; // 6%
const max = 500000; // 50%
let interval;
let state = false;
setInterval(() => {
if (increment > 0) {
if (state === true) {
console.log('tacho alert off');
clearInterval(interval);
state = false;
}
} else {
if (state === false) {
console.log('tacho alert on');
state = true;
interval = sos();
}
}
increment = 0;
exec('cat /sys/class/thermal/thermal_zone0/temp', (err, out) => {
let temp = Math.round(parseInt(out) / 1000);
let duty = parseInt((temp - 40) / (60 - 40) * 100 * 10000);
if (duty >= max) duty = max;
if (duty <= min) duty = min;
pwm.hardwarePwmWrite(hz, duty);
console.log(`temp: ${temp}°C | speed: ${parseInt(duty / 10000)}%`);
});
}, 500);
button.glitchFilter(10000); // stable for 10 ms before alert is emitted.
button.on('alert', (level) => {
if (level === 0) {
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
}, 25);
}, 0);
}
if (level === 1) {
exec(`echo 'true' > /shutdown_signal`);
process.exit();
}
});
const sos = function () {
let s = function () {
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
}, 70);
}, 70);
}, 70);
}, 70);
}, 70);
}, 0);
};
let o = function () {
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
setTimeout(() => {
buzz.digitalWrite(1);
setTimeout(() => {
buzz.digitalWrite(0);
}, 150);
}, 150);
}, 150);
}, 150);
}, 150);
}, 0);
};
let sos = function () {
setTimeout(() => {
s();
setTimeout(() => {
o();
setTimeout(() => {
s();
}, 950);
}, 550);
}, 0);
};
sos();
return setInterval(() => {
sos();
}, 11500);
};