1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - WXC - demos - relayStation-2
3 File: main.c
4
5 Copyright 2005-2009 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 $Date:: 2007-11-15#$
14 $Rev: 2414 $
15 $Author: hatamoto_minoru $
16 *---------------------------------------------------------------------------*/
17
18
19 /*---------------------------------------------------------------------------*
20 This sample runs Chance Encounter Communications over a wireless connection.
21 Child mode becomes 100%, and it behaves as a relay station
22 Unlike relayStation-1, only one GGID is supported and registration data handling can be dynamically configured.
23
24
25 [Description of Each Mode]
26
27 BROADCAST : Continue using current registered data
28 (Same data will be multiplying without limit)
29
30 RELAY : Continue to reregister the data received in exchange as the new send data.
31 (The basic operation of "relay station" corresponds to this)
32
33 END : Shut down exchange processing itself.
34 (If an exchange is currently in progress, processing will continue until the exchange has completed)
35
36 *---------------------------------------------------------------------------*/
37
38 #include <nitro.h>
39 #include <nitro/wxc.h>
40
41 #include "user.h"
42 #include "dispfunc.h"
43
44
45 /*****************************************************************************/
46 /* Functions */
47
48 /*---------------------------------------------------------------------------*
49 Name: WaitNextFrame
50
51 Description: Waits for next frame and detects new key press.
52
53 Arguments: None.
54
55 Returns: Bit set of newly pressed key.
56 *---------------------------------------------------------------------------*/
WaitNextFrame(void)57 static u16 WaitNextFrame(void)
58 {
59 static u16 cont = 0xffff; /* Measures to handle A Button presses inside IPL */
60 u16 keyset, trigger;
61
62 /* Wait for the next frame */
63 OS_WaitVBlankIntr();
64 /* Detect new key press */
65 keyset = PAD_Read();
66 trigger = (u16)(keyset & (keyset ^ cont));
67 cont = keyset;
68
69 return trigger;
70 }
71
72 /*---------------------------------------------------------------------------*
73 Name: InitializeAllocateSystem
74
75 Description: Initializes the memory allocation system in the main memory arena.
76
77 Arguments: None.
78
79 Returns: None.
80 *---------------------------------------------------------------------------*/
InitializeAllocateSystem(void)81 static void InitializeAllocateSystem(void)
82 {
83 void *tempLo;
84 OSHeapHandle hh;
85
86 OS_Printf(" arena lo = %08x\n", OS_GetMainArenaLo());
87 OS_Printf(" arena hi = %08x\n", OS_GetMainArenaHi());
88
89 tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
90 OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
91 hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
92 if (hh < 0)
93 {
94 OS_Panic("ARM9: Fail to create heap...\n");
95 }
96 hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
97 }
98
99 /*---------------------------------------------------------------------------*
100 Name: VBlankIntr
101
102 Description: V-Blank interrupt handler.
103
104 Arguments: None.
105
106 Returns: None.
107 *---------------------------------------------------------------------------*/
VBlankIntr(void)108 static void VBlankIntr(void)
109 {
110 /* Update background screen */
111 BgScrSetVblank();
112 /* Interrupt check flag */
113 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
114 }
115
116 /*---------------------------------------------------------------------------*
117 Name: NitroMain
118
119 Description: Initialization and main loop.
120
121 Arguments: None.
122
123 Returns: None.
124 *---------------------------------------------------------------------------*/
NitroMain(void)125 void NitroMain(void)
126 {
127 int cursor;
128
129 /* Various types of initialization */
130 OS_Init();
131 OS_InitTick();
132 OS_InitAlarm();
133 RTC_Init();
134
135 /* Rendering settings initialization */
136 {
137 GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
138 MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
139 (void)GX_DisableBankForLCDC();
140 MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
141 MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
142 MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);
143 MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);
144 BgInitForPrintStr();
145 GX_SetVisiblePlane(GX_PLANEMASK_BG0);
146 GXS_SetVisiblePlane(GX_PLANEMASK_BG0);
147 }
148
149 /* V-blank interrupt configuration */
150 (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
151 (void)OS_EnableIrqMask(OS_IE_V_BLANK);
152 (void)OS_EnableIrqMask(OS_IE_FIFO_RECV);
153 (void)OS_EnableIrq();
154 (void)OS_EnableInterrupts();
155 (void)GX_VBlankIntr(TRUE);
156
157
158 /* Start display */
159 GX_DispOn();
160 GXS_DispOn();
161 G2_SetBG0Offset(0, 0);
162 G2S_SetBG0Offset(0, 0);
163
164 /* Memory allocation initialization */
165 InitializeAllocateSystem();
166
167 /* Start sample demo */
168 OS_TPrintf("relayStation-2 demo started.\n");
169
170 /* Infinite loop of demo start and reset */
171 for (;;)
172 {
173 /* Wait for A button to be pressed */
174 while ((WaitNextFrame() & PAD_BUTTON_A) == 0)
175 {
176 BgClear();
177 BgPutString(3, 1, WHITE, "Press A to start");
178 }
179
180 /* Initialize WXC and run user configuration (default setting is broadcast) */
181 station_is_relay = FALSE;
182 cursor = 0;
183 User_Init();
184
185 /* Main chance encounter communications loop */
186 while (WXC_GetStateCode() != WXC_STATE_END)
187 {
188 u16 key = WaitNextFrame();
189 enum
190 { MODE_BROADCAST, MODE_RELAY, MODE_STOP, MODE_MAX };
191
192 /* Displays screen */
193 {
194 const char *(mode[MODE_MAX]) =
195 {
196 "BROADCAST", "RELAY", "STOP",};
197 const char *status = "";
198 char tmp[32];
199 u8 mac[6];
200 int i;
201
202 BgClear();
203 /* Current WXC status */
204 switch (WXC_GetStateCode())
205 {
206 case WXC_STATE_END:
207 status = "WXC_STATE_END";
208 break;
209 case WXC_STATE_ENDING:
210 status = "WXC_STATE_ENDING";
211 break;
212 case WXC_STATE_READY:
213 status = "WXC_STATE_READY";
214 break;
215 case WXC_STATE_ACTIVE:
216 status = "WXC_STATE_ACTIVE";
217 break;
218 default:
219 (void)OS_SPrintf(tmp, "?(%d)", WXC_GetStateCode());
220 status = tmp;
221 break;
222 }
223 BgPrintf(2, 2, WHITE, "status = %s", status);
224 /* Menu selection */
225 BgPrintf(4, 5 + (station_is_relay ? 1 : 0), WHITE, "*");
226 for (i = 0; i < MODE_MAX; ++i)
227 {
228 BgPrintf(3, 5 + i, WHITE, "%c", (cursor == i) ? '>' : ' ');
229 BgPrintf(6, 5 + i, (cursor == i) ? YELLOW : WHITE, "%s", mode[i]);
230 }
231 /* Currently registered data */
232 BgPrintf(0, 12, GREEN, "------- CURRENT DATA -------");
233 BgPrintf(2, 14, WHITE, "MAC = ");
234 for (i = 0; i < 6; ++i)
235 {
236 BgPrintf(8 + i * 3, 14, WHITE, "%02X", send_data_owner[i]);
237 }
238 BgPrintf(2, 16, WHITE, "DATA = ");
239 for (i = 0; i < 8; ++i)
240 {
241 BgPrintf(9 + i * 3, 16, WHITE, "%02X", send_data[i]);
242 }
243 /* Your own MAC address for reference */
244 BgPrintf(2, 10, WHITE, "OWN MAC = ");
245 OS_GetMacAddress(mac);
246 for (i = 0; i < 6; ++i)
247 {
248 BgPrintf(12 + i * 3, 10, WHITE, "%02X", mac[i]);
249 }
250 }
251
252 /* Key operations */
253 if (((key & PAD_KEY_DOWN) != 0) && (++cursor >= MODE_MAX))
254 {
255 cursor -= MODE_MAX;
256 }
257 if (((key & PAD_KEY_UP) != 0) && (--cursor < 0))
258 {
259 cursor += MODE_MAX;
260 }
261 if ((key & PAD_BUTTON_A) != 0)
262 {
263 switch (cursor)
264 {
265 case MODE_BROADCAST:
266 station_is_relay = FALSE;
267 break;
268 case MODE_RELAY:
269 station_is_relay = TRUE;
270 break;
271 case MODE_STOP:
272 (void)WXC_End();
273 break;
274 }
275 }
276 }
277 }
278 }
279
280
281 /*----------------------------------------------------------------------------*
282
283 /*---------------------------------------------------------------------------*
284 End of file
285 *---------------------------------------------------------------------------*/
286