/*****************************************************************************
 *  File: main.c
 *
 *  This file is the top level of the application selector.
 *
 ****************************************************************************/

// #include <stdio.h>
// #include <stdlib.h>

#include "system.h"

#include <sys/alt_alarm.h>
#include <sys/alt_stdio.h>
// #include "altera_avalon_pio_regs.h"

#include "VIP_ctrl\vip_wrapper_for_c_func.h"
#include "audio_tvdecoder\tvdecoder_ctrl.h"

#define LCD_W 800
#define LCD_H 480

#define FRAME_W 640
#define FRAME_H 480


#define ALARM_CYCLE_FRAME_UPDATE 1 

static volatile int go_frame_update_flag = 0;
alt_u32 frame_alarm_callback(void * context){

	go_frame_update_flag = 1;

  return ALARM_CYCLE_FRAME_UPDATE;
}   

int main()
{

  init_i2c();
  tv_decoder_init();

  Scaler_init();
  
  Mixer_init();


  Mixer_set_layer_position(1, (LCD_W - FRAME_W)/2, (LCD_H - FRAME_H)/2);
  Scaler_set_output_size(FRAME_W, FRAME_H);


  alt_alarm alarm_frame_update;
  if (alt_alarm_start(&alarm_frame_update, ALARM_CYCLE_FRAME_UPDATE, frame_alarm_callback, NULL) < 0 ){ 
    alt_printf("No system Clock\n");
  }

  move_image(0, 0, FRAME_W, FRAME_H, 1);

  extern int motion_count;
  while(1) {
    if(go_frame_update_flag) {
      go_frame_update_flag=0;

      if (motion_count==2) 
        zoom_image(-1,-1,-1,-1,-1);
      else if (motion_count==1) 
        move_image_center(-1,-1,-1,-1,-1);
      else{
        motion_count=0;
        move_image(-1,-1,-1,-1,-1);
      }
    }
  
  }


  return ( 0 );
}
