/* craps for Teensy USB Development Board
Try to catch "flying light"
 */

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "usb_debug_only.h"
#include "print.h"
#define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))

#define LED1_ON		(PORTD &= ~(1))
#define LED1_OFF		(PORTD |= (1))
#define LED2_ON		(PORTD &= ~(1<<2))
#define LED2_OFF		(PORTD |= (1<<2))
#define LED3_ON		(PORTD &= ~(1<<3))
#define LED3_OFF		(PORTD |= (1<<3))
#define LED4_ON		(PORTD &= ~(1<<4))
#define LED4_OFF		(PORTD |= (1<<4))
#define LED5_ON		(PORTD &= ~(1<<5))
#define LED5_OFF		(PORTD |= (1<<5))
#define LED6_ON		(PORTD &= ~(1<<6))
#define LED6_OFF		(PORTD |= (1<<6))
#define LED7_ON		(PORTD &= ~(1<<7))
#define LED7_OFF		(PORTD |= (1<<7))

int x = 0, y = 1;
short int win = 0;
int point;
short int tabd[8] = {0xf7,0xbe,0xb6,0xaa,0xa2,0x88};
short int tabb[8] = {0xf7,0xbe,0xb6,0xaa,0xa2,0x88};
int roll(void);
void crap_out(int);
void second_roll(int);
void natural(int);
void made_point(int);
void sorry(int);
void prdec(int);

int main(void)
{
		int	b; 			// sum of two to be saved
	// set for 16 MHz clock, and turn on the LED
	CPU_PRESCALE(0);
	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	//while (!usb_configured()) /* wait */ ;

	// wait an extra tw seconds for the PC's operating system
	// to load drivers and do whatever it does to actually
	// be ready for input
	_delay_ms(2000);

	// start printing stuff.  If hid_listen is running on
	// the host, this should appear.
	DDRB = 0xfe;
	DDRD = 0xff;
	PORTB = 0xff;
	PORTD = 0xff;
	// ************  Start  ******************
// ------------------------------------------------------------------------------------------------------------------------	
	while(1)									// main while loop for game
			{
			if(win){print("\n  Same ");}
			else {print("\n  Next ");}
			print("player ... Come Out\n");
			b = roll();					// First roll
			switch (b)
				{
				case (7):
				case (11):
					natural(b);			// if 7, or 11  win
					win = 1;
					break;
				case (2):				// crap out with 2,3, or 12
				case (3):
				case (12):
					crap_out(b);
					win = 0;
					break;
				default:					//  any other is your point
					point = b;
					second_roll(b);
					break;
				}
			}
}  // end of main
// ************************************************************************************
int roll(void)					// roll dice, get two numbers and sum
	{
	int a;
	while((PINB & 1) == 1)	// wait for roll
		{
				x++;
		if(x == 7) 				// bump counters while waiting
			{
			x = 1;
			y++;
			};
		if(y == 7)
			{
			y = 1;
			}
		}
	for(a=40; a<50; a++)		// make dice "roll"
		{
		PORTB = 0xaa;
		PORTD = 0x55;
		_delay_ms(a);
		
		PORTB = 0x55;
		PORTD = 0xaa;
		_delay_ms(a);
		}
	PORTD = tabd[x-1];	
	PORTB = (tabb[y-1]) <<1;	
	//_delay_ms(1000);
	return(x + y);				// return sum of x and y
	}	
		
// ************************************************************************************
void crap_out(int b)
	{
	prdec(b);
		switch (b)
			{
			case (12):
				print("  Box Cars\n");
				break;
			case (2):
				print("  Snake Eyes   You Loose\n");
				break;
			case	(3):
				print(" Ace Duce ...  3 You loose \n");
				break;
			}
		win = 0;
		return;
	}
	
// ************************************************************************************
void second_roll(int b)
	{
	while(b)
		{
		print(" point = is ");
		prdec(point);
		print("   ");
		print(" Roll Again\n");
		b = roll();
		print("  You rolled ");
		prdec(b);
		print("   ");
		if(b == 7)
			{
			sorry(b);
			win = 0;
			break;
			}
		if(b == point)
			{
			made_point(b);
			if(x == y){print(" The Hard Way\n");}
			else {print(" The Easy way\n");}
			win = 1;
			break;
			}
		}
	return;
	}
// ************************************************************************************
void natural(int b)
	{
	prdec(b);
	print(" Natural  you win\n");
	win = 1;
	return;
	}
// ************************************************************************************
void made_point(int b)
	{
	prdec(b);
	print(" *************  You made point...You Win  ***************\n");
	win = 1;
	return;
	}
// ************************************************************************************
void sorry(int b)
	{
	print("   Sorry    You loose ....\n");
	win = 0;
	return;
	}
// ************************************************************************************
void prdec(int b)
	{
	switch (b)
		{
		case (1):
			print("1");
			break;
			
		case (2):
			print("2 (snake eyes)");
			break;
			
		case (3):
			print("3");
			break;
			
		case (4):
			print("4");
			break;
			
		case (5):
			print("5");
			break;
			
		case (6):
			print("6");
			break;
			
		case (7):
			print("7");
			break;
			
		case (8):
			print("8");
			break;
			
		case (9):
			print("9");
			break;
			
		case (10):
			print("10");
			break;
			
		case (11):
			print("11");
			break;
			
		case (12):
			print("12 (box cars)");
			break;
		}
	}
// ************************************************************************************
// ************************************************************************************
// ************************************************************************************
		
			
			