1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     demo1.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  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   $Rev: 47228 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn.h>
17 #include <nn/os.h>
18 #include <nn/fs.h>
19 #include <nn/fnd.h>
20 #include <nn/hid.h>
21 #include <nn/mic.h>
22 #include <nn/camera.h>
23 #include <nn/math.h>
24 #include <nn/swkbd.h>
25 #include <nn/applet.h>
26 #include "device.h"
27 #include "gx.h"
28 #include "rtc.h"
29 #include "pad.h"
30 #include "tp.h"
31 #include "acc.h"
32 #include "mic.h"
33 #include "snd.h"
34 #include "camera.h"
35 #include "fs.h"
36 #include "savedata.h"
37 #include "kbd.h"
38 #include "applet.h"
39 
40 #include "demo.h"
41 
StartDemo()42 bool StartDemo()
43 {
44     // Description of Operations
45     NN_LOG("X + A : Run Optional Device Demo (Ex. Card Backup Device..)\n");
46     NN_LOG("R + A : Run Software Keyboard Applet \n");
47 
48     // #######################################################################################################
49     // Application initialization sequence
50     // #######################################################################################################
51 
52     const u32 s_GxHeapSize = 0x800000;
53 
54     // fs initialization
55     nn::fs::Initialize();
56 
57     const size_t ROMFS_BUFFER_SIZE = 1024 * 64;
58     static char buffer[ROMFS_BUFFER_SIZE];
59     NN_UTIL_PANIC_IF_FAILED(
60         nn::fs::MountRom(16, 16, buffer, ROMFS_BUFFER_SIZE));
61 
62     // Initialize hid
63     nn::hid::Initialize();
64 
65     // Allocate heap
66     nn::fnd::ExpHeap appHeap;
67     appHeap.Initialize(nn::os::GetDeviceMemoryAddress(), nn::os::GetDeviceMemorySize() );
68 
69     // Prepare RenderSystem
70     uptr heapForGx = reinterpret_cast<uptr>(appHeap.Allocate(s_GxHeapSize));
71     demo::RenderSystemDrawing s_RenderSystem;
72     s_RenderSystem.Initialize(heapForGx, s_GxHeapSize);
73 
74 
75     // Graphics demo initialization
76     GraphicsDemo graphicsDemo;
77     graphicsDemo.Initialize(&s_RenderSystem);
78 
79     // Camera demo initialization
80     CameraDemo cameraDemo;
81     cameraDemo.Initialize(&s_RenderSystem, &appHeap);
82 
83     // System clock demo initialization
84     RtcDemo rtcDemo;
85     rtcDemo.Initialize(&s_RenderSystem);
86 
87     // Pad demo initialization
88     PadDemo padDemo;
89     padDemo.Initialize(&s_RenderSystem);
90 
91     // Touch panel demo initialization
92     TouchPanelDemo touchPanelDemo;
93     touchPanelDemo.Initialize(&s_RenderSystem);
94 
95     // Accelerometer demo initialization
96     AccDemo accDemo;
97     accDemo.Initialize(&s_RenderSystem);
98 
99     // Mic demo initialization
100     MicDemo micDemo;
101     micDemo.Initialize(&s_RenderSystem);
102 
103     // Sound demo initialization
104     SndDemo sndDemo;
105     sndDemo.Initialize(&s_RenderSystem, &appHeap);
106 
107     // File system demo initialization
108     FsDemo fsDemo;
109     fsDemo.Initialize(&s_RenderSystem, &appHeap);
110 
111     // Software keyboard demo initialization
112     SwkbdDemo kbdDemo;
113     kbdDemo.Initialize(&s_RenderSystem);
114 
115     // Save data
116     SaveDataDemo saveDataDemo;
117 
118     // Device list
119     Device* deviceList[] = {
120                                 &graphicsDemo,
121                                 &cameraDemo,
122                                 &rtcDemo,
123                                 &padDemo,
124                                 &touchPanelDemo,
125                                 &accDemo,
126                                 &micDemo,
127                                 &sndDemo,
128                                 &fsDemo,
129                                 &kbdDemo
130                            };
131     int deviceNum = sizeof(deviceList) / sizeof(Device*);
132 
133     Device* optionalDeviceList[] =
134                            {
135                                 &saveDataDemo
136                            };
137     int optionalDeviceNum = sizeof(optionalDeviceList) / sizeof(Device*);
138 
139     bool isOptionalDeviceTest = false;
140 
141     // Start each device
142     for(int i = 0; i < deviceNum; i++)
143     {
144         deviceList[i]->Start();
145     }
146 
147     // After this, the application itself handles sleep
148     TransitionHandler::EnableSleep();
149 
150     bool restartFlag = false;
151 
152     // #######################################################################################################
153     // Main loop
154     // #######################################################################################################
155     while(1)
156     {
157         s_RenderSystem.SetRenderTarget(NN_GX_DISPLAY0);
158         s_RenderSystem.Clear();
159         deviceList[0]->Draw();
160         s_RenderSystem.SwapBuffers();
161 
162         s_RenderSystem.SetRenderTarget(NN_GX_DISPLAY1);
163         s_RenderSystem.Clear();
164         for(int i = 1; i < deviceNum; i++)
165         {
166             deviceList[i]->Draw();
167         }
168 
169         s_RenderSystem.SwapBuffers();
170 
171         // Restart if START + L + R are pressed
172         if ( padDemo.GetLatestPadStatus().hold == (nn::hid::BUTTON_START | nn::hid::BUTTON_L | nn::hid::BUTTON_R) )
173         {
174             restartFlag = true;
175             break;
176         }
177 
178         if(padDemo.GetLatestPadStatus().hold == (nn::hid::BUTTON_X | nn::hid::BUTTON_A) && !isOptionalDeviceTest)
179         {
180             //Additional device demo initialization
181             saveDataDemo.Initialize(&s_RenderSystem, &appHeap);
182 
183             for(int i = 0; i < optionalDeviceNum; i++)
184             {
185                 optionalDeviceList[i]->Start();
186             }
187             isOptionalDeviceTest = true;
188             NN_LOG("Run Optional Device Demo.\n");
189 
190         }
191 
192         if(padDemo.GetLatestPadStatus().hold == (nn::hid::BUTTON_R | nn::hid::BUTTON_A))
193         {
194             // Start the software keyboard
195             if( kbdDemo.Exec() )
196             {
197                 break;
198             }
199         }
200 
201         for(int i = 0; i < optionalDeviceNum&&isOptionalDeviceTest; i++)
202         {
203             optionalDeviceList[i]->Draw();
204         }
205 
206         s_RenderSystem.WaitVsync(NN_GX_DISPLAY_BOTH);
207 
208         // Run sleep, HOME Menu, and POWER Menu transitions.
209         TransitionHandler::Process();
210         // Determine whether there has been an exit notification.
211         if (TransitionHandler::IsExitRequired())
212         {
213             break;
214         }
215     }
216 
217     // #######################################################################################################
218     // Application termination processing
219     // #######################################################################################################
220     // If nn::applet::IsExpectedToCloseApplication() == true, then sleep-related callbacks are not called automatically.
221     //
222 
223     //Stop and terminate added devices
224     if( isOptionalDeviceTest == true )
225     {
226         for(int i = 0; i < optionalDeviceNum; i++)
227         {
228             optionalDeviceList[i]->End();
229             optionalDeviceList[i]->Finalize();
230         }
231     }
232 
233     // Stop each device
234     for(int i = 0; i < deviceNum; i++)
235     {
236         deviceList[i]->End();
237     }
238 
239     // Terminate each device
240     for(int i = 0; i < deviceNum; i++)
241     {
242         deviceList[i]->Finalize();
243     }
244 
245     // Exit RenderSystem
246     s_RenderSystem.Finalize();
247 
248     // Destroy heap
249     appHeap.Free(reinterpret_cast<void*>(heapForGx));
250     appHeap.Finalize();
251 
252     return restartFlag;
253 }
254 
nnMain()255 void nnMain()
256 {
257     NN_LOG("Demo1 Start\n");
258 
259     bool restartFlag = false;
260     u8 restartCount = 0;
261 
262     // Initialize via the applet.
263     // Sleep Mode is rejected automatically until TransitionHandler::EnableSleep is called.
264     TransitionHandler::Initialize();
265 
266     // Receive values passed with RestartApplication
267     {
268         u8 dBuf[ NN_APPLET_PARAM_BUF_SIZE ];
269         bool r = nn::applet::GetStartupArgument( dBuf );
270         if ( r )
271         {
272             if (dBuf[NN_APPLET_PARAM_BUF_SIZE - 1] == 0xFF)
273             {
274                 NN_LOG("----demo1 received StartupArgument.\n");
275                 NN_LOG("    jump count = %d\n", dBuf[0] );
276                 restartCount = dBuf[0];
277             }
278         }
279         else
280         {
281             NN_LOG("----demo1 cannot receive startupArgument.\n");
282         }
283     }
284 
285     // Here we need to check for exit notifications.
286     if (!TransitionHandler::IsExitRequired())
287     {
288         restartFlag = StartDemo();
289     }
290     // Finalize via the applet.
291     TransitionHandler::Finalize();
292 
293     NN_LOG("Demo1 End\n");
294 
295     // Performs application restart or finalization.
296     if ( restartFlag )
297     {
298         u8 startupArgBuf[NN_APPLET_PARAM_BUF_SIZE];
299         startupArgBuf[0] = restartCount + 1;
300         startupArgBuf[NN_APPLET_PARAM_BUF_SIZE - 1] = 0xFF;
301         // Restarts the application. The call to nn::applet::RestartApplication does not return.
302         nn::applet::RestartApplication(startupArgBuf);
303     }
304     else
305     {
306         // Finalize the application. The call to nn::applet::CloseApplication does not return.
307         nn::applet::CloseApplication();
308     }
309 }
310 
311 /*---------------------------------------------------------------------------*
312   End of file
313  *---------------------------------------------------------------------------*/
314