<?php

/*
 *  Copyright (C) 2019 Tony L. Clark
 *  Author:  Tony Clark <tclark@villamontage.com> (2016)
 *
 *  hdc_raw_to_celcius.php -- 
 *
 *  This is the backend tool for converting raw HDC sensor data
 *  into its proper celcius temperature reading.

 */


$scriptName="hdc_raw_to_celcius.php";

$version="0.2.2";
print ("Script name is $scriptName and version is $version <br /><br />");


/* Function to convert raw HDC10xx ambient temp hex string into celcius temperature.
 * Example calls:  $chk_amb_temp = hex2temperature(substr($mood_key,20,4));
 * Takes raw mood_key sensor data; converts LSB and MSB order; converts to binary;
 * right shifts by 2 bits; and divides by 32 to arrive at degrees C value.
 *
 * Calls to function made as follows for examaple:  
 *   http://serverIP/hdc_raw_to_celcius.php?rawValue=205C
 */ 
 
function hex2temperature($hex) {
	$hex = strtoupper($hex);
	$temperature = 0;

	$LSBhex = substr($hex,0,2);  // Isolate raw hex string
	$MSBhex = substr($hex,2,2);
	
	echo "<br />LSB value is ".$LSBhex."<br />";
	echo "<br />MSB value is ".$MSBhex."<br />";

	$MSBLSB = $MSBhex.$LSBhex;
	echo "<br />Converted hex string before binary conversion is ".$MSBLSB."<br />";

	echo "<br />Original raw hex string value was ".$hex."<br />";

	$hexInBinary = "";
	For ($i=0; $i <= (strlen($hex)-1); $i++) {
		
		$hexDigit = substr($hex,$i,1);
		
		switch ($hexDigit): 
		case '0':
			$hexInBinary = $hexInBinary."0000";
			break;
		case '1':
			$hexInBinary = $hexInBinary."0001";
			break;
		case '2':
			$hexInBinary = $hexInBinary."0010";
			break;
		case '3':
			$hexInBinary = $hexInBinary."0011";
			break;
		case '4':
			$hexInBinary = $hexInBinary."0100";
			break;
		case '5':
			$hexInBinary = $hexInBinary."0101";
			break;
		case '6':
			$hexInBinary = $hexInBinary."0110";
			break;
		case '7':
			$hexInBinary = $hexInBinary."0111";
			break;
		case '8':
			$hexInBinary = $hexInBinary."1000";
			break;
		case '9':
			$hexInBinary = $hexInBinary."1001";
			break;
		case 'A':
			$hexInBinary = $hexInBinary."1010";
			break;
		case 'B':
			$hexInBinary = $hexInBinary."1011";
			break;
		case 'C':
			$hexInBinary = $hexInBinary."1100";
			break;
		case 'D':
			$hexInBinary = $hexInBinary."1101";
			break;
		case 'E':
			$hexInBinary = $hexInBinary."1110";
			break;
		case 'F':
			$hexInBinary = $hexInBinary."1111";
			break;
		endswitch;
}
echo "<br />Manually converted hex to binary string is ".$hexInBinary."<br />";

	$revString = "";
	//$hex = double(base_convert($hex, 16, 2));
	echo "<br />Binary version of original raw data before reversing is ".$hexInBinary."<br />";

	$unsignedDec = 0;
	For ($i=0; $i <= (strlen($hexInBinary)-1); $i++) {
		$revString = substr($hexInBinary,$i,1).$revString;
		if (substr($hexInBinary,$i,1) == "1"){$unsignedDec = $unsignedDec + pow(2,$i);}
	}
	echo "<br />Reversed binary version of raw submitted data is ".$revString."<br />";
	echo "<br />Unsigned decimal value of reversed binary string is ".$unsignedDec."<br />";
	//$rawTempBin = base_convert($MSBLSB, 16, 2);
	//echo "<br />Binary version of raw data in corrected order before shifting is ".$rawTempBin."<br />";
	//echo "<br />Binary version of raw data in original format is ".base_convert($hex, 16, 2)."<br />";

	//$rawTempBinShift = substr($rawTempBin, 0, strlen($rawTempBin)-2);
	//echo "<br />Binary value after right shift by 2 is ".$rawTempBinShift."<br />";

	$rawTempDec = ($unsignedDec);  // Converted reversed binary to decimal
	//$rawTempDec = base_convert($revString, 2, 10); // Convert from original binary to decimal

	
	$temperature = (($rawTempDec/65536)*165 - 40);   // Calculate celcius value

	
    return $temperature;
}
// End of hex2temperature function


echo "<hr>";

///////////////////////////////////////////////////////////

$rawValue = $_GET['rawValue'];
$rawValue = trim($rawValue);

$result = hex2temperature($rawValue);

echo "<b>01: The raw hex string value to be converted to celcius is the following:</b><br>";
echo $rawValue;
echo "<br />";

echo "<b>02: The corresponding celcius value is the following:</b><br>";
echo $result;

echo "</br>";

echo "<hr>";

///////////////////////////////////////////////////////////

//End of hdc_raw_to_celcius

?> 
