/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "xparameters.h"
#include "xil_cache.h"
#include "xiomodule.h"
//#include "iomodule_header.h"
#include "xil_exception.h"
//#include "iomodule_intr_header.h"
//#include "uartlite_header.h"
int main()
{
	u32 data, ADC_reading, Degree_Symbol, Selected_Degree, Sign_Data;
	float Voltage_mV, Temperature_C, Temperature_F;
	u32 Temperature_out;
	int delay_count;

	XIOModule gpi;
	XIOModule gpo;

	// Initialize GPIO's
	data = XIOModule_Initialize(&gpi, XPAR_IOMODULE_0_DEVICE_ID);
	data = XIOModule_Start(&gpi);

	data = XIOModule_Initialize(&gpo, XPAR_IOMODULE_0_DEVICE_ID);
	data = XIOModule_Start(&gpo);

	while(1)
	{
		ADC_reading = XIOModule_DiscreteRead(&gpi, 1); // Read the ADC value
		ADC_reading = ADC_reading >> 4;		// shift out the 4 lsb's

		Selected_Degree = XIOModule_DiscreteRead(&gpi, 2); // Read the slide switch to determine C or F

		Voltage_mV = (float)(3.3/4095) * (float)ADC_reading * 1000;	// determine voltage reading

		Temperature_C = (Voltage_mV - 500) / 10; // Calculate the Temperature in C
		Degree_Symbol = 70;	// Set degree to C

		if(Temperature_C < 0)
		{
			Sign_Data = 63; // set to negative
			Temperature_C = Temperature_C * -1;
		}

		Temperature_out = Temperature_C * 100;

		if(Selected_Degree == 1)
		{
			// Farenheight
			Temperature_F = (Temperature_C * 1.8) + 32;
			Degree_Symbol = 14;	// set to degree F
			Sign_Data = 127;	// set to positive

			if(Temperature_F < 0)
			{
				Sign_Data = 63;	// set to negative
				Temperature_F = Temperature_F * -1; // get the absolute value of temp F
			}

			Temperature_out = Temperature_F * 100;
		}

		// Write to IO Modules
		XIOModule_DiscreteWrite(&gpo, 1, Sign_Data);		// Sign data
		XIOModule_DiscreteWrite(&gpo, 2, Temperature_out);		// Temperature Data
		XIOModule_DiscreteWrite(&gpo, 3, Degree_Symbol);		// Degree Symbol

		// 0.5s Delay
		delay_count = 0;

		while(delay_count < 50000000)
			delay_count++;

	}

   return 0;
}
