/*---------------------------------------------------------------------------* Project: WPAD demo program File: sampling.c Programmers: HIRATSU Daisuke TOKUNAGA, Yasuhiro Copyright (C) 2005-2006 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: sampling.c,v $ Revision 1.11 06/16/2006 08:37:31 tojo (none) Revision 1.10 06/13/2006 04:55:21 tojo (none) Revision 1.9 06/13/2006 01:57:26 ekwon Minor cleanup. Revision 1.8 06/12/2006 12:03:28 ekwon Fixes for querying DPD status before enabling DPD. Revision 1.7 06/12/2006 08:48:16 ekwon Must re-establish sampling buffer AND DPD/ACC init upon reconnect. Revision 1.6 06/12/2006 08:22:53 ekwon Sampling demo for WPADSetAutoSamplingBuf(). Revision 1.1 06/01/2006 00:40:11 hiratsu Moved from $(SDK)/build/demos/wpaddemo Revision 1.4 2006/03/13 02:10:46 yasuh-to Modified copyright. *---------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #define DEMO_USE_MEMLIB=1 // This turns on the DEMO library's MEM heaps. #include #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 #define SMPBUF_SIZE 100 // Size of WPADStatus array for auto sampling. // ring buffer to store data from auto-sampling feature static WPADStatus s_ringBuf[SMPBUF_SIZE]; /*---------------------------------------------------------------------------* * Function prototypes *---------------------------------------------------------------------------*/ // MEM2 memory allocation routines. The application must provide these to // WPAD, so it can setup the data transfer buffer. This buffer must reside // in MEM2. static void *myAlloc (u32 size); static u8 myFree (void *ptr); static void initialize (void); static void renderStatus (WPADStatus *stat); static void renderAimings (void); /*===========================================================================* * F U N C T I O N D E F I N I T I O N S *===========================================================================*/ /*---------------------------------------------------------------------------* * Name : main() * Description : * Arguments : None. * Returns : None. *---------------------------------------------------------------------------*/ int main( void ) { WPADStatus wpad; s32 wpad_state; s32 status; u32 type; u32 index; initialize(); // we should clear the WPADStatus block returned by // WPADRead(), because if no channel is connected, nothing // is copied. So we would be staring at garbage data. memset( (void *)(&wpad), 0, sizeof(WPADStatus)); // must register MEM2 allocation functions before invoking WPADInit() WPADRegisterAllocator(myAlloc, myFree); WPADInit(); // The WPAD initialization process is asynchronous. // So we should wait until it's completed. do { wpad_state = WPADGetStatus(); } while (WPAD_STATE_SETUP != wpad_state); // Main loop while( 1 ) { status = WPADProbe(WPAD_CHAN0, &type); if (WPAD_ERR_NONE == status) { // Note: If the controller has disconnected and re-connected, // we need to reset and re-enable DPD and the auto-sampling buffer. // But we have to ensure that the device type is correct, because // it's possible a different type of device has connected on channel 0. if ((WPAD_DEV_CORE == type) || (WPAD_DEV_FREESTYLE == type)) { // Are the DPD and Accelerometers enabled? if (FALSE == WPADIsDpdEnabled(WPAD_CHAN0)) { // if not, then set the format and turn on DPD and accelerometer WPADControlDpd(WPAD_CHAN0, WPAD_DPD_EXP, NULL); WPADSetDataFormat(WPAD_CHAN0, WPAD_FMT_CORE_ACC_DPD); // ...and re-establish the sampling buffer here WPADSetAutoSamplingBuf( WPAD_CHAN0, s_ringBuf, SMPBUF_SIZE ); } // if DPD/ACC enabled? } // if device type } // if no error index = WPADGetLatestIndexInBuf(WPAD_CHAN0); DEMOBeforeRender(); renderStatus(&s_ringBuf[index]); renderAimings(); DEMODoneRender(); } // while return 0; } // end /*---------------------------------------------------------------------------* * Name : initialize() * Description : * Arguments : None. * Returns : None. *---------------------------------------------------------------------------*/ static void initialize(void) { const GXColor DARKBLUE = { 0, 0, 40, 255 }; OSInit(); DEMOInit( &GXNtsc480IntDf ); GXSetCopyClear( DARKBLUE, GX_MAX_Z24 ); GXCopyDisp( DEMOGetCurrentBuffer(), GX_TRUE ); DEMOInitCaption( DM_FT_XLU, SCREEN_WIDTH, SCREEN_HEIGHT ); GXSetZMode( GX_ENABLE, GX_ALWAYS, GX_ENABLE ); // Set pixel processing mode GXSetBlendMode( GX_BM_BLEND, GX_BL_ONE, GX_BL_ONE, GX_LO_CLEAR ); // Translucent mode } // end /*---------------------------------------------------------------------------* * Name : renderStatus() * Description : * Arguments : None. * Returns : None. *---------------------------------------------------------------------------*/ static void renderStatus(WPADStatus *stat) { const int FONT_HEIGHT = 8; char buf[] = "___________"; s16 x = FONT_HEIGHT; s16 y = FONT_HEIGHT; int i = 0; // just print accelerometer data DEMOPrintf( x, y+=FONT_HEIGHT, 0, "accX : %d", stat->accX ); DEMOPrintf( x, y+=FONT_HEIGHT, 0, "accY : %d", stat->accY ); DEMOPrintf( x, y+=FONT_HEIGHT, 0, "accZ : %d", stat->accZ ); if( stat->button & WPAD_BUTTON_RIGHT ) buf[0] = '>'; if( stat->button & WPAD_BUTTON_DOWN ) buf[1] = 'v'; if( stat->button & WPAD_BUTTON_UP ) buf[2] = '^'; if( stat->button & WPAD_BUTTON_LEFT ) buf[3] = '<'; if( stat->button & WPAD_BUTTON_START ) buf[4] = 'S'; if( stat->button & WPAD_BUTTON_SELECT ) buf[5] = 'T'; if( stat->button & WPAD_BUTTON_A ) buf[7] = 'A'; if( stat->button & WPAD_BUTTON_B ) buf[6] = 'B'; if( stat->button & WPAD_BUTTON_SMALL_A) buf[8] = 'a'; if( stat->button & WPAD_BUTTON_SMALL_B) buf[9] = 'b'; if( stat->button & WPAD_BUTTON_HOME ) buf[10]= 'H'; DEMOPrintf( x, y+=FONT_HEIGHT, 0, "Buttons: [%s]",buf); y+=FONT_HEIGHT; DEMOPrintf( x, y+=FONT_HEIGHT, 0, "DPD-ID X Y SIZE"); for( i=0; iobj[i] ); DEMOPrintf( x, y+=FONT_HEIGHT, 0, "%1d %4d %4d %3d", p->traceId, p->x, p->y, p->size ); } y+=FONT_HEIGHT; DEMOPrintf( x, y+=FONT_HEIGHT, 0, "Controller Type: %d", stat->dev ); DEMOPrintf( x, y+=FONT_HEIGHT, 0, "ERROR Info : %d", stat->err ); if (WPAD_ERR_NO_CONTROLLER == stat->err) { y+=FONT_HEIGHT; y+=FONT_HEIGHT; DEMOPrintf(x, y+=FONT_HEIGHT, 0, ">> No controller is connected!"); DEMOPrintf(x, y+=FONT_HEIGHT, 0, ">> Please press a button on a paired"); DEMOPrintf(x, y+=FONT_HEIGHT, 0, ">> controller to connect."); } } // end /*---------------------------------------------------------------------------* * Name : renderAimings() * Description : * Arguments : None. * Returns : None. *---------------------------------------------------------------------------*/ static void renderAimings(void) { u32 latest; int i; int index; latest = WPADGetLatestIndexInBuf(WPAD_CHAN0); for (i=0; i