1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - demos.TWL - os - os_jump
3   File:     main.c
4 
5   Copyright 2007-2008 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:: 2008-10-08#$
14   $Rev: 8901 $
15   $Author: okajima_manabu $
16  *---------------------------------------------------------------------------*/
17 
18 #include <twl.h>
19 #include <stdlib.h>
20 
21 #include "screen.h"
22 
23 /*---------------------------------------------------------------------------*
24     Constant Definitions
25  *---------------------------------------------------------------------------*/
26 #define KEY_REPEAT_START    25  // Number of frames until key repeat starts
27 #define KEY_REPEAT_SPAN     10  // Number of frames between key repeats
28 
29 /*---------------------------------------------------------------------------*
30     Structure Definitions
31  *---------------------------------------------------------------------------*/
32 
33 // Key input data
34 typedef struct KeyInfo
35 {
36     u16 cnt;    // Unprocessed input value
37     u16 trg;    // Push trigger input
38     u16 up;     // Release trigger input
39     u16 rep;    // Press and hold repeat input
40 } KeyInfo;
41 
42 // Key input
43 static KeyInfo  gKey;
44 
45 /*---------------------------------------------------------------------------*
46    Prototype
47  *---------------------------------------------------------------------------*/
48 
49 static void VBlankIntr(void);
50 static void InitInterrupts(void);
51 static void InitHeap(void);
52 
53 static void ReadKey(KeyInfo* pKey);
54 
55 /*---------------------------------------------------------------------------*/
56 
TwlMain(void)57 void TwlMain(void)
58 {
59     OS_Init();
60     OS_InitTick();
61     OS_InitAlarm();
62     GX_Init();
63     GX_DispOff();
64     GXS_DispOff();
65 
66     InitHeap();
67     InitScreen();
68     InitInterrupts();
69 
70     GX_DispOn();
71     GXS_DispOn();
72 
73     ClearScreen();
74 
75     // Empty call for getting key input data (strategy for pressing A Button in the IPL)
76     ReadKey(&gKey);
77 
78     while(TRUE)
79     {
80         // Get key input data
81         ReadKey(&gKey);
82 
83         // Clear the main screen
84         ClearScreen();
85 
86         PutMainScreen(0, 0, 0xff, "OS_IsRebooted = %s", OS_IsRebooted()? "TRUE":"FALSE");
87 
88         PutSubScreen(0, 0, 0xff, "APPLICATION JUMP DEMO");
89         PutSubScreen(0, 1, 0xff, "  A: OS_JumpToSystemMenu");
90         PutSubScreen(0, 2, 0xff, "  B: OS_RebootSystem");
91         PutSubScreen(0, 3, 0xff, "  X: OS_JumpToEULAViewer");
92         PutSubScreen(0, 4, 0xff, "  Y: OS_JumpToInternetSetting");
93         PutSubScreen(0, 5, 0xff, "  R: OS_JumpToWirelessSetting");
94 
95         if (gKey.trg & PAD_BUTTON_A)
96         {
97             if (FALSE == OS_JumpToSystemMenu() )
98             {
99                 //Jump failure
100                 OS_Panic("OS_JumpToSystemMenu() is Failed");
101             }
102         }
103 
104         if (gKey.trg & PAD_BUTTON_B)
105         {
106             if (FALSE == OS_RebootSystem() )
107             {
108                 //Jump failure
109                 OS_Panic("OS_RebootSystem() is Failed");
110             }
111         }
112 
113         if (gKey.trg & PAD_BUTTON_X)
114         {
115             if (FALSE == OS_JumpToEULAViewer() )
116             {
117                 //Jump failure
118                 OS_Panic("OS_JumpToEULAViewer() is Failed");
119             }
120         }
121 
122         if (gKey.trg & PAD_BUTTON_Y)
123         {
124             if (FALSE == OS_JumpToInternetSetting() )
125             {
126                 //Jump failure
127                 OS_Panic("OS_JumpToInternetSetting() is Failed");
128             }
129         }
130         if (gKey.trg & PAD_BUTTON_R)
131         {
132             if (FALSE == OS_JumpToWirelessSetting() )
133             {
134                 //Jump failure
135                 OS_Panic("OS_JumpToWirelessSetting() is Failed");
136             }
137         }
138         // Wait for V-Blank (this supports threading)
139         OS_WaitVBlankIntr();
140     }
141 
142     OS_Terminate();
143 }
144 
145 /*---------------------------------------------------------------------------*
146   Name:         VBlankIntr
147 
148   Description:  V-Blank interrupt handler.
149 
150   Arguments:    None.
151 
152   Returns:      None.
153  *---------------------------------------------------------------------------*/
VBlankIntr(void)154 static void VBlankIntr(void)
155 {
156     // Updates text display
157     UpdateScreen();
158 
159     // Sets the IRQ check flag
160     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
161 }
162 
163 /*---------------------------------------------------------------------------*
164   Name:         InitInterrupts
165 
166   Description:  Initializes the interrupt settings.
167                 Allows V-Blank interrupts and configures the interrupt handler.
168 
169   Arguments:    None.
170 
171   Returns:      None.
172  *---------------------------------------------------------------------------*/
InitInterrupts(void)173 static void InitInterrupts(void)
174 {
175     // V-Blank interrupt settings
176     OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
177     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
178     (void)GX_VBlankIntr(TRUE);
179 
180     // Allow interrupts
181     (void)OS_EnableIrq();
182     (void)OS_EnableInterrupts();
183 }
184 
185 /*---------------------------------------------------------------------------*
186   Name:         InitHeap
187 
188   Description:  Initializes the memory allocation system within the main memory arena.
189 
190   Arguments:    None.
191 
192   Returns:      None.
193  *---------------------------------------------------------------------------*/
InitHeap(void)194 static void InitHeap(void)
195 {
196     void*           tempLo;
197     OSHeapHandle    hh;
198 
199     // Creates a heap in the main memory arena
200     tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
201     OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
202     hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
203     if (hh < 0)
204     {
205         // Abnormal end when heap creation fails
206         OS_Panic("ARM9: Fail to create heap...\n");
207     }
208     (void)OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
209 }
210 
211 /*---------------------------------------------------------------------------*
212   Name:         ReadKey
213 
214   Description:  Gets key input data and edits the input data structure.
215                 Detects push, release, and push and hold repeat triggers.
216 
217   Arguments:    pKey: Key input structure to be changed
218 
219   Returns:      None.
220  *---------------------------------------------------------------------------*/
ReadKey(KeyInfo * pKey)221 static void ReadKey(KeyInfo* pKey)
222 {
223     static u16  repeat_count[12];
224     int         i;
225     u16         r;
226 
227     r = PAD_Read();
228     pKey->trg = 0x0000;
229     pKey->up = 0x0000;
230     pKey->rep = 0x0000;
231 
232     for (i = 0; i < 12; i++)
233     {
234         if (r & (0x0001 << i))
235         {
236             if (!(pKey->cnt & (0x0001 << i)))
237             {
238                 pKey->trg |= (0x0001 << i);     // Press trigger
239                 repeat_count[i] = 1;
240             }
241             else
242             {
243                 if (repeat_count[i] > KEY_REPEAT_START)
244                 {
245                     pKey->rep |= (0x0001 << i); // Press-and-hold repeat
246                     repeat_count[i] = (u16) (KEY_REPEAT_START - KEY_REPEAT_SPAN);
247                 }
248                 else
249                 {
250                     repeat_count[i]++;
251                 }
252             }
253         }
254         else
255         {
256             if (pKey->cnt & (0x0001 << i))
257             {
258                 pKey->up |= (0x0001 << i);      // Release trigger
259             }
260         }
261     }
262 
263     pKey->cnt = r;  // Unprocessed key input
264 }
265 
266 /*---------------------------------------------------------------------------*
267   End of file
268  *---------------------------------------------------------------------------*/
269