-
Notifications
You must be signed in to change notification settings - Fork 0
/
Desk.js
68 lines (53 loc) · 1.74 KB
/
Desk.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
import { performance } from 'node:perf_hooks';
import { setTimeout } from 'node:timers/promises';
export const MAX_HEIGHT = 127;
export const MIN_HEIGHT = 62;
const DELAY = 100;
const targetReached = (currentCm, targetCm) => Math.abs(currentCm - targetCm) <= 1;
export default class Desk {
constructor(position, control) {
this.position = position;
this.control = control;
}
async moveUp() {
await this.control.writeAsync(Buffer.from('4700', 'hex'), false);
}
async moveDown() {
await this.control.writeAsync(Buffer.from('4600', 'hex'), false);
}
async stopMoving() {
await this.control.writeAsync(Buffer.from('FF00', 'hex'), false);
}
async getCurrentHeightCm() {
const heightInBytes = await this.position.readAsync();
const relativeHeightCm = heightInBytes.readInt16LE() / 100;
return MIN_HEIGHT + relativeHeightCm;
}
async moveTo(targetCm) {
const currentCm = await this.getCurrentHeightCm();
return currentCm > targetCm
? this._moveTo(targetCm, () => this.moveDown())
: this._moveTo(targetCm, () => this.moveUp());
}
async _moveTo(targetCm, moveFn) {
await this.stopMoving();
let currentCm = await this.getCurrentHeightCm();
let lastCommandNow = performance.now();
while (!targetReached(currentCm, targetCm)) {
currentCm = await this.getCurrentHeightCm();
if (targetReached(currentCm, targetCm)) {
break;
}
if (performance.now() - lastCommandNow <= 300) {
continue;
}
await moveFn();
lastCommandNow = performance.now();
await setTimeout(DELAY);
currentCm = await this.getCurrentHeightCm();
}
await this.stopMoving();
currentCm = await this.getCurrentHeightCm();
return currentCm;
}
}