1 /*---------------------------------------------------------------------------*
2   Project:    WPAD demo program
3   File:       extension.c
4 
5   Copyright (C) 2005-2006 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: handling.c,v $
14   Revision 1.6  08/03/2006 13:33:02  tojo
15   Removed old APIs.
16 
17   Revision 1.5  07/20/2006 06:40:28  tojo
18   (none)
19 
20   Revision 1.4  06/17/2006 12:56:54  ekwon
21   Added explicit demonstration of handling sampling callback and auto-sampling buffer registratiion for various events.
22 
23   Revision 1.3  06/16/2006 14:01:25  ekwon
24   Added scrolling debug buffer. Completed event handling. NOTE: Classic controller support is pending, as I don't have one of those yet.
25 
26   Revision 1.2  06/15/2006 13:47:44  tojo
27   Removed DOLPHIN.
28   Swept warnings.
29 
30   Revision 1.1  06/15/2006 04:25:35  ekwon
31   preliminary checkins for event handling demos.
32 
33 
34 
35  *---------------------------------------------------------------------------*/
36 
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stddef.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <ctype.h>
43 
44 #include <revolution.h>
45 #include <revolution/wpad.h>
46 
47 #define DEMO_USE_MEMLIB=1 // This turns on the DEMO library's MEM heaps.
48 #include <demo.h>
49 
50 /*---------------------------------------------------------------------------*
51  * Local Definitions
52  *---------------------------------------------------------------------------*/
53 
54 #define SCREEN_WIDTH  640
55 #define SCREEN_HEIGHT 480
56 #define FONT_HEIGHT     8
57 
58 /*---------------------------------------------------------------------------*
59  * Local Data
60  *---------------------------------------------------------------------------*/
61 
62 
63 #define SMPBUF_SIZE 100
64 
65 static WPADFSStatus fs_ringbuffer[WPAD_MAX_CONTROLLERS][SMPBUF_SIZE];
66 static WPADStatus   co_ringbuffer[WPAD_MAX_CONTROLLERS][SMPBUF_SIZE];
67 
68 
69 /*---------------------------------------------------------------------------*
70  * Function prototypes
71  *---------------------------------------------------------------------------*/
72 
73 
74 // MEM2 memory allocation routines. The application must provide these to
75 // WPAD, so it can setup the data transfer buffer. This buffer must reside
76 // in MEM2.
77 static void *myAlloc                 (u32 size);
78 static u8    myFree                  (void *ptr);
79 
80 
81 // sampling callback
82 void         samplingCallback        (s32 chan);
83 
84 
85 // callbacks for CONNECT and EXTENSION events
86 void         connectCallback         (s32 chan, s32 reason);
87 void         extensionCallback       (s32 chan, s32 result);
88 
89 // internal functions
90 static void  initialize              (void);
91 static void  renderStatus            (void);
92 static void  printBuffer             (char *string, ...);
93 static u8   *get_dev_name            (u32 type);
94 static u8   *get_status_name         (s32 status);
95 
96 static void  renderAimings           (void);
97 
98 
99 /*===========================================================================*
100  *                   F U N C T I O N    D E F I N I T I O N S
101  *===========================================================================*/
102 
103 
104 /*---------------------------------------------------------------------------*
105  * Name        : main()
106  * Description :
107  * Arguments   : None.
108  * Returns     : None.
109  *---------------------------------------------------------------------------*/
main(void)110 int main( void )
111 {
112 
113     int i;
114 
115     s32 wpad_state;
116 
117         initialize();
118 
119         // - We must now register memory allocation/free functions
120         //   for MEM2.
121         // - WPAD requires some memory in MEM2 for data transfers
122         //   between the controller and WPAD driver stack.
123         // - Memory allocation only occurs once, at the initialization.
124         // - Memory usage is on the order of 1KB.
125         // - NOTE: We are using the MEM library allocators defined by
126         //   the DEMO library.
127         //
128         WPADRegisterAllocator(myAlloc, myFree);
129 
130         // Initialize WPAD!
131         WPADInit();
132 
133         // The WPAD initialization process is asynchronous.
134         // So we should wait until it's completed.
135         do
136         {
137             wpad_state = WPADGetStatus();
138 
139         } while (WPAD_STATE_SETUP != wpad_state);
140 
141 
142         for (i=0; i<WPAD_MAX_CONTROLLERS; i++)
143         {
144             // Register a callback for each channel, for
145             // connect and disconnect events.
146             WPADSetConnectCallback(i, connectCallback);
147         }
148 
149 
150         while(1)
151         {
152             DEMOBeforeRender();
153             renderStatus();
154             renderAimings();
155             DEMODoneRender();
156         }
157 
158 
159 } // end main()
160 
161 /*---------------------------------------------------------------------------*
162  * Name        : connectCallback()
163  *
164  * Description : This callback is invoked when a controller is connected or
165  *               disconnected.
166  *
167  * Arguments   : The channel (chan) for which the event has occurred.
168  *               The channel status (reason):
169  *                 WPAD_ERR_NONE means a controller has been connected.
170  *                 WPAD_ERR_NO_CONTROLLER means a controller disconnected.
171  *
172  * Returns     : None.
173  *---------------------------------------------------------------------------*/
connectCallback(s32 chan,s32 reason)174 void connectCallback(s32 chan, s32 reason)
175 {
176 #pragma unused (chan, reason)
177 
178     s32 status;
179     u32 type;
180 
181 
182         switch(reason)
183         {
184             case WPAD_ERR_NONE:
185 
186 
187                 // If the connection callback is invoked with status WPAD_ERR_NONE,
188                 // that means a controller has successfully connected. We must now
189                 // perform some chores...
190 
191                 printBuffer("Chan %1d is connected.", chan);
192 
193                 // If we want to support dynamic attachment/detachment of an
194                 // extension, we must register an extension callback.
195                 WPADSetExtensionCallback(chan, extensionCallback);
196 
197                 // If we want to use the sampling callback, we need to register
198                 // the callback when a device is connected.
199                 WPADSetSamplingCallback(chan, samplingCallback);
200 
201                 // Once connected, we must check if the device
202                 // has an extension attached, and initialize
203                 // it appropriately. Note that we do not receive
204                 // an extension callback if an extension is already
205                 // attached.
206 
207                 status = WPADProbe(chan, &type);
208                 switch(type)
209                 {
210                     case WPAD_DEV_CORE:
211                         // turn on DPD and accelerometers
212                         WPADControlDpd(chan, WPAD_DPD_EXP, NULL);
213                         WPADSetDataFormat(chan, WPAD_FMT_CORE_ACC_DPD);
214 
215                         // If we want to use the autosampling buffer feature of WPAD,
216                         // we must register the appropriate buffer...
217                         WPADSetAutoSamplingBuf(chan, (void *)(&co_ringbuffer[chan][0]), SMPBUF_SIZE);
218 
219                         break;
220 
221                     case WPAD_DEV_FREESTYLE:
222 
223                         // turn on DPD and accelerometers
224                         // Note that the DPD command is DIFFERENT for FreeStyle
225                         WPADControlDpd(chan, WPAD_DPD_STD, NULL);
226                         WPADSetDataFormat(chan, WPAD_FMT_FREESTYLE_ACC_DPD);
227 
228                         // If we want to use the autosampling buffer feature of WPAD,
229                         // we must register the appropriate buffer...
230                         WPADSetAutoSamplingBuf(chan, (void *)(&fs_ringbuffer[chan][0]), SMPBUF_SIZE);
231 
232                         break;
233 
234                     case WPAD_DEV_CLASSIC:
235                         printBuffer("CLASSIC not supported yet.");
236                         break;
237 
238                     default:
239                         printBuffer("Unknown device on chan %1d.", chan);
240                         break;
241                 } // end switch
242 
243                 break;
244 
245             case WPAD_ERR_NO_CONTROLLER:
246                 printBuffer("Chan %1d has been disconnected.", chan);
247 
248                 // We don't have to do anything.
249                 // Note that all callbacks and the autosampling buffer will be
250                 // cleared on disconnect events.
251                 //
252                 // Of course, the connect callback will still be valid for this channel.
253 
254                 break;
255 
256             default:
257                 // unexpected result!
258                 printBuffer("Unknown event %d on channel %1d.", reason, chan);
259                 break;
260 
261         } // end switch
262 
263 } // end connectCallback()
264 
265 /*---------------------------------------------------------------------------*
266  * Name        : extensionCallback()
267  *
268  * Description : This callback is invoked when an Extension has been attached.
269  *
270  * Arguments   : The channel (chan) for which the extension event occurred.
271  *               The device type (result):
272  *
273  *                 WPAD_DEV_UNKNOWN means that something has been attached, but
274  *                 it's being initialized, and we won't know what it is until
275  *                 initialization is complete.
276  *
277  *                 WPAD_DEV_CORE means that an extension has been removed and
278  *                 we're back to just the core device.
279  *
280  *                 WPAD_DEV_FREESTYLE means that the "NUNCHAK" extension has
281  *                 been attached and initialized.
282  *
283  *                 WPAD_DEV_CLASSIC means that the "CLASSIC" extension has been
284  *                 attached and initialized.
285  *
286  * Returns     : None.
287  *---------------------------------------------------------------------------*/
extensionCallback(s32 chan,s32 result)288 void extensionCallback(s32 chan, s32 result)
289 {
290 #pragma unused (chan, result)
291 
292     OSReport("extension[%d]\n",result);
293 
294     switch(result)
295     {
296         case WPAD_DEV_UNKNOWN:
297 
298             // in this case, the extension has been attached by the user
299             // but we don't know what type it is yet. Initialization of
300             // the attachment may take a few seconds, during which time
301             // button presses, the DPD, and accelerometers will be affected.
302             // This event is provided so applications can notify the
303             // user of the interruption and/or pause the game.
304             printBuffer("Initializing extension on chan %1d...", chan);
305             break;
306 
307         case WPAD_DEV_CORE:
308 
309             // In this case, the user has disconnected an extension.
310             // So, the device type will revert to "core only".
311             WPADControlDpd(chan, WPAD_DPD_EXP, NULL);
312             WPADSetDataFormat(chan, WPAD_FMT_CORE_ACC_DPD);
313 
314             // Note we must reset the auto-sampling buffer so that WPAD
315             // will write into the appropriate buffer
316             WPADSetAutoSamplingBuf(chan, (void *)(&co_ringbuffer[chan][0]), SMPBUF_SIZE);
317 
318             // We must also re-register the sampling callback
319             WPADSetSamplingCallback(chan, samplingCallback);
320 
321             printBuffer("Extension removed on chan %1d.", chan);
322             break;
323 
324         case WPAD_DEV_FREESTYLE:
325 
326             // Now that we know the extension type, we must revise
327             // the data format and control the DPD accordingly.
328             WPADControlDpd(chan, WPAD_DPD_STD, NULL);
329             WPADSetDataFormat(chan, WPAD_FMT_FREESTYLE_ACC_DPD);
330 
331             // Note we must reset the auto-sampling buffer so that WPAD
332             // will write into the appropriate buffer
333             WPADSetAutoSamplingBuf(chan, (void *)(&fs_ringbuffer[chan][0]), SMPBUF_SIZE);
334 
335             // We must also re-register the sampling callback
336             WPADSetSamplingCallback(chan, samplingCallback);
337 
338 
339 
340             printBuffer("Freestyle initialized on chan %1d.", chan);
341             break;
342 
343         case WPAD_DEV_CLASSIC:
344 
345             // Now that we know the extension type, we must revise
346             // the data format and control the DPD accordingly.
347             printBuffer("Classic initialized on chan %1d.", chan);
348             break;
349 
350         case WPAD_DEV_NOT_FOUND:
351         default:
352 
353             // The user plugged in something weird.
354             // The safe thing to do is revert to Core-only operation.
355 
356             WPADControlDpd(chan, WPAD_DPD_EXP, NULL);
357             WPADSetDataFormat(chan, WPAD_FMT_CORE_ACC_DPD);
358 
359             printBuffer("Unknown device on chan %1d.", chan);
360 
361             break;
362 
363     } // end
364 
365 } // end extensionCallback()
366 
367 /*---------------------------------------------------------------------------*
368  * Name        : samplingCallback()
369  * Description : Invoked by WPAD every time data is available for WPADRead().
370  * Arguments   : Channel for which data is available.
371  * Returns     : None.
372  *---------------------------------------------------------------------------*/
373 
samplingCallback(s32 chan)374 void samplingCallback(s32 chan)
375 {
376 #pragma unused(chan)
377 
378     // for this demo, we're not actually doing anything here.
379     // Normally, you can call WPADProbe() and WPADRead() to retrieve
380     // the WPAD Status block for the indicated channel.
381     //
382     // However, since we're using the auto sampling buffer, we won't bother.
383     //
384     // We are using this empty function to demonstrate how to handle
385     // registration of sampling callback during connect/attach events.
386     //
387 
388 
389 } // end sampling Callback
390 
391 
392 
393 /*---------------------------------------------------------------------------*
394  * Name        : myAlloc()
395  * Description : Callback needed by WPAD to allocate mem from MEM2 heap
396  * Arguments   : size of block, in bytes.
397  * Returns     : pointer to allocated block.
398  *---------------------------------------------------------------------------*/
myAlloc(u32 size)399 static void *myAlloc(u32 size)
400 {
401     void *ptr;
402 
403     ptr = MEMAllocFromAllocator(&DemoAllocator2, size);
404     ASSERTMSG(ptr, "Memory allocation failed\n");
405 
406     return(ptr);
407 
408 } // myAlloc()
409 
410 /*---------------------------------------------------------------------------*
411  * Name        : myFree()
412  * Description : Callback needed by WPAD to free mem from MEM2 heap
413  * Arguments   : None.
414  * Returns     : Always 1.
415  *---------------------------------------------------------------------------*/
myFree(void * ptr)416 static u8 myFree(void *ptr)
417 {
418 
419     MEMFreeToAllocator(&DemoAllocator2, ptr);
420 
421     // we should ensure that memory is free'd properly, but oh well
422     return(1);
423 
424 } // myFree()
425 
426 
427 
428 /*===========================================================================*
429  *                     I N T E R N A L    F U N C T I O N S
430  *===========================================================================*/
431 /*---------------------------------------------------------------------------*
432  * The following functions handle the status display of this demo.
433  * They aren't really relevant to the demonstration of the CONNECT
434  * and EXTENSION callbacks.
435  *---------------------------------------------------------------------------*/
436 
437 
438 
439 /*---------------------------------------------------------------------------*
440  * Local Data
441  *---------------------------------------------------------------------------*/
442 
443 #define COL_WIDTH  13*FONT_HEIGHT
444 #define X_START    30
445 #define Y_START    51
446 
447 #define BUFFER_SIZE_BYTES 128
448 #define BUFFER_SIZE_LINES  10
449 static char __buffer[BUFFER_SIZE_LINES][BUFFER_SIZE_BYTES];
450 static u32  __curr_line = 0;
451 
452 
453 
renderStatus(void)454 static void renderStatus(void)
455 {
456 
457     WPADCLStatus  status_block[WPAD_MAX_CONTROLLERS];
458     WPADStatus   *wpad_ptr;
459     WPADCLStatus *cl_ptr;
460     WPADFSStatus *fs_ptr;
461 
462     s32 status;
463     u32 type;
464 
465     s32 i;
466     s16 x;
467     s16 y;
468 
469     u32 line;
470 
471 
472         // clear status block
473         memset((void *)(&status_block), 0, sizeof(WPADCLStatus) * WPAD_MAX_CONTROLLERS);
474 
475         x = X_START;
476         y = Y_START;
477 
478         DEMOPrintf(30, 20, 0, "WPAD Asynchronous Event Handling Demo");
479 
480 
481         DEMOPrintf(x, y, 0, "                Chan0       Chan1        Chan2        Chan3");
482         y+=FONT_HEIGHT;
483         DEMOPrintf(x, y, 0, "             ----------   ----------   ----------   ----------");
484 
485         y+=FONT_HEIGHT;
486         DEMOPrintf(x, y, 0, "     Status :"); y+=FONT_HEIGHT;
487         DEMOPrintf(x, y, 0, "     Type   :"); y+=FONT_HEIGHT; y+=FONT_HEIGHT;
488 
489         DEMOPrintf(x, y, 0, "     Buttons:"); y+=FONT_HEIGHT; y+=FONT_HEIGHT; y+=FONT_HEIGHT;
490 
491         DEMOPrintf(x, y, 0, "     DPD0-x :"); y+=FONT_HEIGHT;
492         DEMOPrintf(x, y, 0, "     DPD0-y :"); y+=FONT_HEIGHT;
493         DEMOPrintf(x, y, 0, "     DPD1-x :"); y+=FONT_HEIGHT;
494         DEMOPrintf(x, y, 0, "     DPD1-y :"); y+=FONT_HEIGHT; y+=FONT_HEIGHT;
495 
496         DEMOPrintf(x, y, 0, "     ACC-X  :"); y+=FONT_HEIGHT;
497         DEMOPrintf(x, y, 0, "     ACC-Y  :"); y+=FONT_HEIGHT;
498         DEMOPrintf(x, y, 0, "     ACC-Z  :"); y+=FONT_HEIGHT; y+=FONT_HEIGHT;
499 
500         DEMOPrintf(x, y, 0, "     StickX :"); y+=FONT_HEIGHT;
501         DEMOPrintf(x, y, 0, "     StickY :"); y+=FONT_HEIGHT; y+=FONT_HEIGHT;
502 
503         DEMOPrintf(x, y, 0, "     ACC-X  :"); y+=FONT_HEIGHT;
504         DEMOPrintf(x, y, 0, "     ACC-Y  :"); y+=FONT_HEIGHT;
505         DEMOPrintf(x, y, 0, "     ACC-Z  :"); y+=FONT_HEIGHT; y+=FONT_HEIGHT;
506 
507         for (i=0; i<WPAD_MAX_CONTROLLERS; i++)
508         {
509 
510             char buf1[] = "________";
511             char buf2[] = "___";
512 
513                 x = (s16)(X_START + ((1+i) * COL_WIDTH));
514                 y = (s16)(Y_START + 2 * FONT_HEIGHT);
515 
516                 status = WPADProbe(i, &type);
517 
518                 DEMOPrintf(x, y, 0, "%s", get_status_name(status)); y+= FONT_HEIGHT;
519                 DEMOPrintf(x, y, 0, "%s", get_dev_name(type));      y+= FONT_HEIGHT; y+=FONT_HEIGHT;
520 
521                 if (WPAD_ERR_NONE == status)
522                 {
523 
524                     WPADRead(i, &status_block[i]);
525                     wpad_ptr = (WPADStatus   *)(&status_block[i]);
526                     cl_ptr   = (WPADCLStatus *)(&status_block[i]);
527                     fs_ptr   = (WPADFSStatus *)(&status_block[i]);
528 
529                     if( wpad_ptr->button & WPAD_BUTTON_LEFT   ) buf1[ 0] = '<';
530                     if( wpad_ptr->button & WPAD_BUTTON_UP     ) buf1[ 1] = '^';
531                     if( wpad_ptr->button & WPAD_BUTTON_DOWN   ) buf1[ 2] = 'v';
532                     if( wpad_ptr->button & WPAD_BUTTON_RIGHT  ) buf1[ 3] = '>';
533 
534                     if( wpad_ptr->button & WPAD_BUTTON_START  ) buf1[ 4] = 'S';
535                     if( wpad_ptr->button & WPAD_BUTTON_SELECT ) buf1[ 5] = 'T';
536                     if( wpad_ptr->button & WPAD_BUTTON_A      ) buf1[ 6] = 'A';
537                     if( wpad_ptr->button & WPAD_BUTTON_B      ) buf1[ 7] = 'B';
538 
539                     if( wpad_ptr->button & WPAD_BUTTON_SMALL_A) buf2[ 0] = 'a';
540                     if( wpad_ptr->button & WPAD_BUTTON_SMALL_B) buf2[ 1] = 'b';
541                     if( wpad_ptr->button & WPAD_BUTTON_HOME   ) buf2[ 2] = 'H';
542 
543                     DEMOPrintf(x, y, 0, " %s",   buf1); y+= FONT_HEIGHT;
544                     DEMOPrintf(x, y, 0, "   %s", buf2); y+= FONT_HEIGHT;
545 
546                     y+= FONT_HEIGHT;
547 
548                     DEMOPrintf(x, y, 0, "   %04X", (u16)(wpad_ptr->obj[0].x)); y+= FONT_HEIGHT;
549                     DEMOPrintf(x, y, 0, "   %04X", (u16)(wpad_ptr->obj[0].y)); y+= FONT_HEIGHT;
550                     DEMOPrintf(x, y, 0, "   %04X", (u16)(wpad_ptr->obj[1].x)); y+= FONT_HEIGHT;
551                     DEMOPrintf(x, y, 0, "   %04X", (u16)(wpad_ptr->obj[1].y)); y+= FONT_HEIGHT;
552 
553                     y+= FONT_HEIGHT;
554 
555                     DEMOPrintf(x, y, 0, "   %04X", (u16)(wpad_ptr->accX)); y+= FONT_HEIGHT;
556                     DEMOPrintf(x, y, 0, "   %04X", (u16)(wpad_ptr->accY)); y+= FONT_HEIGHT;
557                     DEMOPrintf(x, y, 0, "   %04X", (u16)(wpad_ptr->accZ)); y+= FONT_HEIGHT;
558 
559                     y+= FONT_HEIGHT;
560 
561                     if (WPAD_DEV_FREESTYLE == type)
562                     {
563 
564                         DEMOPrintf(x, y, 0, "   %04X", (u16)(fs_ptr->fsStickX)); y+= FONT_HEIGHT;
565                         DEMOPrintf(x, y, 0, "   %04X", (u16)(fs_ptr->fsStickY)); y+= FONT_HEIGHT;
566 
567                         y+= FONT_HEIGHT;
568 
569                         DEMOPrintf(x, y, 0, "   %04X", (u16)(fs_ptr->fsAccX)); y+= FONT_HEIGHT;
570                         DEMOPrintf(x, y, 0, "   %04X", (u16)(fs_ptr->fsAccY)); y+= FONT_HEIGHT;
571                         DEMOPrintf(x, y, 0, "   %04X", (u16)(fs_ptr->fsAccZ)); y+= FONT_HEIGHT;
572 
573                         y+= FONT_HEIGHT;
574 
575                     } // if freestyle
576 
577                 } // if no error
578 
579 
580         } // for...WPAD_MAX_CONTROLLERS
581 
582 
583         // for scrolling debug-message buffer
584         // it's convenient to see past event messages...
585         y = 300;
586 
587         line = __curr_line;
588         for (i=0; i<BUFFER_SIZE_LINES; i++)
589         {
590             DEMOPrintf(15, y, 0, ">> %s", &__buffer[line][0]);
591             line = (line + 1) % BUFFER_SIZE_LINES;
592             y+= FONT_HEIGHT;
593         }
594 
595 
596 
597 } // end renderStatus()
598 
printBuffer(char * string,...)599 static void printBuffer(char *string, ...)
600 {
601 
602     va_list vlist;
603 
604         memset(&__buffer[__curr_line][0], 0, BUFFER_SIZE_BYTES);
605 
606         va_start(vlist, string);
607         vsprintf((char *)(&__buffer[__curr_line][0]), string, vlist);
608         va_end(vlist);
609 
610         __curr_line = (__curr_line + 1) % BUFFER_SIZE_LINES;
611 
612 
613 } // clear and dump string to buffer
614 
615 
get_dev_name(u32 type)616 static u8 *get_dev_name(u32 type)
617 {
618 
619     u8 str_dev_core   [] = "   Core   ";
620     u8 str_dev_extn   [] = "   Extn   ";
621     u8 str_dev_cl     [] = "  Classic ";
622     u8 str_dev_dol    [] = "  Dolphin ";
623     u8 str_dev_unknown[] = "  Unknwon ";
624 
625     u8 *ptr;
626 
627         switch(type)
628         {
629 
630             case WPAD_DEV_CORE:
631                 ptr = &str_dev_core[0];
632                 break;
633 
634             case WPAD_DEV_FREESTYLE:
635                 ptr = &str_dev_extn[0];
636                 break;
637 
638             case WPAD_DEV_CLASSIC:
639                 ptr = &str_dev_cl[0];
640                 break;
641 
642             case WPAD_DEV_UNKNOWN:
643             default:
644                 ptr = &str_dev_unknown[0];
645                 break;
646 
647         } // end switch
648 
649         return(ptr);
650 
651 } // end
652 
653 
get_status_name(s32 status)654 static u8 *get_status_name(s32 status)
655 {
656 
657     u8 str_status_ok  [] = "    OK    ";
658     u8 str_status_none[] = "   NONE   ";
659     u8 str_status_busy[] = "   BUSY   ";
660     u8 str_status_xfer[] = " Transfer ";
661     u8 str_status_inv [] = "  INVALID ";
662     u8 str_status_unk [] = "  UNKNOWN ";
663 
664     u8 *ptr;
665 
666         switch(status)
667         {
668 
669             case WPAD_ERR_NONE:
670                 ptr = &str_status_ok[0];
671                 break;
672 
673             case WPAD_ERR_NO_CONTROLLER:
674                 ptr = &str_status_none[0];
675                 break;
676 
677             case WPAD_ERR_BUSY:
678                 ptr = &str_status_busy[0];
679                 break;
680 
681             case WPAD_ERR_TRANSFER:
682                 ptr = &str_status_xfer[0];
683                 break;
684 
685             case WPAD_ERR_INVALID:
686                 ptr = &str_status_inv[0];
687             default:
688                 ptr = &str_status_unk[0];
689                 break;
690 
691         } // end switch
692 
693         return(ptr);
694 
695 } // end
696 
697 
initialize(void)698 static void initialize( void )
699 {
700     const GXColor DARKBLUE = { 0, 0, 40, 255 };
701 
702         OSInit();
703 
704         DEMOInit( &GXNtsc480IntDf );
705 
706         GXSetCopyClear( DARKBLUE, GX_MAX_Z24 );
707         GXCopyDisp( DEMOGetCurrentBuffer(), GX_TRUE );
708         DEMOInitCaption( DM_FT_XLU, SCREEN_WIDTH, SCREEN_HEIGHT );
709         GXSetZMode( GX_ENABLE, GX_ALWAYS, GX_ENABLE );                       // Set pixel processing mode
710         GXSetBlendMode( GX_BM_BLEND, GX_BL_ONE, GX_BL_ONE, GX_LO_CLEAR );    // Translucent mode
711 
712         memset(__buffer, 0, BUFFER_SIZE_BYTES*BUFFER_SIZE_LINES);
713 
714 } // end
715 
716 
717 /*---------------------------------------------------------------------------*
718  * Name        : renderAimings()
719  * Description :
720  * Arguments   : None.
721  * Returns     : None.
722  *---------------------------------------------------------------------------*/
renderAimings(void)723 static void renderAimings(void)
724 {
725     u32 latest;
726     u32 index;
727 
728     int i;
729     int j;
730 
731     u32 fmt;
732 
733     WPADStatus   *co_ptr;
734     WPADFSStatus *fs_ptr;
735 
736 
737         for (j=0; j<WPAD_MAX_CONTROLLERS; j++)
738         {
739 
740             fmt = WPADGetDataFormat(j);
741 
742             switch (fmt)
743             {
744                 case WPAD_FMT_CORE:
745                 case WPAD_FMT_CORE_ACC:
746                 case WPAD_FMT_CORE_ACC_DPD:
747 
748                     co_ptr = (WPADStatus *)(&co_ringbuffer[j][0]);
749 
750                     latest = WPADGetLatestIndexInBuf(j);
751                     index  = ((latest - 1) + SMPBUF_SIZE) % SMPBUF_SIZE;
752 
753                     for (i=0; i<SMPBUF_SIZE-1; i++)
754                     {
755                         if (WPAD_ERR_NONE == co_ptr[index].err)
756                         {
757 		                    s16 x=(s16)(SCREEN_WIDTH -(co_ptr[index].obj[0].x+co_ptr[index].obj[1].x)/2*SCREEN_WIDTH /WPAD_DPD_IMG_RESO_WX);
758 		                    s16 y=(s16)(SCREEN_HEIGHT-(co_ptr[index].obj[0].y+co_ptr[index].obj[1].y)/2*SCREEN_HEIGHT/WPAD_DPD_IMG_RESO_WY);
759                             DEMOPrintf(x, y, 0, ".");
760                         }
761                         index  = ((index - 1) + SMPBUF_SIZE) % SMPBUF_SIZE;
762                     }
763                     break;
764 
765                 case WPAD_FMT_FREESTYLE:
766                 case WPAD_FMT_FREESTYLE_ACC:
767                 case WPAD_FMT_FREESTYLE_ACC_DPD:
768 
769                     fs_ptr = (WPADFSStatus *)(&fs_ringbuffer[j][0]);
770 
771                     latest = WPADGetLatestIndexInBuf(j);
772                     index  = ((latest - 1) + SMPBUF_SIZE) % SMPBUF_SIZE;
773 
774                     for (i=0; i<SMPBUF_SIZE-1; i++)
775                     {
776                         if (WPAD_ERR_NONE == fs_ptr[index].err)
777                         {
778 		                    s16 x=(s16)(SCREEN_WIDTH -(fs_ptr[index].obj[0].x+fs_ptr[index].obj[1].x)/2*SCREEN_WIDTH /WPAD_DPD_IMG_RESO_WX);
779 		                    s16 y=(s16)(SCREEN_HEIGHT-(fs_ptr[index].obj[0].y+fs_ptr[index].obj[1].y)/2*SCREEN_HEIGHT/WPAD_DPD_IMG_RESO_WY);
780                             DEMOPrintf(x, y, 0, ".");
781                         }
782                         index  = ((index - 1) + SMPBUF_SIZE) % SMPBUF_SIZE;
783                     }
784                     break;
785 
786 
787             } // end switch
788 
789 
790         } // end for...max controllers
791 
792 } // end
793