#include <stdlib.h>
#include <unistd.h>
#include <curses.h>
#include <errno.h>
void output_speed_screen();
void output_camera_screen();
WINDOW *speed;
WINDOW *camera;
struct coord{
	int xCor;
	int yCor;
};

static struct coord center= {10, 5};
struct coord command = {0, 0};
struct coord old_coord = {0, 0};
char center_char = 'O';
char x_char = '|';
char y_char = '=';
char blank_char ='.'; 


int main(int argc, const char * argv[]){
	
	int i=0;
	int key=0;
	
	if(initscr()==NULL) {
		perror("initscr");
		exit(EXIT_FAILURE);
	}
	
	crmode(); //grab keystrokes directly
    //nodelay(stdscr, TRUE); //don't wait on getch()
    noecho(); //don't echo output
	keypad(stdscr, TRUE);
	
	refresh();
	speed = subwin(stdscr, 12, 80, 0, 0);
	touchwin(speed);
	camera = subwin(stdscr, 12, 80, 12, 0);
	touchwin(camera);
	//box(speed, ACS_VLINE, ACS_HLINE);
	box(camera, ACS_VLINE, ACS_HLINE);
	wmove(speed, 5, 5);
	wmove(camera, 5, 5);
	wprintw(camera, "This is the camera window");
	output_speed_screen();
	wrefresh(speed);
	wrefresh(camera);
	while(key != 'q'){
		key = getch();
		switch(key) {
			case 'q':
				break;
			case KEY_RIGHT:
				if(command.xCor < 10){
					command.xCor++;
				}else{
					beep();
				}
				
				break;
			case KEY_LEFT:
				if(command.xCor > -10){
					command.xCor--;
				}else{
					beep();
				}
				break;
			case KEY_UP:
				if(command.yCor < 10){
					command.yCor++;
				}else{
					beep();
				}
				break;
			case KEY_DOWN:
				if(command.yCor > -10){
					command.yCor--;
				}else{
					beep();
				}
				break;
			case ' ':
				command.yCor = 0;
				command.xCor = 0;
				break;
			default: beep();
		}
		output_speed_screen();
	}


    //get acces to io port
    //ThreadCtl(_NTO_TCTL_IO, 0);
//	printw("input a char");
//	move(1, 0);
	
	refresh();
	sleep(1);
	endwin();
	exit(EXIT_SUCCESS);
}

void output_speed_screen(){

	int i = 0;
	
	wmove(speed, old_coord.yCor, old_coord.xCor);
	waddch(speed, ' ');
	
	for(i = 0; i<11; i++) {
		wmove(speed, i, center.xCor);
		waddch(speed, y_char);
	}
	
	wmove(speed, center.yCor, center.xCor-10);
	wprintw(speed, "||||||||||O||||||||||");
	
	if(command.yCor%2){
		old_coord.yCor = center.yCor-(int)((command.yCor+1)/2);
	} else{
		old_coord.yCor = center.yCor-(int)(command.yCor/2);
	}
	old_coord.xCor = center.xCor+command.xCor;
	wmove(speed, old_coord.yCor, old_coord.xCor);
	waddch(speed, '*');
	wmove(speed, 0, 0);
	wrefresh(speed);

}
void output_camera_screen();


