#include <stdio.h>
#include <time.h>
#include "uc1394.h"

#define DEBUG 1

#define DATA_SIZE 4096 // in bytes:  81171200 for .9 meter, 8820000 for 1.8 meter

// private function prototypes
int set_up_uc1394_interfaces(void);
int set_uc1394_streaming_recv(void);
int set_uc1394_streaming_tran(void);
int do_uc1394_streaming(void);
int turn_on_io_pin(struct interface);
int turn_off_io_pin(struct interface);
int display_data_segment(int, int);

// private varables
struct interface streaming_port, iso_ack; //used for the streaming port
struct interface io_pin8;
unsigned int *data;

int main()
{
  int i;
  printf("UC1394 controller\n");
  set_up_uc1394_interfaces();

  do{
    printf("Options:\n");
    printf("1) Recieve Data \t2) Send Data\n");
    printf("3) Turn on Pin 8\t4) Turn off Pin 8\n");
    printf("5) Display Data\n");
    printf("0) Quit\n");

    scanf("%d", &i);
    switch(i){
    case 0:
      UC1394_close();
      return 0;
      break;
    case 1:
      if(set_uc1394_streaming_recv())
	do_uc1394_streaming();
      printf("Streaming done\n");
      break;
    case 2:
      if(set_uc1394_streaming_tran())
	do_uc1394_streaming();
      printf("Streaming done\n");
    case 3:
      turn_on_io_pin(io_pin8);
      printf("IO pin 8 on\n");
      break;
    case 4:
      turn_off_io_pin(io_pin8);
      printf("IO pin 8 off\n");
      break;
    case 5:
      display_data_segment(0, DATA_SIZE);
      break;
    }
  }while(1);
}

int UC1394_EXTERNAL_notifier()
{
  printf("Bus Reset occured\n");
  //  UC1394_close();
  //  set_up_uc1394_interfaces();
  return 1;
}

int set_up_uc1394_interfaces(void)
{
  if(DEBUG)
    printf("tring to init uc1394\n");

  if(!UC1394_init())
    return 0;
  
  if(DEBUG)
    printf("Initialized the UC1394\n");
  
  iso_ack = UC1394_get_interface(IO_PIN, 13);
  streaming_port = UC1394_get_interface(STREAMING_PORT, 0);
  io_pin8 = UC1394_get_interface(IO_PIN, 8);
  
  if(DEBUG)
    printf("Initialized the streaming port\n");

  if(streaming_port.interface_type == NOT_FOUND || 
     iso_ack.interface_type == NOT_FOUND ||
     io_pin8.interface_type == NOT_FOUND ) {
    printf("Could not find one of the interfaces\n");
    return 0;
  }
  
  if(DEBUG)
    printf("Got all of the interfaces\n");

  // ************************************************************************
  //UC1394_set_STREAMING_ACK(iso_ack); // set up the ack for the streaming port
 
  //  if(DEBUG)
  //    printf("Set the iso ack pin\n");

  return 1;
}

int set_uc1394_streaming_recv(void)
{
  if(!UC1394_init_STREAMING(streaming_port, STREAMING_RECV)) {
    printf("Could not init the streaming port\n");
    return 0;
  }
  return 1;
}

int set_uc1394_streaming_tran(void)
{
  if(!UC1394_init_STREAMING(streaming_port, STREAMING_TRANS)) {
    printf("Could not init the streaming port\n");
    return 0;
  }
  return 1;
}

int do_uc1394_streaming(void)
{
    system("date");
    UC1394_start_STREAMING(streaming_port, data, DATA_SIZE);
    UC1394_stop_STREAMING(streaming_port);  
    system("date");
}

int display_data_segment(int beg, int end)
{
  int i;
  
  if(end < DATA_SIZE)
    for(i = beg; i < end; i++)
      printf("Data block %d value = %uc\n", i, data[i]);

  return 1;
}

int turn_on_io_pin(struct interface inst)
{
  UC1394_set_IO_PIN_state(inst, IO_PIN_ON);
  return 1;
}

int turn_off_io_pin(struct interface inst)
{
  UC1394_set_IO_PIN_state(inst, IO_PIN_OFF);
  return 1;
}


