Welcome! Log In Create A New Profile

Advanced

Wiimote Power Button

Posted by Arikado 
Wiimote Power Button
December 07, 2008 09:48PM
I'd like to add the following functionality to my apps: When the power button is pressed on the wii remote, the wii turns itself off. Looking through the wiiuse and wpad headers I was unable find support for the power button.

Thanks in advance for any and all help,
Arikado
Re: Wiimote Power Button
December 07, 2008 11:23PM
You should be able to use the Wii remote power button with the current release of libogc ( 1.7.0 )

Straight from wpad.h:
void WPAD_SetPowerButtonCallback(WPADShutdownCallback cb);
typedef void (*WPADShutdownCallback)(s32 chan);

The usage is pretty much like SYS_SetResetCallback() and SYS_SetPowerCallback(), except that the callback function must have a s32 as parameter. This is useful if you only want a certain wii remote to be able to shut down the Wii.

So if you had a callback function like this:
// WPADShutdownCallback 
void doPadPowerOff( s32 chan )
{	
	if ( chan == WPAD_CHAN_0 )
		SYS_ResetSystem( SYS_POWEROFF, 0, 0 );

	return;
}

You'd simply add this line somewhere to your main function to make it work.
WPAD_SetPowerButtonCallback( doPadPowerOff );
I hope this helps.



Edited 1 time(s). Last edit at 12/07/2008 11:43PM by Botskiz.
Re: Wiimote Power Button
December 19, 2008 04:41PM
Thank you for your reply.

I used your code in my app exactly as you posted it. Unfortunately, when I press the power button my app freezes. Do you have any further suggestions?



Edited 1 time(s). Last edit at 12/19/2008 04:41PM by Arikado.
Re: Wiimote Power Button
December 19, 2008 07:29PM
You can't do the power off in the callback itself, or it'll freeze. Use the callback to set a flag, and check that flag.

do it like this:

WPAD_SetPowerButtonCallback( triggerPowerOff );

// WPADShutdownCallback
void triggerPowerOff( s32 chan )
{
powerOffRequested = true;
}

...
in your main loop:
if(powerOffRequested)
{
SYS_ResetSystem( SYS_POWEROFF, 0, 0 );
}



Edited 1 time(s). Last edit at 12/19/2008 07:30PM by Tantric.
Re: Wiimote Power Button
December 19, 2008 07:38PM
Hmm, I'm not sure why it freezes. But you could try using the callback functions to set some poweroff / reset flags, terminate your main loop if one of these flags is set, and reset or shut down the Wii after that. That's the way I usually do it, and I've never encountered any freezing.

Edit: Bah, I'm too slow. Tantric already covered it :)

// Replaced "<" with "(" here to make it work with the forums.
#include (iostream>
#include (gccore.h>
#include (wiiuse/wpad.h>

using namespace std;

bool bPowerOff = false;	// Poweroff flag.
bool bReset = false;		// Reset flag.

//====================================================================
//	Reset callback function.
//====================================================================
void doSystemReset( void )
{
	bReset = true;
	return;
}

//====================================================================
//	PowerOff callback function.
//====================================================================
void doPowerOff( void )
{
	bPowerOff = true;
	return;
}

//====================================================================
//	PowerOff callback function for the Wii Remote power button.
//====================================================================
void doPadPowerOff( s32 chan )
{
	if ( chan == WPAD_CHAN_0 )
		bPowerOff = true;
	return;
}

//====================================================================
//	Main program.
//====================================================================
int main ( int* argc, char** argv )
{
	// Initialise the video system, etc. here.
	// ...

	// Set Reset and Shutdown Callbacks.
	SYS_SetResetCallback( doSystemReset );
	SYS_SetPowerCallback( doPowerOff );
	WPAD_SetPowerButtonCallback( doPadPowerOff );

	// Run the main loop.
	while ( !bReset && !bPowerOff )
	{
		// Do whatever you want your programme to do here.
		// ...
	}
	
	// Reset the system.
	if ( bReset )
		SYS_ResetSystem( SYS_RESTART, 0, 0 );
		
	// Shut the system down.
	if ( bPowerOff )
		SYS_ResetSystem( SYS_POWEROFF, 0, 0 );
	
	return 0;
}



Edited 1 time(s). Last edit at 12/19/2008 07:39PM by Botskiz.
Re: Wiimote Power Button
December 19, 2008 10:04PM
I don't think you need to use a return at the end of each callback.

I don't know why it's not working, your code looks a lot like the one I added in the Developer tips: [wiibrew.org]



Edited 1 time(s). Last edit at 12/19/2008 10:05PM by Crayon.
Re: Wiimote Power Button
February 15, 2009 06:26AM
I get nothing but instant crashes too. Here's my code:

controls.c
static signed int shutdownType = -1; /* Not 0 as ogc/system.h uses that for SYS_RESTART */

static void WiiResetCB(void) {
    shutdownType = SYS_RESTART;
}

static void WiiPowerCB(void) {
    shutdownType = SYS_POWEROFF;
}

static void WiimotePowerCB(int chan) {
    shutdownType = SYS_POWEROFF;
}

extern void Init_Controllers(void) {
    /* Irrelevant code removed */
    
    SYS_SetResetCallback(WiiResetCB);
    SYS_SetPowerCallback(WiiPowerCB);
    WPAD_SetPowerButtonCallback(WiimotePowerCB);

    /* Irrelevant code removed */
}

extern void Check_Controllers(void) {
    /* Irrelevant code removed */

    if (shutdownType != -1) {
        Shutdown(shutdownType);
    }

    /* Irrelevant code removed */
}

main.c
extern void Shutdown(const signed int shutdownType) {
    /* Shutdown various things before exit */
    SYS_ResetSystem(shutdownType, 0, 0);
}

Init_Controllers() is called during startup, and Check_Controllers() is called once per frame. The Shutdown(...) function is called from within the app elsewhere too, and it exits cleanly. If I press the power or reset buttons though, instant crash... any ideas suggestions?

Edit: Forgot to mention that if I remove
if (shutdownType != -1) {
    Shutdown(shutdownType);
}
from Check_Controllers() I still get instant crash when I press power or reset.



Edited 2 time(s). Last edit at 02/15/2009 06:44AM by DrTwox.
Re: Wiimote Power Button
February 15, 2009 04:05PM
DrTwox: Here is how I was able to fix my code.

I put this globally:
bool shutdown = false; //If true shotdown the wii

// WPADShutdownCallback 
    void doPadPowerOff(s32 chan){	
	
	if(chan == WPAD_CHAN_0)
		shutdown = true;

	return;
    }

Put this somewhere in your code. You only need to call it once after WPAD_Init();
WPAD_SetPowerButtonCallback(doPadPowerOff);//Set up power button

And then put this in any loops that need it:
if(shutdown == true)
SYS_ResetSystem( SYS_POWEROFF, 0, 0 );

NOTE: This code copied directly out of my WiiBreaker source code. I made no changes to it in copying and pasting it.
Re: Wiimote Power Button
February 15, 2009 11:03PM
Thanks for the help Arikado!

However, I don't see any functional difference between what you've suggested (and previous posts) to what I'm doing... yet mine crashes instantly when the callback functions are triggered. I must be missing/overlooking something obvious, but I just can't see it!
Re: Wiimote Power Button
February 15, 2009 11:47PM
You cant call SYS_ResetSystem() in the callback function. I dont know why. So I just used a bool instead. Sure, its an extra step but at least it works.
Sorry, only registered users may post in this forum.

Click here to login