/*
 * Userspace program that communicates with the led_vga device driver
 * primarily through ioctls
 *
 * Stephen A. Edwards
 * Columbia University
 */

#include <stdio.h>
#include "vga_led.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "usbkeyboard.h"


#define BUFFER_size 128

struct libusb_device_handle *keyboard;
uint8_t endpoint_address;
int sub0;
int sub1;
int sub2;
int vga_led_fd;
static unsigned char message[8] = { 0x00, 0x00, 0x00, 0x00,
				      0x00, 0x00, 0x00, 0x00 };
int x_angle_l;
int y_angle_l;
int x_angle_h;
int y_angle_h;

/* Read and print the segment values */
void print_segment_info() {

  vga_led_arg_t vla;
  int i;

  for (i = 0 ; i < VGA_LED_DIGITS ; i++) {
    vla.digit = i;
    if (ioctl(vga_led_fd, VGA_LED_READ_DIGIT, &vla)) {
      perror("ioctl(VGA_LED_READ_DIGIT) failed");
      return;
    }
    printf("%02x ", vla.segments);
  }
  printf("\n");
}

/* Write the contents of the array to the display */
void write_segments(const unsigned char segs[8])
{
  vga_led_arg_t vla;
  int i;
  for (i = 0 ; i < VGA_LED_DIGITS ; i++) {
    vla.digit = i;
    vla.segments = segs[i];
    if (ioctl(vga_led_fd, VGA_LED_WRITE_DIGIT, &vla)) {
      perror("ioctl(VGA_LED_WRITE_DIGIT) failed");
      return;
    }
  }
}

int main()
{
	int operation;
	int err, col;
 	struct usb_keyboard_packet packet;
	int transferred;
  	char keystate[12];


	x_angle_l = 0;
	y_angle_l = 0;
	x_angle_h = 0;
	y_angle_h = 0;
 /* Open the keyboard */
  if ( (keyboard = openkeyboard(&endpoint_address)) == NULL ) {
    fprintf(stderr, "Did not find a keyboard\n");
    exit(1);
  }

	vga_led_arg_t vla;
	 static const char filename[] = "/dev/vga_led";
 printf("VGA LED Userspace program started\n");

  if ( (vga_led_fd = open(filename, O_RDWR)) == -1) {
    fprintf(stderr, "could not open %s\n", filename);
    return -1;
  }

  printf("initial state: ");
  print_segment_info();

  write_segments(message);

  printf("current state: ");
  print_segment_info();

/* Look for and handle keypresses */
  for (;;) {
	operation = 0;
    libusb_interrupt_transfer(keyboard, endpoint_address,
			      (unsigned char *) &packet, sizeof(packet),
			      &transferred, 0);
    if (transferred == sizeof(packet)) {
      sprintf(keystate, "%02x %02x %02x", packet.modifiers, packet.keycode[0],
	      packet.keycode[1]);
	sub0 = packet.modifiers;
	sub1 = packet.keycode[0];
	sub2 = packet.keycode[1];

	printf("%s\n",keystate);
      if (packet.keycode[0] == 0x29) { /* ESC pressed? */
	break;
      }
	if(sub0 == 0)
	{
		switch(packet.keycode[0])
		{
			case 0x50: operation = 1;break; // L R U D
			case 0x4F: operation = 2;break;
			case 0x52: operation = 3;break;
			case 0x51: operation = 4;break;	
		}
	}

    }

	transfervalue(operation);
  }

	printf("VGA LED Userspace program terminating\n");
  return 0;
}
	

void transfervalue(int op)
{


	
	switch(op)
	{
		case 1: 
			{	
				if(x_angle_l==255){
					x_angle_l = 0;
					message[0] = 0x00;
					if(x_angle_h == 7)
					{
						x_angle_h = 0;	
						message[1] = 0x00;				
					}
					else	
					{
						x_angle_h++;
						message[1] = message[1]+1;
					}
				}
				else
				{
					x_angle_l++;
					message[0] = message[0]+1;
				}
				break;
			}
		case 2: 
			{
				if(x_angle_l==0){
					x_angle_l = 255;
					message[0] = 0xFF;
					if(x_angle_h == 0)
					{
						x_angle_h = 7;	
						message[1] = 0x07;				
					}
					else	
					{
						x_angle_h--;
						message[1] = message[1]-1;
					}
				}
				else
				{
					x_angle_l--;
					message[0] = message[0]-1;
				}
				break;
			}
		case 3: 
			{	
				if(y_angle_l==255){
					y_angle_l = 0;
					message[2] = 0x00;
					if(y_angle_h == 7)
					{
						y_angle_h = 0;	
						message[3] = 0x00;				
					}
					else	
					{
						y_angle_h++;
						message[3] = message[3]+1;
					}
				}
				else
				{
					y_angle_l++;
					message[2] = message[2]+1;
				}
				break;
			}
		case 4: 
			{
				if(y_angle_l==0){
					y_angle_l = 255;
					message[2] = 0xFF;
					if(y_angle_h == 0)
					{
						y_angle_h = 7;	
						message[3] = 0x07;				
					}
					else	
					{
						y_angle_h--;
						message[3] = message[3]-1;
					}
				}
				else
				{
					y_angle_l--;
					message[2] = message[2]-1;
				}
				break;
			}
		
	}
	write_segments(message);
  return NULL;
 	

}

  
  

  

 

  
  
  

