[rescalc] - update to v0.02

pull/2797/head
stweedo 2023-06-04 20:43:19 -05:00
parent 9888c6c5cf
commit b4abf8571c
3 changed files with 83 additions and 144 deletions

View File

@ -1 +1,2 @@
0.01: New App! 0.01: New App!
0.02: Fixes colors not matching user input from color menu in some cases, 3 bands are now shown larger, various code improvements.

View File

@ -3,75 +3,19 @@
// https://icons8.com/icon/ISAVBnskZod0/resistor // https://icons8.com/icon/ISAVBnskZod0/resistor
let colorData = { let colorData = {
black: { black: { value: 0, multiplier: 1, hex: '#000' },
value: 0, brown: { value: 1, multiplier: 10, tolerance: 1, hex: '#8B4513' },
multiplier: Math.pow(10, 0), red: { value: 2, multiplier: 100, tolerance: 2, hex: '#f00' },
hex: '#000' orange: { value: 3, multiplier: 1000, hex: '#FF9900' },
}, yellow: { value: 4, multiplier: 10000, hex: '#ff0' },
brown: { green: { value: 5, multiplier: 100000, tolerance: 0.5, hex: '#0f0' },
value: 1, blue: { value: 6, multiplier: 1000000, tolerance: 0.25, hex: '#00f' },
multiplier: Math.pow(10, 1), violet: { value: 7, multiplier: 10000000, tolerance: 0.1, hex: '#f0f' },
tolerance: 1, grey: { value: 8, multiplier: 100000000, tolerance: 0.05, hex: '#808080' },
hex: '#8B4513' white: { value: 9, multiplier: 1000000000, hex: '#fff' },
}, gold: { multiplier: 0.1, tolerance: 5, hex: '#FFD700' },
red: { silver: { multiplier: 0.01, tolerance: 10, hex: '#C0C0C0' },
value: 2, none: { tolerance: 20 },
multiplier: Math.pow(10, 2),
tolerance: 2,
hex: '#f00'
},
orange: {
value: 3,
multiplier: Math.pow(10, 3),
hex: '#FF9900'
},
yellow: {
value: 4,
multiplier: Math.pow(10, 4),
hex: '#ff0'
},
green: {
value: 5,
multiplier: Math.pow(10, 5),
tolerance: 0.5,
hex: '#0f0'
},
blue: {
value: 6,
multiplier: Math.pow(10, 6),
tolerance: 0.25,
hex: '#00f'
},
violet: {
value: 7,
multiplier: Math.pow(10, 7),
tolerance: 0.1,
hex: '#f0f'
},
grey: {
value: 8,
multiplier: Math.pow(10, 8),
tolerance: 0.05,
hex: '#808080'
},
white: {
value: 9,
multiplier: Math.pow(10, 9),
hex: '#fff'
},
gold: {
multiplier: Math.pow(10, -1),
tolerance: 5,
hex: '#FFD700'
},
silver: {
multiplier: Math.pow(10, -2),
tolerance: 10,
hex: '#C0C0C0'
},
none: {
tolerance: 20
},
}; };
function clearScreen() { // Except Back Button function clearScreen() { // Except Back Button
@ -91,20 +35,26 @@ function colorBandsToResistance(colorBands) {
return [resistance, tolerance]; return [resistance, tolerance];
} }
function log10(val) {
return Math.log(val) / Math.log(10);
}
function resistanceToColorBands(resistance, tolerance) { function resistanceToColorBands(resistance, tolerance) {
let multiplier = Math.floor(log10(resistance)); let resistanceStr = resistance.toString();
let firstDigit = Math.floor(resistance / Math.pow(10, multiplier)); let firstDigit, secondDigit, multiplier;
resistance -= firstDigit * Math.pow(10, multiplier); if (resistanceStr.length === 1) { // Check if resistance is a single digit
multiplier--; // for the next digit firstDigit = 0;
let secondDigit = Math.floor(resistance / Math.pow(10, multiplier)); secondDigit = Number(resistanceStr.charAt(0));
resistance -= secondDigit * Math.pow(10, multiplier); multiplier = 0;
console.log("First Digit: " + firstDigit); } else if (resistance >= 100) {
console.log("Second Digit: " + secondDigit); // Extract the first two digits from the resistance value
console.log("Multiplier: " + multiplier); firstDigit = Number(resistanceStr.charAt(0));
secondDigit = Number(resistanceStr.charAt(1));
// Calculate the multiplier
multiplier = resistanceStr.length - 2;
} else {
// For values between 10-99, shift the color to the first band
firstDigit = Number(resistanceStr.charAt(0));
secondDigit = Number(resistanceStr.charAt(1));
multiplier = 0;
}
let firstBandEntry = Object.entries(colorData).find(function(entry) { let firstBandEntry = Object.entries(colorData).find(function(entry) {
return entry[1].value === firstDigit; return entry[1].value === firstDigit;
}); });
@ -121,8 +71,9 @@ function resistanceToColorBands(resistance, tolerance) {
return entry[1].tolerance === tolerance; return entry[1].tolerance === tolerance;
}); });
let toleranceBand = toleranceBandEntry ? toleranceBandEntry[1].hex : undefined; let toleranceBand = toleranceBandEntry ? toleranceBandEntry[1].hex : undefined;
console.log("Color bands: " + [firstBand, secondBand, multiplierBand, toleranceBand]); let bands = [firstBand, secondBand, multiplierBand];
return [firstBand, secondBand, multiplierBand, toleranceBand]; if (toleranceBand) bands.push(toleranceBand);
return bands;
} }
function drawResistor(colorBands, tolerance) { function drawResistor(colorBands, tolerance) {
@ -130,32 +81,27 @@ function drawResistor(colorBands, tolerance) {
let resistorBodyWidth = 51; let resistorBodyWidth = 51;
let resistorBodyHeight = 43; let resistorBodyHeight = 43;
let resistorStartX = 52; let resistorStartX = 52;
var bandColors = colorBands; let bandColors = colorBands;
var numcolorBands = bandColors.length; let numColorBands = bandColors.length;
var resistorStartY = ((g.getHeight() - resistorBodyHeight) / 2) + 48; let resistorStartY = ((g.getHeight() - resistorBodyHeight) / 2) + 48;
clearScreen(); clearScreen();
g.drawImage(img, 0, 112); g.drawImage(img, 0, 112);
var bandWidth = (resistorBodyWidth - (numcolorBands * 2 - 1)) / numcolorBands; // width of each band, accounting for the spacing let bandWidth = (resistorBodyWidth - (numColorBands * 2 - 1)) / numColorBands; // width of each band, accounting for the spacing
var bandHeight = resistorBodyHeight; // height of each band let bandHeight = resistorBodyHeight; // height of each band
var currentX = resistorStartX; // starting point for the first band let currentX = resistorStartX; // starting point for the first band
// Define the tolerance values that will trigger the fourth band // Define the tolerance values that will trigger the fourth band
var validTolerances = [1, 2, 0.5, 0.25, 0.1, 0.05, 5, 10]; let validTolerances = [1, 2, 0.5, 0.25, 0.1, 0.05, 5, 10];
for (let i = 0; i < numColorBands; i++) {
for (var i = 0; i < numcolorBands; i++) {
// Skip the fourth band and its outlines if the tolerance is not in the valid list // Skip the fourth band and its outlines if the tolerance is not in the valid list
if (i === 3 && !validTolerances.includes(tolerance)) continue; if (i === 3 && !validTolerances.includes(tolerance)) continue;
let bandX = currentX; // calculate the x-coordinate of the band
var bandX = currentX; // calculate the x-coordinate of the band let bandY = resistorStartY; // y-coordinate of the band
var bandY = resistorStartY; // y-coordinate of the band
g.setColor(bandColors[i]); // set the color for the band g.setColor(bandColors[i]); // set the color for the band
g.fillRect(bandX, bandY, bandX + bandWidth, bandY + bandHeight); g.fillRect(bandX, bandY, bandX + bandWidth, bandY + bandHeight);
// Draw band outlines // Draw band outlines
g.setColor('#000'); // set the color for the outline g.setColor('#000'); // set the color for the outline
g.drawLine(bandX, bandY, bandX, bandY + bandHeight); // left outline g.drawLine(bandX, bandY, bandX, bandY + bandHeight); // left outline
g.drawLine(bandX + bandWidth, bandY, bandX + bandWidth, bandY + bandHeight); // right outline g.drawLine(bandX + bandWidth, bandY, bandX + bandWidth, bandY + bandHeight); // right outline
// if it's the fourth band, shift it over by an additional 12 pixels // if it's the fourth band, shift it over by an additional 12 pixels
if (i === 2) { if (i === 2) {
currentX = bandX + bandWidth + 5 + 12; // update the current X position for the next band, accounting for the spacing currentX = bandX + bandWidth + 5 + 12; // update the current X position for the next band, accounting for the spacing
@ -170,14 +116,14 @@ function omega() {
} }
function formatResistance(resistance) { function formatResistance(resistance) {
var units = ["", "k", "M", "G"]; let units = ["", "k", "M", "G"];
var unitIndex = 0; let unitIndex = 0;
while (resistance >= 1000 && unitIndex < units.length - 1) { while (resistance >= 1000 && unitIndex < units.length - 1) {
resistance /= 1000; resistance /= 1000;
unitIndex++; unitIndex++;
} }
// Convert to string and truncate unnecessary zeroes // Convert to string and truncate unnecessary zeroes
var resistanceStr = String(resistance); let resistanceStr = String(resistance);
if (resistanceStr.length > 5) { // if length is more than 5 including decimal point if (resistanceStr.length > 5) { // if length is more than 5 including decimal point
resistanceStr = resistance.toFixed(2); resistanceStr = resistance.toFixed(2);
} }
@ -188,49 +134,40 @@ function formatResistance(resistance) {
} }
function drawResistance(resistance, tolerance) { function drawResistance(resistance, tolerance) {
var x = g.getWidth() / 2; let x = g.getWidth() / 2;
var y = 40; let y = 40;
var formattedResistance = formatResistance(resistance); let formattedResistance = formatResistance(resistance);
var resistanceStr = formattedResistance.value; let resistanceStr = formattedResistance.value;
var unit = formattedResistance.unit; let unit = formattedResistance.unit;
g.reset(); g.reset();
// draw resistance value // draw resistance value
g.setFontAlign(0, 0).setFont("Vector", 54); g.setFontAlign(0, 0).setFont("Vector", 54);
g.clearRect(0, y - 15, g.getWidth(), y + 25); // clear the background g.clearRect(0, y - 15, g.getWidth(), y + 25); // clear the background
g.drawString(resistanceStr, x + 4, y); g.drawString(resistanceStr, x + 4, y);
// draw unit, symbol, and tolerance
// draw unit, symbol and tolerance
y += 46; y += 46;
g.setFontAlign(-1, 0).setFont("Vector", 27); g.setFontAlign(-1, 0).setFont("Vector", 27);
let toleranceShift = tolerance.toString().replace('.', '').length > 2 ? 8 : 0;
var toleranceShift = tolerance.toString().replace('.', '').length > 2 ? 8 : 0; let unitX = ((unit === "M" || unit === "G") ? 0 : 8) - toleranceShift;
var unitX = ((unit === "M" || unit === "G") ? 0 : 8) - toleranceShift; let omegaX = (unit ? 46 : 36) - toleranceShift; // Shift the Omega symbol to the left if there is no unit
var omegaX = (unit ? 46 : 36) - toleranceShift; // Shift the Omega symbol to the left if there is no unit
g.drawString(unit.padStart(3), unitX, y); g.drawString(unit.padStart(3), unitX, y);
// Draw the Ohm symbol to the right of the unit // Draw the Ohm symbol to the right of the unit
g.drawImage(omega(), omegaX, y - 13, { g.drawImage(omega(), omegaX, y - 13, { scale: 0.45 });
scale: 0.45
});
g.setFontAlign(1, 0).setFont("Vector", 27); g.setFontAlign(1, 0).setFont("Vector", 27);
// Define the tolerance values that will trigger the fourth band // Define the tolerance values that will trigger the fourth band
var validTolerances = [1, 2, 0.5, 0.25, 0.1, 0.05, 5, 10]; let validTolerances = [1, 2, 0.5, 0.25, 0.1, 0.05, 5, 10];
// Check if the tolerance is not in the valid list, and if it's not, set it to 20 // Check if the tolerance is not in the valid list, and if it's not, set it to 20
if (!validTolerances.includes(tolerance)) { if (!validTolerances.includes(tolerance)) {
tolerance = 20; tolerance = 20;
} }
let toleranceStr = "±" + tolerance + "%";
var toleranceStr = "±" + tolerance + "%"; let toleranceX = tolerance.toString().replace('.', '').length > 2 ? 10 : 14;
var toleranceX = tolerance.toString().replace('.', '').length > 2 ? 10 : 14;
g.drawString(toleranceStr.padEnd(4), 176 - toleranceX, y); g.drawString(toleranceStr.padEnd(4), 176 - toleranceX, y);
} }
(function() { (function() {
let colorBands;
let inputColorBands;
let settings = { let settings = {
resistance: 0, resistance: 0,
tolerance: 0, tolerance: 0,
@ -244,6 +181,8 @@ function drawResistance(resistance, tolerance) {
colorBands: ["", "", "", ""] colorBands: ["", "", "", ""]
}; };
settings = emptySettings; settings = emptySettings;
colorBands = null;
inputColorBands = null;
} }
function showColorBandMenu(bandNumber) { function showColorBandMenu(bandNumber) {
@ -289,11 +228,8 @@ function drawResistance(resistance, tolerance) {
function setBandColor(bandNumber, color) { function setBandColor(bandNumber, color) {
settings.colorBands[bandNumber - 1] = color; // arrays are 0-indexed settings.colorBands[bandNumber - 1] = color; // arrays are 0-indexed
console.log(`Band ${bandNumber} color set to ${color}`);
// Update the color band in the colorEntryMenu // Update the color band in the colorEntryMenu
colorEntryMenu[`${bandNumber}:`].value = color; colorEntryMenu[`${bandNumber}:`].value = color;
showColorEntryMenu(); showColorEntryMenu();
} }
@ -339,8 +275,8 @@ function drawResistance(resistance, tolerance) {
} }
}, },
'Draw Resistor': function() { 'Draw Resistor': function() {
let colorBands = settings.colorBands; inputColorBands = settings.colorBands;
let values = colorBandsToResistance(colorBands); let values = colorBandsToResistance(inputColorBands);
settings.resistance = values[0]; settings.resistance = values[0];
settings.tolerance = values[1]; settings.tolerance = values[1];
showDrawingMenu(); showDrawingMenu();
@ -367,7 +303,6 @@ function drawResistance(resistance, tolerance) {
let formattedMultiplier = formatMultiplier(multiplierValue); let formattedMultiplier = formatMultiplier(multiplierValue);
multiplierMenu[`${formattedMultiplier}`] = () => { multiplierMenu[`${formattedMultiplier}`] = () => {
settings.multiplier = multiplierValue; settings.multiplier = multiplierValue;
console.log(`Multiplier changed to: ${settings.multiplier}`);
// Update the value of 'Multiplier' in resistanceEntryMenu // Update the value of 'Multiplier' in resistanceEntryMenu
resistanceEntryMenu["Multiplier"] = function() { resistanceEntryMenu["Multiplier"] = function() {
showMultiplierMenu(); showMultiplierMenu();
@ -406,7 +341,6 @@ function drawResistance(resistance, tolerance) {
let tolerance = parseFloat(colorData[color].tolerance); // Parse the tolerance as a float let tolerance = parseFloat(colorData[color].tolerance); // Parse the tolerance as a float
toleranceMenu[`${tolerance}%`] = () => { toleranceMenu[`${tolerance}%`] = () => {
settings.tolerance = tolerance; settings.tolerance = tolerance;
console.log(settings.tolerance);
// Update the value of 'Tolerance (%)' in resistanceEntryMenu // Update the value of 'Tolerance (%)' in resistanceEntryMenu
resistanceEntryMenu["Tolerance (%)"] = function() { resistanceEntryMenu["Tolerance (%)"] = function() {
showToleranceMenu(); showToleranceMenu();
@ -415,13 +349,21 @@ function drawResistance(resistance, tolerance) {
}; };
} }
} }
E.showMenu(toleranceMenu); E.showMenu(toleranceMenu);
} }
function drawResistorAndResistance(resistance, tolerance, multipliedResistance) { function drawResistorAndResistance(resistance, tolerance) {
console.log('Draw Resistor clicked'); if (inputColorBands) {
let colorBands = resistanceToColorBands(multipliedResistance || resistance, tolerance); colorBands = inputColorBands.map(color => {
if (colorData.hasOwnProperty(color)) {
return colorData[color].hex;
} else {
return;
}
});
} else {
colorBands = resistanceToColorBands(resistance, tolerance);
}
drawResistor(colorBands, tolerance); drawResistor(colorBands, tolerance);
drawResistance(resistance, tolerance); drawResistance(resistance, tolerance);
resetSettings(); resetSettings();
@ -469,25 +411,21 @@ function drawResistance(resistance, tolerance) {
}; };
resistanceEntryMenu['Ohms'].onchange = v => { resistanceEntryMenu['Ohms'].onchange = v => {
settings.resistance = v || 0; settings.resistance = v || 0;
console.log('Resistance changed to: ', settings.resistance);
}; };
E.showMenu(resistanceEntryMenu); E.showMenu(resistanceEntryMenu);
} }
function showDrawingMenu() { function showDrawingMenu() {
let drawingMenu = { let drawingMenu = {
'': { '': {
'title': 'Resistor Drawing' 'title': ''
}, },
'< Back': function() { '< Back': function() {
clearScreen(); clearScreen();
E.showMenu(mainMenu); E.showMenu(mainMenu);
}, },
}; };
E.showMenu(drawingMenu); E.showMenu(drawingMenu);
let resistance = settings.resistance * (settings.multiplier || 1); let resistance = settings.resistance * (settings.multiplier || 1);
let tolerance = settings.tolerance; let tolerance = settings.tolerance;
drawResistorAndResistance(resistance, tolerance); drawResistorAndResistance(resistance, tolerance);

View File

@ -3,7 +3,7 @@
"name": "Resistor Calculator", "name": "Resistor Calculator",
"shortName": "Resistor Calc", "shortName": "Resistor Calc",
"icon": "rescalc.png", "icon": "rescalc.png",
"version":"0.01", "version":"0.02",
"screenshots": [ "screenshots": [
{"url": "screenshot.png"}, {"url": "screenshot.png"},
{"url": "screenshot-1.png"}, {"url": "screenshot-1.png"},