Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
checkpoint
in progress
  • Loading branch information
rickkas7 committed May 12, 2025
commit ee33ae736d555e79957ef2b6501d372ffeff9660
191 changes: 188 additions & 3 deletions src/assets/js/resistor-calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,49 @@ $(document).ready(function () {

calculator.partial = thisPartial;

calculator.decimalStringTruncate = function(value, decimalPlaces) {
let s = value.toString();

let dotIndex = s.indexOf('.');
if (dotIndex > 0) {
if (decimalPlaces > 0) {
s = s.substring(0, dotIndex + decimalPlaces);
}
else {
s = s.substring(0, dotIndex);
}
}

return s;
}

calculator.resistorWithUnits = function(value, options = {}) {
let unit = '';

if (value < 1000) {
}
else
if (value < 1000000) {
value /= 1000;
unit = 'K';
}
else {
value /= 1000000;
unit = 'M';
}

let valueStr = '';

if (typeof options.truncate != 'undefined') {
valueStr = calculator.decimalStringTruncate(value, options.truncate);
}
else {
valueStr = value.toString();
}

return valueStr + unit;
}

// Common resistor values for 10% and 5%
calculator.tenPct = [];
{
Expand Down Expand Up @@ -113,14 +156,14 @@ $(document).ready(function () {
if (p.style == 'auto') {
$(thisPartial).find('.autoRow').show();
$(thisPartial).find('.manualRow').hide();
p.solveFor = p.solveForElem = null;
$(thisPartial).find('input[type="radio"][name="solveFor"][value="vout"]').prop('checked', true);
}
else {
$(thisPartial).find('.autoRow').hide();
$(thisPartial).find('.manualRow').show();
p.solveFor = $(thisPartial).find('input[type="radio"][name="solveFor"]:checked').val();
p.solveForElem = calculator.parameters[p.solveFor].inputElem;
}
p.solveFor = $(thisPartial).find('input[type="radio"][name="solveFor"]:checked').val();
p.solveForElem = calculator.parameters[p.solveFor].inputElem;

for(const groupName in calculator.parameters) {
p[groupName] = parseFloat($(calculator.parameters[groupName].inputElem).val());
Expand All @@ -132,35 +175,177 @@ $(document).ready(function () {

if (p.style == 'auto') {
// Auto
p.useKey = $(thisPartial).find('input[name="use"]:checked').val();
p.rmin *= 1000;
p.rmax *= 1000;

// nearest, above, below
p.show = $(thisPartial).find('input[name="show"]:checked').val();

p.resToTry = [];
for(const r of calculator[p.useKey]) {
if (r >= p.rmin && r <= p.rmax) {
p.resToTry.push(r);
}
}

$(thisPartial).find('.autoResults > tbody').empty();


p.results = [];

for(const r1 of p.resToTry) {
const resultR1 = {
r1,
};

for(const r2 of p.resToTry) {
let result;
let delta;

switch(p.solveFor) {
case 'vin':
result = (p.vout * (r1 + r2)) / r2;
delta = result - p.vin;
break;

case 'vout':
result = (p.vin * r2) / (r1 + r2);
delta = result - p.vout;
break;
}

switch(p.show) {
case 'above':
if (delta >= 0) {
if (typeof resultR1.delta == 'undefined' || delta < resultR1.delta) {
resultR1.r2 = r2;
resultR1.result = result;
resultR1.delta = delta;
}
}
break;

case 'below':
if (delta <= 0) {
if (typeof resultR1.delta == 'undefined' || -delta < -resultR1.delta) {
resultR1.r2 = r2;
resultR1.result = result;
resultR1.delta = delta;
}
}
break;

case 'nearest':
if (typeof resultR1.delta == 'undefined' || Math.abs(delta) < Math.abs(resultR1.delta)) {
resultR1.r2 = r2;
resultR1.result = result;
resultR1.delta = delta;
}
break;
}
}
if (typeof resultR1.delta != 'undefined') {
switch(p.solveFor) {
case 'vin':
resultR1.deltaPct = resultR1.delta * 100 / p.vin;
break;

case 'vout':
resultR1.deltaPct = resultR1.delta * 100 / p.vout;
break;
}

if (Math.abs(resultR1.deltaPct) < 10) {
p.results.push(resultR1);
}
}
}
p.results.sort(function(a, b) {
// Sort by descending abs(delta)
let cmp = Math.floor(b.delta) - Math.floor(a.delta);
if (cmp != 0) {
return cmp;
}
// Then ascending R1
cmp = a.r1 - b.r1;

return cmp;
});

for(const result of p.results) {
const trElem = document.createElement('tr');

{
const tdElem = document.createElement('td');
$(tdElem).text(calculator.resistorWithUnits(result.r1, {truncate:1}));
$(trElem).append(tdElem);
}
{
const tdElem = document.createElement('td');
$(tdElem).text(calculator.resistorWithUnits(result.r2, {truncate:1}));
$(trElem).append(tdElem);
}
{
const tdElem = document.createElement('td');
$(tdElem).text(calculator.decimalStringTruncate(result.result, 3));
$(trElem).append(tdElem);
}
{
const tdElem = document.createElement('td');
$(tdElem).text(calculator.decimalStringTruncate(result.delta, 3));
$(trElem).append(tdElem);
}
{
const tdElem = document.createElement('td');
$(tdElem).text(calculator.decimalStringTruncate(result.deltaPct, 2) + ' %');
$(trElem).append(tdElem);
}

$(thisPartial).find('.autoResults > tbody').append(trElem);
}

$(thisPartial).find('.autoResults').show();
}
else {
// Manual
let result;
let decimalPlaces = 0;

switch(p.solveFor) {
case 'vin':
result = (p.vout * (p.r1 + p.r2)) / p.r2;
decimalPlaces = 3;
break;

case 'r1':
result = p.r2 * ((p.vin / p.vout) - 1);
decimalPlaces = 1;
break;

case 'r2':
result = p.r1 * 1 / ((p.vin / p.vout) - 1);
decimalPlaces = 1;
break;

case 'vout':
result = (p.vin * p.r2) / (p.r1 + p.r2);
decimalPlaces = 3;
break;
}
result = calculator.decimalStringTruncate(result, decimalPlaces);

$(p.solveForElem).val(result);

$(thisPartial).find('.autoResults').hide();
}

console.log('solve', p);
};

$(thisPartial).find('input[name="style"]').on('click', calculator.solve);
$(thisPartial).find('input[name="use"]').on('click', calculator.solve);
$(thisPartial).find('input[name="show"]').on('click', calculator.solve);


$(thisPartial).find('.inputText').on('input blur', calculator.solve);
Expand Down
35 changes: 28 additions & 7 deletions templates/partials/resistor-calculator.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<td class="apiHelperProductSelectorLabel">V<sub>IN</sub></td>
<td><input type="text" size="10" value="3.3" name="vin" class="inputText"></td>
<td>volts</td>
<td class="manualRow" ><label><input type="radio" name="solveFor" value="vin">Solve for V<sub>IN</sub></label></td>
<td><label><input type="radio" name="solveFor" value="vin">Solve for V<sub>IN</sub></label></td>
</tr>
<tr class="manualRow" data-group="r1">
<td class="apiHelperProductSelectorLabel">R<sub>1</sub></td>
Expand All @@ -31,7 +31,7 @@
<td class="apiHelperProductSelectorLabel">V<sub>OUT</sub></td>
<td><input type="text" size="10" value="" name="vout" class="inputText"></td>
<td>volts</td>
<td class="manualRow"><label><input type="radio" name="solveFor" value="vout" checked>Solve for V<sub>OUT</sub></label></td>
<td><label><input type="radio" name="solveFor" value="vout" checked>Solve for V<sub>OUT</sub></label></td>
</tr>
<tr class="autoRow" data-group="rmin" style="display:none;">
<td class="apiHelperProductSelectorLabel">R<sub>MIN</sub></td>
Expand All @@ -41,18 +41,39 @@
</tr>
<tr class="autoRow" data-group="rmax" style="display:none;">
<td class="apiHelperProductSelectorLabel">R<sub>MAX</sub></td>
<td><input type="text" size="10" value="1000" name="rmax" class="inputText"></td>
<td><input type="text" size="10" value="100" name="rmax" class="inputText"></td>
<td>K ohm</td>
<td></td>
</tr>
<tr class="autoRow" style="display:none;">
<td>Use</td>
<td colspan="2">
<label><input type="radio" name="use" value="10" checked>10%</label> &nbsp;
<label><input type="radio" name="use" value="5">5%</label> &nbsp;
<td class="apiHelperProductSelectorLabel">Use</td>
<td colspan="3">
<label><input type="radio" name="use" value="tenPct" checked>10%</label> &nbsp;
<label><input type="radio" name="use" value="onePct">5%</label> &nbsp;
standard resistor values
</td>
</tr>
<tr class="autoRow" style="display:none;">
<td class="apiHelperProductSelectorLabel">Show</td>
<td colspan="3">
<label><input type="radio" name="show" value="nearest" checked>Nearest</label> &nbsp;
<label><input type="radio" name="show" value="below">Nearest below voltage</label> &nbsp;
<label><input type="radio" name="show" value="above">Nearest above voltage</label> &nbsp;
</td>
</tr>
</tbody>
</table>
<table class="apiHelperTableNoMargin autoResults" style="display:none">
<thead>
<tr>
<th>R<sub>1</sub></th>
<th>R<sub>2</sub></th>
<th>V</th>
<th>Delta</th>
<th>Percent</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
Expand Down