Skip to content

Commit

Permalink
Fix some sonar complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Jan 2, 2025
1 parent 335719b commit b7a9cc2
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 45 deletions.
14 changes: 9 additions & 5 deletions src/js/msp/MSPHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ MspHelper.prototype.process_data = function (dataHandler) {
let flags = 0;

if (!crcError) {
if (!dataHandler.unsupported)
if (!dataHandler.unsupported) {
switch (code) {
case MSPCodes.MSP_STATUS:
FC.CONFIG.cycleTime = data.readU16();
Expand Down Expand Up @@ -773,13 +773,14 @@ MspHelper.prototype.process_data = function (dataHandler) {
FC.CONFIG.apiVersion = `${data.readU8()}.${data.readU8()}.0`;
break;

case MSPCodes.MSP_FC_VARIANT:
case MSPCodes.MSP_FC_VARIANT: {
let fcVariantIdentifier = "";
for (let i = 0; i < 4; i++) {
fcVariantIdentifier += String.fromCharCode(data.readU8());
}
FC.CONFIG.flightControllerIdentifier = fcVariantIdentifier;
break;
}

case MSPCodes.MSP_FC_VERSION:
FC.CONFIG.flightControllerVersion = `${data.readU8()}.${data.readU8()}.${data.readU8()}`;
Expand Down Expand Up @@ -898,7 +899,7 @@ MspHelper.prototype.process_data = function (dataHandler) {
console.log("Channel forwarding saved");
break;

case MSPCodes.MSP_CF_SERIAL_CONFIG:
case MSPCodes.MSP_CF_SERIAL_CONFIG: {
FC.SERIAL_CONFIG.ports = [];
const bytesPerPort = 1 + 2 + 1 * 4;

Expand All @@ -916,6 +917,7 @@ MspHelper.prototype.process_data = function (dataHandler) {
FC.SERIAL_CONFIG.ports.push(serialPort);
}
break;
}

case MSPCodes.MSP2_COMMON_SERIAL_CONFIG:
FC.SERIAL_CONFIG.ports = [];
Expand Down Expand Up @@ -1701,7 +1703,7 @@ MspHelper.prototype.process_data = function (dataHandler) {
default:
console.log(`Unknown code detected: ${code} (${getMSPCodeName(code)})`);
}
else {
} else {
console.log(`FC reports unsupported message error: ${code} (${getMSPCodeName(code)})`);

if (code === MSPCodes.MSP_SET_REBOOT) {
Expand All @@ -1726,7 +1728,9 @@ MspHelper.prototype.process_data = function (dataHandler) {
dataHandler.callbacks.splice(i, 1);
if (!crcError || callbackOnError) {
// fire callback
if (callback) callback({ command: code, data: data, length: data.byteLength, crcError: crcError });
if (callback) {
callback({ command: code, data: data, length: data.byteLength, crcError: crcError });
}
} else {
console.warn(`code: ${code} - crc failed. No callback`);
}
Expand Down
6 changes: 4 additions & 2 deletions src/js/protocols/webusbdfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ class WEBUSBDFU_protocol extends EventTarget {

switch (unit) {
case "M":
page_size *= 1024; // fall through to K as well
// fall through to K as well
case "K":
page_size *= 1024;
break;
Expand Down Expand Up @@ -891,7 +891,9 @@ class WEBUSBDFU_protocol extends EventTarget {
const idx = erase_pages.findIndex((element, index, array) => {
return element.sector === i && element.page === j;
});
if (idx === -1) erase_pages.push({ sector: i, page: j });
if (idx === -1) {
erase_pages.push({ sector: i, page: j });
}
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/js/tabs/gps.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,10 @@ gps.initialize = async function (callback) {
const used = i18n.getMessage(usedArray[(FC.GPS_DATA.quality[i] & 0x8) >> 3]);

// Add color to the text
const lockedOrLow = quality.startsWith(i18n.getMessage("gnssQualityLocked")) ? "locked" : "low";
const qualityColor = quality.startsWith(i18n.getMessage("gnssQualityFullyLocked"))
? "ready"
: quality.startsWith(i18n.getMessage("gnssQualityLocked"))
? "locked"
: "low";
: lockedOrLow;
const qualityHtml = `<span class="colorToggle ${qualityColor}">${quality}</span>`;

const usedColor = used.startsWith(i18n.getMessage("gnssUsedUsed")) ? "ready" : "low";
Expand Down
68 changes: 50 additions & 18 deletions src/js/tabs/led_strip.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,15 @@ led_strip.initialize = function (callback, scrollPosition) {

for (let colorIndex = 0; colorIndex < 16; colorIndex++) {
colorButtons.removeClass("btnOn");
if (selectedModeColor == undefined) $(".ui-selected").removeClass(`color-${colorIndex}`);
if (selectedModeColor == undefined) {
$(".ui-selected").removeClass(`color-${colorIndex}`);
}

if ($(that).is(`.color-${colorIndex}`)) {
selectedColorIndex = colorIndex;
if (selectedModeColor == undefined) $(".ui-selected").addClass(`color-${colorIndex}`);
if (selectedModeColor == undefined) {
$(".ui-selected").addClass(`color-${colorIndex}`);
}
}
}

Expand Down Expand Up @@ -480,22 +484,32 @@ led_strip.initialize = function (callback, scrollPosition) {
case "y":
case "o":
case "s":
if (areModifiersActive(`function-${f}`)) p.addClass(`function-${letter}`);
if (areModifiersActive(`function-${f}`)) {
p.addClass(`function-${letter}`);
}
break;
case "b":
case "i":
case "p":
case "e":
case "u":
if (areOverlaysActive(`function-${f}`)) p.addClass(`function-${letter}`);
if (areOverlaysActive(`function-${f}`)) {
p.addClass(`function-${letter}`);
}
break;
case "w":
if (areOverlaysActive(`function-${f}`))
if (isWarningActive(`function-${f}`)) p.addClass(`function-${letter}`);
if (areOverlaysActive(`function-${f}`)) {
if (isWarningActive(`function-${f}`)) {
p.addClass(`function-${letter}`);
}
}
break;
case "v":
if (areOverlaysActive(`function-${f}`))
if (isVtxActive(`function-${f}`)) p.addClass(`function-${letter}`);
if (areOverlaysActive(`function-${f}`)) {
if (isVtxActive(`function-${f}`)) {
p.addClass(`function-${letter}`);
}
}
break;
}
}
Expand Down Expand Up @@ -982,7 +996,9 @@ led_strip.initialize = function (callback, scrollPosition) {
$(".ui-selected")
.find(".wire")
.each(function () {
if ($(this).text() != "") $(this).parent().addClass(`function-${letter}`);
if ($(this).text() != "") {
$(this).parent().addClass(`function-${letter}`);
}
});

unselectOverlays(letter);
Expand Down Expand Up @@ -1074,7 +1090,9 @@ led_strip.initialize = function (callback, scrollPosition) {
setModeBackgroundColor($(this));
});

if (change) updateBulkCmd();
if (change) {
updateBulkCmd();
}
}

function drawColorBoxesInColorLedPoints() {
Expand All @@ -1092,9 +1110,8 @@ led_strip.initialize = function (callback, scrollPosition) {
if ($(this).is(`.${className}`)) {
$(this).find(".overlay-color").addClass(className);
$(this).find(".overlay-color").css("background-color", HsvToColor(FC.LED_COLORS[colorIndex]));
} else {
if ($(this).find(".overlay-color").is(`.${className}`))
$(this).find(".overlay-color").removeClass(className);
} else if ($(this).find(".overlay-color").is(`.${className}`)) {
$(this).find(".overlay-color").removeClass(className);
}
}
} else {
Expand All @@ -1107,7 +1124,9 @@ led_strip.initialize = function (callback, scrollPosition) {
const sliders = $("div.colorDefineSliders input");
let change = false;

if (!FC.LED_COLORS[colorIndex]) return;
if (!FC.LED_COLORS[colorIndex]) {
return;
}

if (FC.LED_COLORS[colorIndex].h != Number(sliders.eq(0).val())) {
sliders.eq(0).val(FC.LED_COLORS[colorIndex].h);
Expand All @@ -1128,22 +1147,35 @@ led_strip.initialize = function (callback, scrollPosition) {
}

// only fire events when all values are set
if (change) sliders.trigger("input");
if (change) {
sliders.trigger("input");
}
}

function HsvToColor(input) {
if (input == undefined) return "";
if (input == undefined) {
return "";
}

let HSV = { h: Number(input.h), s: Number(input.s), v: Number(input.v) };

if (HSV.s == 0 && HSV.v == 0) return "";
if (HSV.s == 0 && HSV.v == 0) {
return "";
}

HSV = { h: HSV.h, s: 1 - HSV.s / 255, v: HSV.v / 255 };

const HSL = { h: 0, s: 0, v: 0 };
HSL.h = HSV.h;
HSL.l = ((2 - HSV.s) * HSV.v) / 2;
HSL.s = HSL.l && HSL.l < 1 ? (HSV.s * HSV.v) / (HSL.l < 0.5 ? HSL.l * 2 : 2 - HSL.l * 2) : HSL.s;

if (HSL.l && HSL.l < 1) {
if (HSL.l < 0.5) {
HSL.s = (HSV.s * HSV.v) / (HSL.l * 2);
} else {
HSL.s = (HSV.s * HSV.v) / (2 - HSL.l * 2);
}
}

return `hsl(${HSL.h},${HSL.s * 100}%,${HSL.l * 100}%)`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/js/tabs/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ logging.initialize = function (callback) {
if (logBuffer.length) {
// only execute when there is actual data to write
append_to_file(logBuffer.join("\n").concat("\n"));

$(".samples").text((samples += logBuffer.length));
samples += logBuffer.length;
$(".samples").text(samples);

logBuffer = [];
}
Expand Down
12 changes: 5 additions & 7 deletions src/js/tabs/motors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,13 +1019,11 @@ motors.initialize = async function (callback) {
if (FC.MOTOR_DATA[i] > zeroThrottleValue) {
motorsRunning = true;
}
} else {
if (
FC.MOTOR_DATA[i] < FC.MOTOR_3D_CONFIG.deadband3d_low ||
FC.MOTOR_DATA[i] > FC.MOTOR_3D_CONFIG.deadband3d_high
) {
motorsRunning = true;
}
} else if (
FC.MOTOR_DATA[i] < FC.MOTOR_3D_CONFIG.deadband3d_low ||
FC.MOTOR_DATA[i] > FC.MOTOR_3D_CONFIG.deadband3d_high
) {
motorsRunning = true;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/js/tabs/onboard_logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ onboard_logging.initialize = function (callback) {
}

function gcd(a, b) {
if (b === 0) return a;
if (b === 0) {
return a;
}

return gcd(b, a % b);
}
Expand Down
7 changes: 5 additions & 2 deletions src/js/tabs/pid_tuning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2344,8 +2344,11 @@ pid_tuning.updateRatesLabels = function () {
// adjust the coordinates for determine where the balloon background should be drawn
x += (align == "right" ? -(width + DEFAULT_OFFSET) : 0) + (align == "left" ? DEFAULT_OFFSET : 0);
y -= height / 2;
if (y < 0) y = 0;
else if (y > context.height) y = context.height; // prevent balloon from going out of canvas
if (y < 0) {
y = 0;
} else if (y > context.height) {
y = context.height; // prevent balloon from going out of canvas
}

// check that the balloon does not already overlap
for (let i = 0; i < dirty.length; i++) {
Expand Down
5 changes: 1 addition & 4 deletions src/js/tabs/vtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,7 @@ vtx.initialize = function (callback) {
}

if (save_vtx_powerlevels.counter < FC.VTX_CONFIG.vtx_table_powerlevels) {
FC.VTXTABLE_POWERLEVEL = Object.assign(
{},
TABS.vtx.VTXTABLE_POWERLEVEL_LIST[save_vtx_powerlevels.counter],
);
FC.VTXTABLE_POWERLEVEL = { ...TABS.vtx.VTXTABLE_POWERLEVEL_LIST[save_vtx_powerlevels.counter] };
MSP.send_message(
MSPCodes.MSP_SET_VTXTABLE_POWERLEVEL,
mspHelper.crunch(MSPCodes.MSP_SET_VTXTABLE_POWERLEVEL),
Expand Down
4 changes: 3 additions & 1 deletion src/js/workers/hex_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default async function read_hex_file(data) {
const checksum = parseInt(data[i].substr(9 + byte_count * 2, 2), 16); // (this is a 2's complement value)

switch (record_type) {
case 0x00: // data record
case 0x00: {
// data record
if (address !== next_address || next_address === 0) {
result.data.push({ address: extended_linear_address + address, bytes: 0, data: [] });
}
Expand Down Expand Up @@ -65,6 +66,7 @@ export default async function read_hex_file(data) {
hexfile_valid = false;
}
break;
}
case 0x01: // end of file record
result.end_of_file = true;
break;
Expand Down

0 comments on commit b7a9cc2

Please sign in to comment.