Skip to content

Commit c82116d

Browse files
authored
refactor: MSP binding to this (#3557)
1 parent 20c3764 commit c82116d

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

src/js/msp.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const MSP = {
6161

6262
JUMBO_FRAME_SIZE_LIMIT: 255,
6363

64-
read: function (readInfo) {
64+
read(readInfo) {
6565
if (CONFIGURATOR.virtualMode) {
6666
return;
6767
}
@@ -209,11 +209,11 @@ const MSP = {
209209
}
210210
this.last_received_timestamp = Date.now();
211211
},
212-
_initialize_read_buffer: function() {
212+
_initialize_read_buffer() {
213213
this.message_buffer = new ArrayBuffer(this.message_length_expected);
214214
this.message_buffer_uint8_view = new Uint8Array(this.message_buffer);
215215
},
216-
_dispatch_message: function(expectedChecksum) {
216+
_dispatch_message(expectedChecksum) {
217217
if (this.message_checksum === expectedChecksum) {
218218
// message received, store dataview
219219
this.dataView = new DataView(this.message_buffer, 0, this.message_length_expected);
@@ -229,21 +229,20 @@ const MSP = {
229229
this.messageIsJumboFrame = false;
230230
this.crcError = false;
231231
},
232-
notify: function() {
233-
const self = this;
234-
self.listeners.forEach(function(listener) {
235-
listener(self);
232+
notify() {
233+
this.listeners.forEach((listener) => {
234+
listener(this);
236235
});
237236
},
238-
listen: function(listener) {
237+
listen(listener) {
239238
if (this.listeners.indexOf(listener) == -1) {
240239
this.listeners.push(listener);
241240
}
242241
},
243-
clearListeners: function() {
242+
clearListeners() {
244243
this.listeners = [];
245244
},
246-
crc8_dvb_s2: function(crc, ch) {
245+
crc8_dvb_s2(crc, ch) {
247246
crc ^= ch;
248247
for (let ii = 0; ii < 8; ii++) {
249248
if (crc & 0x80) {
@@ -254,14 +253,14 @@ const MSP = {
254253
}
255254
return crc;
256255
},
257-
crc8_dvb_s2_data: function(data, start, end) {
256+
crc8_dvb_s2_data(data, start, end) {
258257
let crc = 0;
259258
for (let ii = start; ii < end; ii++) {
260259
crc = this.crc8_dvb_s2(crc, data[ii]);
261260
}
262261
return crc;
263262
},
264-
encode_message_v1: function(code, data) {
263+
encode_message_v1(code, data) {
265264
const dataLength = data ? data.length : 0;
266265
// always reserve 6 bytes for protocol overhead !
267266
const bufferSize = dataLength + 6;
@@ -284,7 +283,7 @@ const MSP = {
284283
bufView[5 + dataLength] = checksum;
285284
return bufferOut;
286285
},
287-
encode_message_v2: function (code, data) {
286+
encode_message_v2(code, data) {
288287
const dataLength = data ? data.length : 0;
289288
// 9 bytes for protocol overhead
290289
const bufferSize = dataLength + 9;
@@ -304,7 +303,7 @@ const MSP = {
304303
bufView[bufferSize - 1] = this.crc8_dvb_s2_data(bufView, 3, bufferSize - 1);
305304
return bufferOut;
306305
},
307-
send_message: function (code, data, callback_sent, callback_msp, doCallbackOnError) {
306+
send_message(code, data, callback_sent, callback_msp, doCallbackOnError) {
308307
if (code === undefined || !serial.connectionId || CONFIGURATOR.virtualMode) {
309308
if (callback_msp) {
310309
callback_msp();
@@ -314,7 +313,7 @@ const MSP = {
314313

315314
// Check if request already exists in the queue
316315
let requestExists = false;
317-
for (const instance of MSP.callbacks) {
316+
for (const instance of this.callbacks) {
318317
if (instance.code === code) {
319318
requestExists = true;
320319

@@ -333,25 +332,25 @@ const MSP = {
333332
};
334333

335334
if (!requestExists) {
336-
obj.timer = setTimeout(function () {
337-
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length} (${MSP.callbacks.map(function (e) { return e.code; })})`);
338-
serial.send(bufferOut, function (_sendInfo) {
335+
obj.timer = setTimeout(() => {
336+
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${this.timeout} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`);
337+
serial.send(bufferOut, (_sendInfo) => {
339338
obj.stop = performance.now();
340339
const executionTime = Math.round(obj.stop - obj.start);
341-
MSP.timeout = Math.max(MSP.MIN_TIMEOUT, Math.min(executionTime, MSP.MAX_TIMEOUT));
340+
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
342341
});
343-
}, MSP.timeout);
342+
}, this.timeout);
344343
}
345344

346-
MSP.callbacks.push(obj);
345+
this.callbacks.push(obj);
347346

348347
// always send messages with data payload (even when there is a message already in the queue)
349348
if (data || !requestExists) {
350-
if (MSP.timeout > MSP.MIN_TIMEOUT) {
351-
MSP.timeout--;
349+
if (this.timeout > this.MIN_TIMEOUT) {
350+
this.timeout--;
352351
}
353352

354-
serial.send(bufferOut, function (sendInfo) {
353+
serial.send(bufferOut, (sendInfo) => {
355354
if (sendInfo.bytesSent === bufferOut.byteLength) {
356355
if (callback_sent) {
357356
callback_sent();
@@ -366,23 +365,21 @@ const MSP = {
366365
/**
367366
* resolves: {command: code, data: data, length: message_length}
368367
*/
369-
promise: async function(code, data) {
370-
const self = this;
371-
372-
return new Promise(function(resolve) {
373-
self.send_message(code, data, false, function(_data) {
368+
async promise(code, data) {
369+
return new Promise((resolve) => {
370+
this.send_message(code, data, false, (_data) => {
374371
resolve(_data);
375372
});
376373
});
377374
},
378-
callbacks_cleanup: function () {
375+
callbacks_cleanup() {
379376
for (const callback of this.callbacks) {
380377
clearInterval(callback.timer);
381378
}
382379

383380
this.callbacks = [];
384381
},
385-
disconnect_cleanup: function () {
382+
disconnect_cleanup() {
386383
this.state = 0; // reset packet state for "clean" initial entry (this is only required if user hot-disconnects)
387384
this.packet_error = 0; // reset CRC packet error counter for next session
388385

0 commit comments

Comments
 (0)