From 9f065fc1a6c72fa50f0d3295caa9aa89f127deca Mon Sep 17 00:00:00 2001 From: "Pawel Spychalski (DzikuVx)" Date: Sun, 24 Mar 2024 14:33:42 +0100 Subject: [PATCH] Furter decouple the serial port handling from the ports tab --- gulpfile.js | 1 + js/msp/MSPHelper.js | 60 +------------ js/serialPortHelper.js | 191 +++++++++++++++++++++++++++++++++++++++++ tabs/ports.js | 135 ++--------------------------- 4 files changed, 202 insertions(+), 185 deletions(-) create mode 100644 js/serialPortHelper.js diff --git a/gulpfile.js b/gulpfile.js index b482299f..dd59c9bb 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -87,6 +87,7 @@ sources.js = [ './js/simple_smooth_filter.js', './js/walking_average_filter.js', './js/gui.js', + './js/serialPortHelper.js', './js/msp/MSPCodes.js', './js/msp/MSPHelper.js', './js/msp/MSPchainer.js', diff --git a/js/msp/MSPHelper.js b/js/msp/MSPHelper.js index 4e40a66c..275eabd5 100644 --- a/js/msp/MSPHelper.js +++ b/js/msp/MSPHelper.js @@ -20,33 +20,6 @@ var mspHelper = (function (gui) { '921600' ]; - self.SERIAL_PORT_FUNCTIONS = { - 'MSP': 0, - 'GPS': 1, - 'TELEMETRY_FRSKY': 2, - 'TELEMETRY_HOTT': 3, - 'TELEMETRY_LTM': 4, // LTM replaced MSP - 'TELEMETRY_SMARTPORT': 5, - 'RX_SERIAL': 6, - 'BLACKBOX': 7, - 'TELEMETRY_MAVLINK': 8, - 'TELEMETRY_IBUS': 9, - 'RUNCAM_DEVICE_CONTROL': 10, - 'TBS_SMARTAUDIO': 11, - 'IRC_TRAMP': 12, - 'OPFLOW': 14, - 'LOG': 15, - 'RANGEFINDER': 16, - 'VTX_FFPV': 17, - 'ESC': 18, - 'GSM_SMS': 19, - 'FRSKY_OSD': 20, - 'DJI_FPV': 21, - 'SBUS_OUTPUT': 22, - 'SMARTPORT_MASTER': 23, - 'MSP_DISPLAYPORT': 25, - }; - // Required for MSP_DEBUGMSG because console.log() doesn't allow omitting // the newline at the end, so we keep the pending message here until we find a // '\0', then print it. Messages sent by MSP_DEBUGMSG are guaranteed to @@ -849,7 +822,7 @@ var mspHelper = (function (gui) { var serialPort = { identifier: data.getUint8(offset), - functions: mspHelper.serialPortFunctionMaskToFunctions(data.getUint32(offset + 1, true)), + functions: helper.serialPortHelper.maskToFunctions(data.getUint32(offset + 1, true)), msp_baudrate: BAUD_RATES[data.getUint8(offset + 5)], sensors_baudrate: BAUD_RATES[data.getUint8(offset + 6)], telemetry_baudrate: BAUD_RATES[data.getUint8(offset + 7)], @@ -1933,7 +1906,7 @@ var mspHelper = (function (gui) { buffer.push(serialPort.identifier); - var functionMask = mspHelper.SERIAL_PORT_FUNCTIONSToMask(serialPort.functions); + var functionMask = helper.serialPortHelper.functionsToMask(serialPort.functions); buffer.push(specificByte(functionMask, 0)); buffer.push(specificByte(functionMask, 1)); buffer.push(specificByte(functionMask, 2)); @@ -2676,35 +2649,6 @@ var mspHelper = (function (gui) { } }; - /** - * @return {number} - */ - self.SERIAL_PORT_FUNCTIONSToMask = function (functions) { - var mask = 0; - for (var index = 0; index < functions.length; index++) { - var key = functions[index]; - var bitIndex = mspHelper.SERIAL_PORT_FUNCTIONS[key]; - if (bitIndex >= 0) { - mask = bit_set(mask, bitIndex); - } - } - return mask; - }; - - self.serialPortFunctionMaskToFunctions = function (functionMask) { - var functions = []; - - var keys = Object.keys(mspHelper.SERIAL_PORT_FUNCTIONS); - for (var index = 0; index < keys.length; index++) { - var key = keys[index]; - var bit = mspHelper.SERIAL_PORT_FUNCTIONS[key]; - if (bit_check(functionMask, bit)) { - functions.push(key); - } - } - return functions; - }; - self.sendServoMixRules = function (onCompleteCallback) { // TODO implement onCompleteCallback(); diff --git a/js/serialPortHelper.js b/js/serialPortHelper.js new file mode 100644 index 00000000..7fc48fc7 --- /dev/null +++ b/js/serialPortHelper.js @@ -0,0 +1,191 @@ +'use strict'; + +var helper = helper || {}; + +helper.serialPortHelper = (function () { + + let publicScope = {}, + privateScope = {}; + + privateScope.namesGenerated = false; + + // This is a list of all the rules for the serial ports as well as their names + privateScope.rules = [ + { name: 'MSP', groups: ['data', 'msp'], maxPorts: 2 }, + { name: 'GPS', groups: ['sensors'], maxPorts: 1, defaultBaud: 115200 }, + { name: 'TELEMETRY_FRSKY', groups: ['telemetry'], sharableWith: ['msp'], notSharableWith: ['blackbox'], maxPorts: 1 }, + { name: 'TELEMETRY_HOTT', groups: ['telemetry'], sharableWith: ['msp'], notSharableWith: ['blackbox'], maxPorts: 1 }, + { name: 'TELEMETRY_SMARTPORT', groups: ['telemetry'], maxPorts: 1 }, + { name: 'TELEMETRY_LTM', groups: ['telemetry'], sharableWith: ['msp'], notSharableWith: ['blackbox'], maxPorts: 1 }, + { name: 'RX_SERIAL', groups: ['rx'], maxPorts: 1 }, + { name: 'BLACKBOX', groups: ['peripherals'], sharableWith: ['msp'], notSharableWith: ['telemetry'], maxPorts: 1 }, + { + name: 'TELEMETRY_MAVLINK', + groups: ['telemetry'], + sharableWith: ['msp'], + notSharableWith: ['blackbox'], + maxPorts: 1 + }, + { + name: 'TELEMETRY_IBUS', + groups: ['telemetry'], + sharableWith: ['msp'], + notSharableWith: ['blackbox'], + maxPorts: 1 + }, + { + name: 'RANGEFINDER', + groups: ['sensors'], + maxPorts: 1 + }, + { + name: 'GSM_SMS', + groups: ['telemetry'], + maxPorts: 1 + }, + { + name: 'RUNCAM_DEVICE_CONTROL', + groups: ['peripherals'], + maxPorts: 1 + }, + { + name: 'TBS_SMARTAUDIO', + groups: ['peripherals'], + maxPorts: 1 + }, + { + name: 'IRC_TRAMP', + groups: ['peripherals'], + maxPorts: 1 + }, + { + name: 'VTX_FFPV', + groups: ['peripherals'], + maxPorts: 1 + }, + { + name: 'ESC', + groups: ['peripherals'], + maxPorts: 1, + defaultBaud: 115200 + }, + { + name: 'OPFLOW', + groups: ['sensors'], + maxPorts: 1 + }, + { + name: 'FRSKY_OSD', + groups: ['peripherals'], + maxPorts: 1, + defaultBaud: 250000 + }, + { + name: 'DJI_FPV', + groups: ['peripherals'], + maxPorts: 1, + defaultBaud: 115200 + }, + { + name: 'MSP_DISPLAYPORT', + groups: ['peripherals'], + maxPorts: 1 + }, + { + name: 'SMARTPORT_MASTER', + groups: ['peripherals'], + maxPorts: 1, + defaultBaud: 57600 + }, + { + name: 'SBUS_OUTPUT', + groups: ['peripherals'], + maxPorts: 1, + defaultBaud: 115200 + } + ]; + + // This is a mapping of the function names to their IDs required by the firmware and MSP protocol + privateScope.functionIDs = { + 'MSP': 0, + 'GPS': 1, + 'TELEMETRY_FRSKY': 2, + 'TELEMETRY_HOTT': 3, + 'TELEMETRY_LTM': 4, // LTM replaced MSP + 'TELEMETRY_SMARTPORT': 5, + 'RX_SERIAL': 6, + 'BLACKBOX': 7, + 'TELEMETRY_MAVLINK': 8, + 'TELEMETRY_IBUS': 9, + 'RUNCAM_DEVICE_CONTROL': 10, + 'TBS_SMARTAUDIO': 11, + 'IRC_TRAMP': 12, + 'OPFLOW': 14, + 'LOG': 15, + 'RANGEFINDER': 16, + 'VTX_FFPV': 17, + 'ESC': 18, + 'GSM_SMS': 19, + 'FRSKY_OSD': 20, + 'DJI_FPV': 21, + 'SBUS_OUTPUT': 22, + 'SMARTPORT_MASTER': 23, + 'MSP_DISPLAYPORT': 25, + }; + + privateScope.generateNames = function () { + if (privateScope.namesGenerated) { + return; + } + + for (var i = 0; i < privateScope.rules.length; i++) { + privateScope.rules[i].displayName = chrome.i18n.getMessage('portsFunction_' + privateScope.rules[i].name); + } + + privateScope.namesGenerated = true; + }; + + publicScope.getRules = function () { + privateScope.generateNames(); + + return privateScope.rules; + }; + + /** + * + * @param {array} functions + * @returns {number} + */ + publicScope.functionsToMask = function (functions) { + let mask = 0; + for (let index = 0; index < functions.length; index++) { + let key = functions[index]; + let bitIndex = privateScope.functionIDs[key]; + if (bitIndex >= 0) { + mask = bit_set(mask, bitIndex); + } + } + return mask; + }; + + /** + * + * @param {number} mask + * @returns {array} + */ + publicScope.maskToFunctions = function (mask) { + let functions = []; + + let keys = Object.keys(privateScope.functionIDs); + for (let index = 0; index < keys.length; index++) { + let key = keys[index]; + let bit = privateScope.functionIDs[key]; + if (bit_check(mask, bit)) { + functions.push(key); + } + } + return functions; + }; + + return publicScope; +})(); \ No newline at end of file diff --git a/tabs/ports.js b/tabs/ports.js index 45dd5a24..9e337786 100644 --- a/tabs/ports.js +++ b/tabs/ports.js @@ -2,129 +2,8 @@ TABS.ports = {}; -var portFunctionRules; - TABS.ports.initialize = function (callback) { - /* ** portFunctionRules Notes ** - Do not set a defaultBaud for functions in the telemetry group. - These should default to AUTO, which is handled in the onchange function. The baud rate is then set by the firmware. - */ - portFunctionRules = [ - {name: 'MSP', groups: ['data', 'msp'], maxPorts: 2}, - {name: 'GPS', groups: ['sensors'], maxPorts: 1, defaultBaud: 115200}, - {name: 'TELEMETRY_FRSKY', groups: ['telemetry'], sharableWith: ['msp'], notSharableWith: ['blackbox'], maxPorts: 1}, - {name: 'TELEMETRY_HOTT', groups: ['telemetry'], sharableWith: ['msp'], notSharableWith: ['blackbox'], maxPorts: 1}, - {name: 'TELEMETRY_SMARTPORT', groups: ['telemetry'], maxPorts: 1}, - {name: 'TELEMETRY_LTM', groups: ['telemetry'], sharableWith: ['msp'], notSharableWith: ['blackbox'], maxPorts: 1}, - {name: 'RX_SERIAL', groups: ['rx'], maxPorts: 1}, - {name: 'BLACKBOX', groups: ['peripherals'], sharableWith: ['msp'], notSharableWith: ['telemetry'], maxPorts: 1} - ]; - - portFunctionRules.push({ - name: 'TELEMETRY_MAVLINK', - groups: ['telemetry'], - sharableWith: ['msp'], - notSharableWith: ['blackbox'], - maxPorts: 1 - }); - - /* - * Support for FlySky iBus Telemetry - */ - portFunctionRules.push({ - name: 'TELEMETRY_IBUS', - groups: ['telemetry'], - sharableWith: ['msp'], - notSharableWith: ['blackbox'], - maxPorts: 1 - }); - - portFunctionRules.push({ - name: 'RANGEFINDER', - groups: ['sensors'], - maxPorts: 1 } - ); - - portFunctionRules.push({ - name: 'GSM_SMS', - groups: ['telemetry'], - maxPorts: 1 } - ); - - // support configure RunCam Device - portFunctionRules.push({ - name: 'RUNCAM_DEVICE_CONTROL', - groups: ['peripherals'], - maxPorts: 1 } - ); - - portFunctionRules.push({ - name: 'TBS_SMARTAUDIO', - groups: ['peripherals'], - maxPorts: 1 } - ); - portFunctionRules.push({ - name: 'IRC_TRAMP', - groups: ['peripherals'], - maxPorts: 1 } - ); - portFunctionRules.push({ - name: 'VTX_FFPV', - groups: ['peripherals'], - maxPorts: 1 } - ); - - portFunctionRules.push({ - name: 'OPFLOW', - groups: ['sensors'], - maxPorts: 1 } - ); - - portFunctionRules.push({ - name: 'ESC', - groups: ['peripherals'], - maxPorts: 1, - defaultBaud: 115200 } - ); - - portFunctionRules.push({ - name: 'FRSKY_OSD', - groups: ['peripherals'], - maxPorts: 1, - defaultBaud: 250000 } - ); - - portFunctionRules.push({ - name: 'DJI_FPV', - groups: ['peripherals'], - maxPorts: 1, - defaultBaud: 115200 } - ); - - portFunctionRules.push({ - name: 'MSP_DISPLAYPORT', - groups: ['peripherals'], - maxPorts: 1 } - ); - - portFunctionRules.push({ - name: 'SMARTPORT_MASTER', - groups: ['peripherals'], - maxPorts: 1, - defaultBaud: 57600 } - ); - portFunctionRules.push({ - name: 'SBUS_OUTPUT', - groups: ['peripherals'], - maxPorts: 1, - defaultBaud: 115200 } - ); - - for (var i = 0; i < portFunctionRules.length; i++) { - portFunctionRules[i].displayName = chrome.i18n.getMessage('portsFunction_' + portFunctionRules[i].name); - } - var mspBaudRates = [ '9600', '19200', @@ -249,8 +128,8 @@ TABS.ports.initialize = function (callback) { let functions_e_id = "portFunc-" + column + "-" + portIndex; functions_e.attr("id", functions_e_id); - for (i = 0; i < portFunctionRules.length; i++) { - var functionRule = portFunctionRules[i]; + for (i = 0; i < helper.serialPortHelper.getRules().length; i++) { + var functionRule = helper.serialPortHelper.getRules()[i]; var functionName = functionRule.name; if (functionRule.groups.indexOf(column) == -1) { @@ -379,10 +258,12 @@ function updateDefaultBaud(baudSelect, column) { let portName = section.find('.function-' + column).val(); let baudRate = (column === 'telemetry') ? "AUTO" : 115200;; - for (i = 0; i < portFunctionRules.length; i++) { - if (portFunctionRules[i].name === portName) { - if (typeof portFunctionRules[i].defaultBaud !== 'undefined') { - baudRate = portFunctionRules[i].defaultBaud; + let rules = helper.serialPortHelper.getRules(); + + for (i = 0; i < rules.length; i++) { + if (rules[i].name === portName) { + if (typeof rules[i].defaultBaud !== 'undefined') { + baudRate = rules[i].defaultBaud; } break; }