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