<!DOCTYPE html>
<!--
********************************************************************************

    filename:     dochwvoltagedivider.php
    description:  plot probe temp vs bits of voltage divider
                  for specified fixed resistance

                  Vc---^^^^Rp^^^^---Vo---^^^^Rf^^^^---G

                  I = Vo / Rf
                  Rp = (Vc - Vo) / I
                  Rp = (Vc - Vo) / Vo * Rf
                  Rp = (Vc / Vo - 1) * Rf
                  Vo = Bo / 1024 * Vc
                  Rp = (1024 / Bo - 1) * Rf

                  Vc = supply voltage
                  Vo = measured voltage
                  G = ground
                  Rp = probe resistance
                  Rf = fixed resistance
                  Bo = measured bits (0 - 1023)

    inputs:       probe id, fixed resistance
    outputs:      probe id, fixed resistance
    actions:      design, quit
    redirections: none

********************************************************************************
-->
<html>
    <head>
        <style>
            table {
                border-collapse: collapse;
            }

            table, td, th {
                border: 1px solid black;
            }
        </style>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback(drawChart);
            function drawChart() {
                var data = google.visualization.arrayToDataTable([
            <?php
                $probeId = $_POST["probeId"];
                if (!$probeId) {
                    $probeId = 1;
                }
                $rFixed = $_POST["rFixed"];
                if (!$rFixed) {
                    $rFixed = 10000;
                }
                $values = $_POST["values"];
                if (!$values) {
                   $values = "temps"; 
                }
                if ($values == "temps") {
                   echo "['Bits', 'Temp(F)']";
                } else {
                   echo "['Temp(F)', 'Diff Temp(F)']";
                }
/*
                ... connect to database
*/
                include 'functions.php';
                $conn = connectToDB();
/*
                ... get probe parameters
*/
                $sql = "SELECT manufacturer, model, a, b, c FROM probes WHERE probeId=$probeId";
                $results = mysqli_query($conn, $sql);
                if (mysqli_num_rows($results) > 0) {
                    $row = mysqli_fetch_assoc($results);
                    $manufacturer = $row[manufacturer];
                    $model = $row[model];
                    $a = $row[a];
                    $b = $row[b];
                    $c = $row[c];
                    $lasttF = -459.67;
                    for ($bits = 1; $bits < 1024; $bits++) {
                        $rProbe =  ((1024 / $bits) - 1) * $rFixed;                  
                        $rf = (float)$rProbe;
                        $tK = 1 / ($a + $b * log($rf) + $c * pow(log($rf),3));
                        $tF = tF($tK);
                        if ($values == "temps") {
                            if ($tF > 0 and $tF < 600) {
                                echo ",[" . $bits . ", " . $tF . "]";
                            }
                        } else {
                            $difftF = abs($tF - $lasttF);
                            if ($difftF < 5) {
                                echo ",[" . $tF . ", " . $difftF . "]";
                            }
                            $lasttF = $tF;
                        }
                    }
               }
               mysqli_close($conn);
        echo "]);";

        echo "var options = {";
          if ($values == "temps") {
              echo "title: 'Probe Temperture (F)',";
          } else {
              echo "title: 'Temperture Difference',";
          }
          echo "curveType: 'function',";
          echo "legend: 'none',";
          echo "hAxis: {";
              if ($values == "temps") {
                  echo "title: 'ADC bits'";
              } else {
                  echo "title: 'Probe Temperture (F)'";
              }
          echo "}";
       echo "};";
       ?>
        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }
      </script>
    </head>
    <body>
        <?php include 'navigationmanagehwdocs.html' ?>
        <div id="content">
        <h3>VOLTAGE DIVIDER</h3>
        <?php
            echo "<table>";
                echo "<tr>";
                    echo "<th>PARAMETER</th>";
                    echo "<th>VALUE</th>";
                    echo "<th>DESCRIPTION</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>Probe ID</th>";
                    echo "<th>$probeId</th>";
                    echo "<th>$manufacturer $model</th>";
                echo "</tr>";
                echo"<tr>";
                    echo "<th>Voltage Divider</th>";
                    echo "<th></th>";
                    echo "<th>Vc---^^^^Rp^^^^---Vo/Bo---^^^^Rf^^^^---G</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>Vc</th>";
                    echo "<th>3.3 volts</th>";
                    echo "<th>Supply Voltage</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>Rp</th>";
                    echo "<th></th>";
                    echo "<th>Probe Resistance</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>Vo</th>";
                    echo "<th>0-3.3 volts</th>";
                    echo "<th>Output Voltage</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>Bo</th>";
                    echo "<th>0-1023 bits</th>";
                    echo "<th>10 bit ADC</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>Rf</th>";
                    echo "<th>$rFixed ohms</th>";
                    echo "<th>Fixed Resistance</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>G</th>";
                    echo "<th></th>";
                    echo "<th>Ground</th>";
                echo "</tr>";
            echo "</table>";
        ?>
        </div>
        <div id="curve_chart" style="width: 900px; height: 500px"></div>
        <div id="content">
            <?php
/*
                ... form actions: probe, fixed resistance, chart values (temp or diff)
*/
                echo '<form action="dochwvoltagedivider.php" method="post">';
/*
                    ... probe selection
*/
                    echo '<select name="probeId">';
                        $probes = getProbes();
                        foreach($probes as $probe) {
                            $probeId = $probe['probeId'];
                            if ($probeId == $selectedProbeId) {
                                echo '<option value="' . $probe['probeId'] . ' selected">' . $probe['manufacturer'] . ' ' . $probe['model'] . '</option>';
                            } else {
                                echo '<option value="' . $probe['probeId'] . '">' . $probe['manufacturer'] . ' ' . $probe['model'] . '</option>';
                            }
                        }
                echo '</select>';
/*
                ... fixed resistance selection
*/
                echo '<select name="rFixed">';
                    for ($i = 1000; $i <= 100000; $i+=1000) {
                        if ($i == $rFixed) {
                            echo '<option value="' . $i . '" selected>' . $i . '</option>';
                        } else {
                            echo '<option value="' . $i . '">' . $i . '</option>';
                        }
                    }
                echo '</select>';
/*
                ... chart values (temps or diffs)
*/
                echo '<select name="values">';
                   if ($values == "temps") {
                       echo '<option value="temps" selected>Temps</option>';
                   } else {
                       echo '<option value="temps">Temps</option>';
                   }
                   if ($values == "diffs") {
                       echo '<option value="diffs" selected>Diffs</option>'; 
                   } else {
                       echo '<option value="diffs">Diffs</option>'; 
                   }
                echo '</select>';
                echo '<input type="submit" name="chart" value="Chart"> ';
            echo '</form>';
            echo "<br>";
/*
        ... list temps and diffs by ADC bits
*/
            echo "<table>";
                echo "<tr>";
                    echo "<th>Bits</th>";
                    echo "<th>Temp(F)</th>";
                    echo "<th>Diff</th>";
                echo "</tr>";
                $lasttF = -459.67;
                for ($bits = 0; $bits < 1024; $bits++) {
                    if ($bits > 0) {
                        $rProbe =  ((1024 / $bits) - 1) * $rFixed;                  
                        $rf = (float)$rProbe;
                        $tK = 1 / ($a + $b * log($rf) + $c * pow(log($rf),3));
                        $tF = tF($tK);
                    } else {
                        $tF = -459.67;
                    }
                    echo "<tr>";
                        echo "<th>" . $bits . "</th>";
                        echo "<th>" . number_format($tF, 1) . "</th>";
                        $difftF = abs($tF - $lasttF);
                        echo "<th>" . number_format($difftF, 1) . "</th>";
                        $lasttF = $tF;
                    echo "<tr>";
                }
            echo "</table>";
            ?>
        </div>
    </body>
</html>