-
Notifications
You must be signed in to change notification settings - Fork 4
/
nr-util.js
58 lines (47 loc) · 1.5 KB
/
nr-util.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
// Simple utilities
"use strict";
exports.convertToBuffer= (d) => {
try{
if(Buffer.isBuffer(d)) return d; // if is a buffer nothing to do
if(typeof d === "string") return Buffer.from(d); // string -> Buffer in utf8
if(d instanceof ArrayBuffer ) return Buffer.from(d); // ArrayBuffer -> Buffer
if(typeof d === "boolean") { // boolean is transformed to 0/1
let b=Buffer.alloc(1);
b.writeUInt8(( (d) ? 1: 0));
return b;
}
if(typeof d === "number") { // Basic comverions of numbers as 32bit int or 32bit float
let b=Buffer.alloc(4);
if(Number.isInteger(d)) {
b.writeUInt32LE(d);
} else {
b.writeFloatLE(d);
}
return b;
}
return null;
} catch(err) {
return null;
}
};
exports.convertToString= function(d) {
if(Buffer.isBuffer(d)) return d.toString(); // utf8
if(typeof d === "string") return d;
//node.error("RF24 not valid buffer to string conversion");
return "";
};
const fs = require('fs');
const path = require('path');
exports.loadProfiles=function() {
let bfile=path.join(__dirname,"boards.json");
return new Promise((resolve,reject) => {
fs.readFile(bfile, (err, data) => {
if (err) reject(err);
try {
resolve(JSON.parse(data));
} catch(e) {
reject(e);
}
});
});
};