-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
227 lines (198 loc) · 5.41 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const insideReceiver = require('@amperka/ir-receiver').connect(P0);
const outsideReceiver = require('@amperka/ir-receiver').connect(P1);
// power switches
const openGateKeyPin = P10;
const closeGateKeyPin = P8;
// audio signals
const mainAudioSignalPin = P5;
/*
* Pins to related numbers
* P0 = 11
* P1 = 10
* P2 = 6
* P3 = 7
* P4 = 3
* P5 = 1
* P6 = 0
* P7 = 2
* P8 = 6
* P9 = 7
* P10 = 8
* P11 = 9
* P12 = 8
* P13 = 10
*
* Grouped pins that relates to the same number
* P0, (P1-P13), (P2-P8), (P3-P9), P4, P5, P6, P7, (P10-P12), P11
* */
// barrier indicators
const lockRightBarrier = P2;
const lockLeftBarrier = P3;
const topRightBarrier = P6;
const topLeftBarrier = P7;
const bottomLeftBarrier = P11;
const bottomRightBarrier = P12;
// control buttons
const powerButton = 0xfd00ff;
const gateUpButton = 0xfd48b7;
const gateUpDuplicateButton = 3;
const gateDownButton = 0xfd6897;
const switchGateStateButton = 0xfd40bf;
// function helpers
function beep() {
mainAudioSignalPin.reset();
setTimeout(() => {
mainAudioSignalPin.set();
}, 1000);
}
function isGateLocked() {
return !lockRightBarrier.read();
// return !lockLeftBarrier.read() === 1 || !lockRightBarrier.read() === 1;
}
function openGatePowerSwitch(keyPin) {
if (isGateLocked()) {
beep();
return false;
}
keyPin.set();
return true;
}
function closeGatePowerSwitch(keyPin) {
keyPin.reset();
}
// controllers
const ButtonController = function (isButtonClicked) {
this._isButtonClicked = isButtonClicked;
this._holdTimeoutId = null;
insideReceiver.on('receive', this._onButtonAction.bind(this));
outsideReceiver.on('receive', this._onButtonAction.bind(this));
};
ButtonController.prototype._onButtonAction = function (code, repeat) {
const isButtonHolding = this._isButtonClicked(code) && repeat;
const isButtonPressed = this._isButtonClicked(code) && !repeat;
if (isButtonPressed) {
this.emit('press');
this._updateReleaseTimeout(300);
}
if (isButtonHolding) {
this.emit('hold');
this._updateReleaseTimeout(150);
}
};
ButtonController.prototype._updateReleaseTimeout = function (timeout) {
const self = this;
if (this._holdTimeoutId) {
clearTimeout(this._holdTimeoutId);
}
this._holdTimeoutId = setTimeout(function () {
self.emit('release');
}, timeout);
};
const GateStateController = function () {
this.reset();
const self = this;
this._setWatch([topRightBarrier], function () {
if (!self._isOpening) {
return;
}
closeGatePowerSwitch(openGateKeyPin);
self._isOpened = true;
self._isOpening = false;
});
this._setWatch([bottomRightBarrier], function () {
if (!self._isClosing) {
return;
}
closeGatePowerSwitch(closeGateKeyPin);
self._isClosed = true;
self._isClosing = false;
});
};
GateStateController.prototype.switchState = function () {
const inProgress = this._isClosing || this._isOpening;
if (this._isClosed && !inProgress) {
this._openGate();
} else if (this._isOpened && !inProgress) {
this._closeGate();
}
};
GateStateController.prototype.reset = function () {
this._isClosed = true;
this._isClosing = false;
this._isOpened = false;
this._isOpening = false;
};
GateStateController.prototype._openGate = function () {
const result = openGatePowerSwitch(openGateKeyPin);
if (result) {
this._isOpening = true;
this._isClosed = false;
}
setTimeout(() => {
closeGatePowerSwitch(openGateKeyPin);
}, 1000);
setTimeout(() => {
openGatePowerSwitch(openGateKeyPin);
}, 1500);
setTimeout(() => {
closeGatePowerSwitch(openGateKeyPin);
}, 2500);
setTimeout(() => {
openGatePowerSwitch(openGateKeyPin);
}, 3000);
};
GateStateController.prototype._closeGate = function () {
const result = openGatePowerSwitch(closeGateKeyPin);
if (result) {
this._isClosing = true;
this._isOpened = false;
}
};
GateStateController.prototype._setWatch = function (pins, cb) {
pins.forEach(
(pin) => setWatch(cb, pin, {
repeat: true,
edge: 'falling',
debounce: 10
})
);
};
const gateStateController = new GateStateController();
// Handlers
function startGateUpHandler() {
const gateUpButtonController = new ButtonController((code) => code === gateUpButton || code === gateUpDuplicateButton);
gateUpButtonController.on('press', function () {
openGatePowerSwitch(openGateKeyPin);
});
gateUpButtonController.on('release', function () {
closeGatePowerSwitch(openGateKeyPin);
});
}
function startGateDownHandler() {
const gateDownButtonController = new ButtonController((code) => code === gateDownButton);
gateDownButtonController.on('press', function () {
openGatePowerSwitch(closeGateKeyPin);
});
gateDownButtonController.on('release', function () {
closeGatePowerSwitch(closeGateKeyPin);
});
}
function startPowerOffHandler() {
const powerButtonController = new ButtonController((code) => code === powerButton);
powerButtonController.on('press', function () {
closeGatePowerSwitch(openGateKeyPin);
closeGatePowerSwitch(closeGateKeyPin);
gateStateController.reset();
mainAudioSignalPin.set();
});
}
function startSwitchGateStateHandler() {
const switchGateStateButtonController = new ButtonController((code) => code === switchGateStateButton);
switchGateStateButtonController.on('press', function () {
gateStateController.switchState();
});
}
startPowerOffHandler();
startGateUpHandler();
startGateDownHandler();
startSwitchGateStateHandler();