1
0
Fork 0
mirror of https://github.com/iNavFlight/inav-configurator.git synced 2025-07-25 01:05:12 +03:00

Furter decouple the serial port handling from the ports tab

This commit is contained in:
Pawel Spychalski (DzikuVx) 2024-03-24 14:33:42 +01:00
parent a65ff23f8d
commit 9f065fc1a6
4 changed files with 202 additions and 185 deletions

View file

@ -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',

View file

@ -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();

191
js/serialPortHelper.js Normal file
View file

@ -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;
})();

View file

@ -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;
}