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: check.c,v $
14 Revision 1.4 06/17/2006 05:38:33 ekwon
15 Corrected BD_ADDR length.
16
17 Revision 1.3 06/16/2006 14:44:55 ekwon
18 Simple demo for retrieving unique BD ADDR of each connected device.
19
20 Revision 1.1 06/15/2006 04:25:35 ekwon
21 preliminary checkins for event handling demos.
22
23
24
25 *---------------------------------------------------------------------------*/
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stddef.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33
34 #include <revolution.h>
35 #include <revolution/wpad.h>
36
37 #define DEMO_USE_MEMLIB=1 // This turns on the DEMO library's MEM heaps.
38 #include <demo.h>
39
40
41 #define SCREEN_WIDTH 320
42 #define SCREEN_HEIGHT 240
43 #define FONT_HEIGHT 9
44
45
46 /*---------------------------------------------------------------------------*
47 * Function prototypes
48 *---------------------------------------------------------------------------*/
49
50
51 // MEM2 memory allocation routines. The application must provide these to
52 // WPAD, so it can setup the data transfer buffer. This buffer must reside
53 // in MEM2.
54 static void *myAlloc (u32 size);
55 static u8 myFree (void *ptr);
56
57 // internal functions
58 static void initialize (void);
59
60
61 /*---------------------------------------------------------------------------*
62 * Local Data
63 *---------------------------------------------------------------------------*/
64
65
66 /*===========================================================================*
67 * F U N C T I O N D E F I N I T I O N S
68 *===========================================================================*/
69
70
71 /*---------------------------------------------------------------------------*
72 * Name : main()
73 * Description :
74 * Arguments : None.
75 * Returns : None.
76 *---------------------------------------------------------------------------*/
77
main(void)78 int main( void )
79 {
80 s16 y;
81 u8 num_paired;
82 s32 wpad_state;
83 u8 addr[WPAD_ADDR_LEN];
84
85 s32 status;
86 u32 type;
87 int i;
88
89 initialize();
90
91 // - We must now register memory allocation/free functions
92 // for MEM2.
93 // - WPAD requires some memory in MEM2 for data transfers
94 // between the controller and WPAD driver stack.
95 // - Memory allocation only occurs once, at the initialization.
96 // - Memory usage is on the order of 1KB.
97 // - NOTE: We are using the MEM library allocators defined by
98 // the DEMO library.
99 //
100 WPADRegisterAllocator(myAlloc, myFree);
101
102 // Initialize WPAD!
103 WPADInit();
104
105 // The WPAD initialization process is asynchronous.
106 // So we should wait until it's completed.
107 do
108 {
109 wpad_state = WPADGetStatus();
110
111 } while (WPAD_STATE_SETUP != wpad_state);
112
113
114 while(1)
115 {
116
117 num_paired = WPADGetRegisteredDevNum();
118
119 DEMOBeforeRender();
120
121
122 y = 30;
123
124 DEMOPrintf(5, y, 0, "%1d device(s) paired to this host.", (u32)num_paired);
125 y+=FONT_HEIGHT;
126 y+=FONT_HEIGHT;
127
128 DEMOPrintf(5, y, 0, "Addresses of connected devices:");
129 y+=FONT_HEIGHT;
130
131 for (i=0; i<WPAD_MAX_CONTROLLERS; i++)
132 {
133 status = WPADProbe(i, &type);
134
135 if (WPAD_ERR_NONE == status)
136 {
137 WPADGetAddress(i, addr);
138 DEMOPrintf(5, y, 0, "Chan %1d: %02X%02X%02X%02X%02X%02X",
139 i,
140 addr[0], addr[1], addr[2],
141 addr[3], addr[4], addr[5]);
142
143 }
144 else
145 {
146 DEMOPrintf(5, y, 0, "Chan %1d: ----", i);
147 }
148 y+=FONT_HEIGHT;
149
150
151 }
152 DEMODoneRender();
153
154 } // end while
155
156
157 } // end main()
158
159 /*---------------------------------------------------------------------------*
160 * Name : myAlloc()
161 * Description : Callback needed by WPAD to allocate mem from MEM2 heap
162 * Arguments : size of block, in bytes.
163 * Returns : pointer to allocated block.
164 *---------------------------------------------------------------------------*/
myAlloc(u32 size)165 static void *myAlloc(u32 size)
166 {
167 void *ptr;
168
169 ptr = MEMAllocFromAllocator(&DemoAllocator2, size);
170 ASSERTMSG(ptr, "Memory allocation failed\n");
171
172 return(ptr);
173
174 } // myAlloc()
175
176 /*---------------------------------------------------------------------------*
177 * Name : myFree()
178 * Description : Callback needed by WPAD to free mem from MEM2 heap
179 * Arguments : None.
180 * Returns : Always 1.
181 *---------------------------------------------------------------------------*/
myFree(void * ptr)182 static u8 myFree(void *ptr)
183 {
184
185 MEMFreeToAllocator(&DemoAllocator2, ptr);
186
187 // we should ensure that memory is free'd properly, but oh well
188 return(1);
189
190 } // myFree()
191
192
193
194 /*---------------------------------------------------------------------------*
195 * Name : initialize()
196 * Description : Performs basic system initialization.
197 * Arguments : None.
198 * Returns : None.
199 *---------------------------------------------------------------------------*/
200
initialize(void)201 static void initialize( void )
202 {
203 const GXColor DARKBLUE = { 0, 0, 40, 255 };
204
205 OSInit();
206
207 DEMOInit( &GXNtsc480IntDf );
208
209 GXSetCopyClear( DARKBLUE, GX_MAX_Z24 );
210 GXCopyDisp( DEMOGetCurrentBuffer(), GX_TRUE );
211 DEMOInitCaption( DM_FT_XLU, SCREEN_WIDTH, SCREEN_HEIGHT );
212 GXSetZMode( GX_ENABLE, GX_ALWAYS, GX_ENABLE ); // Set pixel processing mode
213 GXSetBlendMode( GX_BM_BLEND, GX_BL_ONE, GX_BL_ONE, GX_LO_CLEAR ); // Translucent mode
214
215
216 } // end
217