From bd540380a5e4e87018b88adda541c6eace305a8c Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Fri, 2 Jun 2023 18:09:55 -0500 Subject: [PATCH 01/24] Add files via upload --- apps/ohmcalc/ChangeLog | 1 + apps/ohmcalc/README.md | 22 +++ apps/ohmcalc/app-icon.js | 1 + apps/ohmcalc/app.js | 300 +++++++++++++++++++++++++++++++++++++ apps/ohmcalc/app.png | Bin 0 -> 2098 bytes apps/ohmcalc/metadata.json | 22 +++ apps/ohmcalc/ohmcalc.js | 300 +++++++++++++++++++++++++++++++++++++ 7 files changed, 646 insertions(+) create mode 100644 apps/ohmcalc/ChangeLog create mode 100644 apps/ohmcalc/README.md create mode 100644 apps/ohmcalc/app-icon.js create mode 100644 apps/ohmcalc/app.js create mode 100644 apps/ohmcalc/app.png create mode 100644 apps/ohmcalc/metadata.json create mode 100644 apps/ohmcalc/ohmcalc.js diff --git a/apps/ohmcalc/ChangeLog b/apps/ohmcalc/ChangeLog new file mode 100644 index 000000000..5560f00bc --- /dev/null +++ b/apps/ohmcalc/ChangeLog @@ -0,0 +1 @@ +0.01: New App! diff --git a/apps/ohmcalc/README.md b/apps/ohmcalc/README.md new file mode 100644 index 000000000..f45b8e977 --- /dev/null +++ b/apps/ohmcalc/README.md @@ -0,0 +1,22 @@ +# Ohm's Law Calculator + +This is a simple and intuitive Ohm's Law Calculator application designed for the Bangle.js 2 Smartwatch. The calculator focuses on providing an easy-to-navigate user interface with automatic calculation logic, allowing you to quickly and efficiently calculate electrical measurements, including Voltage, Current, Resistance, and Power. + +## Usage Guide + +* __Select the Electrical Measurement:__ On the main menu, select the electrical measurement that you wish to calculate (Voltage, Current, Resistance, or Power). +* __Enter Known Values:__ You will then be prompted to enter the known values. The application will present a menu listing the remaining electrical measurements. Select one and you will be taken to a numeric input screen. Repeat this step for the second known value. +* __View Results:__ Once the two known values have been entered, the application will automatically calculate and display the value of the selected electrical measurement. The Results menu will show the calculated value, the unit of measurement, and the formula used for calculation. +* __Navigation:__ Whether you're deep in calculations or perusing the results, the 'Back' button is always available to help you step back through the app. In case you'd like to start afresh from the Results menu, just tap on 'Main Menu'. + +## Compatibility + +This app was built and tested with Bangle.js 2. + +## Feedback + +If you have any issues or suggestions, please open an issue on this GitHub repository. Contributions to improve the application are also welcomed. + +## Creator + +[stweedo](https://github.com/stweedo) diff --git a/apps/ohmcalc/app-icon.js b/apps/ohmcalc/app-icon.js new file mode 100644 index 000000000..091392f44 --- /dev/null +++ b/apps/ohmcalc/app-icon.js @@ -0,0 +1 @@ +require("heatshrink").decompress(atob("mEwxH+AH4AyQBIvmnM/AAk5F8pWBFwoABMDweGgEHF48HCAw2VE4MGDAgvCRIIACSoIvFgEGG4wuPKAYZCgENn8UgFVqsAik/hoNDC4ouUAAMOLAYoBF4Ok0gGEFwUOTJQuPAAM4DIIJCFAI2GRYM4ZRguNg7pIh0NhpXDAwIVIGBguFdwJlHABbTCgwwPCIgQB1V6dYSXGQ4zzCvWqIwhNBMBwvBvVPbokUKQQADg7BBSQUAp5FBDojANFAQmCAoTxCgEsmc0mkzlgxCMoQwBFwYFBFxgvBgAaCcYcUgEBmdfAA0zCoJiCegc4BIIvNeQwuCg4qEg8rAwowDdhwvLI4IuFLIIHFGAT4EF5rdEJAkHgImHF41fI4p2BAAYtIg8HhpGECwK7FXAQvHBQJIEikNEYIxGgE5UQh2FLw0rlYvHMAwAEnIvDGgIJDigABAwUAlhTGwAvBmgABnQMDlgfDDwRVDMAYvETocOAwSOFLwPVp6wEGAY8BC4MOBgYvMqukgENXwU0Lw3V5+kAALDFBoLaBhsA0lVF6U4F4rFBFgXP6uANoovVR5U6RQlP6ryGR6jvMnTqCYQJeGd6AwBBIYAFRIIiEeQheGr7FBDxE5FwZgCCQMHhqkBIwZTGYYYKGRwJ4DDIMNEYIoCF4YxEAAIWEMBAIBLxhIBAAYtFGYwXEnAmHF4JeFA4J4EAwIrLF5JICGAq9GE4J2EF6JsBI4UNhx5EYY67CFwcOhp3DGB0AFQTQCAoU4AwUsmYAClgHBg5EChwGCAoaNQE4N6p4wDMQIxCAAcHRYYoBp96DoouLgyjE1QZBPYQAEnDmEAAUNIoOqbYkGGBTsFCIL0GABhsCJoZgOFAkHFxEOAASZDNwYVFFxgwHdogJCii+DXoIGCgyXGFxwwHboIoG0mkAwgWBgBnDFyIwFVYQICQgIoBqtVF4TrBBoQXFFyAwDeAI4GnJmDnImGSYIuUJQbKMKxAXGAC4eBF44oeGBCJBAAiVBF0hgCAA4vlGI4tnADg=")) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js new file mode 100644 index 000000000..28f3eb9d5 --- /dev/null +++ b/apps/ohmcalc/app.js @@ -0,0 +1,300 @@ +let Layout = require("Layout"); + +// Definitions for units and formulas for electrical measurements. +const UNITS = { + "Voltage (V)": "Volts", + "Current (I)": "Amps", + "Resistance (R)": "Ohms", + "Power (P)": "Watts", +}; +const FORMULAS = { + 'Voltage (V)': { + 'Current (I), Resistance (R)': "{0} * {1}", + 'Power (P), Current (I)': "{0} / {1}", + 'Power (P), Resistance (R)': "Math.sqrt({0} * {1})" + }, + 'Current (I)': { + 'Voltage (V), Resistance (R)': "{0} / {1}", + 'Power (P), Voltage (V)': "{0} / {1}", + 'Power (P), Resistance (R)': "Math.sqrt({0} / {1})" + }, + 'Resistance (R)': { + 'Voltage (V), Current (I)': "{0} / {1}", + 'Power (P), Current (I)': "{0} / (Math.pow({1}, 2))", + 'Power (P), Voltage (V)': "(Math.pow({0}, 2)) / {1}" + }, + 'Power (P)': { + 'Voltage (V), Current (I)': "{0} * {1}", + 'Current (I), Resistance (R)': "(Math.pow({0}, 2)) * {1}", + 'Voltage (V), Resistance (R)': "(Math.pow({0}, 2)) / {1}" + }, +}; + +// Screen positioning settings +let btnSize = 23; +let xCenter = g.getWidth() / 2; +let yCenter = g.getHeight() / 2; + +// Variables to hold state +let lastStringWidth = 0; +let halfStringWidth = lastStringWidth / 2; +let calculatedVariable; +let selectedVariable; +let variableValues = {}; +let inputStr = ""; + +// Function references +let handleEnter; +let showVariableSelectionMenu; +let showInputMenu; +let resultsMenu; + +// Setup the layout for input buttons +let layout = new Layout({ + type: "v", + c: [ + { type: "txt", font: "6x8:3", label: "", id: "label" }, + { type: "h", c: "123".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: "456".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: "789".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: ".0C".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: [{ type: "btn", font: "6x8:2", label: "Enter", cb: () => { handleEnter(); }, fillx: 1, filly: 1 }] } + ] +}, { lazy: false }); + +// Clears area at the top of the screen where the text is displayed +function clearTextArea() { // Except Back Button + let x2 = g.getWidth(); + g.clearRect(0, 0, x2, btnSize); +} + +// Function to clear the entire screen, except for the Back button +function clearScreen() { + let x2 = g.getWidth(); + let y2 = g.getHeight(); + g.clearRect(btnSize, 0, x2, btnSize); + g.clearRect(0, btnSize, x2, y2); +} + +// Function to set up and display the calculator input screen +function showCalculatorInputScreen(variable) { + selectedVariable = variable; + layout.setUI(); + layout.render(); +} + +// Function to set and display the current value of the input +// Adjusts the font size to fit the screen width +function setValue(newStr) { + clearTextArea(); + inputStr = newStr; + + // Here we check the width of the string and adjust the font size + // Start with a standard font size and increase if the string is too wide + let fontSize = "6x8:3"; // start with a standard size + g.setFont(fontSize); + + // If the string is too long to fit on the screen, adjust the font size + while (g.stringWidth(inputStr) > g.getWidth() - 10 && fontSize !== "6x8:1") { + fontSize = "6x8:" + (Number(fontSize.split(":")[1]) - 1).toString(); + g.setFont(fontSize); + } + + layout.label.label = inputStr; + g.drawString(inputStr, layout.label.x, layout.label.y + 11); + lastStringWidth = g.stringWidth(inputStr); +} + +// Function to handle the press of a button and append its value to the current input +function handleButtonPress(value) { + inputStr = value === 'C' ? '' : inputStr + value; + setValue(inputStr); +} + +// Function to format the unit of measurement +function formatUnit(unit, value) { + return parseFloat(value) === 1 ? unit.slice(0, -1) : unit; +} + +// Calculates the value of the selected variable based on the entered values +// Also handles rounding and trimming of long decimal numbers +function calculateValue(calculatedVariable, variableValues) { + let formulas = FORMULAS[calculatedVariable]; + let formulaKeys = Object.keys(formulas); + for (let i = 0; i < formulaKeys.length; i++) { + let formulaKey = formulaKeys[i]; + let variables = formulaKey.split(', '); + if (variables.every(variable => variableValues.hasOwnProperty(variable))) { + let formula = formulas[formulaKey]; + let formulaValues = variables.map(variable => variableValues[variable]); + let calculatedValue = eval(formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index])); + + // Check if the number is an integer + let isInteger = Math.floor(calculatedValue) === calculatedValue; + + // Round and trim long decimal numbers + if (!isInteger) { + calculatedValue = Math.round(calculatedValue * 1000) / 1000; + calculatedValue = calculatedValue.toString().replace(/(\.\d*?)0+$/, '$1'); + } else { + calculatedValue = calculatedValue.toFixed(0); + } + + let result = Object.entries(variableValues).map(function (entry) { + let variable = entry[0]; + let value = entry[1]; + return [variable, `${value} ${formatUnit(UNITS[variable], value.toString())}`]; + }); + result.push([calculatedVariable, `${calculatedValue} ${formatUnit(UNITS[calculatedVariable], calculatedValue.toString())}`]); + return { + formula: formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index]), + value: calculatedValue, + unit: formatUnit(UNITS[calculatedVariable], calculatedValue), + result: result, + }; + } + } +} + +// Main function to initialize the application and setup the main menu +(function () { + let mainMenu = { + '': { 'title': 'Ohm\'s Law Calc' }, + '< Back': () => Bangle.showClock() + }; + + Object.keys(UNITS).forEach(unit => { + mainMenu[unit] = () => handleUnitSelection(unit); + }); + + // Function to present the menu for selecting the variable + // Filters out the calculated variable and already set variables from the menu + showVariableSelectionMenu = function () { + clearScreen(); + let variableSelectionMenu = { + '': { 'title': 'Select Variable' }, + '< Back': () => E.showMenu(mainMenu) + }; + let variables = Object.keys(UNITS); + let remainingVariables = variables.filter(v => v !== calculatedVariable && !variableValues[v]); + remainingVariables.forEach(variable => { + variableSelectionMenu[variable] = function () { + showInputMenu(variable); + }; + }); + E.showMenu(variableSelectionMenu); + }; + + // Function to handle the input of variable values + // It sets the current selected variable and displays the calculator input screen + showInputMenu = function (variable) { + setValue(""); + selectedVariable = variable; + let inputMenu = { + '': { 'title': variable }, + }; + E.showMenu(inputMenu); + showCalculatorInputScreen(variable); + }; + + // Function to handle the event of pressing 'Enter' + // It checks if the input is valid, if so, it saves the value and + // either calculates the result (if enough variables are present) or opens variable selection menu + handleEnter = function () { + // Check if the input is valid + if (inputStr === "" || isNaN(inputStr) || (inputStr.match(/\./g) || []).length > 1) { + // Show error message + setValue("Invalid Input"); + // Clear error message after 2 seconds + setTimeout(() => setValue(''), 2000); + return; + } + + if (calculatedVariable === null) { + return; + } + variableValues[selectedVariable] = parseFloat(inputStr); + if (Object.keys(variableValues).length === 2) { + let result = calculateValue(calculatedVariable, variableValues); + showResultsScreen(result); + calculatedVariable = null; + variableValues = {}; + } else { + setValue(""); + showVariableSelectionMenu(); + return; + } + return; + }; + + // Function to handle the selection of a unit of electical measurement + function handleUnitSelection(unit) { + calculatedVariable = unit; + showVariableSelectionMenu(); + } + + // Function to display the results screen with the calculated value + function drawValueScreen + (result) { + let drawPage = function () { + clearScreen(); + let fontSize = g.getHeight() / 3; + g.setFontVector(fontSize); + + // Reduce the font size until both the value and unit fit on the screen + while (g.stringWidth(result.value) > g.getWidth() - 20 || g.getFontHeight() > g.getHeight() / 2) { + fontSize--; + g.setFontVector(fontSize); + } + + let valueY = yCenter - g.getFontHeight() / 2; + let unitY = yCenter + g.getFontHeight() / 2; + let valueWidth = g.stringWidth(result.value); + let unitWidth = g.stringWidth(result.unit); + let valueX = (g.getWidth() - valueWidth) / 2; + let unitX = (g.getWidth() - unitWidth) / 2; + + g.drawString(result.value, valueX, valueY); + g.drawString(result.unit, unitX, unitY); + g.flip(); + }; + + // Shows the back button on the value screen + return function () { + clearScreen(); + let valueMenu = { + '': { 'title': 'Results' }, + '< Back': function () { + E.showMenu(resultsMenu); + } + }; + E.showMenu(valueMenu); + drawPage(); + }; + } + + // Shows the results menu with the calculated results and options + function showResultsScreen(result) { + let backButton = function () { + clearScreen(); + E.showMenu(resultsMenu); + }; + g.clear(); + resultsMenu = { + '': { 'title': 'Results' }, + 'Main Menu': function () { + E.showMenu(mainMenu); + }, + }; + resultsMenu[result.value + ' ' + result.unit] = drawValueScreen(result); + resultsMenu[result.formula + " = " + calculatedVariable] = {}; + resultsMenu['Exit'] = function () { + Bangle.showClock(); + }; + E.showMenu(resultsMenu); + } + + clearTextArea(); + E.showMenu(mainMenu); + +})(); diff --git a/apps/ohmcalc/app.png b/apps/ohmcalc/app.png new file mode 100644 index 0000000000000000000000000000000000000000..65d6c2bf2076b7e692e26259f617c01b76fad0a7 GIT binary patch literal 2098 zcmV-22+jA2P)ATK5s4{Aq^rCB(0@ja{@PX1KkkoJ zndvdlx%W=*y>n+erA6!RzR5|Z@5gzcbMAT1d(L@F^#A5)bBL&eh#pBn9p;>I*`lPh zABl+5k};=(#$kH^083waoTq)T&yz4_3WB-guI*LXfmkZ=7O5OPMnrpl3xGG0Pklj3 z{ey_=J;vYiSCz$w6Tm++631MR_4UHrr#_dx#<(?+rp5e~W$~=3!5DrMQ6~|p#0;PM zDyOl|FqM}dWnZ?QuzUPQAI2~Tb8Wm85p4!f@b*Vb<0k*6u>yY!7zXA{k4O4oBAP-(6ELTs^?Ct0W#2Mkgzy`E7%OPKj!7eY1wRo5Nz;yFJiB4; zVC0>rx%}N{Sg(yj2+V026V2;274h;@yB(%b)Y-z?gseCz^)Dhi01%<4?sW*z|JRK| zb^(IjYppM{4r{>!yzIQ8q(IG#QGbb);4KRGZDD_Pc}&al4d$-6d~n)8k0HQ81GJsc zhq039HYNJ9N{eIQ;l+EjLfmM8pWy&s_Qnp@qf(!a}?f2N0XS6t3|v{d$R2{C92F@l5bQ=j9&y1@s^Lagf(M0+{c z)TR|8Qz}2xBfP*9y!A*ZNeDUEKJ{7V0x0znJc6Ym5Hzn}v#~Z>xx5A* zNKC;FX7aB4IA{3+_T}XyodNWL$K>7Zjy1upcL*==v=oDR*0^{)!-TxK zKo92*xlbn%xQz2u4{18uXSjTm(2f2RZYeMtyI>6H#S~-wkY?#WtD2I=A zIX#-v4AZ>2mdoGYo9H(cu$j)IDblnPaC6}ox)SyZv$1Qw__0=20Y6_<80NZlhq&oz zjGOyd=%(IpdDZ^!5`Q-xIVXbYlS)|WIu~zmb9@Q#w$2p<)cjfSLMI#-<_KFcDfPYG zlOv4@XtzZ~M%Ojuq zXU@`O0bn-JJixCx{8JLhh3zlO=MPmkB#nWvCRnvdzJ&FK?S!*LO8uLNCT0pYYKFOc zCD=l|g2SWKV9CLbqzz#6FdMh#?b=T#fMsHO!4%eOtCIMa9D3L~Z!t4=ddtN2fLA^- z$4W+2wjT3WMdAp$5Zi>GPrfPPZ7>%$?*+`#e!!oVR)FR{mIIEtlb^XiX`j%}dn9&( z1t8KzxNb{|F0~8LIb1U0{=D!u_5;iSFJ2fOR)D4>=fpeZUUx-Cd)tyCU{17d zblN9Qefd$Iyl1@-0try9biiT)gBi z_T}gCw1Hm3+r76(_O$i~7CSamfbO&c*yDLn;ri8ykV4F-^`N)*OJ{L))l`muv!7Qk zo8y@mFW>%&%Z&C*f7$UTWDo4+xCFEHkqdyoZ)>`OPyXdW`CYKii1kYj8_{Ha$LlGz z|KhW4%x4eqcb`5j>p>^}yq=2+b45T1YF}5s;Z<1|KH4dJU@!cQb->_-?Ysp*Y$F0} zd@V`)1*NmNs4!2!TEHO$gn>|42O-^pFWT^mDZu8j6JR(Kg7>h4ShnqBPji78!JJ@L zUfOSl3EgRcDtmJbO(^LQpLQSO)+8V}p!NeR2l&cLInb%J-;6`Nc&+mgpC0)k9+4YG zvUQ%7k1*O%V+Fx_zXZGh6NkTIkH#&RhA`2K7+0yyOMP}Ni^CD|c z>51F|;4(|6{ma(&roPCYG%vEoIw{BCk~J@~|F|zQGOuUPP{PV*9ihbBs6>?r%+d)U z4j_g0|Jd`jT!@~b#Iuf2;!7erZWhGuC!*;z>R_&K3oAV!QKeTfXBWUr`)i*2k&7x9 z3Xe3w+z*0b6fJ!?y{*m2%&HbiVCDviP#R%{Tl>&_u<{SG)}9l1gwtn0gN;blZekdN&BJWu>upby(DZFF>Q0*W_W33>nWvP;x>%oAX+)qC zgA&^yJirYL0%*8OcpFJ6YpWb)Q-st}GgHc|<_)Ekn{Kr{4zSRLPwIEKi#zm9%uP<} z^YdiP?QMd?CH0N-t)zbOuUJ;;HOeYSJB5t0ig8J~DZgdu1t_cd)MuWBtw@Li7r3w$ zD03FJfB{&@r#`)4rA4-rdgw?A;4 z3(U4Za4g8^f#dGm>xJwBV2Kj{pDw07*qoM6N<$f@2pO;Q#;t literal 0 HcmV?d00001 diff --git a/apps/ohmcalc/metadata.json b/apps/ohmcalc/metadata.json new file mode 100644 index 000000000..1e1985a8b --- /dev/null +++ b/apps/ohmcalc/metadata.json @@ -0,0 +1,22 @@ +{ + "id": "ohmcalc", + "name": "Ohm's Law Calculator", + "version": "0.01", + "description": "A smart and simple calculator for Ohm's Law calculations, designed specifically for Bangle.js 2 smartwatches. Handles voltage, current, resistance, and power calculations with smart logic to prevent invalid inputs.", + "icon": "app.png", + "type": "app", + "tags": "calculator, utilities, electric", + "supports": ["BANGLEJS2"], + "readme": "README.md", + "allow_emulator": true, + "storage": [{ + "name": "ohmcalc.app.js", + "url": "app.js" + }, + { + "name": "ohmcalc.img", + "url": "app-icon.js", + "evaluate": true + } + ] +} diff --git a/apps/ohmcalc/ohmcalc.js b/apps/ohmcalc/ohmcalc.js new file mode 100644 index 000000000..d3dc25cfd --- /dev/null +++ b/apps/ohmcalc/ohmcalc.js @@ -0,0 +1,300 @@ +let Layout = require("Layout"); + +// Definitions for units and formulas for electrical measurements. +const UNITS = { + "Voltage (V)": "Volts", + "Current (I)": "Amps", + "Resistance (R)": "Ohms", + "Power (P)": "Watts", +}; +const FORMULAS = { + 'Voltage (V)': { + 'Current (I), Resistance (R)': "{0} * {1}", + 'Power (P), Current (I)': "{0} / {1}", + 'Power (P), Resistance (R)': "Math.sqrt({0} * {1})" + }, + 'Current (I)': { + 'Voltage (V), Resistance (R)': "{0} / {1}", + 'Power (P), Voltage (V)': "{0} / {1}", + 'Power (P), Resistance (R)': "Math.sqrt({0} / {1})" + }, + 'Resistance (R)': { + 'Voltage (V), Current (I)': "{0} / {1}", + 'Power (P), Current (I)': "{0} / (Math.pow({1}, 2))", + 'Power (P), Voltage (V)': "(Math.pow({0}, 2)) / {1}" + }, + 'Power (P)': { + 'Voltage (V), Current (I)': "{0} * {1}", + 'Current (I), Resistance (R)': "(Math.pow({0}, 2)) * {1}", + 'Voltage (V), Resistance (R)': "(Math.pow({0}, 2)) / {1}" + }, +}; + +// Screen positioning settings +let btnSize = 23; +let xCenter = g.getWidth() / 2; +let yCenter = g.getHeight() / 2; + +// Variables to hold state +let lastStringWidth = 0; +let halfStringWidth = lastStringWidth / 2; +let calculatedVariable; +let selectedVariable; +let variableValues = {}; +let inputStr = ""; + +// Function references +let handleEnter; +let showVariableSelectionMenu; +let showInputMenu; +let resultsMenu; + +// Setup the layout for input buttons +let layout = new Layout({ + type: "v", + c: [ + { type: "txt", font: "6x8:3", label: "", id: "label" }, + { type: "h", c: "123".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: "456".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: "789".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: ".0C".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: [{ type: "btn", font: "6x8:2", label: "Enter", cb: () => { handleEnter(); }, fillx: 1, filly: 1 }] } + ] +}, { lazy: false }); + +// Clears area at the top of the screen where the text is displayed +function clearTextArea() { // Except Back Button + let x2 = g.getWidth(); + g.clearRect(0, 0, x2, btnSize); +} + +// Function to clear the entire screen, except for the Back button +function clearScreen() { + let x2 = g.getWidth(); + let y2 = g.getHeight(); + g.clearRect(btnSize, 0, x2, btnSize); + g.clearRect(0, btnSize, x2, y2); +} + +// Function to set up and display the calculator input screen +function showCalculatorInputScreen(variable) { + selectedVariable = variable; + layout.setUI(); + layout.render(); +} + +// Function to set and display the current value of the input +// Adjusts the font size to fit the screen width +function setValue(newStr) { + clearTextArea(); + inputStr = newStr; + + // Here we check the width of the string and adjust the font size + // Start with a standard font size and increase if the string is too wide + let fontSize = "6x8:3"; // start with a standard size + g.setFont(fontSize); + + // If the string is too long to fit on the screen, adjust the font size + while (g.stringWidth(inputStr) > g.getWidth() - 10 && fontSize !== "6x8:1") { + fontSize = "6x8:" + (Number(fontSize.split(":")[1]) - 1).toString(); + g.setFont(fontSize); + } + + layout.label.label = inputStr; + g.drawString(inputStr, layout.label.x, layout.label.y + 11); + lastStringWidth = g.stringWidth(inputStr); +} + +// Function to handle the press of a button and append its value to the current input +function handleButtonPress(value) { + inputStr = value === 'C' ? '' : inputStr + value; + setValue(inputStr); +} + +// Function to format the unit of measurement +function formatUnit(unit, value) { + return parseFloat(value) === 1 ? unit.slice(0, -1) : unit; +} + +// Calculates the value of the selected variable based on the entered values +// Also handles rounding and trimming of long decimal numbers +function calculateValue(calculatedVariable, variableValues) { + let formulas = FORMULAS[calculatedVariable]; + let formulaKeys = Object.keys(formulas); + for (let i = 0; i < formulaKeys.length; i++) { + let formulaKey = formulaKeys[i]; + let variables = formulaKey.split(', '); + if (variables.every(variable => variableValues.hasOwnProperty(variable))) { + let formula = formulas[formulaKey]; + let formulaValues = variables.map(variable => variableValues[variable]); + let calculatedValue = eval(formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index])); + + // Check if the number is an integer + let isInteger = Math.floor(calculatedValue) === calculatedValue; + + // Round and trim long decimal numbers + if (!isInteger) { + calculatedValue = Math.round(calculatedValue * 1000) / 1000; + calculatedValue = calculatedValue.toString().replace(/(\.\d*?)0+$/, '$1'); + } else { + calculatedValue = calculatedValue.toFixed(0); + } + + let result = Object.entries(variableValues).map(function (entry) { + let variable = entry[0]; + let value = entry[1]; + return [variable, `${value} ${formatUnit(UNITS[variable], value.toString())}`]; + }); + result.push([calculatedVariable, `${calculatedValue} ${formatUnit(UNITS[calculatedVariable], calculatedValue.toString())}`]); + return { + formula: formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index]), + value: calculatedValue, + unit: formatUnit(UNITS[calculatedVariable], calculatedValue), + result: result, + }; + } + } +} + +// Main function to initialize the application and setup the main menu +(function () { + let mainMenu = { + '': { 'title': 'Ohm\'s Law Calc' }, + '< Back': () => Bangle.showClock() + }; + + Object.keys(UNITS).forEach(unit => { + mainMenu[unit] = () => handleUnitSelection(unit); + }); + + // Function to present the menu for selecting the variable + // Filters out the calculated variable and already set variables from the menu + showVariableSelectionMenu = function () { + clearScreen(); + let variableSelectionMenu = { + '': { 'title': 'Select Variable' }, + '< Back': () => E.showMenu(mainMenu) + }; + let variables = Object.keys(UNITS); + let remainingVariables = variables.filter(v => v !== calculatedVariable && !variableValues[v]); + remainingVariables.forEach(variable => { + variableSelectionMenu[variable] = function () { + showInputMenu(variable); + }; + }); + E.showMenu(variableSelectionMenu); + }; + + // Function to handle the input of variable values + // It sets the current selected variable and displays the calculator input screen + showInputMenu = function (variable) { + setValue(""); + selectedVariable = variable; + let inputMenu = { + '': { 'title': variable }, + }; + E.showMenu(inputMenu); + showCalculatorInputScreen(variable); + }; + + // Function to handle the event of pressing 'Enter' + // It checks if the input is valid, if so, it saves the value and + // either calculates the result (if enough variables are present) or opens variable selection menu + handleEnter = function () { + // Check if the input is valid + if (inputStr === "" || isNaN(inputStr) || (inputStr.match(/\./g) || []).length > 1) { + // Show error message + setValue("Invalid Input"); + // Clear error message after 2 seconds + setTimeout(() => setValue(''), 2000); + return; + } + + if (calculatedVariable === null) { + return; + } + variableValues[selectedVariable] = parseFloat(inputStr); + if (Object.keys(variableValues).length === 2) { + let result = calculateValue(calculatedVariable, variableValues); + showResultsScreen(result); + calculatedVariable = null; + variableValues = {}; + } else { + setValue(""); + showVariableSelectionMenu(); + return; + } + return; + }; + + // Function to handle the selection of a unit of electical measurement + function handleUnitSelection(unit) { + calculatedVariable = unit; + showVariableSelectionMenu(); + } + + // Function to display the results screen with the calculated value + function drawValueScreenpage + (result) { + let drawPage = function () { + clearScreen(); + let fontSize = g.getHeight() / 3; + g.setFontVector(fontSize); + + // Reduce the font size until both the value and unit fit on the screen + while (g.stringWidth(result.value) > g.getWidth() - 20 || g.getFontHeight() > g.getHeight() / 2) { + fontSize--; + g.setFontVector(fontSize); + } + + let valueY = yCenter - g.getFontHeight() / 2; + let unitY = yCenter + g.getFontHeight() / 2; + let valueWidth = g.stringWidth(result.value); + let unitWidth = g.stringWidth(result.unit); + let valueX = (g.getWidth() - valueWidth) / 2; + let unitX = (g.getWidth() - unitWidth) / 2; + + g.drawString(result.value, valueX, valueY); + g.drawString(result.unit, unitX, unitY); + g.flip(); + }; + + // Shows the back button on the value screen + return function () { + clearScreen(); + let valueMenu = { + '': { 'title': 'Results' }, + '< Back': function () { + E.showMenu(resultsMenu); + } + }; + E.showMenu(valueMenu); + drawPage(); + }; + } + + // Shows the results menu with the calculated results and options + function showResultsScreen(result) { + let backButton = function () { + clearScreen(); + E.showMenu(resultsMenu); + }; + g.clear(); + resultsMenu = { + '': { 'title': 'Results' }, + 'Main Menu': function () { + E.showMenu(mainMenu); + }, + }; + resultsMenu[result.value + ' ' + result.unit] = drawValueScreen(result); + resultsMenu[result.formula + " = " + calculatedVariable] = {}; + resultsMenu['Exit'] = function () { + Bangle.showClock(); + }; + E.showMenu(resultsMenu); + } + + clearTextArea(); + E.showMenu(mainMenu); + +})(); \ No newline at end of file From c4fd9ca09612c2a0d73949cb8a5156c444ebf1db Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Fri, 2 Jun 2023 18:15:55 -0500 Subject: [PATCH 02/24] Delete ohmcalc.js --- apps/ohmcalc/ohmcalc.js | 300 ---------------------------------------- 1 file changed, 300 deletions(-) delete mode 100644 apps/ohmcalc/ohmcalc.js diff --git a/apps/ohmcalc/ohmcalc.js b/apps/ohmcalc/ohmcalc.js deleted file mode 100644 index d3dc25cfd..000000000 --- a/apps/ohmcalc/ohmcalc.js +++ /dev/null @@ -1,300 +0,0 @@ -let Layout = require("Layout"); - -// Definitions for units and formulas for electrical measurements. -const UNITS = { - "Voltage (V)": "Volts", - "Current (I)": "Amps", - "Resistance (R)": "Ohms", - "Power (P)": "Watts", -}; -const FORMULAS = { - 'Voltage (V)': { - 'Current (I), Resistance (R)': "{0} * {1}", - 'Power (P), Current (I)': "{0} / {1}", - 'Power (P), Resistance (R)': "Math.sqrt({0} * {1})" - }, - 'Current (I)': { - 'Voltage (V), Resistance (R)': "{0} / {1}", - 'Power (P), Voltage (V)': "{0} / {1}", - 'Power (P), Resistance (R)': "Math.sqrt({0} / {1})" - }, - 'Resistance (R)': { - 'Voltage (V), Current (I)': "{0} / {1}", - 'Power (P), Current (I)': "{0} / (Math.pow({1}, 2))", - 'Power (P), Voltage (V)': "(Math.pow({0}, 2)) / {1}" - }, - 'Power (P)': { - 'Voltage (V), Current (I)': "{0} * {1}", - 'Current (I), Resistance (R)': "(Math.pow({0}, 2)) * {1}", - 'Voltage (V), Resistance (R)': "(Math.pow({0}, 2)) / {1}" - }, -}; - -// Screen positioning settings -let btnSize = 23; -let xCenter = g.getWidth() / 2; -let yCenter = g.getHeight() / 2; - -// Variables to hold state -let lastStringWidth = 0; -let halfStringWidth = lastStringWidth / 2; -let calculatedVariable; -let selectedVariable; -let variableValues = {}; -let inputStr = ""; - -// Function references -let handleEnter; -let showVariableSelectionMenu; -let showInputMenu; -let resultsMenu; - -// Setup the layout for input buttons -let layout = new Layout({ - type: "v", - c: [ - { type: "txt", font: "6x8:3", label: "", id: "label" }, - { type: "h", c: "123".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, - { type: "h", c: "456".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, - { type: "h", c: "789".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, - { type: "h", c: ".0C".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, - { type: "h", c: [{ type: "btn", font: "6x8:2", label: "Enter", cb: () => { handleEnter(); }, fillx: 1, filly: 1 }] } - ] -}, { lazy: false }); - -// Clears area at the top of the screen where the text is displayed -function clearTextArea() { // Except Back Button - let x2 = g.getWidth(); - g.clearRect(0, 0, x2, btnSize); -} - -// Function to clear the entire screen, except for the Back button -function clearScreen() { - let x2 = g.getWidth(); - let y2 = g.getHeight(); - g.clearRect(btnSize, 0, x2, btnSize); - g.clearRect(0, btnSize, x2, y2); -} - -// Function to set up and display the calculator input screen -function showCalculatorInputScreen(variable) { - selectedVariable = variable; - layout.setUI(); - layout.render(); -} - -// Function to set and display the current value of the input -// Adjusts the font size to fit the screen width -function setValue(newStr) { - clearTextArea(); - inputStr = newStr; - - // Here we check the width of the string and adjust the font size - // Start with a standard font size and increase if the string is too wide - let fontSize = "6x8:3"; // start with a standard size - g.setFont(fontSize); - - // If the string is too long to fit on the screen, adjust the font size - while (g.stringWidth(inputStr) > g.getWidth() - 10 && fontSize !== "6x8:1") { - fontSize = "6x8:" + (Number(fontSize.split(":")[1]) - 1).toString(); - g.setFont(fontSize); - } - - layout.label.label = inputStr; - g.drawString(inputStr, layout.label.x, layout.label.y + 11); - lastStringWidth = g.stringWidth(inputStr); -} - -// Function to handle the press of a button and append its value to the current input -function handleButtonPress(value) { - inputStr = value === 'C' ? '' : inputStr + value; - setValue(inputStr); -} - -// Function to format the unit of measurement -function formatUnit(unit, value) { - return parseFloat(value) === 1 ? unit.slice(0, -1) : unit; -} - -// Calculates the value of the selected variable based on the entered values -// Also handles rounding and trimming of long decimal numbers -function calculateValue(calculatedVariable, variableValues) { - let formulas = FORMULAS[calculatedVariable]; - let formulaKeys = Object.keys(formulas); - for (let i = 0; i < formulaKeys.length; i++) { - let formulaKey = formulaKeys[i]; - let variables = formulaKey.split(', '); - if (variables.every(variable => variableValues.hasOwnProperty(variable))) { - let formula = formulas[formulaKey]; - let formulaValues = variables.map(variable => variableValues[variable]); - let calculatedValue = eval(formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index])); - - // Check if the number is an integer - let isInteger = Math.floor(calculatedValue) === calculatedValue; - - // Round and trim long decimal numbers - if (!isInteger) { - calculatedValue = Math.round(calculatedValue * 1000) / 1000; - calculatedValue = calculatedValue.toString().replace(/(\.\d*?)0+$/, '$1'); - } else { - calculatedValue = calculatedValue.toFixed(0); - } - - let result = Object.entries(variableValues).map(function (entry) { - let variable = entry[0]; - let value = entry[1]; - return [variable, `${value} ${formatUnit(UNITS[variable], value.toString())}`]; - }); - result.push([calculatedVariable, `${calculatedValue} ${formatUnit(UNITS[calculatedVariable], calculatedValue.toString())}`]); - return { - formula: formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index]), - value: calculatedValue, - unit: formatUnit(UNITS[calculatedVariable], calculatedValue), - result: result, - }; - } - } -} - -// Main function to initialize the application and setup the main menu -(function () { - let mainMenu = { - '': { 'title': 'Ohm\'s Law Calc' }, - '< Back': () => Bangle.showClock() - }; - - Object.keys(UNITS).forEach(unit => { - mainMenu[unit] = () => handleUnitSelection(unit); - }); - - // Function to present the menu for selecting the variable - // Filters out the calculated variable and already set variables from the menu - showVariableSelectionMenu = function () { - clearScreen(); - let variableSelectionMenu = { - '': { 'title': 'Select Variable' }, - '< Back': () => E.showMenu(mainMenu) - }; - let variables = Object.keys(UNITS); - let remainingVariables = variables.filter(v => v !== calculatedVariable && !variableValues[v]); - remainingVariables.forEach(variable => { - variableSelectionMenu[variable] = function () { - showInputMenu(variable); - }; - }); - E.showMenu(variableSelectionMenu); - }; - - // Function to handle the input of variable values - // It sets the current selected variable and displays the calculator input screen - showInputMenu = function (variable) { - setValue(""); - selectedVariable = variable; - let inputMenu = { - '': { 'title': variable }, - }; - E.showMenu(inputMenu); - showCalculatorInputScreen(variable); - }; - - // Function to handle the event of pressing 'Enter' - // It checks if the input is valid, if so, it saves the value and - // either calculates the result (if enough variables are present) or opens variable selection menu - handleEnter = function () { - // Check if the input is valid - if (inputStr === "" || isNaN(inputStr) || (inputStr.match(/\./g) || []).length > 1) { - // Show error message - setValue("Invalid Input"); - // Clear error message after 2 seconds - setTimeout(() => setValue(''), 2000); - return; - } - - if (calculatedVariable === null) { - return; - } - variableValues[selectedVariable] = parseFloat(inputStr); - if (Object.keys(variableValues).length === 2) { - let result = calculateValue(calculatedVariable, variableValues); - showResultsScreen(result); - calculatedVariable = null; - variableValues = {}; - } else { - setValue(""); - showVariableSelectionMenu(); - return; - } - return; - }; - - // Function to handle the selection of a unit of electical measurement - function handleUnitSelection(unit) { - calculatedVariable = unit; - showVariableSelectionMenu(); - } - - // Function to display the results screen with the calculated value - function drawValueScreenpage - (result) { - let drawPage = function () { - clearScreen(); - let fontSize = g.getHeight() / 3; - g.setFontVector(fontSize); - - // Reduce the font size until both the value and unit fit on the screen - while (g.stringWidth(result.value) > g.getWidth() - 20 || g.getFontHeight() > g.getHeight() / 2) { - fontSize--; - g.setFontVector(fontSize); - } - - let valueY = yCenter - g.getFontHeight() / 2; - let unitY = yCenter + g.getFontHeight() / 2; - let valueWidth = g.stringWidth(result.value); - let unitWidth = g.stringWidth(result.unit); - let valueX = (g.getWidth() - valueWidth) / 2; - let unitX = (g.getWidth() - unitWidth) / 2; - - g.drawString(result.value, valueX, valueY); - g.drawString(result.unit, unitX, unitY); - g.flip(); - }; - - // Shows the back button on the value screen - return function () { - clearScreen(); - let valueMenu = { - '': { 'title': 'Results' }, - '< Back': function () { - E.showMenu(resultsMenu); - } - }; - E.showMenu(valueMenu); - drawPage(); - }; - } - - // Shows the results menu with the calculated results and options - function showResultsScreen(result) { - let backButton = function () { - clearScreen(); - E.showMenu(resultsMenu); - }; - g.clear(); - resultsMenu = { - '': { 'title': 'Results' }, - 'Main Menu': function () { - E.showMenu(mainMenu); - }, - }; - resultsMenu[result.value + ' ' + result.unit] = drawValueScreen(result); - resultsMenu[result.formula + " = " + calculatedVariable] = {}; - resultsMenu['Exit'] = function () { - Bangle.showClock(); - }; - E.showMenu(resultsMenu); - } - - clearTextArea(); - E.showMenu(mainMenu); - -})(); \ No newline at end of file From 1ff9ae7271a21cb1086d1fd0cb29504c78a4a653 Mon Sep 17 00:00:00 2001 From: stweedo Date: Fri, 2 Jun 2023 20:55:37 -0500 Subject: [PATCH 03/24] Fixed incorrect formula --- apps/ohmcalc/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 28f3eb9d5..1fb157b5c 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -21,7 +21,7 @@ const FORMULAS = { 'Resistance (R)': { 'Voltage (V), Current (I)': "{0} / {1}", 'Power (P), Current (I)': "{0} / (Math.pow({1}, 2))", - 'Power (P), Voltage (V)': "(Math.pow({0}, 2)) / {1}" + 'Power (P), Voltage (V)': "(Math.pow({1}, 2)) / {0}" }, 'Power (P)': { 'Voltage (V), Current (I)': "{0} * {1}", From c562097aba5e334d47d3866132bc20664243a489 Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:21:56 -0500 Subject: [PATCH 04/24] Update app.js - New results menu item --- apps/ohmcalc/app.js | 98 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 15 deletions(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 1fb157b5c..9cc7b86c8 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -7,26 +7,27 @@ const UNITS = { "Resistance (R)": "Ohms", "Power (P)": "Watts", }; + const FORMULAS = { 'Voltage (V)': { - 'Current (I), Resistance (R)': "{0} * {1}", - 'Power (P), Current (I)': "{0} / {1}", - 'Power (P), Resistance (R)': "Math.sqrt({0} * {1})" + 'Current (I), Resistance (R)': { equation: "{0} * {1}", display: "V = I * R" }, + 'Power (P), Current (I)': { equation: "{0} / {1}", display: "V = P / I" }, + 'Power (P), Resistance (R)': { equation: "Math.sqrt({0} * {1})", display: "V = sqrt(P * R)" } }, 'Current (I)': { - 'Voltage (V), Resistance (R)': "{0} / {1}", - 'Power (P), Voltage (V)': "{0} / {1}", - 'Power (P), Resistance (R)': "Math.sqrt({0} / {1})" + 'Voltage (V), Resistance (R)': { equation: "{0} / {1}", display: "I = V / R" }, + 'Power (P), Voltage (V)': { equation: "{0} / {1}", display: "I = P / V" }, + 'Power (P), Resistance (R)': { equation: "Math.sqrt({0} / {1})", display: "I = sqrt(P / R)" } }, 'Resistance (R)': { - 'Voltage (V), Current (I)': "{0} / {1}", - 'Power (P), Current (I)': "{0} / (Math.pow({1}, 2))", - 'Power (P), Voltage (V)': "(Math.pow({1}, 2)) / {0}" + 'Voltage (V), Current (I)': { equation: "{0} / {1}", display: "R = V / I" }, + 'Power (P), Current (I)': { equation: "{0} / (Math.pow({1}, 2))", display: "R = P / I^2" }, + 'Power (P), Voltage (V)': { equation: "(Math.pow({1}, 2)) / {0}", display: "R = V^2 / P" } }, 'Power (P)': { - 'Voltage (V), Current (I)': "{0} * {1}", - 'Current (I), Resistance (R)': "(Math.pow({0}, 2)) * {1}", - 'Voltage (V), Resistance (R)': "(Math.pow({0}, 2)) / {1}" + 'Voltage (V), Current (I)': { equation: "{0} * {1}", display: "P = V * I" }, + 'Current (I), Resistance (R)': { equation: "(Math.pow({0}, 2)) * {1}", display: "P = I^2 * R" }, + 'Voltage (V), Resistance (R)': { equation: "(Math.pow({0}, 2)) / {1}", display: "P = V^2 / R" } }, }; @@ -125,7 +126,9 @@ function calculateValue(calculatedVariable, variableValues) { let formulaKey = formulaKeys[i]; let variables = formulaKey.split(', '); if (variables.every(variable => variableValues.hasOwnProperty(variable))) { - let formula = formulas[formulaKey]; + let formulaData = formulas[formulaKey]; + let formula = formulaData.equation; + let formulaDisplay = formulaData.display; let formulaValues = variables.map(variable => variableValues[variable]); let calculatedValue = eval(formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index])); @@ -147,7 +150,7 @@ function calculateValue(calculatedVariable, variableValues) { }); result.push([calculatedVariable, `${calculatedValue} ${formatUnit(UNITS[calculatedVariable], calculatedValue.toString())}`]); return { - formula: formula.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index]), + formula: formulaDisplay.replace(/\{(\d+)\}/g, (_, index) => formulaValues[index]), value: calculatedValue, unit: formatUnit(UNITS[calculatedVariable], calculatedValue), result: result, @@ -273,6 +276,70 @@ function calculateValue(calculatedVariable, variableValues) { }; } + // Function to display the results screen with the values from the result.result array + function drawResultScreen(result) { + let drawPage = function() { + clearScreen(); + let fontSize = 30; // Adjust to fit the display + let lineSpacing = 15; // Space between lines + + // Define the vertical positions of the titles + let titlePositions = [10, 72, 132]; + + for (let i = 0; i < result.result.length; i++) { + let currentResult = result.result[i]; + let resultTitle = currentResult[0]; + let resultValue = currentResult[1]; + + // Draw title + g.setFontVector(fontSize / 2); // Small font for title + let titleX = (g.getWidth() - g.stringWidth(resultTitle)) / 2; + let titleY = titlePositions[i]; // Get the vertical position for the title + g.drawString(resultTitle, titleX, titleY); // Draw at the desired position + + let underlineYPosition = titleY + g.getFontHeight() - 3; + g.drawLine(titleX, underlineYPosition, titleX + g.stringWidth(resultTitle), underlineYPosition); // Draw underline + + let valueX; + let valueY = titleY + g.getFontHeight(); // Draw below the title + let valueFontSize = fontSize; + + // Draw value with smaller font size if necessary + g.setFontVector(valueFontSize); // Large font for value + if (g.stringWidth(resultValue) > g.getWidth()) { + valueFontSize /= 1.5; // Small font for value + g.setFontVector(valueFontSize); + if (g.stringWidth(resultValue) > g.getWidth()) { + valueFontSize /= 1.5; // Smallest font for value + g.setFontVector(valueFontSize); + } + } + + valueY += g.getFontHeight() / 2 + 2; + valueX = (g.getWidth() - g.stringWidth(resultValue)) / 2; + g.drawString(resultValue, valueX, valueY); // Draw at the desired position + + // Move down for the next entry + let nextTitleY = (i + 1 < titlePositions.length) ? titlePositions[i + 1] : titleY + 1.5 * fontSize + lineSpacing; + yPosition = nextTitleY; + } + g.flip(); + }; + + // Shows the back button on the result screen + return function() { + clearScreen(); + let resultMenu = { + '': { 'title': 'Results' }, + '< Back': function() { + E.showMenu(resultsMenu); + } + }; + E.showMenu(resultMenu); + drawPage(); + }; + } + // Shows the results menu with the calculated results and options function showResultsScreen(result) { let backButton = function () { @@ -286,8 +353,9 @@ function calculateValue(calculatedVariable, variableValues) { E.showMenu(mainMenu); }, }; + console.log(result); resultsMenu[result.value + ' ' + result.unit] = drawValueScreen(result); - resultsMenu[result.formula + " = " + calculatedVariable] = {}; + resultsMenu[result.formula] = drawResultScreen(result); resultsMenu['Exit'] = function () { Bangle.showClock(); }; From b0ad5522ce3830deb93f800f11db4ef8d0318c93 Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:22:55 -0500 Subject: [PATCH 05/24] Update ChangeLog --- apps/ohmcalc/ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/ohmcalc/ChangeLog b/apps/ohmcalc/ChangeLog index 5560f00bc..68790afee 100644 --- a/apps/ohmcalc/ChangeLog +++ b/apps/ohmcalc/ChangeLog @@ -1 +1,2 @@ 0.01: New App! +0.02: New Results menu item to show the formula and values used. From 8baafd0809590fa67f254d1bf656937e9fac7145 Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:23:09 -0500 Subject: [PATCH 06/24] Update metadata.json --- apps/ohmcalc/metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ohmcalc/metadata.json b/apps/ohmcalc/metadata.json index 1e1985a8b..822203024 100644 --- a/apps/ohmcalc/metadata.json +++ b/apps/ohmcalc/metadata.json @@ -1,7 +1,7 @@ { "id": "ohmcalc", "name": "Ohm's Law Calculator", - "version": "0.01", + "version": "0.02", "description": "A smart and simple calculator for Ohm's Law calculations, designed specifically for Bangle.js 2 smartwatches. Handles voltage, current, resistance, and power calculations with smart logic to prevent invalid inputs.", "icon": "app.png", "type": "app", From acd6c4aa39137e0fa6311a6f9a826b45cb753dd8 Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:30:49 -0500 Subject: [PATCH 07/24] Update metadata.json - shorten launcher name --- apps/ohmcalc/metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ohmcalc/metadata.json b/apps/ohmcalc/metadata.json index 822203024..51a21b9d5 100644 --- a/apps/ohmcalc/metadata.json +++ b/apps/ohmcalc/metadata.json @@ -1,6 +1,6 @@ { "id": "ohmcalc", - "name": "Ohm's Law Calculator", + "name": "Ohm's Law Calc", "version": "0.02", "description": "A smart and simple calculator for Ohm's Law calculations, designed specifically for Bangle.js 2 smartwatches. Handles voltage, current, resistance, and power calculations with smart logic to prevent invalid inputs.", "icon": "app.png", From feafb53854d736ff189d93c39dd1fd4bc0f6c1cc Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 11:34:40 -0500 Subject: [PATCH 08/24] Update metadata.json - Added shortened name --- apps/ohmcalc/metadata.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/ohmcalc/metadata.json b/apps/ohmcalc/metadata.json index 51a21b9d5..1272b9f91 100644 --- a/apps/ohmcalc/metadata.json +++ b/apps/ohmcalc/metadata.json @@ -1,6 +1,7 @@ { "id": "ohmcalc", - "name": "Ohm's Law Calc", + "name": "Ohm's Law Calculator", + "shortName": "Ohm's Law Calc", "version": "0.02", "description": "A smart and simple calculator for Ohm's Law calculations, designed specifically for Bangle.js 2 smartwatches. Handles voltage, current, resistance, and power calculations with smart logic to prevent invalid inputs.", "icon": "app.png", From 79c98c2eac081a64302fee7c5f00f812347b1fab Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 12:42:40 -0500 Subject: [PATCH 09/24] Update app.js - Fixed back button bug and disallow input during message --- apps/ohmcalc/app.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 9cc7b86c8..3bd225baf 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -43,6 +43,7 @@ let calculatedVariable; let selectedVariable; let variableValues = {}; let inputStr = ""; +let invalidInput = false; // Function references let handleEnter; @@ -108,6 +109,9 @@ function setValue(newStr) { // Function to handle the press of a button and append its value to the current input function handleButtonPress(value) { + if (invalidInput) { + return; // Don't allow input if an invalid input error message is displayed + } inputStr = value === 'C' ? '' : inputStr + value; setValue(inputStr); } @@ -176,7 +180,10 @@ function calculateValue(calculatedVariable, variableValues) { clearScreen(); let variableSelectionMenu = { '': { 'title': 'Select Variable' }, - '< Back': () => E.showMenu(mainMenu) + '< Back': () => { + E.showMenu(mainMenu); + variableValues = {}; + } }; let variables = Object.keys(UNITS); let remainingVariables = variables.filter(v => v !== calculatedVariable && !variableValues[v]); @@ -208,8 +215,12 @@ function calculateValue(calculatedVariable, variableValues) { if (inputStr === "" || isNaN(inputStr) || (inputStr.match(/\./g) || []).length > 1) { // Show error message setValue("Invalid Input"); + invalidInput = true; // Prevent further input // Clear error message after 2 seconds - setTimeout(() => setValue(''), 2000); + setTimeout(() => { + setValue(''); + invalidInput = false; // Allow input again + }, 2000); return; } From 0c5d339e26ed27f53253ca37c2dae7fd2405af9b Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 15:17:49 -0500 Subject: [PATCH 10/24] Update app.js- remove console log --- apps/ohmcalc/app.js | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 3bd225baf..4a3d97e8c 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -364,7 +364,6 @@ function calculateValue(calculatedVariable, variableValues) { E.showMenu(mainMenu); }, }; - console.log(result); resultsMenu[result.value + ' ' + result.unit] = drawValueScreen(result); resultsMenu[result.formula] = drawResultScreen(result); resultsMenu['Exit'] = function () { From 225b4be09de364f32ca79f9876adfc74b52af9fa Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 21:11:55 -0500 Subject: [PATCH 11/24] Update README.md --- apps/ohmcalc/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ohmcalc/README.md b/apps/ohmcalc/README.md index f45b8e977..a890e590a 100644 --- a/apps/ohmcalc/README.md +++ b/apps/ohmcalc/README.md @@ -7,7 +7,7 @@ This is a simple and intuitive Ohm's Law Calculator application designed for the * __Select the Electrical Measurement:__ On the main menu, select the electrical measurement that you wish to calculate (Voltage, Current, Resistance, or Power). * __Enter Known Values:__ You will then be prompted to enter the known values. The application will present a menu listing the remaining electrical measurements. Select one and you will be taken to a numeric input screen. Repeat this step for the second known value. * __View Results:__ Once the two known values have been entered, the application will automatically calculate and display the value of the selected electrical measurement. The Results menu will show the calculated value, the unit of measurement, and the formula used for calculation. -* __Navigation:__ Whether you're deep in calculations or perusing the results, the 'Back' button is always available to help you step back through the app. In case you'd like to start afresh from the Results menu, just tap on 'Main Menu'. +* __Navigation:__ Even when you're deeply involved in entering calculations or examining the results, the power to navigate the app remains in your hands. While the 'Back' button is present in most scenarios, it isn't available in the Results menu and the Input screen. However, you can always return to the beginning by selecting 'Main Menu' from the Results menu. And if you're on the numeric Input screen, simply hold down the 'Clear' button to navigate back. ## Compatibility From ad0b24a597ce51a60ed358cc471db7bc5445f37e Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:22:00 -0500 Subject: [PATCH 12/24] Update app.js - Adds haptics to Input screen and long press "C" to go back --- apps/ohmcalc/app.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 4a3d97e8c..61ab4a0be 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -50,6 +50,7 @@ let handleEnter; let showVariableSelectionMenu; let showInputMenu; let resultsMenu; +let mainMenu; // Setup the layout for input buttons let layout = new Layout({ @@ -59,7 +60,7 @@ let layout = new Layout({ { type: "h", c: "123".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, { type: "h", c: "456".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, { type: "h", c: "789".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, - { type: "h", c: ".0C".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, + { type: "h", c: ".0C".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, cbl: i === "C" ? () => { E.showMenu(mainMenu); Bangle.buzz(20); } : undefined, fillx: 1, filly: 1 })) }, { type: "h", c: [{ type: "btn", font: "6x8:2", label: "Enter", cb: () => { handleEnter(); }, fillx: 1, filly: 1 }] } ] }, { lazy: false }); @@ -109,6 +110,7 @@ function setValue(newStr) { // Function to handle the press of a button and append its value to the current input function handleButtonPress(value) { + Bangle.buzz(20); if (invalidInput) { return; // Don't allow input if an invalid input error message is displayed } @@ -165,7 +167,7 @@ function calculateValue(calculatedVariable, variableValues) { // Main function to initialize the application and setup the main menu (function () { - let mainMenu = { + mainMenu = { '': { 'title': 'Ohm\'s Law Calc' }, '< Back': () => Bangle.showClock() }; From dcf3a33a29181b59e8c0b24956dbff8538902f95 Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:22:42 -0500 Subject: [PATCH 13/24] Update ChangeLog --- apps/ohmcalc/ChangeLog | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/ohmcalc/ChangeLog b/apps/ohmcalc/ChangeLog index 68790afee..af752b896 100644 --- a/apps/ohmcalc/ChangeLog +++ b/apps/ohmcalc/ChangeLog @@ -1,2 +1,3 @@ 0.01: New App! 0.02: New Results menu item to show the formula and values used. +0.03: Adds haptics to Input screen and long press "C" to go back. From a0fcef0b92a1511ecd58db5055756fbb6efa49e7 Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:22:57 -0500 Subject: [PATCH 14/24] Update metadata.json --- apps/ohmcalc/metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ohmcalc/metadata.json b/apps/ohmcalc/metadata.json index 1272b9f91..de81ec6d7 100644 --- a/apps/ohmcalc/metadata.json +++ b/apps/ohmcalc/metadata.json @@ -2,7 +2,7 @@ "id": "ohmcalc", "name": "Ohm's Law Calculator", "shortName": "Ohm's Law Calc", - "version": "0.02", + "version": "0.03", "description": "A smart and simple calculator for Ohm's Law calculations, designed specifically for Bangle.js 2 smartwatches. Handles voltage, current, resistance, and power calculations with smart logic to prevent invalid inputs.", "icon": "app.png", "type": "app", From 02a5f81a0efbbade9cfcdf39875224052893b9ee Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:24:45 -0500 Subject: [PATCH 15/24] Update README.md --- apps/ohmcalc/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ohmcalc/README.md b/apps/ohmcalc/README.md index a890e590a..3add03736 100644 --- a/apps/ohmcalc/README.md +++ b/apps/ohmcalc/README.md @@ -7,7 +7,7 @@ This is a simple and intuitive Ohm's Law Calculator application designed for the * __Select the Electrical Measurement:__ On the main menu, select the electrical measurement that you wish to calculate (Voltage, Current, Resistance, or Power). * __Enter Known Values:__ You will then be prompted to enter the known values. The application will present a menu listing the remaining electrical measurements. Select one and you will be taken to a numeric input screen. Repeat this step for the second known value. * __View Results:__ Once the two known values have been entered, the application will automatically calculate and display the value of the selected electrical measurement. The Results menu will show the calculated value, the unit of measurement, and the formula used for calculation. -* __Navigation:__ Even when you're deeply involved in entering calculations or examining the results, the power to navigate the app remains in your hands. While the 'Back' button is present in most scenarios, it isn't available in the Results menu and the Input screen. However, you can always return to the beginning by selecting 'Main Menu' from the Results menu. And if you're on the numeric Input screen, simply hold down the 'Clear' button to navigate back. +* __Navigation:__ Whether you're deep in calculations or perusing the results, the power to navigate the app remains in your hands. While the 'Back' button is present in most scenarios, it isn't available in the Results menu and the Input screen. However, you can always return to the beginning by selecting 'Main Menu' from the Results menu. And if you're on the numeric Input screen, simply hold down the 'Clear' button to navigate back. ## Compatibility From ae174c6860d7b0376b8d6e4d1675b566793c6778 Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sat, 3 Jun 2023 22:53:49 -0500 Subject: [PATCH 16/24] Update app.js - Fixes small rounding bug --- apps/ohmcalc/app.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 61ab4a0be..b0a28854b 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -143,10 +143,7 @@ function calculateValue(calculatedVariable, variableValues) { // Round and trim long decimal numbers if (!isInteger) { - calculatedValue = Math.round(calculatedValue * 1000) / 1000; - calculatedValue = calculatedValue.toString().replace(/(\.\d*?)0+$/, '$1'); - } else { - calculatedValue = calculatedValue.toFixed(0); + calculatedValue = calculatedValue.toFixed(3); } let result = Object.entries(variableValues).map(function (entry) { From 592b6777889c5e5c47848513e885deb8dbed6d8f Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sun, 4 Jun 2023 00:53:14 -0500 Subject: [PATCH 17/24] Update app.js - Fixed a small rounding error --- apps/ohmcalc/app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index b0a28854b..63829d42a 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -144,6 +144,7 @@ function calculateValue(calculatedVariable, variableValues) { // Round and trim long decimal numbers if (!isInteger) { calculatedValue = calculatedValue.toFixed(3); + calculatedValue = calculatedValue.replace(/\.0+$/, '').replace(/(\.\d*[1-9])0+$/, '$1'); } let result = Object.entries(variableValues).map(function (entry) { From caec426589b53898da8704b3a2a9cb53a30ed65f Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sun, 4 Jun 2023 01:10:15 -0500 Subject: [PATCH 18/24] Update app.js - Fixes "C" back button --- apps/ohmcalc/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 63829d42a..4101fba11 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -60,7 +60,7 @@ let layout = new Layout({ { type: "h", c: "123".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, { type: "h", c: "456".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, { type: "h", c: "789".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, fillx: 1, filly: 1 })) }, - { type: "h", c: ".0C".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, cbl: i === "C" ? () => { E.showMenu(mainMenu); Bangle.buzz(20); } : undefined, fillx: 1, filly: 1 })) }, + { type: "h", c: ".0C".split("").map(i => ({ type: "btn", font: "6x8:3", label: i, cb: () => { handleButtonPress(i); }, cbl: i === "C" ? () => { showVariableSelectionMenu(); Bangle.buzz(20); } : undefined, fillx: 1, filly: 1 })) }, { type: "h", c: [{ type: "btn", font: "6x8:2", label: "Enter", cb: () => { handleEnter(); }, fillx: 1, filly: 1 }] } ] }, { lazy: false }); From bbbd200e2fcb2564015792747334bbc8590b580d Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sun, 4 Jun 2023 01:25:21 -0500 Subject: [PATCH 19/24] Update app.js - Dynamically adjust Results screen font size --- apps/ohmcalc/app.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 4101fba11..fc6bda81c 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -291,7 +291,7 @@ function calculateValue(calculatedVariable, variableValues) { function drawResultScreen(result) { let drawPage = function() { clearScreen(); - let fontSize = 30; // Adjust to fit the display + let fontSize = 30; // Initial font size let lineSpacing = 15; // Space between lines // Define the vertical positions of the titles @@ -313,17 +313,13 @@ function calculateValue(calculatedVariable, variableValues) { let valueX; let valueY = titleY + g.getFontHeight(); // Draw below the title - let valueFontSize = fontSize; - // Draw value with smaller font size if necessary - g.setFontVector(valueFontSize); // Large font for value - if (g.stringWidth(resultValue) > g.getWidth()) { - valueFontSize /= 1.5; // Small font for value + // Calculate the font size for value dynamically + let valueFontSize = fontSize; // Initialize with maximum possible font size + g.setFontVector(valueFontSize); + while (g.stringWidth(resultValue) > g.getWidth() && valueFontSize > 1) { + valueFontSize *= 0.9; // Reduce the font size by 10% g.setFontVector(valueFontSize); - if (g.stringWidth(resultValue) > g.getWidth()) { - valueFontSize /= 1.5; // Smallest font for value - g.setFontVector(valueFontSize); - } } valueY += g.getFontHeight() / 2 + 2; From 38711975540d7957275cde1aec19657dbde97fdf Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sun, 4 Jun 2023 02:02:42 -0500 Subject: [PATCH 20/24] Update app.js - Minor font sizing changes --- apps/ohmcalc/app.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index fc6bda81c..600c12431 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -256,7 +256,7 @@ function calculateValue(calculatedVariable, variableValues) { g.setFontVector(fontSize); // Reduce the font size until both the value and unit fit on the screen - while (g.stringWidth(result.value) > g.getWidth() - 20 || g.getFontHeight() > g.getHeight() / 2) { + while (g.stringWidth(result.value) > g.getWidth() - 10 || g.getFontHeight() > g.getHeight() / 2) { fontSize--; g.setFontVector(fontSize); } @@ -317,8 +317,8 @@ function calculateValue(calculatedVariable, variableValues) { // Calculate the font size for value dynamically let valueFontSize = fontSize; // Initialize with maximum possible font size g.setFontVector(valueFontSize); - while (g.stringWidth(resultValue) > g.getWidth() && valueFontSize > 1) { - valueFontSize *= 0.9; // Reduce the font size by 10% + while (g.stringWidth(resultValue) > g.getWidth() - 10) { + valueFontSize--; // Reduce the font size by 1 g.setFontVector(valueFontSize); } From e480f21b288c7b0a9fec32243bdf16e729b4e94f Mon Sep 17 00:00:00 2001 From: stweedo <108593831+stweedo@users.noreply.github.com> Date: Sun, 4 Jun 2023 02:17:10 -0500 Subject: [PATCH 21/24] Update app.js - Remove unnecessary variable --- apps/ohmcalc/app.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/ohmcalc/app.js b/apps/ohmcalc/app.js index 600c12431..aa470401c 100644 --- a/apps/ohmcalc/app.js +++ b/apps/ohmcalc/app.js @@ -315,11 +315,10 @@ function calculateValue(calculatedVariable, variableValues) { let valueY = titleY + g.getFontHeight(); // Draw below the title // Calculate the font size for value dynamically - let valueFontSize = fontSize; // Initialize with maximum possible font size - g.setFontVector(valueFontSize); + g.setFontVector(fontSize); while (g.stringWidth(resultValue) > g.getWidth() - 10) { - valueFontSize--; // Reduce the font size by 1 - g.setFontVector(valueFontSize); + fontSize--; // Reduce the font size by 1 + g.setFontVector(fontSize); } valueY += g.getFontHeight() / 2 + 2; From b4abf8571cba7cdb87bf417014d650a2ae94329a Mon Sep 17 00:00:00 2001 From: stweedo Date: Sun, 4 Jun 2023 20:43:19 -0500 Subject: [PATCH 22/24] [rescalc] - update to v0.02 --- apps/rescalc/ChangeLog | 1 + apps/rescalc/app.js | 224 ++++++++++++++----------------------- apps/rescalc/metadata.json | 2 +- 3 files changed, 83 insertions(+), 144 deletions(-) diff --git a/apps/rescalc/ChangeLog b/apps/rescalc/ChangeLog index 5560f00bc..43f41aa76 100644 --- a/apps/rescalc/ChangeLog +++ b/apps/rescalc/ChangeLog @@ -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. \ No newline at end of file diff --git a/apps/rescalc/app.js b/apps/rescalc/app.js index cfb88e015..1986ac2db 100644 --- a/apps/rescalc/app.js +++ b/apps/rescalc/app.js @@ -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); -})(); +})(); \ No newline at end of file diff --git a/apps/rescalc/metadata.json b/apps/rescalc/metadata.json index c14bbd6e5..aaa5f2acf 100644 --- a/apps/rescalc/metadata.json +++ b/apps/rescalc/metadata.json @@ -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"}, From 2f0f5e8dd7dea229c211487075aa54fbd796ed4a Mon Sep 17 00:00:00 2001 From: stweedo Date: Mon, 5 Jun 2023 00:31:08 -0500 Subject: [PATCH 23/24] [rescalc] - bugfix with decimal values to colors --- apps/rescalc/app.js | 49 ++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/apps/rescalc/app.js b/apps/rescalc/app.js index 1986ac2db..6805589b2 100644 --- a/apps/rescalc/app.js +++ b/apps/rescalc/app.js @@ -36,25 +36,35 @@ function colorBandsToResistance(colorBands) { } function resistanceToColorBands(resistance, tolerance) { - 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; + if (resistance < 1) { + // The resistance is less than 1, so we need to handle this case specially + let count = 0; + while (resistance < 1) { + resistance *= 10; + count++; + } + // Now, resistance is a whole number and count is how many times we had to multiply by 10 + let resistanceStr = resistance.toString(); + firstDigit = 0; // Set the first band color to be black + secondDigit = Number(resistanceStr.charAt(0)); // Set the second band color to be the significant digit + // Use count to determine the multiplier + multiplier = count === 1 ? 0.1 : 0.01; } 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; + // Convert the resistance to a string so we can manipulate it easily + let resistanceStr = resistance.toString(); + if (resistanceStr.length === 1) { // Check if resistance is a single digit + firstDigit = 0; + secondDigit = Number(resistanceStr.charAt(0)); + multiplier = 1; // Set multiplier to 1 for single digit resistance values + } else { + // Extract the first two digits from the resistance value + firstDigit = Number(resistanceStr.charAt(0)); + secondDigit = Number(resistanceStr.charAt(1)); + // Calculate the multiplier by matching it directly with the length of digits + multiplier = resistanceStr.length - 2 >= 0 ? Math.pow(10, resistanceStr.length - 2) : Math.pow(10, resistanceStr.length - 1); + } } - let firstBandEntry = Object.entries(colorData).find(function(entry) { return entry[1].value === firstDigit; }); @@ -64,7 +74,7 @@ function resistanceToColorBands(resistance, tolerance) { }); let secondBand = secondBandEntry ? secondBandEntry[1].hex : undefined; let multiplierBandEntry = Object.entries(colorData).find(function(entry) { - return entry[1].multiplier === Math.pow(10, multiplier); + return entry[1].multiplier === multiplier; }); let multiplierBand = multiplierBandEntry ? multiplierBandEntry[1].hex : undefined; let toleranceBandEntry = Object.entries(colorData).find(function(entry) { @@ -352,7 +362,7 @@ function drawResistance(resistance, tolerance) { E.showMenu(toleranceMenu); } - function drawResistorAndResistance(resistance, tolerance) { + function drawResistorAndResistance(resistance, tolerance) { if (inputColorBands) { colorBands = inputColorBands.map(color => { if (colorData.hasOwnProperty(color)) { @@ -362,8 +372,11 @@ function drawResistance(resistance, tolerance) { } }); } else { + console.log("Using colorBands = resistanceToColorBands(resistance, tolerance)" + "\nResistance: " + resistance + "\nTolerance: " + tolerance); colorBands = resistanceToColorBands(resistance, tolerance); } + console.log(inputColorBands); + console.log(colorBands); drawResistor(colorBands, tolerance); drawResistance(resistance, tolerance); resetSettings(); From b62d8baf75e13f77f9892db11cd533cad7ad2ac6 Mon Sep 17 00:00:00 2001 From: stweedo Date: Mon, 5 Jun 2023 00:34:44 -0500 Subject: [PATCH 24/24] [rescalc] - Remove console logs and formatting --- apps/rescalc/app.js | 57 +++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/apps/rescalc/app.js b/apps/rescalc/app.js index 6805589b2..566809837 100644 --- a/apps/rescalc/app.js +++ b/apps/rescalc/app.js @@ -65,19 +65,19 @@ function resistanceToColorBands(resistance, tolerance) { multiplier = resistanceStr.length - 2 >= 0 ? Math.pow(10, resistanceStr.length - 2) : Math.pow(10, resistanceStr.length - 1); } } - let firstBandEntry = Object.entries(colorData).find(function(entry) { + let firstBandEntry = Object.entries(colorData).find(function (entry) { return entry[1].value === firstDigit; }); let firstBand = firstBandEntry ? firstBandEntry[1].hex : undefined; - let secondBandEntry = Object.entries(colorData).find(function(entry) { + let secondBandEntry = Object.entries(colorData).find(function (entry) { return entry[1].value === secondDigit; }); let secondBand = secondBandEntry ? secondBandEntry[1].hex : undefined; - let multiplierBandEntry = Object.entries(colorData).find(function(entry) { + let multiplierBandEntry = Object.entries(colorData).find(function (entry) { return entry[1].multiplier === multiplier; }); let multiplierBand = multiplierBandEntry ? multiplierBandEntry[1].hex : undefined; - let toleranceBandEntry = Object.entries(colorData).find(function(entry) { + let toleranceBandEntry = Object.entries(colorData).find(function (entry) { return entry[1].tolerance === tolerance; }); let toleranceBand = toleranceBandEntry ? toleranceBandEntry[1].hex : undefined; @@ -175,7 +175,7 @@ function drawResistance(resistance, tolerance) { g.drawString(toleranceStr.padEnd(4), 176 - toleranceX, y); } -(function() { +(function () { let colorBands; let inputColorBands; let settings = { @@ -200,7 +200,7 @@ function drawResistance(resistance, tolerance) { '': { 'title': `Band ${bandNumber}` }, - '< Back': function() { + '< Back': function () { E.showMenu(colorEntryMenu); }, }; @@ -209,24 +209,24 @@ function drawResistance(resistance, tolerance) { for (let color in colorData) { if (bandNumber === 1 || bandNumber === 2) { if (color !== 'none' && color !== 'gold' && color !== 'silver') { - (function(color) { - colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function() { + (function (color) { + colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function () { setBandColor(bandNumber, color); }; })(color); } } else if (bandNumber === 3) { if (color !== 'none') { - (function(color) { - colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function() { + (function (color) { + colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function () { setBandColor(bandNumber, color); }; })(color); } } else if (bandNumber === 4) { if (colorData[color].hasOwnProperty('tolerance')) { - (function(color) { - colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function() { + (function (color) { + colorBandMenu[color.charAt(0).toUpperCase() + color.slice(1)] = function () { setBandColor(bandNumber, color); }; })(color); @@ -248,7 +248,7 @@ function drawResistance(resistance, tolerance) { '': { 'title': 'Band Color' }, - '< Back': function() { + '< Back': function () { clearScreen(); E.showMenu(mainMenu); }, @@ -284,7 +284,7 @@ function drawResistance(resistance, tolerance) { setTimeout(() => showColorBandMenu(4), 5); } }, - 'Draw Resistor': function() { + 'Draw Resistor': function () { inputColorBands = settings.colorBands; let values = colorBandsToResistance(inputColorBands); settings.resistance = values[0]; @@ -301,7 +301,7 @@ function drawResistance(resistance, tolerance) { '': { 'title': 'Multiplier' }, - '< Back': function() { + '< Back': function () { showResistanceEntryMenu(); } }; @@ -314,7 +314,7 @@ function drawResistance(resistance, tolerance) { multiplierMenu[`${formattedMultiplier}`] = () => { settings.multiplier = multiplierValue; // Update the value of 'Multiplier' in resistanceEntryMenu - resistanceEntryMenu["Multiplier"] = function() { + resistanceEntryMenu["Multiplier"] = function () { showMultiplierMenu(); }; showResistanceEntryMenu(); @@ -340,7 +340,7 @@ function drawResistance(resistance, tolerance) { '': { 'title': 'Tolerance' }, - '< Back': function() { + '< Back': function () { showResistanceEntryMenu(); } }; @@ -352,7 +352,7 @@ function drawResistance(resistance, tolerance) { toleranceMenu[`${tolerance}%`] = () => { settings.tolerance = tolerance; // Update the value of 'Tolerance (%)' in resistanceEntryMenu - resistanceEntryMenu["Tolerance (%)"] = function() { + resistanceEntryMenu["Tolerance (%)"] = function () { showToleranceMenu(); }; showResistanceEntryMenu(); @@ -362,7 +362,7 @@ function drawResistance(resistance, tolerance) { E.showMenu(toleranceMenu); } - function drawResistorAndResistance(resistance, tolerance) { + function drawResistorAndResistance(resistance, tolerance) { if (inputColorBands) { colorBands = inputColorBands.map(color => { if (colorData.hasOwnProperty(color)) { @@ -372,11 +372,8 @@ function drawResistance(resistance, tolerance) { } }); } else { - console.log("Using colorBands = resistanceToColorBands(resistance, tolerance)" + "\nResistance: " + resistance + "\nTolerance: " + tolerance); colorBands = resistanceToColorBands(resistance, tolerance); } - console.log(inputColorBands); - console.log(colorBands); drawResistor(colorBands, tolerance); drawResistance(resistance, tolerance); resetSettings(); @@ -386,7 +383,7 @@ function drawResistance(resistance, tolerance) { '': { 'title': 'Resistance' }, - '< Back': function() { + '< Back': function () { clearScreen(); E.showMenu(mainMenu); }, @@ -396,15 +393,15 @@ function drawResistance(resistance, tolerance) { max: 99, wrap: true, format: v => '', - onchange: v => {} + onchange: v => { } }, - 'Multiplier': function() { + 'Multiplier': function () { showMultiplierMenu(); }, - 'Tolerance (%)': function() { + 'Tolerance (%)': function () { showToleranceMenu(); }, - 'Draw Resistor': function() { + 'Draw Resistor': function () { showDrawingMenu(); } }; @@ -433,7 +430,7 @@ function drawResistance(resistance, tolerance) { '': { 'title': '' }, - '< Back': function() { + '< Back': function () { clearScreen(); E.showMenu(mainMenu); }, @@ -449,11 +446,11 @@ function drawResistance(resistance, tolerance) { 'title': 'Resistor Calc' }, '< Back': () => Bangle.showClock(), // return to the clock app - 'Resistance': function() { + 'Resistance': function () { resetSettings(); showResistanceEntryMenu(); }, - 'Colors': function() { + 'Colors': function () { resetSettings(); showColorEntryMenu(); },