/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */


//
//  main.c
//  WMT
//
//  Created by User on 14/04/2013.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

// input signal x

#include <stdio.h>
#include <system.h>
#include <io.h>
#include "functions.h"


#define random(x) (rand()%x)


int hex_to_ten(int hex)
{
    char buf[100];
    sprintf(buf,"%d",hex);
    return atoi(buf);
}

int mole(int num){
    if (num==1) return 100000000;
    else if (num==2) return 010000000;
    else if (num==3) return 001000000;
    else if (num==4) return 000100000;
    else if (num==5) return 000010000;
    else if (num==6) return 000001000;
    else if (num==7) return 000000100;
    else if (num==8) return 000000010;
    else if (num==9) return 000000001;
    else return 000000000;
}

int touch(){
	int x;
	int y;
	x = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
	y = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
	if(x>0xA10 && x< 0xED0 && y> 0x430 && y< 0x7F0)
		return 0;
	else if(x>0xA10 && x< 0xED0 && y>0x7F1 && y< 0xBB0)
		return 1;
	else if(x>0xA10 && x< 0xED0 && y>0xBB0 && y< 0xF40)
		return 2;
	else if(x>0x530 && x< 0xA10 && y> 0x430 && y< 0x7F0)
		return 3;
	else if(x>0x530 && x< 0xA10 && y>0x7F1 && y< 0xBB0)
		return 4;
	else if(x>0x530 && x< 0xA10 && y>0xBB0 && y< 0xF40)
		return 5;
	else if(x>0x0C0 && x< 0x530 && y> 0x430 && y< 0x7F0)
		return 6;
	else if(x>0x0C0 && x< 0x530 && y>0x7F1 && y< 0xBB0)
		return 7;
	else if(x>0x0C0 && x< 0x530 && y>0xBB0 && y< 0xF40)
		return 8;
	else
		return 9;
}

int mole1(int num){
    if (num==0) return 0x001;
    if (num==1) return 0x002;
    if (num==2) return 0x004;
    if (num==3) return 0x008;
    if (num==4) return 0x010;
    if (num==5) return 0x020;
    if (num==6) return 0x040;
    if (num==7) return 0x080;
    if (num==8) return 0x100;
}


int main (int argc, const char * argv[])
{
	init();
	IOWR_CONTROL(COMMAND_0_BASE,1,0x00000001);
	IOWR_CONTROL(COMMAND_0_BASE,0,0x00000004);
////	init();
//	int state = 0x4;
//	int life = 5;
//	int score0 = 0;
//	int score1 = 0;
//	int score2 = 0;
//    int counter=0;
//    int previousx=0;
//    int previousy=0;
//    int inputx = 0;
//    int inputy = 0;
//    int random_number = 0;
//    long int panel_command;
//  //  int mole_position=100000000;
//    int mole_position=0;
//    int touch_position= 0;
//
//    IOWR_CONTROL(COMMAND_0_BASE,1,0x00000000);
//	previousx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
//    previousy = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
//
//    while(1)
//    {
//    	random_number = rand()%9;
//    	panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//    	IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//    	//if(random_number == 9) random_number = 0;
//    	mole_position=mole1(random_number);
//    	printf("mole_position = 0x%x",mole_position);
//        while(counter<100000){
//            counter++;
//        	inputx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
//        	inputy = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
//        	touch_position = touch();
//            if (inputx!=previousx || inputy != previousy)
//            {
//                if (touch_position==random_number)
//                {	if(score0<9)
//                		score0++;
//                	else if(score1<9)
//                	{
//                		score0 = 0;
//                		score1++;
//                	}
//                	else
//                	{
//						score0 = 0;
//						score1 = 0;
//						score2 ++;
//                	}
//
//
//                    break;
//                }
//                else
//                {
//                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
//                }
//
//            }
//            else
//            {
//            //	printf("E");
//            	IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
//            }
//        	previousx = inputx;
//            previousy = inputy;
//        }
//      //  IOWR_LED_DATA(base,0,000000000);
//        IOWR_CONTROL(COMMAND_0_BASE,1,0x00000000);
//        counter = 0;
//        //random_number++;
//    }

    return 0;
}








