/*******************************************************************************
*
*     XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
*     SOLELY forUSE IN DEVELOPING PROGRAMS AND SOLUTIONS FOR
*     XILINX DEVICES.  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 forOBTAINING ANY RIGHTS YOU MAY REQUIRE
*     forYOUR 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
*     forA PARTICULAR PURPOSE.
*     
*     (c) Copyright 2004 Xilinx, Inc.
*     All rights reserved.
*
******************************************************************************/

#include "globaltypes.h"
#include "sleep.h"
#include "xrom_lcd.h"
#include "xgpio_l.h"
#include "xparameters.h"
#include "xrom_push_button.h"

#define GPIO_0_BASEADDR XPAR_OPB_GPIO_0_BASEADDR
#define ERROR_LED_BASEADDR 0x9000000C

void XromLedInit(void)
{
  XGpio_mSetDataDirection(GPIO_0_BASEADDR, 1, 0x00000000);
  XGpio_mSetDataDirection(ERROR_LED_BASEADDR, 1, 0x00000000);
}

void XromTestLed(void)
{
  int i, moveRight;
  unsigned long value, printvalue;

  value = 0x00000001;
  moveRight = 0;
  i = 0;

  XromPBsInit();	
	xil_printf("You should see the LEDs move in a pattern\r\n");
	xil_printf("Press ESC to return to the main menu\r\n");

  for (;;)
  {
	usleep(250000);

	if(value == 0x00000001){
		moveRight = 0;
		value = value*2;
	}

	else if(value == 0x00000008){
		moveRight = 1;
		value = value/2;
	}

	else{
		if(moveRight) value = value /2;
		else	value = value *2;
	}
		
	switch(i){
	case 0:	printvalue = value | 0x00000100; break;
	case 1:	printvalue = value | 0x00000010; break;
	case 2:	printvalue = value | 0x00000040; break;
	case 3:	printvalue = value | 0x00000020; break;
	case 4:	printvalue = value | 0x00000010; break;
	case 5:	printvalue = value | 0x00000080; break;
	}

	if(i%2) {XGpio_mSetDataReg(ERROR_LED_BASEADDR, 1, 0x00000018);}
	else {XGpio_mSetDataReg(ERROR_LED_BASEADDR, 1, 0x00000024);}

	if(i==5) i=0;
	else i++;

    XGpio_mSetDataReg(GPIO_0_BASEADDR, 1, printvalue);

	if(WaitForEscape()) break;
  }
	
  XGpio_mSetDataReg(GPIO_0_BASEADDR, 1, 0x0);
  XGpio_mSetDataReg(ERROR_LED_BASEADDR, 1, 0x00000014);
  XGpio_mSetDataDirection(ERROR_LED_BASEADDR, 1, 0x0000003C);

}
