app.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. /*
  3. Resistor divider generator, by Leah Rowe
  4. Written as a web application, for hosting on a PHP-enabled HTTP server.
  5. Uses GET data for user input. Output displayed as text, in HTML.
  6. Given specified input voltage in mV and intended output voltage in mV use a
  7. list of known standard resistor values, show to the engineer which resistor
  8. values are ideal when designing a resistor divider circuit.
  9. Copyright (C) 2020 Leah Rowe
  10. Contact email: info@minifree.org
  11. Twitter: https://twitter.com/n4of7/
  12. Website: https://retrofreedom.com/
  13. This program is free software: you can redistribute it and/or modify
  14. it under the terms of the GNU Affero General Public License as
  15. published by the Free Software Foundation, either version 3 of the
  16. License, or (at your option) any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU Affero General Public License for more details.
  21. You should have received a copy of the GNU Affero General Public License
  22. along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. $timeStart=(float)microtime();
  25. if (false)
  26. {
  27. ini_set('display_errors', 1);
  28. ini_set('display_startup_errors', 1);
  29. error_reporting(E_ALL);
  30. set_time_limit (6);
  31. }
  32. //sleep(1);
  33. $arrE24 = array
  34. (
  35. 1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
  36. 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1
  37. );
  38. $arrE48 = array
  39. (
  40. 1.00, 1.05, 1.10, 1.15, 1.21, 1.27, 1.33, 1.40, 1.47, 1.54, 1.62, 1.69,
  41. 1.78, 1.87, 1.96, 2.05, 2.15, 2.26, 2.37, 2.49, 2.61, 2.74, 2.87, 3.01,
  42. 3.16, 3.32, 3.48, 3.65, 3.83, 4.02, 4.22, 4.42, 4.64, 4.87, 5.11, 5.36,
  43. 5.62, 5.90, 6.19, 6.49, 6.81, 7.15, 7.50, 7.87, 8.25, 8.66, 9.09, 9.53
  44. );
  45. $arrE96 = array
  46. (
  47. 1.00, 1.02, 1.05, 1.07, 1.10, 1.13, 1.15, 1.18, 1.21, 1.24, 1.27, 1.30,
  48. 1.33, 1.37, 1.40, 1.43, 1.47, 1.50, 1.54, 1.58, 1.62, 1.65, 1.69, 1.74,
  49. 1.78, 1.82, 1.87, 1.91, 1.96, 2.00, 2.05, 2.10, 2.15, 2.21, 2.26, 2.32,
  50. 2.37, 2.43, 2.49, 2.55, 2.61, 2.67, 2.74, 2.80, 2.87, 2.94, 3.01, 3.09,
  51. 3.16, 3.24, 3.32, 3.40, 3.48, 3.57, 3.65, 3.74, 3.83, 3.92, 4.02, 4.12,
  52. 4.22, 4.32, 4.42, 4.53, 4.64, 4.75, 4.87, 4.99, 5.11, 5.23, 5.36, 5.49,
  53. 5.62, 5.76, 5.90, 6.04, 6.19, 6.34, 6.49, 6.65, 6.81, 6.98, 7.15, 7.32,
  54. 7.50, 7.68, 7.87, 8.06, 8.25, 8.45, 8.66, 8.87, 9.09, 9.31, 9.53, 9.76
  55. );
  56. $arrE192 = array
  57. (
  58. 1.00, 1.01, 1.02, 1.04, 1.05, 1.06, 1.07, 1.09, 1.10, 1.11, 1.13, 1.14,
  59. 1.15, 1.17, 1.18, 1.20, 1.21, 1.23, 1.24, 1.26, 1.27, 1.29, 1.30, 1.32,
  60. 1.33, 1.35, 1.37, 1.38, 1.40, 1.42, 1.43, 1.45, 1.47, 1.49, 1.50, 1.52,
  61. 1.54, 1.56, 1.58, 1.60, 1.62, 1.64, 1.65, 1.67, 1.69, 1.72, 1.74, 1.76,
  62. 1.78, 1.80, 1.82, 1.84, 1.87, 1.89, 1.91, 1.93, 1.96, 1.98, 2.00, 2.03,
  63. 2.05, 2.08, 2.10, 2.13, 2.15, 2.18, 2.21, 2.23, 2.26, 2.29, 2.32, 2.34,
  64. 2.37, 2.40, 2.43, 2.46, 2.49, 2.52, 2.55, 2.58, 2.61, 2.64, 2.67, 2.71,
  65. 2.74, 2.77, 2.80, 2.84, 2.87, 2.91, 2.94, 2.98, 3.01, 3.05, 3.09, 3.12,
  66. 3.16, 3.20, 3.24, 3.28, 3.32, 3.36, 3.40, 3.44, 3.48, 3.52, 3.57, 3.61,
  67. 3.65, 3.70, 3.74, 3.79, 3.83, 3.88, 3.92, 3.97, 4.02, 4.07, 4.12, 4.17,
  68. 4.22, 4.27, 4.32, 4.37, 4.42, 4.48, 4.53, 4.59, 4.64, 4.70, 4.75, 4.81,
  69. 4.87, 4.93, 4.99, 5.05, 5.11, 5.17, 5.23, 5.30, 5.36, 5.42, 5.49, 5.56,
  70. 5.62, 5.69, 5.76, 5.83, 5.90, 5.97, 6.04, 6.12, 6.19, 6.26, 6.34, 6.42,
  71. 6.49, 6.57, 6.65, 6.73, 6.81, 6.90, 6.98, 7.06, 7.15, 7.23, 7.32, 7.41,
  72. 7.50, 7.59, 7.68, 7.77, 7.87, 7.96, 8.06, 8.16, 8.25, 8.35, 8.45, 8.56,
  73. 8.66, 8.76, 8.87, 8.98, 9.09, 9.20, 9.31, 9.42, 9.53, 9.65, 9.76, 9.88
  74. );
  75. $mVin = isset($_GET["vin"])
  76. ? (float)(int)floatval($_GET['vin'])
  77. : (float)0;
  78. $mVout_target = isset($_GET["vtarget"])
  79. ? (float)(int)floatval($_GET['vtarget'])
  80. : (float)0;
  81. $strRTarget = isset($_GET["rtarget"])
  82. ? (string)$_GET["rtarget"]
  83. : "";
  84. $strRTarget = (float)floatval($strRTarget) > 0
  85. ? (string)(float)floatval($strRTarget)
  86. : "";
  87. $tolerance = isset($_GET["tolerance"])
  88. ? (float)floatval($_GET["tolerance"])
  89. : (float)0.1;
  90. $tolerance = $tolerance>=0 && $tolerance<100
  91. ? $tolerance
  92. : (float)0.1;
  93. $order= isset($_GET["order"])
  94. ? (string)$_GET["order"]
  95. : "ascending";
  96. $order = $order != "ascending" && $order != "descending"
  97. ? "ascending"
  98. : $order;
  99. $arrE=array();
  100. $eSeries=isset($_GET["e"])
  101. ? (int)floatval($_GET["e"])
  102. : 24;
  103. switch($eSeries) {
  104. case 24: $arrE=$arrE24; break;
  105. case 48: $arrE=$arrE48; break;
  106. case 96: $arrE=$arrE96; break;
  107. case 192:$arrE=$arrE192; break;
  108. default:
  109. $arrE=$arrE24;
  110. $eSeries=24;
  111. break; }
  112. $arrEFull=$arrE;
  113. for ($power=1; $power <= 6; $power++)
  114. for ($arrE_index = 0; $arrE_index < count($arrE); $arrE_index++)
  115. array_push(
  116. $arrEFull,
  117. $fResistor = floatval($arrE[$arrE_index]) * (10**$power)
  118. );
  119. function strResistanceReadable($fResistor)
  120. {
  121. $fResistor=(float)$fResistor;
  122. for ($i = 0; $i < 3; $i++)
  123. if ($fResistor>=1000**$i && $fResistor<1000**($i+1))
  124. return
  125. (string)round(($fResistor / (1000**$i)),2)
  126. .(string)array("Ω","K","M")[$i];
  127. return (string)round($fResistor,2)."Ω";
  128. }
  129. function fReadableFloat($i)
  130. {
  131. $i=(float)$i;
  132. $negative=false; if($negative=$i<0) $i -= 2*$i;
  133. for ($power=1; $power<12; $power++)
  134. if ($i >= 10/(10**$power) && $i < 10/(10**($power-1)))
  135. return round($negative ? $i-(2*$i) : $i, $power);
  136. return round($negative ? $i-(2*$i) : $i, 1);
  137. }
  138. function rFind($r, &$arrEFull)
  139. {
  140. $r=(float)$r;
  141. if(!is_array($arrEFull)) return -1;
  142. $eCount = count($arrEFull);
  143. $eBaseCount = $eCount / 7;
  144. $eHalfCount = $eBaseCount / 2;
  145. // These are the 3 if's that hold the universe together. DO NOT REMOVE!
  146. if ($r<0.95 || $r>(1.05*$arrEFull[$eCount-1])) return -1;
  147. if ($r>=0.95 && $r<=1) return 1;
  148. if ($r<=(1.05*$arrEFull[$eCount-1]) && $r>=$arrEFull[$eCount-1])
  149. return $eCount-1;
  150. $power=0; $n=$r; for (;$n>=10; ++$power) $n /= 10;
  151. $rPos = ($power * $eBaseCount) + $eHalfCount;
  152. while ($eHalfCount > 1) {
  153. if ($eHalfCount == 2) ++$rPos;
  154. $eHalfCount -= (int)($eHalfCount / 2);
  155. if ($r < $arrEFull[$rPos-1]) { $rPos -= $eHalfCount; continue; }
  156. if ($r > $arrEFull[$rPos]) { $rPos += $eHalfCount; continue; }
  157. break; }
  158. return $rPos;
  159. }
  160. function rAddonSeriesGet($r1, $arrR, $diffLeft, &$arrEFull)
  161. {
  162. $r1 = (float)$r1;
  163. $r1_ideal = $diffLeft = (float)$diffLeft;
  164. if(!is_array($arrR)) $arrR=array();
  165. if(!is_array($arrEFull)) return $arrR;
  166. if ($diffLeft<=0 || $r1 <= 0) return $arrR;
  167. $rPos = rFind($r1_ideal, $arrEFull);
  168. if ($rPos<0) return $arrR;
  169. if ($rPos==0) { array_push($arrR,array($r1,(float)1)); return $arrR; }
  170. if ($rPos == count($arrEFull)-1) {
  171. array_push($arrR, array($r1, $arrEFull[count($arrEFull)-2]));
  172. return $arrR; }
  173. $diffLeft = $r1_ideal - $arrEFull[$rPos-1];
  174. $diffRight = $arrEFull[$rPos] - $r1_ideal;
  175. if ($diffLeft == $diffRight) {
  176. array_push($arrR, array($r1, $arrEFull[$rPos-1]));
  177. array_push($arrR, array($r1, $arrEFull[$rPos])); }
  178. else if ($diffLeft < $diffRight)
  179. array_push($arrR, array($r1, $arrEFull[$rPos-1]));
  180. else if ($diffLeft > $diffRight)
  181. array_push($arrR, array($r1, $arrEFull[$rPos]));
  182. return $arrR;
  183. }
  184. function rAddonParallelGet($r1, $arrR, $diffRight, &$arrEFull)
  185. {
  186. $r1=(float)$r1;
  187. $diffRight=(float)$diffRight;
  188. if(!is_array($arrR)) $arrR=array();
  189. if(!is_array($arrEFull)) return $arrR;
  190. $arrR1 = $arrR;
  191. $r1_ideal = $r1 - $diffRight;
  192. $rAddIdeal = ($r1 * $r1_ideal) / ($r1 - $r1_ideal);
  193. $rAddIndex = rFind($rAddIdeal, $arrEFull);
  194. if ($rAddIndex == -1)
  195. return $arrR1;
  196. if ($rAddIndex == count($arrEFull)-1) {
  197. array_push($arrR1, array($r1,-$arrEFull[$rAddIndex-1]));
  198. return $arrR1; }
  199. if ($rAddIndex == 0) {
  200. array_push($arrR1, array($r1,-(float)1));
  201. return $arrR1; }
  202. $diffLeft = $rAddIdeal - $arrEFull[$rAddIndex-1];
  203. $diffRight = $arrEFull[$rAddIndex] - $rAddIdeal;
  204. if ($diffLeft == $diffRight) {
  205. array_push($arrR1, array($r1,-$arrEFull[$rAddIndex-1]));
  206. array_push($arrR1, array($r1,-$arrEFull[$rAddIndex])); }
  207. else if ($diffLeft < $diffRight)
  208. array_push($arrR1, array($r1,-$arrEFull[$rAddIndex-1]));
  209. else if ($diffLeft > $diffRight)
  210. array_push($arrR1, array($r1,-$arrEFull[$rAddIndex]));
  211. return $arrR1;
  212. }
  213. function r1Expand($r2,$mVin,$mVout_target,&$arrEFull)
  214. {
  215. $r2=(float)$r2;
  216. $mVin=(float)$mVin;
  217. $mVout_target=(float)$mVout_target;
  218. if(!is_array($arrEFull)) return array();
  219. $r1_ideal = $r2 * (($mVin - $mVout_target) / $mVout_target);
  220. $rPos = rFind($r1_ideal, $arrEFull);
  221. $arrR1 = array();
  222. if ($rPos < 0 || $rPos == count($arrEFull)-1) return array();
  223. if ($rPos == 0) return array($arrR1,array((float)1));
  224. if ($rPos == count($arrEFull)-1)
  225. return array($arrR1,array($arrEFull[count($arrEFull)-2]));
  226. $diffLeft = $r1_ideal - $arrEFull[$rPos-1];
  227. $diffRight = $arrEFull[$rPos] - $r1_ideal;
  228. if ($diffLeft == $diffRight) {
  229. array_push($arrR1,array($arrEFull[$rPos-1]));
  230. $arrR1 = $diffLeft > 0
  231. ? rAddonSeriesGet($arrEFull[$rPos-1], $arrR1, $diffLeft, $arrEFull)
  232. : $arrR1;
  233. $arrR1 = $diffRight > 0
  234. ? rAddonParallelGet($arrEFull[$rPos], $arrR1, $diffRight, $arrEFull)
  235. : $arrR1;
  236. array_push($arrR1,array($arrEFull[$rPos])); }
  237. else if ($diffLeft < $diffRight) {
  238. array_push($arrR1,array($arrEFull[$rPos-1]));
  239. $arrR1 = $diffLeft > 0
  240. ? rAddonSeriesGet($arrEFull[$rPos-1], $arrR1, $diffLeft, $arrEFull)
  241. : $arrR1;
  242. $arrR1 =
  243. rAddonParallelGet($arrEFull[$rPos], $arrR1, $diffRight,$arrEFull);}
  244. else if ($diffLeft > $diffRight) {
  245. $arrR1 =
  246. rAddonSeriesGet($arrEFull[$rPos-1], $arrR1, $diffLeft, $arrEFull);
  247. $arrR1 = $diffRight > 0
  248. ? rAddonParallelGet($arrEFull[$rPos], $arrR1, $diffRight, $arrEFull)
  249. : $arrR1;
  250. array_push($arrR1,array($arrEFull[$rPos])); }
  251. return $arrR1;
  252. }
  253. function arrGetResults($mVin, $mVout_target, &$arrEFull, $tolerance, $rtarget)
  254. {
  255. $timeStart = microtime();
  256. if(!is_array($arrEFull)) return array();
  257. if (!is_float($tolerance)) $tolerance=(float)0.1;
  258. $tolerance=(float)$tolerance;
  259. if($tolerance<0||$tolerance>=100) $tolerance=(float)0.1;
  260. $rtarget = (float)floatval($rtarget);
  261. if ($rtarget<0) $rtarget=0;
  262. $multiplier = (float)(1+($tolerance/100));
  263. $mVin=(float)$mVin;
  264. $mVout_target=(float)$mVout_target;
  265. $arrResults=array();
  266. $eCount = count($arrEFull);
  267. if ($mVin <= 0 || $mVout_target <= 0) return array();
  268. else if ($mVin == $mVout_target) return array();
  269. else if ($mVout_target > $mVin) return array();
  270. for ($r2index=0; $r2index<$eCount; $r2index++) {
  271. $r2 = $arrEFull[$r2index];
  272. $arrR1 = r1Expand($r2,$mVin,$mVout_target,$arrEFull);
  273. if ($rtarget != 0) {
  274. $r1_ideal = $r2 * (($mVin - $mVout_target) / $mVout_target);
  275. $rOut_ideal = ($r1_ideal * $r2) / ($r1_ideal + $r2);
  276. if ($rOut_ideal < ($rtarget * 0.925) || $rOut_ideal > ($rtarget * 1.075))
  277. continue; }
  278. for ($matchIndex=0; $matchIndex<count($arrR1); $matchIndex++) {
  279. $r1 = (float)0;
  280. $arrR1part = $arrR1[$matchIndex];
  281. for ($i=0; $i<count($arrR1part); $i++)
  282. $r1 = $arrR1part[$i] >= 0
  283. ? $r1 + $arrR1part[$i]
  284. : ($r1 * (-$arrR1part[$i])) / ($r1 + (-$arrR1part[$i]));
  285. $arrmVout = array();
  286. $arrOhmOut = array();
  287. $arrmAout = array();
  288. $arrErrOut = array();
  289. $arrR = array(
  290. array($r1 * $multiplier, $r2 * ((float)2 - $multiplier)),
  291. array($r1, $r2),
  292. array($r1 * ((float)2 - $multiplier), $r2 * $multiplier));
  293. for ($r=0; $r < count($arrR); $r++) {
  294. $realR1 = $arrR[$r][0];
  295. $realR2 = $arrR[$r][1];
  296. $mVout = ($mVin * $realR2) / ($realR2 + $realR1);
  297. array_push($arrmVout, $mVout);
  298. array_push($arrOhmOut, ($realR1 * $realR2) / ($realR2 + $realR1));
  299. array_push($arrmAout, $mVin / ($realR1 + $realR2));
  300. array_push($arrErrOut,
  301. 100 * (($mVout - $mVout_target) / $mVout_target)); }
  302. array_push(
  303. $arrResults, array(
  304. $arrR1part,$r2,$arrmVout,$arrOhmOut,$arrmAout,$arrErrOut)); } }
  305. $timeEnd = microtime();
  306. array_push($arrResults,
  307. array(
  308. $mVin,
  309. (float)(1000*((float)$timeEnd-(float)$timeStart)),
  310. $tolerance,
  311. $rtarget,
  312. $mVout_target
  313. ));
  314. return $arrResults;
  315. }
  316. function arrGetHumanReadableResults(&$arrResults)
  317. {
  318. if (!is_array($arrResults)) return array();
  319. if (count($arrResults)==0) return array();
  320. $arrReadable =
  321. array(
  322. array(
  323. array("R1","(high)"),
  324. array("R2","(low)"),
  325. array("Output(mV)", "mVin*(r2/(r1+r2))"),
  326. array("Output(Ω)", "(r1*r2)/(r1+r2)"),
  327. array("Output(mA)", "(mVin/(r1+r2))"),
  328. array("Error%", "(V)")));
  329. for ($i=0; $i<count($arrResults)-1; $i++) {
  330. $arrR1 = $arrResults[$i][0];
  331. $series = $parallel = false;
  332. if (count($arrR1)>1) {
  333. if ($arrR1[1]<0) {
  334. $parallel=true;
  335. $arrR1[1]-=2*$arrR1[1]; }
  336. else $series=true; }
  337. $r1 = "";
  338. for ($j=0; $j<count($arrR1) && $j<2; $j++) {
  339. $arrR1[$j]=strResistanceReadable($arrR1[$j]);
  340. $r1 .= ($j>0?" + ":"") . $arrR1[$j]; }
  341. $r2 = strResistanceReadable($arrResults[$i][1]);
  342. $arrmVout = array(
  343. $arrResults[$i][2][0], $arrResults[$i][2][1], $arrResults[$i][2][2]);
  344. $arrOhmOut = array(
  345. $arrResults[$i][3][0], $arrResults[$i][3][1], $arrResults[$i][3][2]);
  346. $arrmAout = array(
  347. $arrResults[$i][4][0], $arrResults[$i][4][1], $arrResults[$i][4][2]);
  348. $arrErrOut = array(
  349. $arrResults[$i][5][0], $arrResults[$i][5][1], $arrResults[$i][5][2]);
  350. for ($arrIndex=0; $arrIndex<count($arrmVout); $arrIndex++) {
  351. $arrmVout[$arrIndex] = (string)fReadableFloat($arrmVout[$arrIndex]);
  352. $arrOhmOut[$arrIndex] = strResistanceReadable($arrOhmOut[$arrIndex]);
  353. $arrmAout[$arrIndex] = (string)fReadableFloat($arrmAout[$arrIndex]);
  354. $arrErrOut[$arrIndex] = (string)fReadableFloat($arrErrOut[$arrIndex]); }
  355. array_push($arrReadable,
  356. array (
  357. array($r1,($series?"(series)":"").($parallel?"(parallel)":"")),
  358. array($r2,""),
  359. array(
  360. $arrmVout[1]."mV",
  361. $arrmVout[0]!=$arrmVout[1]
  362. ? "(".$arrmVout[0]." to ".$arrmVout[2]."mV)"
  363. : ""),
  364. array($arrOhmOut[1],
  365. $arrOhmOut[0]!=$arrOhmOut[1]
  366. ? "(".$arrOhmOut[0]." to ".$arrOhmOut[2].")"
  367. : ""),
  368. array($arrmAout[1]."mA",
  369. $arrmAout[0]!=$arrmAout[1]
  370. ? "(".$arrmAout[0]." to ".$arrmAout[2]."mA)"
  371. : ""),
  372. array($arrErrOut[1]."%",
  373. $arrErrOut[0]!=$arrErrOut[1]
  374. ? "(".$arrErrOut[0]." to ".$arrErrOut[2]."%)"
  375. : "")
  376. )); }
  377. array_push($arrReadable,
  378. array(
  379. (string)fReadableFloat($arrResults[count($arrResults)-1][0])."mV",
  380. (string)fReadableFloat($arrResults[count($arrResults)-1][1])." milliseconds",
  381. (string)fReadableFloat($arrResults[count($arrResults)-1][2])."%",
  382. (string)strResistanceReadable($arrResults[count($arrResults)-1][3]),
  383. (string)fReadableFloat($arrResults[count($arrResults)-1][4])."mV"
  384. )
  385. );
  386. return $arrReadable;
  387. }
  388. function strHtmlResults(&$arrReadable, $ascending, $eSeries)
  389. {
  390. $ascending = is_bool($ascending) ? $ascending : true;
  391. if (!is_array($arrReadable))
  392. return "";
  393. if (count($arrReadable)==0)
  394. return "";
  395. ob_start();
  396. $strROut = $arrReadable[count($arrReadable)-1][3];
  397. if ($strROut == "0Ω") $strROut="";
  398. else $strROut = ", output resistance target ".$strROut;
  399. ?>
  400. <p style="text-align:center;">
  401. <a href="app.php">Click here to reset the form</a>
  402. </p>
  403. <h1 id="computations">Results</h1>
  404. <p>
  405. <?php echo (string)(count($arrReadable)-2); ?> results
  406. computed in <?php echo $arrReadable[count($arrReadable)-1][1]; ?>,
  407. displayed in <?php echo $ascending?"ascending":"descending"; ?> order with
  408. <?php echo $arrReadable[count($arrReadable)-1][0]; ?> input voltage and
  409. <?php echo $arrReadable[count($arrReadable)-1][4]; ?> target.<br/>
  410. Resistor tolerance
  411. <?php echo $arrReadable[count($arrReadable)-1][2]; ?><?php echo $strROut; ?>.
  412. E<?php echo (string)$eSeries; ?> resistor values used.
  413. </p>
  414. <?php
  415. ?><table class="results"><tr><?php
  416. for ($i=0; $i<count($arrReadable[0]) && $i>=0; $i++) { ?>
  417. <th>
  418. <?php echo $arrReadable[0][$i][0]
  419. ."<br/><small>".$arrReadable[0][$i][1]."</small>";
  420. ?></th>
  421. <?php }
  422. ?></tr><?php
  423. for ($i=$ascending?1:count($arrReadable)-1; $i<count($arrReadable)-1 && $i>=1;) {
  424. ?><tr><?php
  425. for ($j=0; $j<count($arrReadable[$i]); $j++) { ?>
  426. <td class="results">
  427. <?php echo
  428. $arrReadable[$i][$j][0]."<br/>"
  429. ."<small>".$arrReadable[$i][$j][1]."</small>";
  430. ?></td> <?php }
  431. ?></tr><?php
  432. $i+=$ascending?1:-1;}
  433. ?></table><?php
  434. $strHtml = ob_get_clean();
  435. return $strHtml;
  436. }
  437. ?>
  438. <!DOCTYPE html>
  439. <html>
  440. <head>
  441. <meta charset="utf-8">
  442. <meta name="generator" content="pandoc">
  443. <meta name="viewport"
  444. content="width=device-width, initial-scale=0.5, user-scalable=yes">
  445. <title>Resistor divider generator</title>
  446. <link rel="stylesheet" href="global.css">
  447. <link rel="stylesheet" href="headercenter.css">
  448. <?php include_once "mobile.php"; ?>
  449. </head>
  450. <body>
  451. <header>
  452. <p>
  453. <img src="diagram.svg" alt="Resistor divider circuit diagram" id="logo" />
  454. <img
  455. src="diagram.svg"
  456. alt="Resistor divider circuit diagram" id="logo" class="logoleft" />
  457. </p>
  458. <h1 class="title">Resistor divider generator</h1>
  459. </header>
  460. <p>
  461. A resistor divider (see diagram) divides a larger voltage into a smaller
  462. one.<br/>
  463. <strong>mVout = (mVin * R2) / (R1 + R2)</strong>
  464. </p>
  465. <p>
  466. This program generates a table of resistor pairs for your circuit.
  467. <a href="../">Click here to return to retrofreedom.com homepage</a>
  468. </p>
  469. <h2>Enter your data here:</h2>
  470. <form method="get" action="app.php#computations">
  471. <table>
  472. <tr>
  473. <th>Input(mV)</th>
  474. <th>Output(mV)</th>
  475. <th>Output(Ω)<br/><small>(optional)</small></th>
  476. <th>Tolerance% (R1/R2)</th>
  477. <th>E series</th>
  478. <th><label for="order">Order</label></th>
  479. </tr>
  480. <tr>
  481. <td class="input">
  482. <input
  483. type="text"
  484. onfocus="this.value=''"
  485. name="vin"
  486. value="<?php if ($mVin!=0) printf("%dmV",$mVin); ?>"
  487. />
  488. </td>
  489. <td class="input">
  490. <input
  491. type="text"
  492. onfocus="this.value=''"
  493. name="vtarget"
  494. value="<?php if ($mVout_target!=0) printf("%dmV",$mVout_target); ?>"
  495. />
  496. </td>
  497. <td class="input">
  498. <input
  499. type="text"
  500. onfocus="this.value=''"
  501. name="rtarget"
  502. value="<?php printf("%s%s",$strRTarget, $strRTarget!=""?"Ω":""); ?>"
  503. />
  504. </td>
  505. <td class="input">
  506. <input
  507. type="text"
  508. onfocus="this.value=''"
  509. name="tolerance"
  510. value="<?php printf("%s%%",(string)$tolerance); ?>"
  511. />
  512. </td>
  513. <td class="input">
  514. <select id="e" name="e">
  515. <option value="24"<?php if ($eSeries=="24") printf(" selected"); ?>>
  516. E24
  517. </option>
  518. <option value="48"<?php if ($eSeries=="48") printf(" selected"); ?>>
  519. E48
  520. </option>
  521. <option value="96"<?php if ($eSeries=="96") printf(" selected"); ?>>
  522. E96
  523. </option>
  524. <option value="192"<?php if ($eSeries=="192") printf(" selected"); ?>>
  525. E192
  526. </option>
  527. </select>
  528. </td>
  529. <td class="input">
  530. <select id="order" name="order">
  531. <option
  532. value="ascending"
  533. <?php if ($order=="ascending") printf(" selected"); ?>>
  534. Ascending
  535. </option>
  536. <option
  537. value="descending"
  538. <?php if ($order=="descending") printf(" selected"); ?>>
  539. Descending
  540. </option>
  541. </select>
  542. </td>
  543. </tr>
  544. </table>
  545. <input class="redbutton" type="submit" value="Click here for results" />
  546. </form>
  547. <?php
  548. $arrResults =
  549. arrGetResults(
  550. $mVin, $mVout_target, $arrEFull, $tolerance, $strRTarget);
  551. $arrReadable = arrGetHumanReadableResults($arrResults);
  552. $strHtml = strHtmlResults($arrReadable, $order == "ascending", $eSeries);
  553. echo $strHtml;
  554. ?>
  555. <h2>About this program</h2>
  556. <p>
  557. Sometimes, you want to precisely tune a signal and to do that, you might
  558. use a resistor divider. Other resistor divider calculators exist, but
  559. they usually give you the most ideal values. In the real world,
  560. resistors come in fixed sizes. With this web application, you can give
  561. a voltage and a desired output voltage, where you will then be given a
  562. precisely calculated list of resistor combinations to use, for actual
  563. real world resistor values, in your divider circuit. To my knowledge,
  564. no other such program existed, simple though it may be. My name is
  565. Leah Rowe, and you can learn more about me from my personal website
  566. at <a href="https://vimuser.org/">https//vimuser.org/</a>.
  567. </p>
  568. <p>
  569. You can learn more about voltage dividers by reading
  570. <a href="https://en.wikipedia.org/wiki/Voltage_divider">the following Web
  571. page on WikiPedia</a>.
  572. </p>
  573. <p>
  574. This program is <a href="https://www.gnu.org/philosophy/free-sw.html">Free
  575. Software</a>, released under the terms of the GNU Affero General Public
  576. License, either version 3 of the license as published by the
  577. <a href="https://www.fsf.org/">Free Software Foundation</a>, or (at your
  578. option) any later version as published by the
  579. <a href="https://www.fsf.org/">Free Software Foundation</a>. A copy of this
  580. license is included; see <a href="COPYING.txt">COPYING.txt</a>.
  581. Source code is available via
  582. <a href="https://notabug.org/vimuser/resistordivider/">https://notabug.org/vimuser/resistordivider/</a>
  583. </p>
  584. <p>
  585. The resistor divider diagram is released under Creative Commons Zero
  586. license version 1.0, as per this wikipedia page where it was found:
  587. <a href="https://en.wikipedia.org/wiki/Voltage_divider#/media/File:Resistive_divider2.svg">https://en.wikipedia.org/wiki/Voltage_divider#/media/File:Resistive_divider2.svg</a>
  588. </p>
  589. </body>
  590. </html>
  591. <?php
  592. ?>