/*!
	Linear Technology Low Pass Filter Example
	LTC2990: 14-bit ADC Quad I2C Voltage, Current, and Temperature monitor.

	@verbatim

	USER INPUT DATA FORMAT:
	decimal : 1024
	hex     : 0x400
	octal   : 02000  (leading 0 "zero")
	binary  : B10000000000
	float   : 1024.0

	@endverbatim

	http://www.linear.com/product/LTC2990

	http://www.linear.com/product/LTC2990#demoboards

	Copyright (c) 2014, Linear Technology Corp.(LTC)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice, this
	list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	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.

	The views and conclusions contained in the software and documentation are those
	of the authors and should not be interpreted as representing official policies,
	either expressed or implied, of Linear Technology Corp.

	The Linear Technology Linduino is not affiliated with the official Arduino team.
	However, the Linduino is only possible because of the Arduino team's commitment
	to the open-source community.  Please, visit http://www.arduino.cc and
	http://store.arduino.cc , and consider a purchase that will help fund their
	ongoing work.
	*/

	/*! @file
	@ingroup LTC2990
	*/

	#include <arduino.h>
	#include <stdint.h>
	#include "Linduino.h"
	#include "UserInterface.h"
	#include "LT_I2C.h"
	#include "QuikEval_EEPROM.h"
	#include "LTC2990.h"
	#include <wire.h>
	#include <spi.h>

	// Function Declaration
	void print_title();                     // Print the title block
	void print_prompt();                    // Prompt the user for an input command

	int8_t menu_1_single_ended_voltage();   // Sub-menu functions
	int8_t menu_2_read_differential_voltage();
	int8_t menu_3_read_temperature();
	int8_t menu_4_settings();

	// Global variables
	static uint8_t demo_board_connected;    //!< Set to 1 if the board is connected
	const uint16_t LTC2990_TIMEOUT=1000;    //!< Configures the maximum timeout allowed for an LTC2990 read.

	// Calibration Variables
	const float LTC2990_SINGLE_ENDED_lsb = 3.05176E-04;     //!< Typical single-ended LSB weight in volts
	const float LTC2990_DIFFERENTIAL_lsb = 1.90735E-05;     //!< Typical differential LSB weight in volts
	const float LTC2990_VCC_lsb = 3.05176E-04;              //!< Typical VCC LSB weight in volts
	// Used for internal temperature as well as remote diode temperature measurements.
	const float LTC2990_TEMPERATURE_lsb = 0.0625;           //!< Typical temperature LSB weight in degrees Celsius (and Kelvin).
	// Used to readback diode voltage when in temperature measurement mode.
	const float LTC2990_DIODE_VOLTAGE_lsb = 3.815E-05;      //!< Typical remote diode LSB weight in volts.

	float sensorReading = 0;
	float movingAverage = 0;      //!< Initial Seed Value for Low Pass Filter. This was set to 21.
	boolean isKelvin = false;     // Keeps track of the unit of measurement

	//! Initialize Linduino
	void setup()
	{
		char demo_name[] = "DC1338";  // Demo Board Name stored in QuikEval EEPROM
		int8_t ack=0;
		quikeval_I2C_init();          // Initializes Linduino I2C port.
		quikeval_I2C_connect();       // Connects I2C port to the QuikEval connector
		Serial.begin(115200);         // Initialize the serial port to the PC
		print_title();
		demo_board_connected = discover_demo_board(demo_name);  // Checks if correct demo board is connected.
		if (demo_board_connected)
		{
			print_prompt();
			ack |= LTC2990_register_write(LTC2990_I2C_ADDRESS, LTC2990_CONTROL_REG, LTC2990_ENABLE_ALL);   // Enables all channels
			ack |= LTC2990_register_write(LTC2990_I2C_ADDRESS, LTC2990_TRIGGER_REG, 0x01);
		}

	}

	//! Repeats Linduino loop
void loop()
{
	int8_t ack=0;
	uint8_t user_command;
	if(demo_board_connected)
	{

		if(Serial.available())
		{
			// Displays temperature menu
			user_command = read_int();                                  // Reads the user command
			if (user_command != 'm')
				Serial.println(user_command);
			ack = 0;
			switch (user_command)           // Prints the appropriate submenu
			{
				case 1:
				ack |= temperature_measurement_V1_V2();       // Print single-ended voltage menu
				break;
				case 2:
				ack |= temperature_measurement_V1_V2_low_pass();
				default:
				Serial.println("Incorrect Option");
				break;
			}
			if (ack != 0)
			{
				Serial.print(F("Error: No Acknowledge. Check I2C Address.\n"));
			}
			print_prompt();

		}
	}	
}



uint8_t  temperature_measurement_V1_V2(){

	uint8_t ack = 0;
	uint8_t user_command;
	ack |= LTC2990_register_set_clear_bits(LTC2990_I2C_ADDRESS, LTC2990_CONTROL_REG, LTC2990_TR1_TR2, LTC2990_VOLTAGE_MODE_MASK);

	do
	{
		int8_t data_valid = 0;
		int16_t adc_code;
		uint8_t reg_data;
		float temperature;
	 // Flush one ADC reading in case it is stale.  Then, take a new fresh reading.
		ack |= LTC2990_adc_read_new_data(LTC2990_I2C_ADDRESS, LTC2990_V1_MSB_REG, &adc_code, &data_valid, LTC2990_TIMEOUT);
		ack |= LTC2990_register_read(LTC2990_I2C_ADDRESS, LTC2990_CONTROL_REG, &reg_data);
		if (reg_data & LTC2990_KELVIN_ENABLE) isKelvin= true;
		else isKelvin=false;   
		temperature = LTC2990_temperature(adc_code, LTC2990_TEMPERATURE_lsb, isKelvin);
		Serial.print(F("\n  V1-V2: "));
		Serial.print(temperature, 2);  
		if (isKelvin) Serial.print(F(" K\n"));
		else Serial.print(F(" C\n"));

	}
	while ((Serial.available()==false)); 
	read_int();
	return(ack);
}


uint8_t temperature_measurement_V1_V2_low_pass() {

	uint8_t N = 5; //Number of samples
	filteredReading = 0;

	do{

		int8_t data_valid = 0;
		int16_t adc_code;
		uint8_t reg_data;
		float temperature;
	 // Flush one ADC reading in case it is stale.  Then, take a new fresh reading.
		ack |= LTC2990_adc_read_new_data(LTC2990_I2C_ADDRESS, LTC2990_V1_MSB_REG, &adc_code, &data_valid, LTC2990_TIMEOUT);
		ack |= LTC2990_register_read(LTC2990_I2C_ADDRESS, LTC2990_CONTROL_REG, &reg_data);
		if (reg_data & LTC2990_KELVIN_ENABLE) isKelvin= true;
		else isKelvin=false;   
		temperature = LTC2990_temperature(adc_code, LTC2990_TEMPERATURE_lsb, isKelvin);

		filteredReading = filteredReading + temperature - filteredReading/N;

		Serial.print(F("\n Filtered Temperature V1-V2: "));
		Serial.print(filteredReading/N, 2);  
		if (isKelvin) Serial.print(F(" K\n"));
		else Serial.print(F(" C\n"));

	}
	while((Serial.available() == false));
	read_int();
	return(ack);
}



	// Function Definitions

	//! Prints the title block when program first starts.
void print_title()
{
	Serial.print(F("\n*****************************************************************\n"));
	Serial.print(F("* DC1338B Demonstration Program                                 *\n"));
	Serial.print(F("*                                                               *\n"));
	Serial.print(F("* This program demonstrates how to implement a low pass filter  *\n"));
	Serial.print(F("* using the LTC2990 14-Bit Quad I2C Voltage, Current, and       *\n"));
	Serial.print(F("* Temperature Monitor. The program allows filtered and          *\n"));
	Serial.print(F("* unfiltered measurement from a remote diode connected to V1-V2.*\n"));
	Serial.print(F("*                                                               *\n"));
	Serial.print(F("* Set the baud rate to 115200 and select the newline terminator.*\n"));
	Serial.print(F("*                                                               *\n"));
	Serial.print(F("*****************************************************************\n"));
}

	//! Prints main menu.
void print_prompt()
{
	Serial.print(F("\n Start Temperature Measurement\n\n"));
	Serial.print(F("  1-V1-V2\n"));
	Serial.print(F("  2- Filtered Temperature V1-V2\n"));
	Serial.print(F("Enter a command:"));
}