-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchBoard.js
45 lines (38 loc) · 1.45 KB
/
TouchBoard.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
/* allow generators and iterators
*
* i didn't want to have to rewrite all of this async stuff
* in a functional style. sorry. :c
*/
/* eslint-disable no-restricted-syntax */
// this import only works with this capitalization, but eslint complains about it
const { SerialPort } = require('SerialPort'); // eslint-disable-line import/no-unresolved
const fs = require('fs');
async function FindPath(vendorId, productId) {
const list = await SerialPort.list();
for (const port of list) {
if (port.vendorId !== undefined && port.productId !== undefined) {
if (port.vendorId.toLowerCase() === vendorId.toLowerCase()
&& port.productId.toLowerCase() === productId.toLowerCase()) {
// matching port!!
return port.path;
}
}
}
// no matching port found
throw new Error(`No port found for [${vendorId}:${productId}]`);
}
async function TouchBoard() {
const { BOARD_VID, BOARD_PID } = process.env;
const path = await FindPath(BOARD_VID, BOARD_PID);
const board = new SerialPort({ path, baudRate: 9600 });
// upload touch/release thresholds
const thresholdsBuffer = fs.readFileSync('thresholds.json');
const thresholds = JSON.parse(thresholdsBuffer);
for (const animal of Object.keys(thresholds)) {
board.write(`{${animal}-tths:${thresholds[animal].touch}}`);
board.write(`{${animal}-rths:${thresholds[animal].release}}`);
}
board.write('{set-running:1}');
return board;
}
module.exports = TouchBoard;