#include <stdlib.h>
#include "input.h"

Input::Input()
{
	// Initialize the first wiimote controller
	WPAD_Init();
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
	
	// Do initial scan
	WPAD_ScanPads();
	WPAD_IR(WPAD_CHAN_0, &c_ir);
	
	// Set a random number based on the controller input
	void *ptr = &(c_ir.angle);
	
	// Get as many bits filled as we can
	srand((int)(c_ir.sx) + (((int)(c_ir.sy)) << 8) + (int)ptr);
}

Input::~Input()
{
	// Close connection
	WPAD_Disconnect(WPAD_CHAN_0);
}

int Input::getCursorX()
{
	// Derrived from spritetest, estimated values for correction
	return c_ir.sx + X_OFFSET;
}

int Input::getCursorY()
{
	// Derrived from spritetest, estimated values for correction
	return c_ir.sy + Y_OFFSET;
}

f32 Input::getCursorRot()
{
	return c_ir.angle/2;
}

u32 Input::getButtonsDown()
{
	return WPAD_ButtonsDown(WPAD_CHAN_0);
}

u32 Input::getButtonsUp()
{
	return WPAD_ButtonsUp(WPAD_CHAN_0);
}

void Input::read()
{
	WPAD_ScanPads();
	WPAD_IR(WPAD_CHAN_0, &c_ir);
}
