<?php

include "libraries/func.php";
$reg82 = 4688;
$reg85 = 257;
$bits = 12;
$lvds = 300000000.0;
for ($time = 100; $time < 10000000; $time *= 3) {
    $reg = CalcExposureRegisters($time, $reg82, $reg85, $bits, $lvds);
    $ns0 = CalcExposureTime($reg[1] * 65536 + $reg[0], $reg82, $reg85, $bits, $lvds);
    $new = CalcExposureRegisters($ns0, $reg82, $reg85, $bits, $lvds);
    printf("%04X %04X %04X %04X %04X %04X %10d %10d %d %d\n", $reg[0], $reg[1], $new[0], $new[1], $reg82, $reg85, $time, $ns0, $bits, $lvds);
}
     echo ">Image flipping in X and Y</option>\n\t\t\t\t</select></td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"flippingapply\" name=\"flippingapply\"></td></tr>";
 }
 if ($i == 72) {
     $exposure_ns = CalcExposureTime(hexdec($registers[$i]) * 65536 + hexdec($registers[$i - 1]), $registers[82], $registers[85], 12, 250000000);
     echo "<tr class=\"success\"><td></td>\n\t\t\t\t<td>Exposure Time</td>\n\t\t\t\t<td>" . round($exposure_ns, 3) . " ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"text\" id=\"exptime\" name=\"exptime\" size=\"8\" value=\"" . round($exposure_ns, 3) . "\"> ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"exptimeapply\" name=\"exptimeapply\"></td></tr>";
 }
 if ($i == 74) {
     $exposure_ns = CalcExposureTime(hexdec($registers[$i]) * 65536 + hexdec($registers[$i - 1]), $registers[82], $registers[85], 12, 250000000);
     echo "<tr class=\"success\"><td></td>\n\t\t\t\t<td>Exposure Time 2</td>\n\t\t\t\t<td>" . round($exposure_ns, 3) . " ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"text\" id=\"exptime2\" name=\"exptime2\" size=\"8\" value=\"" . round($exposure_ns, 3) . "\"> ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"exptime2apply\" name=\"exptime2apply\"></td></tr>";
 }
 if ($i == 76) {
     $exposurekp1_ns = CalcExposureTime(hexdec($registers[$i]) * 65536 + hexdec($registers[$i - 1]), $registers[82], $registers[85], 12, 250000000);
     echo "<tr class=\"success\"><td></td>\n\t\t\t\t<td>Exposure Time Kneepoint 1</td>\n\t\t\t\t<td>" . round($exposurekp1_ns, 3) . " ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"text\" id=\"exptimekp1\" name=\"exptimekp1\" size=\"8\" value=\"" . round($exposurekp1_ns, 3) . "\"> ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"exptimekp1apply\" name=\"exptimekp1apply\"></td></tr>";
 }
 if ($i == 78) {
     $exposurekp2_ns = CalcExposureTime(hexdec($registers[$i]) * 65536 + hexdec($registers[$i - 1]), $registers[82], $registers[85], 12, 250000000);
     echo "<tr class=\"success\"><td></td>\n\t\t\t\t<td>Exposure Time Kneepoint 2</td>\n\t\t\t\t<td>" . round($exposurekp2_ns, 3) . " ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"text\" id=\"exptimekp2\" name=\"exptimekp2\" size=\"8\" value=\"" . round($exposurekp2_ns, 3) . "\"> ms</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"exptimekp2apply\" name=\"exptimekp2apply\"></td></tr>";
 }
 if ($i == 102) {
     echo "<tr class=\"success\"><td></td>\n\t\t\t\t<td>Black Sun Protection</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td>Default: 120</td>\n\t\t\t\t<td><input type=\"text\" id=\"blacksun\" name=\"blacksun\" size=\"8\" value=\"" . GetBlackSunProtection() . "\"></td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"blacksunapply\" name=\"blacksunapply\"></td></tr>";
 }
 if ($i == 106) {
     $hdrvoltage2enabled = ExtractBits($registers[$i], 6);
     $hdrvoltage3enabled = ExtractBits($registers[$i], 13);
     $hdrvoltage2 = ExtractBits($registers[$i], 0, 6);
     $hdrvoltage3 = ExtractBits($registers[$i], 7, 6);
     echo "<tr class=\"success\"><td></td>\n\t\t\t\t<td>HDR Voltage Level 2 Enabled</td>\n\t\t\t\t<td>" . ($hdrvoltage2enabled ? 'enabled' : 'disabled') . "</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"text\" id=\"Vtfl2en\" name=\"Vtfl2en\" size=\"8\" value=\"" . ($hdrvoltage2enabled ? '1' : '0') . "\"></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"Vtfl2enapply\" name=\"Vtfl2enapply\"></td></tr>\n\t\t\t\t<tr class=\"success\"><td></td>\n\t\t\t\t<td>HDR Voltage Level 2 </td>\n\t\t\t\t<td>" . $hdrvoltage2 . "</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td>Range: 0-63</td>\n\t\t\t\t<td><input type=\"text\" id=\"Vtfl2\" name=\"Vtfl2\" size=\"8\" value=\"" . $hdrvoltage2 . "\"></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"Vtfl2apply\" name=\"Vtfl2apply\"></td></tr>\n\t\t\t\t<tr class=\"success\"><td></td>\n\t\t\t\t<td>HDR Voltage Level 3 Enabled</td>\n\t\t\t\t<td>" . ($hdrvoltage3enabled ? 'enabled' : 'disabled') . "</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><input type=\"text\" id=\"Vtfl3en\" name=\"Vtfl3en\" size=\"8\" value=\"" . ($hdrvoltage3enabled ? '1' : '0') . "\"></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"Vtfl3enapply\" name=\"Vtfl3enapply\"></td></tr>\n\t\t\t\t<tr class=\"success\"><td></td>\n\t\t\t\t<td>HDR Voltage Level 3 </td>\n\t\t\t\t<td>" . $hdrvoltage3 . "</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td>Range: 0-63</td>\n\t\t\t\t<td><input type=\"text\" id=\"Vtfl3\" name=\"Vtfl3\" size=\"8\" value=\"" . $hdrvoltage3 . "\"></td>\n\t\t\t\t<td><input type=\"checkbox\" id=\"Vtfl3apply\" name=\"Vtfl3apply\"></td></tr>";
 }
 if ($i == 115) {
     echo "<tr class=\"success\"><td></td>\n\t\t\t\t<td>Analog Gain</td>\n\t\t\t\t<td></td>\n\t\t\t\t<td></td>\n\t\t\t\t<td><select name=\"gain\" id=\"gain\">\n\t\t\t\t\t<option value=\"0\" ";
     if ($registers[115] == 0) {
function GetExposureTime()
{
    $registers = GetRegisters();
    return CalcExposureTime(hexdec($registers[72]) * 65536 + hexdec($registers[71]), $registers[82], $registers[85], 12, 250000000);
}
        }
        if ($_POST["VTFL3en"] > 1) {
            $_POST["VTFL3en"] = 1;
        }
        $Vtfl2en = $_POST["VTFL2en"];
        $Vtfl3en = $_POST["VTFL3en"];
        $Vtfl2 = $_POST["VTFL2"];
        $Vtfl3 = $_POST["VTFL3"];
        $tmpreg = $Vtfl3en * pow(2, 13) + $Vtfl3 * pow(2, 7) + $Vtfl2en * pow(2, 6) + $Vtfl2;
        SetRegisterValue(106, $tmpreg);
        $registers[106] = strtoupper(dechex($tmpreg));
    }
}
$exposure1_ns = CalcExposureTime(hexdec($registers[72]) * 65536 + hexdec($registers[71]), $registers[82], $registers[85], 12, 250000000);
$exposure2_ns = CalcExposureTime(hexdec($registers[76]) * 65536 + hexdec($registers[75]), $registers[82], $registers[85], 12, 250000000);
$exposure3_ns = CalcExposureTime(hexdec($registers[78]) * 65536 + hexdec($registers[77]), $registers[82], $registers[85], 12, 250000000);
$PLR_exp2 = $exposure2_ns / $exposure1_ns;
//range 0..1 fraction of exposure time 1
$PLR_exp3 = $exposure3_ns / $exposure1_ns;
//range 0..1 fraction of exposure time 1
$hdrvoltage2enabled = ExtractBits($registers[106], 6);
$hdrvoltage3enabled = ExtractBits($registers[106], 13);
$PLR_vtfl2 = ExtractBits($registers[106], 0, 6);
//range 0..63
$PLR_vtfl3 = ExtractBits($registers[106], 7, 6);
//range 0..63
$slopes = hexdec($registers[79]);
// angle of the first part of the response curve in degrees
$alpha = 45;
$alpha = $alpha / 180 * M_PI;
// convert to RAD