/*									tab:4
 * "Copyright (c) 2000-2003 The Regents of the University  of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice, the following
 * two paragraphs and the author appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Copyright (c) 2002-2003 Intel Corporation
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE
 * file. If you do not find these files, copies can be found by writing to
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, Berkeley, CA,
 * 94704.  Attention:  Intel License Inquiry.
 */
/*
* Modifications
* Copyright (c) 2004-2005, The Hong Kong University of Science and Technology (HKUST)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*   * Redistributions of source code must retain the above copyright notice,
*     this list of conditions,the authors and the following disclaimer.
*   * Redistributions in binary form must reproduce the above copyright notice,
*     this list of conditions,the authors and the following disclaimer in
*     the documentation and/or other materials provided with the distribution.
*   * Neither the name of the university nor the names of its
*     contributors may be used to endorse or promote products derived from
*     this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.*;
/*typedef struct {
	int vref;
    int thermistor;
    int light;
    int mic;
} XSensorMTS300Data;

typedef struct {
    int vref;
    int thermistor;
    int light;
    int mic;
    int accelX;
    int accelY;
    int magX;
    int magY;
} XSensorMTS310Data;*/
public class MTS300{

	static HashMap tempHS=new HashMap();
	static HashMap lightHS=new HashMap();

	public static void create_map(int minTemp, int maxTemp, int minLight, int maxLight)
	{
		for(int i=minTemp;i<maxTemp;i++)
			tempHS.put(new Integer((int)mts300_convert_thermistor_temperature(i)),new Integer(i));
		for(int i=minLight;i<maxLight;i++)
			lightHS.put(new Integer((int)mts300_convert_light(i,3000)),new Integer(i));

	}

/**
 * Converts mica2 battery reading from raw ADC data to engineering units.
 *
 * @author    Martin Turon
 *
 * To compute the battery voltage after measuring the voltage ref:
 *   BV = RV*ADC_FS/data
 *   where:
 *   BV = Battery Voltage
 *   ADC_FS = 1023
 *   RV = Voltage Reference for mica2 (1.223 volts)
 *   data = data from the adc measurement of channel 1
 *   BV (volts) = 1252.352/data
 *   BV (mv) = 1252352/data
 *
 * Note:
 *   The thermistor resistance to temperature conversion is highly non-linear.
 *
 * @version   2004/3/29       mturon      Initial revision
 *
 */
public static double mts300_convert_battery(double vref)
{
    double    x     = (double)vref;
    double vdata = (double) (1252352 / x);
    return vdata;
}


/**
 * Converts thermistor reading from raw ADC data to engineering units.
 *
 * @author    Martin Turon, Alan Broad
 *
 * To compute the thermistor resistance after measuring the thermistor voltage:
 * - Thermistor is a temperature variable resistor
 * - There is a 10K resistor in series with the thermistor resistor.
 * - Compute expected adc output from voltage on thermistor as:
 *       ADC= 1023*Rthr/(R1+Rthr)
 *       where  R1 = 10K
 *              Rthr = unknown thermistor resistance
 *       Rthr = R1*ADC/(ADC_FS-ADC)
 *       where  ADC_FS = 1023
 *
 * Note:
 *   The thermistor resistance to temperature conversion is highly non-linear.
 *
 * @return    Thermistor resistance as a uint16 in unit (Ohms)
 *
 * @version   2004/3/11       mturon      Initial revision
 *
 */
public static double mts300_convert_thermistor_resistance(double thermistor)
{
    double    x     = (double)thermistor;
    double vdata = 10000*x / (1023-x);
    return vdata;
}

/**
 * Converts thermistor reading from raw ADC data to engineering units.
 *
 * @author    Martin Turon
 *
 * @return    Temperature reading from thermistor as a double in degrees Celcius
 *
 * @version   2004/3/22       mturon      Initial revision
 * @version   2004/4/19       husq
 *
 */
public static double mts300_convert_thermistor_temperature(double thermistor)
{
    double temperature, a, b, c, Rt;
    a  = 0.001307050;
    b  = 0.000214381;
    c  = 0.000000093;
    Rt = mts300_convert_thermistor_resistance(thermistor);

    temperature = 1 / (a + b * Math.log(Rt) + c * Math.pow(Math.log(Rt),3));
    temperature -= 273.15;   // Convert from Kelvin to Celcius

    //printf("debug: a=%f b=%f c=%f Rt=%f temp=%f\n",a,b,c,Rt,temperature);

    return temperature;
}

/**
 * Computes the Clairex CL94L light sensor reading
 *
 * @author    Hu Siquan
 *
 * @return    Voltage of ADC channel as an unsigned integer in mV
 *
 * @version   2004/4/19       husq      Initial revision
 *
 */
public static double mts300_convert_light(int light, double vref)
{
    double    Vbat = mts300_convert_battery(vref);
    double Vadc = (double) (light * Vbat / 1023);
    return Vadc;
}



/**
 * Computes the ADC count of a Accelerometer - for X axis reading into
 *  Engineering Unit (g), per calibration
 *
 * Calibration done for one test sensor - should be repeated for each unit.
 * @author    Jaidev Prabhu, Hu Siquan
 *
 * @version   2004/3/24       jdprabhu      Initial revision
 * @n         2004/4/19       husq      adaption to mts310
 *
 */
public static double mts310_convert_accel_x(int data)
{

    int minus_one_calibration;
    int plus_one_calibration;

    float zero_value;
    double reading;

    minus_one_calibration = 490;
    plus_one_calibration = 615;

    zero_value =  ( plus_one_calibration - minus_one_calibration ) / 2;
    reading =   (zero_value - (plus_one_calibration - data) ) / zero_value;

    return reading;
}

/**
 * Computes the ADC count of a Accelerometer - for Y axis reading into
 *  Engineering Unit (g), per calibration
 * Calibration done for one test sensor - should be repeated for each unit.
 *
 * @author     Jaidev Prabhu,Hu Siquan
 *
 * @version   2004/3/24       jdprabhu      Initial revision
 * @version   2004/4/19       husq      adaption to mts310
 *
 */
public static double mts310_convert_accel_y(int data)
{

    int minus_one_calibration;
    int plus_one_calibration;

    float zero_value;
    double reading;

    minus_one_calibration = 432;
    plus_one_calibration = 552;

    zero_value =  ( plus_one_calibration - minus_one_calibration ) / 2;
    reading =   ( zero_value - (plus_one_calibration - data) ) / zero_value;

    return reading;

}
public static int mts300_getraw_temp(int temp)
{
	Integer tmpInt=(Integer)(tempHS.get(new Integer(temp)));
	if(tmpInt==null)
	return 400;
	else if(tmpInt.intValue()<0) return 400;
	else if(tmpInt.intValue()>1024) return 1024;
	else
	return tmpInt.intValue();
}

public static int mts300_getraw_light(int light)
{
	Integer tmpInt=(Integer)(lightHS.get(new Integer(light)));
	if(tmpInt==null)
	return 400;
	else if(tmpInt.intValue()<0) return 400;
	else if(tmpInt.intValue()>1024) return 1024;
	else
	return tmpInt.intValue();
}

public static void main(String [] argv)
{
	MTS300.create_map(100,800,100,1800);
	System.out.println(MTS300.mts300_getraw_light((int)mts300_convert_light(1100,3000)));
}



}