User:Megazig

From WiiBrew
Jump to navigation Jump to search
en This user is a native speaker of English.
C This user codes in C.
C++ This user codes in C++.
C# This user codes in C Sharp.
Py This user codes in Python.
Linux thumb.png This user develops using Linux.
Developer icon.png
This user has developed homebrew for the Wii.

My name is Megazig.

Please put talk on my talk page, not here.

PROJECTS

Riisk: A version of the board game Risk for the Wii. Just started on 01/29/09, but should have a beta out soon. Edit 08/09/2009: I have not forgotten about this project. I will get back to work on it and convert my code to LibWiiGUI.

Wiimote data code

#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <ogc/system.h>

#define VERSION       "INFO"
#define RELEASE_DATE  "08-09-2009"

int numberOfAttachedControllers();
void waitForWiimote();

static u32 *xfb;
static GXRModeObj *rmode;

int totalWiiMotes;
int rumbleOn = 0;

// Wiimote IR
ir_t ir;

// Orientation vars
orient_t orient;
int xRotation;
int yRotation;
int zRotation;

//Gforce vars
gforce_t gforce;

//---------------------------------------------------------------------------------------------------
//Initialize the video and wii remotes
//---------------------------------------------------------------------------------------------------
void Initialize() {
	
	//initialize video
	VIDEO_Init();
	//initialize wii remotes
	WPAD_Init();
	//set IR resolution to 640 width and 480 height
	WPAD_SetVRes(0, 640, 480);
	//return data for wii remotes should contain Button Data, Accelerometer, and IR
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
	
	//find the video mode of the wii
	rmode = VIDEO_GetPreferredMode(NULL);
	
	//create buffer for console terminal output
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	//initialize the console
	console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
	
	//set video to wii's video mode
	VIDEO_Configure(rmode);
	//set buffer to write to
	VIDEO_SetNextFramebuffer(xfb);
	//whether to set background black, I have chosen no
	VIDEO_SetBlack(FALSE);
	//send VIDEO_ commands for setup
	VIDEO_Flush();
	//wait for vertical sync to ensure it's at the bottom
	VIDEO_WaitVSync();
	//if video is progressive then wait for another vertical sync
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

	//find out how many wii remotes are attached at the start
	waitForWiimote();
	totalWiiMotes = numberOfAttachedControllers();
}

//---------------------------------------------------------------------------------------------------
//Exit back to the HBC - can be used for cleanup or other non-sense
//---------------------------------------------------------------------------------------------------
void exitToHomebrewChannel(void) {
	exit(0);
}

//---------------------------------------------------------------------------------------------------
//Test for values of wii remote accelerometer and IR
//---------------------------------------------------------------------------------------------------
void showWiiMoteAccelerometer(void) {
	
	printf("\x1b[2J");
	while(1) {

		u32 ext;//Extension type
		u32 ret = 0;//remote scan and probe return
		ret = WPAD_ScanPads();
		ret = WPAD_Probe(WPAD_CHAN_0, &ext);//probe remote 1 with extension

		WPADData *Data = WPAD_Data(WPAD_CHAN_0);//store data from remote 1
		WPADData data = *Data;
        
		WPAD_IR(WPAD_CHAN_0, &(data.ir));//get IR data
		WPAD_Orientation(WPAD_CHAN_0, &(data.orient));//get rotation data
		WPAD_GForce(WPAD_CHAN_0, &(data.gforce));//get "speed" data
		WPAD_Accel(WPAD_CHAN_0, &(data.accel));//get accelerometer data
		WPAD_Expansion(WPAD_CHAN_0, &(data.exp));//get expansion data

		printf("\x1b[1;0HWii Remote Info  \n");
	
		//print gforce data for x, y, and z
		printf("\x1b[2;0HGForce x: %1.3f  \n", data.gforce.x);
		printf("\x1b[3;0HGForce y: %1.3f  \n", data.gforce.y);
		printf("\x1b[4;0HGForce z: %1.3f  \n", data.gforce.z);

		
		//get and print the rotation orientation
		xRotation = (int)data.orient.roll;
		yRotation = (int)data.orient.pitch;
		zRotation = (int)data.orient.yaw;

		if(xRotation<0){
			xRotation=360+xRotation;
		}
		if(yRotation<0){
			yRotation=360+yRotation;
		}
		if(zRotation<0){
			zRotation=360+zRotation;
		}

		printf("\x1b[5;0HxRotation: %d  \n", xRotation);
		printf("\x1b[6;0HyRotation: %d  \n", yRotation);
		printf("\x1b[7;0HzRotation: %d  \n", zRotation);

		//print different IR data
		printf("\x1b[9;0HIR x: %1.3f  \n", data.ir.x);
		printf("\x1b[10;0HIR y: %1.3f  \n", data.ir.y);

		printf("\x1b[11;0HIR RAW X %1.3f  ", data.ir.ax);
		printf("\x1b[12;0HIR RAW Y %1.3f  ", data.ir.ay);
          
		printf("\x1b[13;0HIR SMOOTH X %1.3f  ", data.ir.sx);
		printf("\x1b[14;0HIR SMOOTH Y %1.3f  ", data.ir.sy);
          
		printf("\x1b[15;0HIR ANGLE %1.3f  ", data.ir.angle);

		//print accelerometer data
		printf("\x1b[1;25HACCEL X %d  \n", (int)data.accel.x);
		printf("\x1b[2;25HACCEL Y %d  \n", (int)data.accel.y);
		printf("\x1b[3;25HACCEL Z %d  \n", (int)data.accel.z);


		if((ret == WPAD_ERR_NONE) && (ext == WPAD_EXP_NUNCHUK))
		{
			printf("\x1b[4;25HNCUHCK MIN X %d Y %d  ", (int)data.exp.nunchuk.js.min.x, (int)data.exp.nunchuk.js.min.y);
			printf("\x1b[5;25HNCUHCK MAX X %d Y %d  ", (int)data.exp.nunchuk.js.max.x, (int)data.exp.nunchuk.js.max.y);
			printf("\x1b[6;25HNCUHCK CENTER X %d Y %d  ", (int)data.exp.nunchuk.js.center.x, (int)data.exp.nunchuk.js.center.y);
			printf("\x1b[7;25HNCUHCK POS X %d Y %d  ", (int)data.exp.nunchuk.js.pos.x, (int)data.exp.nunchuk.js.pos.y);
			printf("\x1b[8;25HNCUHCK ANG %1.3f MAG %1.3f  ", (float)data.exp.nunchuk.js.ang, (float)data.exp.nunchuk.js.mag);

			printf("\x1b[9;25HGFORCE X %1.3f  \n", (float)data.exp.nunchuk.gforce.x);
			printf("\x1b[10;25HGFORCE Y %1.3f  \n", (float)data.exp.nunchuk.gforce.y);
			printf("\x1b[11;25HGFORCE Z %1.3f  \n", (float)data.exp.nunchuk.gforce.z);
		
			printf("\x1b[12;25HROTATE X %1.3f  \n", (float)data.exp.nunchuk.orient.roll);
			printf("\x1b[13;25HROTATE Y %1.3f  \n", (float)data.exp.nunchuk.orient.pitch);
			printf("\x1b[14;25HROTATE Z %1.3f  \n", (float)data.exp.nunchuk.orient.yaw);

			printf("\x1b[15;25HACCEL X %1.3f  \n", (float)data.exp.nunchuk.accel.x);
			printf("\x1b[16;25HACCEL Y %1.3f  \n", (float)data.exp.nunchuk.accel.y);
			printf("\x1b[17;25HACCEL Z %1.3f  \n", (float)data.exp.nunchuk.accel.z);
		}
		
		u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
		u32 buttonDown = WPAD_ButtonsDown(WPAD_CHAN_0);

		if (pressed & WPAD_NUNCHUK_BUTTON_Z) printf("\x1b[18;0HButton Z pressed on Nunchuk");
		if (pressed & WPAD_NUNCHUK_BUTTON_C) printf("\x1b[19;0HButton C pressed on Nunchuk");
		if (pressed & WPAD_BUTTON_A) printf("\x1b[20;0HButton A pressed");
		if (pressed & WPAD_BUTTON_B) printf("\x1b[21;0HButton B pressed");
		if (pressed & WPAD_BUTTON_1) printf("\x1b[22;0HButton 1 pressed");
		if (pressed & WPAD_BUTTON_2) printf("\x1b[23;0HButton 2 pressed");
		if (pressed & WPAD_BUTTON_PLUS) printf("\x1b[18;25HButton PLUS pressed");
		if (pressed & WPAD_BUTTON_MINUS) printf("\x1b[19;25HButton MINUS pressed");
		if (pressed & WPAD_BUTTON_UP) printf("\x1b[20;25HButton UP pressed");
		if (pressed & WPAD_BUTTON_DOWN) printf("\x1b[21;25HButton DOWN pressed");
		if (pressed & WPAD_BUTTON_LEFT) printf("\x1b[22;25HButton LEFT pressed");
		if (pressed & WPAD_BUTTON_RIGHT) printf("\x1b[23;25HButton RIGHT pressed");

		printf("\x1b[24;25HNumber of wiimotes: %d", totalWiiMotes);
		printf("\x1b[24;1Hret: %d", ret);

		if (buttonDown & (WPAD_BUTTON_1 | WPAD_BUTTON_2)) {
			if ( rumbleOn == 0 ) {
				rumbleOn = 1;
				WPAD_Rumble(WPAD_CHAN_0, 1);
			}
			else {
				rumbleOn = 0;
				WPAD_Rumble(WPAD_CHAN_0, 0);
			}
		}
		if (buttonDown & (WPAD_BUTTON_A | WPAD_BUTTON_B)) {
			WPAD_Rumble(WPAD_CHAN_0, 0);
			break;
		}
		if (pressed & WPAD_BUTTON_HOME) {
			WPAD_Rumble(WPAD_CHAN_0, 0);
			exitToHomebrewChannel();
		}
		if(SYS_ResetButtonDown()) exitToHomebrewChannel();
		VIDEO_WaitVSync();
		printf("\x1b[2J");
	}
}

//---------------------------------------------------------------------------------------------------
//Find out how many controllers are attached
//---------------------------------------------------------------------------------------------------
int numberOfAttachedControllers() {
	// used to check how many controllers are attached
	int i, numAttached = 0;
	u32 type;  //find the type of the expansion

	for(i=0; i<WPAD_MAX_WIIMOTES; i++) {
		if (WPAD_Probe(i, &type) == WPAD_ERR_NONE) {
			numAttached++;
		}
	}
	return numAttached;
}

void waitForWiimote()
{
	while(1)
	{
		printf("\x1b[20;20HWaiting for wiimote");
		totalWiiMotes = numberOfAttachedControllers();
		if (totalWiiMotes > 0) break;
	}
	return;
}


//---------------------------------------------------------------------------------------------------
//Main function
//---------------------------------------------------------------------------------------------------
int main() {
	
	Initialize();

	showWiiMoteAccelerometer();
	
	exitToHomebrewChannel();
	
	return 0;
}