/*!
 @brief Driver for Wiimote operation menu

 Handles parsing wiimote input to determine correct operating mode.

 This function will never exit once entered.
 */
ErrorID_t main_menu(cwiid_wiimote_t *wiimote,
		volatile WiimoteStatusDataType *wiimote_status)
{
	ErrorID_t error;
	uint8_t menu_count;
	uint16_t last_button_state = 0;
	WII_OPERATE_STATE wii_operate_state = WII_OPERATE_INIT_MENU;

	write_status_led(STATUS_LED_OFF, 0);

	for (;;)
	{
		switch (wii_operate_state)
		{
		case WII_OPERATE_INIT_MENU:
			menu_count = 0;
			last_button_state = 0;
			wii_operate_state = WII_OPERATE_WAIT_FOR_BUTTON_PRESS;
			write_status_led(STATUS_LED_FLASH, 500);

			if (0 > cwiid_set_led(wiimote, CWIID_LED1_ON))
				return WII_ERROR_TRANSMIT;
			if (0 > cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC))
				return WII_ERROR_TRANSMIT;
			if (0 > cwiid_set_rpt_mode(wiimote, CWIID_RPT_BTN))
				return WII_ERROR_TRANSMIT;

			/* Menu */
			debug_print("%s", MENU);
			set_lcd(0, "Connected.");
			set_lcd(1, "Select Mode.");
			break;

		case WII_OPERATE_WAIT_FOR_BUTTON_PRESS:
			error = wait_for_wiimotedata(wiimote_status, 2000);
			if (error == ERR_WII_DATA_TIMEOUT)
			{
				set_lcd(0, "Select Mode");
				set_lcd(1, (char *) lcd_menu_strings[menu_count]);
				if (++menu_count >= MENU_ENTRIES)
					menu_count = 0;
			}
			else if (0 < error)
			{
				shutdown_application(error);
			}
			else if (wiimote_status->button_data != last_button_state)
			{
				last_button_state = wiimote_status->button_data;
				if (last_button_state & CWIID_BTN_A) // collect acceleration data
				{
					wii_operate_state = WII_OPERATE_DISPLAY_ACCELEROMETER_INFO;
				}
				else if (last_button_state & CWIID_BTN_B) // collect IR data
				{
					wii_operate_state = WII_OPERATE_DISPLAY_IR_STATUS;
				}
				else if (last_button_state & CWIID_BTN_HOME) // reset
				{
					shutdown_application(0);
				}
				else if (last_button_state & CWIID_BTN_PLUS)
				{
					sensor_cutoff_fwd += 5;
					if (sensor_cutoff_fwd > 100)
						sensor_cutoff_fwd = 100;
#if REVERSE_SENSOR_PRESENT
					sensor_cutoff_rev += 5;
					if (sensor_cutoff_rev > 100)
					sensor_cutoff_rev = 100;
#endif

				}
				else if (last_button_state & CWIID_BTN_MINUS)
				{
					sensor_cutoff_fwd -= 5;
					if (sensor_cutoff_fwd < 0)
						sensor_cutoff_fwd = 0;
#if REVERSE_SENSOR_PRESENT
					sensor_cutoff_rev -= 5;
					if (sensor_cutoff_rev < 0)
					sensor_cutoff_rev = 0;
#endif
				}
				else if (last_button_state & (CWIID_BTN_2 | CWIID_BTN_1))
				{
					wii_operate_state = WII_OPERATE_DISPLAY_BUTTON_STATUS;
				}
			}
			break;

		case WII_OPERATE_DISPLAY_ACCELEROMETER_INFO:
			if (ERR_NONE != acceleration_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_ERROR_STATE;
			else
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;

		case WII_OPERATE_DISPLAY_IR_STATUS:
			if (ERR_NONE != infrared_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_ERROR_STATE;
			else
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;

		case WII_OPERATE_DISPLAY_BUTTON_STATUS:
			if (ERR_NONE != button_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_ERROR_STATE;
			else
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;

		case WII_OPERATE_ERROR_STATE:
			if (ERR_NONE == error_mode(wiimote, wiimote_status))
				wii_operate_state = WII_OPERATE_INIT_MENU;
			break;
		}
	}
	return ERR_NONE;
}
int main(void)
{
	setup();
	uart_init();
	TWI_Init();
	input = getchar();
	while((input != 's'))//wait for user input to begin program
	{
		input = getchar();
	}
	sei();//global interrupts on
	IMU_setup();
	printf("\nLet's Begin\n\nChoose an option:\n\n  space bar - PID (loops forever)\n        'm' - manual control (loops forever)\n        'i' - check IMU\n        'x' - get x acceleration\n        'y' - get y acceleration\n        'z' - get z acceleration\n        'f' - bluetooth fast mode\n        't' - test IMU write\n        'a' - enter acceleration mode\n");
	while((1))
	{
		input = getchar();
		if ((input == ' '))//PID algorithm
		{
			output_timer_on();
			while((1))
			{
				PID();
			}
		}
		else if ((input == 'm'))//manual control
		{
			printf("\nManual Mode:\n\nInstructions:\n\n    'n' - forwards\n    'v' - reverse\n    'b' - stop\n  '1-9' - use the number keys to select the power level\n");
			while((1))
			{
				manual();
			}
		}
		else if ((input == 'i'))//check WHO_AM_I register
		{
			check_imu();	
		}
		else if ((input == 'x'))//get x direction acceleration
		{
			acceleration = get_accel('x');
			print_float(acceleration);
			printf("\n");
		}
		else if ((input == 'y'))//get y direction acceleration
		{
			acceleration = get_accel('y');
			print_float(acceleration);
			printf("\n");
		}
		else if ((input == 'z'))//get z direction acceleration
		{
			acceleration = get_accel('z');
			print_float(acceleration);
			printf("\n");
		}
		else if ((input == 'f'))//place RN-42 HID into fast mode
		{
			fast_mode();
		}
		else if ((input == 't'))//write a regsiter of the IMU and check it worked
		{
			TWI_WRITEBYTE(MPU6050_PWR_MGMT_1, 0x02);
			if ((TWI_READBYTE(MPU6050_PWR_MGMT_1) == 0x02))
				printf("\nSuccess\n");
			else
				printf("\nFailure\n");
		}
		else if ((input == 'a'))//acceleration mode for acquiring data
		{
			acceleration_mode();
		}
	}

}