// --------------------------------------------------------------------
// Copyright (c) 2011 by Terasic Technologies Inc. 
// --------------------------------------------------------------------
//
// Permission:
//
//   Terasic grants permission to use and modify this code for use
//   in synthesis for all Terasic Development Boards and Altera Development 
//   Kits made by Terasic.  Other use of this code, including the selling 
//   ,duplication, or modification of any portion is strictly prohibited.
//
// Disclaimer:
//
//   This VHDL/Verilog or C/C++ source code is intended as a design reference
//   which illustrates how these types of functions can be implemented.
//   It is the user's responsibility to verify their design for
//   consistency and functionality through the use of formal
//   verification methods.  Terasic provides no warranty regarding the use 
//   or functionality of this code.
//
// --------------------------------------------------------------------
//           
//                     Terasic Technologies Inc
//                     356 Fu-Shin E. Rd Sec. 1. JhuBei City,
//                     HsinChu County, Taiwan
//                     302
//
//                     web: http://www.terasic.com/
//                     email: support@terasic.com
//
// --------------------------------------------------------------------


#include "terasic_includes.h"
#include "i2c.h"
#include "system.h"
#include "multi_touch.h"
#include "gui.h"
#include "sys/alt_irq.h"

MTC_INFO *pTouch;
int gui_vpg_mask = 0;

void GUI_VPG(MTC_INFO *pTouch);

void key_isr(){
       gui_vpg_mask = 1;

	   IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BASE,0x01);
}



int main()
{
    
	int error;
    gui_vpg_mask = 0;

    IOWR(LED_BASE, 0x00, 0x3FF); // high-active

    printf("Terasic Painter Demo\r\n");
     
    
    pTouch = MTC_Init(MULTI_TOUCH_BASE, MULTI_TOUCH_IRQ);
    if (!pTouch){
        printf("Failed to init multi-touch\r\n");
    }else{
        printf("Init touch successfully\r\n");
      
    }

   //Register interrupt for key
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(KEY_BASE,0x01);
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(KEY_BASE,0x01);
    error = alt_ic_isr_register(KEY_IRQ_INTERRUPT_CONTROLLER_ID, KEY_IRQ, key_isr,  NULL, NULL);
    //alt_ic_irq_enable (KEY_IRQ_INTERRUPT_CONTROLLER_ID,KEY_IRQ);
    if (error){
        	printf("alt_ic_isr_register failed!\r\n");
        }


    GUI(pTouch);

    while(1){

        if(gui_vpg_mask)
    	 {
      	   GUI_VPG(pTouch);
    	   }
    }

  return 0;
}





