-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
212 lines (182 loc) · 6.84 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voltage Divider Calculator</title>
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
form, table {
display: flex;
flex-direction: column;
gap: 10px;
margin-bottom: 20px;
}
input, button {
padding: 5px;
font-size: 16px;
}
#result {
white-space: pre-wrap;
}
table {
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>Voltage Divider Calculator</h1>
<h2>Available Resistors</h2>
<table id="resistor-table">
<thead>
<tr>
<th>Value (Ω)</th>
<th>Name</th>
<th>Action</th>
</tr>
</thead>
<tbody id="resistor-list">
<!-- Resistors will be added here dynamically -->
</tbody>
</table>
<form id="add-resistor-form">
<input type="number" id="resistor-value" placeholder="Resistor Value (Ω)" required>
<input type="text" id="resistor-name" placeholder="Resistor Name (e.g., 1kΩ)" required>
<button type="submit">Add Resistor</button>
</form>
<h2>Voltage Divider Calculation</h2>
<form id="calculator-form">
<label for="input-voltage">Input Voltage (V):</label>
<input type="number" id="input-voltage" required step="0.01">
<label for="output-voltage">Desired Output Voltage (V):</label>
<input type="number" id="output-voltage" required step="0.01">
<label for="max-resistors">Max Resistors Count:</label>
<input type="number" id="max-resistors" required min="1" max="5">
<button type="submit">Calculate</button>
</form>
<div id="result"></div>
<script type="module">
import { calculateVoltageDivider } from './dist/resistor-calculator.js';
// Initialize mermaid
mermaid.initialize({ startOnLoad: false });
let resistorsInOhms = [
{ value: 240, name: "240Ω" },
{ value: 470, name: "470Ω" },
{ value: 510, name: "510Ω" },
{ value: 1200, name: "1.2kΩ" },
{ value: 100000, name: "100kΩ" },
{ value: 10000, name: "10kΩ" },
{ value: 1000, name: "1kΩ" },
{ value: 2200, name: "2.2kΩ" },
{ value: 200000, name: "200kΩ" },
{ value: 20000, name: "20kΩ" },
{ value: 220000, name: "220kΩ" },
{ value: 22000, name: "22kΩ" },
{ value: 27000, name: "27kΩ" },
{ value: 30000, name: "30kΩ" },
{ value: 3000, name: "3kΩ" },
{ value: 4700, name: "4.7kΩ" },
{ value: 470000, name: "470kΩ" },
{ value: 47000, name: "47kΩ" },
{ value: 510000, name: "510kΩ" },
{ value: 71000, name: "71kΩ" },
{ value: 97000, name: "97kΩ" },
];
function saveToLocalStorage() {
localStorage.setItem('resistorsInOhms', JSON.stringify(resistorsInOhms));
localStorage.setItem('inputVoltage', document.getElementById('input-voltage').value);
localStorage.setItem('outputVoltage', document.getElementById('output-voltage').value);
localStorage.setItem('maxResistors', document.getElementById('max-resistors').value);
}
function loadFromLocalStorage() {
const savedResistors = localStorage.getItem('resistorsInOhms');
if (savedResistors) {
resistorsInOhms = JSON.parse(savedResistors);
}
document.getElementById('input-voltage').value = localStorage.getItem('inputVoltage') || '';
document.getElementById('output-voltage').value = localStorage.getItem('outputVoltage') || '';
document.getElementById('max-resistors').value = localStorage.getItem('maxResistors') || '';
}
function updateResistorList() {
const resistorList = document.getElementById('resistor-list');
resistorList.innerHTML = '';
resistorsInOhms.forEach((resistor, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${resistor.value}</td>
<td>${resistor.name}</td>
<td><button class="remove-resistor" data-index="${index}">Remove</button></td>
`;
resistorList.appendChild(row);
});
saveToLocalStorage();
}
function removeResistor(index) {
resistorsInOhms.splice(index, 1);
updateResistorList();
}
document.getElementById('resistor-list').addEventListener('click', (e) => {
if (e.target.classList.contains('remove-resistor')) {
const index = parseInt(e.target.getAttribute('data-index'));
removeResistor(index);
}
});
document.getElementById('add-resistor-form').addEventListener('submit', (e) => {
e.preventDefault();
const value = parseInt(document.getElementById('resistor-value').value);
const name = document.getElementById('resistor-name').value;
resistorsInOhms.push({value, name});
updateResistorList();
e.target.reset();
});
document.getElementById('calculator-form').addEventListener('submit', async (e) => {
e.preventDefault();
const inputVoltage = parseFloat(document.getElementById('input-voltage').value);
const outputVoltage = parseFloat(document.getElementById('output-voltage').value);
const maxResistors = parseInt(document.getElementById('max-resistors').value);
saveToLocalStorage();
console.log('Input Voltage:', resistorsInOhms);
const result = calculateVoltageDivider(inputVoltage, outputVoltage, maxResistors, resistorsInOhms);
const resultHtml = `
## Results
Actual Output Voltage: ${result.actualVoltage.toFixed(3)}V
Error: ${result.error.toFixed(3)}V
### GND Network
<div class="mermaid">
${result.gndNetwork}
</div>
### VCC Network
<div class="mermaid">
${result.vccNetwork}
</div>
`;
document.getElementById('result').innerHTML = marked.parse(resultHtml);
// Render mermaid diagrams
mermaid.init(undefined, document.querySelectorAll('.mermaid'));
return false;
});
// Load saved data and update the resistor list on page load
loadFromLocalStorage();
updateResistorList();
// Add event listeners to save input values when they change
['input-voltage', 'output-voltage', 'max-resistors'].forEach(id => {
document.getElementById(id).addEventListener('change', saveToLocalStorage);
});
</script>
</body>
</html>