forked from espruino/BangleApps
/
pcas.js
128 lines (108 loc) · 2.92 KB
/
pcas.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
const PCAS="$PCAS";
const PCAS_UPDATE_RATE="02";
const PCAS_SENTENCE_OUTPUT="03";
const PCAS_NMEA_VERSION="05";
const PCAS_REBOOT="10";
const PCAS_DYNAMIC_MODEL="11";
const PCAS_STANDBY="12";
const NMEA_VERSION_MAP = {
"4.1": "2",
"4.0": "5",
"2.2": "9",
};
const REBOOT={
"hot": "0",
"cold": "1",
"warm": "2",
"factory": "3",
};
const MODEL={
"portable": "0",
"stationary": "1",
"pedestrian": "2",
"automotive": "3",
"sea": "4",
"airborne<1G": "5",
"airborne<2G": "6",
"airborne<4G": "7"
};
const SENTENCE_POSITION={
"GGA": "0",
"GLL": "1",
"GSA": "2",
"GSV": "3",
"RMC": "4",
"VTG": "5",
"ZDA": "6",
"ANT": "7",
"DHV": "8",
"LPS": "9",
"UTC": "12",
"GST": "13"
};
const NMEA_UPDATE_RATES = ["100","200","250","500","1000"];
let cleanupTimeout;
function sendCommand(command, value){
Bangle.setGPSPower(1,"gpsconf");
if (value === undefined)
throw "No value given";
Serial1.println(checksum(PCAS + command + "," + value));
if (cleanupTimeout) clearTimeout(cleanupTimeout);
cleanupTimeout = setTimeout(()=>{
Bangle.setGPSPower(0,"gpsconf");
cleanupTimeout = undefined;
}, 1000);
}
function checksum(cmd) {
var cs = 0;
for (var i = 1; i < cmd.length; i++)
cs = cs ^ cmd.charCodeAt(i);
return cmd + "*" + cs.toString(16).toUpperCase().padStart(2, '0');
}
function arrayBasedCommand(command, possibleValues, value){
if (possibleValues.includes(value+""))
sendCommand(command, value);
else
throw "Invalid value \"" + value + "\" expected one of:" + possibleValues.join(",");
}
function mapBasedCommand(command, valueMap, value){
let keys = Object.keys(valueMap);
if (keys.includes(value+"")){
sendCommand(command, valueMap[value]);
}
else
throw "Invalid value \"" + value + "\" expected one of:" + keys.join(",");
}
function setUpdateInterval(interval) {
arrayBasedCommand(PCAS_UPDATE_RATE, NMEA_UPDATE_RATES, interval);
}
function setNMEA(version) {
mapBasedCommand(PCAS_NMEA_VERSION, NMEA_VERSION_MAP, version);
}
function reboot(type){
mapBasedCommand(PCAS_REBOOT, REBOOT, type);
}
function setDynamicModel(model){
mapBasedCommand(PCAS_DYNAMIC_MODEL, MODEL, type);
}
function setStandby(seconds){
sendCommand(PCAS_STANDBY, seconds);
}
function setSentenceOutput(type, value){
let types = Object.keys(SENTENCE_POSITION);
if (!types.includes(type))
throw "Invalid type \"" + type + "\" expected one of:" + types.join(",");
if (value < 0 || value >= 10)
throw "Invalid value, expected number between 0 and 10";
let flags = new Array(types.length);
flags[SENTENCE_POSITION[type]] = value;
sendCommand(PCAS_SENTENCE_OUTPUT, flags.join(","));
}
exports.sendCommand = sendCommand;
exports.checksum = checksum;
exports.setUpdateInterval = setUpdateInterval;
exports.setNMEA = setNMEA;
exports.reboot = reboot;
exports.setDynamicModel = setDynamicModel;
exports.setStandby = setStandby;
exports.setSentenceOutput = setSentenceOutput;