1 /*---------------------------------------------------------------------------*
2   Project:    WPAD demo program
3   File:       sync.c
4 
5   Copyright (C) 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: sync.c,v $
14   Revision 1.8.2.1  2007/12/11 01:18:54  tojo
15   Modified to handle the status code of WPADProbe.
16 
17   Revision 1.8  2007/07/11 11:54:22  tojo
18   Fixed the message handling.
19 
20   Revision 1.7  2007/07/10 12:14:46  tojo
21   (none)
22 
23   Revision 1.6  2007/04/23 09:02:45  tojo
24   (none)
25 
26   Revision 1.5  2006/10/23 01:19:08  tojo
27   Called WPADSetSyncCallback before initialization complete.
28 
29   Revision 1.4  2006/10/02 07:00:21  takano_makoto
30   fix PrintIntro messages.
31 
32   Revision 1.3  2006/09/19 02:16:19  tojo
33   Added fast sync.
34 
35   Revision 1.2  2006/09/08 09:44:18  tojo
36   (none)
37 
38   Revision 1.1  2006/09/06 03:13:43  tojo
39   (none)
40 
41 
42  *---------------------------------------------------------------------------*/
43 
44 #define DEMO_USE_MEMLIB = 1
45 #include <revolution/mem.h>
46 #include <revolution/wpad.h>
47 #include <demo.h>
48 #include <string.h>
49 
50 #define SCREEN_WIDTH  320
51 #define SCREEN_HEIGHT 240
52 
53 /*---------------------------------------------------------------------------*
54  * Private function prototypes
55  *---------------------------------------------------------------------------*/
56 void *myAlloc( u32 size  );
57 u8    myFree ( void *ptr );
58 
59 /*---------------------------------------------------------------------------*
60  * Globals
61  *---------------------------------------------------------------------------*/
62 static const s16 LINE = 8;
63 
64 static u8 standard = 0;
65 static u8 simple   = 0;
66 static s32 stdNum   = 0;
67 static s32 smpNum   = 0;
68 
69 /*---------------------------------------------------------------------------*
70  * For WPAD testing
71  *---------------------------------------------------------------------------*/
72 
73 /*---------------------------------------------------------------------------*
74  * Name        : myAlloc()
75  * Description :
76  * Arguments   : None.
77  * Returns     : None.
78  *---------------------------------------------------------------------------*/
myAlloc(u32 size)79 void *myAlloc( u32 size )
80 {
81     void *ptr;
82 
83     ptr = MEMAllocFromAllocator(&DemoAllocator2, size);
84     ASSERTMSG(ptr, "Memory allocation failed\n");
85 
86     return(ptr);
87 }
88 
89 /*---------------------------------------------------------------------------*
90  * Name        : myFree()
91  * Description :
92  * Arguments   : None.
93  * Returns     : None.
94  *---------------------------------------------------------------------------*/
myFree(void * ptr)95 u8 myFree( void *ptr )
96 {
97     MEMFreeToAllocator(&DemoAllocator2, ptr);
98 
99     return(1);
100 }
101 
102 
103 /*---------------------------------------------------------------------------*
104  * Name        : RenderConsoleStatus()
105  * Description :
106  * Arguments   : None.
107  * Returns     : None.
108  *---------------------------------------------------------------------------*/
RenderConsoleStatus(void)109 static void RenderConsoleStatus( void )
110 {
111     s16 y = 16;
112     s32 chan;
113     u8 devAddr[6];
114     u32 dev_type;
115     u8 sync_type;
116 
117     const char *std[] = {"----", "start", "busy", "done"};
118     const char *smp[] = {"----", "start", "busy", "stopping", "done"};
119 
120     DEMOPrintf( 16, y+=LINE, 0, "WPAD Demo -- Sync");
121 
122     y += LINE;
123 
124     DEMOPrintf( 16, y+=LINE, 0, "Registered Device Number: %d", WPADGetRegisteredDevNum());
125     DEMOPrintf( 16, y+=LINE, 0, "Temporary  Device Number: %d", WPADGetTemporaryDevNum());
126 
127     y += LINE;
128 
129     for(chan=0; chan<WPAD_MAX_CONTROLLERS; chan++)
130     {
131         if (WPADProbe(chan, &dev_type) != WPAD_ERR_NO_CONTROLLER)
132         {
133             WPADGetAddress(chan, devAddr);
134             WPADGetSyncType(chan, &sync_type);
135             DEMOPrintf( 16, y += LINE, 0, "chan%d: %02x:%02x:%02x:%02x:%02x:%02x [%s]",
136                 chan,
137                 devAddr[0],
138                 devAddr[1],
139                 devAddr[2],
140                 devAddr[3],
141                 devAddr[4],
142                 devAddr[5],
143                 (sync_type == WPAD_SYNC_TYPE_STD) ? "Standard" : "Simple"
144             );
145         }
146         else
147         {
148             DEMOPrintf( 16, y += LINE, 0, "chan%d: not connected", chan);
149         }
150     }
151 
152     DEMOPrintf( 16, y+=LINE*3, 0, "standard: %s [%d device(s)]", std[standard], stdNum);
153     DEMOPrintf( 16, y+=LINE,   0, "simple  : %s [%d device(s)]", smp[simple], smpNum);
154 }
155 
156 /*---------------------------------------------------------------------------*
157  * Name        : initialize()
158  * Description :
159  * Arguments   : None.
160  * Returns     : None.
161  *---------------------------------------------------------------------------*/
initialize(void)162 static void initialize( void )
163 {
164     const GXColor DARKBLUE = { 0, 0, 64, 255 };
165 
166     OSInit();
167 
168     DEMOInit( &GXNtsc480IntDf );
169     GXSetCopyClear( DARKBLUE, GX_MAX_Z24 );
170     GXCopyDisp( DEMOGetCurrentBuffer(), GX_TRUE );
171     DEMOInitCaption( DM_FT_XLU, SCREEN_WIDTH, SCREEN_HEIGHT );
172     GXSetZMode( GX_ENABLE, GX_ALWAYS, GX_ENABLE );                       // Set pixel processing mode
173     GXSetBlendMode( GX_BM_BLEND, GX_BL_ONE, GX_BL_ONE, GX_LO_CLEAR );    // Translucent mode
174 
175     DEMOPadInit();
176 
177 } /* end initialize() */
178 
SyncCallback(s32 result,s32 num)179 static void SyncCallback( s32 result, s32 num )
180 {
181     switch (result)
182     {
183         case WPAD_SYNC_EVT_START:
184             if (WPADStartSyncDevice())
185             {
186                 standard = 1;
187                 simple = 0;
188             }
189             break;
190 
191         case WPAD_SYNC_EVT_DONE:
192             standard = 3;
193             stdNum = num;
194             break;
195     }
196     OSReport("SYNC STD EVT: %d, %d\n", result, num);
197 }
198 
SyncSmpCallback(s32 result,s32 num)199 static void SyncSmpCallback( s32 result, s32 num )
200 {
201     switch (result)
202     {
203         case WPAD_SYNC_EVT_DONE:
204             simple = 4;
205             smpNum = num;
206             break;
207     }
208 
209     OSReport("SYNC SMP EVT: %d, %d\n", result, num);
210 }
211 
212 /*---------------------------------------------------------------------------*
213  * Name        : PrintIntro()
214  * Description :
215  * Arguments   : None.
216  * Returns     : None.
217  *---------------------------------------------------------------------------*/
PrintIntro(void)218 static void PrintIntro( void )
219 {
220     OSReport("+------------ WPAD Sample Demo Program ---------------+\n");
221     OSReport(" MENU:                                               \n");
222     OSReport("  A button: Start standard pairing.                   \n");
223     OSReport("  B button: Start deleting all controllers.           \n");
224     OSReport("  X button: Start simple pairing.                     \n");
225     OSReport("  Y button: Stop  simple pairing.                     \n");
226     OSReport(" Trigger L: Start fast standard pairing.              \n");
227     OSReport(" Trigger R: Start fast simple pairing.                \n");
228     OSReport("+-----------------------------------------------------+\n");
229 
230 }
231 
232 /*---------------------------------------------------------------------------*
233  * Name        : main()
234  * Description :
235  * Arguments   : None.
236  * Returns     : None.
237  *---------------------------------------------------------------------------*/
main(void)238 int main( void )
239 {
240     s32 bb_stat;
241     u32 curr = 0;
242     u32 prev = 0;
243 
244     initialize();
245 
246     // register allocator for stack before WPADInit().
247     WPADRegisterAllocator(myAlloc, myFree);
248 
249     // Initialize WPAD.
250     WPADInit();
251 
252     WPADSetSyncDeviceCallback(SyncCallback);
253     WPADSetSimpleSyncCallback(SyncSmpCallback);
254 
255     // wait that BT stack is enabled
256     do {
257         bb_stat = WPADGetStatus();
258     } while (bb_stat != WPAD_STATE_SETUP);
259 
260     PrintIntro();
261 
262     // Main loop
263     while( 1 )
264     {
265         DEMOPadRead();
266 
267         curr = DEMOPadGetButton(0);
268 
269         if (PADButtonDown(prev, curr) & PAD_BUTTON_A)
270         {
271             if (WPADStartSyncDevice())
272             {
273                 standard = 1;
274                 simple = 0;
275             }
276         }
277         if (PADButtonDown(prev, curr) & PAD_BUTTON_B)
278         {
279             WPADStartClearDevice();
280         }
281         if (PADButtonDown(prev, curr) & PAD_BUTTON_X)
282         {
283             if (WPADStartSimpleSync())
284             {
285                 standard = 0;
286                 simple = 1;
287             }
288         }
289         if (PADButtonDown(prev, curr) & PAD_BUTTON_Y)
290         {
291             if (simple == 1 || simple == 2)
292             {
293                 simple = 3;
294                 WPADStopSimpleSync();
295             }
296         }
297         if (PADButtonDown(prev, curr) & PAD_TRIGGER_L)
298         {
299             if (WPADStartFastSyncDevice())
300             {
301                 standard = 1;
302                 simple = 0;
303             }
304         }
305         if (PADButtonDown(prev, curr) & PAD_TRIGGER_R)
306         {
307             if (WPADStartFastSimpleSync())
308             {
309                 standard = 0;
310                 simple = 1;
311             }
312         }
313         prev = curr;
314 
315         DEMOBeforeRender();
316         RenderConsoleStatus();
317         DEMODoneRender();
318 
319     }
320 
321     return 0;
322 }
323 
324 
325