/* 
Simple example of MB program by GS 
Reads Switches and write in leds

for Vitis Unified and classic

*/

#include <stdio.h>
#include "xparameters.h"
#include "xgpio.h"
#include "xtmrctr.h"

XGpio MyLeds, *MyLedsPtr;
XGpio MySw, *MySwPtr;
XTmrCtr TmrCtrInstancePtr;

int main() 
{
   
   print("\n\n\r---Entering main, Course SoC-Ess feb 2025---\n\r");
   print("---          Press 's' stop           ---\n\r");

   int status;
   unsigned int time1;
   u32 value;
   char ch = 'n';

   MyLedsPtr = &MyLeds;
   MySwPtr = &MySw;
   
   #ifdef SDT // Vitis Unified
   status = XGpio_Initialize(MyLedsPtr, XPAR_LEDS_BASEADDR);
   if (status != XST_SUCCESS) return XST_FAILURE;
   XGpio_SetDataDirection(MyLedsPtr, 1, 0x0);

   status = XGpio_Initialize(MySwPtr, XPAR_SWITCHES_BASEADDR);
   if (status != XST_SUCCESS) return XST_FAILURE;
   XGpio_SetDataDirection(MySwPtr, 1, 0xFFFFFFFF);

   status = XTmrCtr_Initialize(&TmrCtrInstancePtr, XPAR_AXI_TIMER_0_BASEADDR);
   if (status != XST_SUCCESS) return XST_FAILURE;
   XTmrCtr_Start(&TmrCtrInstancePtr, 0);
   #else //Vitis classic
   status = XGpio_Initialize(MyLedsPtr, XPAR_LEDS_DEVICE_ID);
   if (status != XST_SUCCESS) return XST_FAILURE;
   XGpio_SetDataDirection(MyLedsPtr, 1, 0x0);

   status = XGpio_Initialize(MySwPtr, XPAR_SWITCHES_DEVICE_ID);
   if (status != XST_SUCCESS) return XST_FAILURE;
   XGpio_SetDataDirection(MySwPtr, 1, 0xFFFFFFFF);

   status = XTmrCtr_Initialize(&TmrCtrInstancePtr, XPAR_AXI_TIMER_0_DEVICE_ID);
   if (status != XST_SUCCESS) return XST_FAILURE;
   XTmrCtr_Start(&TmrCtrInstancePtr, 0);
   #endif

   while (ch != 's'){
	   value = XGpio_DiscreteRead(MySwPtr, 1);
	   time1 = XTmrCtr_GetValue(&TmrCtrInstancePtr, 0);
	   xil_printf("SWitches %d ---  Time: %u      \r", value, time1);
	   XGpio_DiscreteWrite(MyLedsPtr, 1, value);
	   //ch = getchar();
	   //ch = inbyte();
	   //xil_printf("\n\rPressed Key: %c\n\r", ch);
   }
   
   print("\n\r---Exiting main---\n\r");

   return 0;
}
