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