/*
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A 
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR 
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION 
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE 
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO 
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO 
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE 
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 */

/*
 * 
 *
 * This file is a generated sample test application.
 *
 * This application is intended to test and/or illustrate some 
 * functionality of your system.  The contents of this file may
 * vary depending on the IP in your system and may use existing
 * IP driver functions.  These drivers will be generated in your
 * SDK application project when you run the "Generate Libraries" menu item.
 *
 */

#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() 
{

	//init_platform();

	u32 data, ADC_reading, Degree_Symbol, Selected_Degree, Sign_Data;
	float Voltage_mV, Temperature_C, Temperature_F;
	u32 Temperature_out;

	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
		Selected_Degree = XIOModule_DiscreteRead(&gpi, 1); // 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
			Temperture_C = Temperature_C * -1;
		}


		if(Selected_Degree == 1)
		{
			// Farenheight

			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;
		}


		// 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

		// Delay

	}

   static XIOModule intc;
   static XIOModule iomodule_0_IOModule;
   Xil_ICacheEnable();
   Xil_DCacheEnable();
   print("---Entering main---\n\r");
   
   {
      XStatus status;

      print("\r\nRunning IOModuleSelfTestExample() for iomodule_0...\r\n");

      status = IOModuleSelfTestExample(XPAR_IOMODULE_0_DEVICE_ID);

      if (status == 0) {
         print("IOModuleSelfTestExample PASSED\r\n");
      }
      else {
         print("IOModuleSelfTestExample FAILED\r\n");
      }
   }
   {
      XStatus status;

      print("\r\nRunning Interrupt Test for iomodule_0...\r\n");

      status = IOModuleIntrExample(&iomodule_0_IOModule, XPAR_IOMODULE_0_DEVICE_ID);

      if (status == 0) {
         print("IOModule Interrupt Test PASSED\r\n");
      } 
      else {
         print("IOModule Interrupt Test FAILED\r\n");
      }
   }
   

   {
      int status;
      
      print("\r\nRunning UartLiteSelfTestExample() for debug_mdm_0...\r\n");
      status = UartLiteSelfTestExample(XPAR_DEBUG_MDM_0_DEVICE_ID);
      if (status == 0) {
         print("UartLiteSelfTestExample PASSED\r\n");
      }
      else {
         print("UartLiteSelfTestExample FAILED\r\n");
      }
      
   }
   print("---Exiting main---\n\r");
   Xil_DCacheDisable();
   Xil_ICacheDisable();
   return 0;
}
