1 /*---------------------------------------------------------------------------*
2   Project:  Basic pad demo
3   File:     basic.c
4 
5   Copyright (C) 1998-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: basic.c,v $
14   Revision 1.2  2006/10/24 12:32:40  yasuh-to
15   Fixed hot plug bug.
16 
17   Revision 1.1  2006/02/02 06:11:49  yasuh-to
18   Initial import
19 
20 
21     9     03/04/16 21:15 Hashida
22     Added a caution for controller hot-swap.
23 
24     8     8/20/02 21:58 Shiki
25     Added a call to PADClampCircle().
26 
27     7     5/21/01 10:53a Shiki
28     Removed analog A/B outputs.
29 
30     6     01/04/11 12:46 Shiki
31     Revised using escape sequences for smoother screen update.
32 
33     5     01/04/11 11:36 Shiki
34     Fixed report message.
35 
36     4     01/03/27 13:19 Shiki
37     Bug fix.
38 
39     3     01/03/23 15:01 Shiki
40     Fixed resetBits handling code.
41 
42     2     01/03/22 21:09 Shiki
43     Revised.
44 
45     1     3/01/00 11:48p Shiki
46     Initial check-in.
47   $NoKeywords: $
48  *---------------------------------------------------------------------------*/
49 
50 #include <string.h>
51 #include <revolution.h>
52 
53 /*
54  * NOTES:
55  *    This program supports controller hot-swap only for the controller
56  *    ports where a controller was connected to when booted.
57  *    As for how to support hot-swap for all the controller ports,
58  *    see libraries/demo/src/DEMOPad.c.
59  */
60 
61 PADStatus Pads[PAD_MAX_CONTROLLERS];
62 
PrintPads(void)63 static void PrintPads(void)
64 {
65     int chan;
66 
67     OSReport("Port  AB XY S ZLR +Pad Left         Right        Trigger\n");
68     //        1[-2] AB XY S ZLR <>^v (1234, 1234) (1234, 1234) (123, 123)
69     for (chan = 0; chan < PAD_MAX_CONTROLLERS; ++chan)
70     {
71         OSReport("%d[%-2d] %c%c %c%c %c %c%c%c %c%c%c%c (%4d, %4d) (%4d, %4d) (%3d, %3d)\n",
72             chan,
73             Pads[chan].err,
74             (Pads[chan].button & PAD_BUTTON_A) ? 'O' : '_',
75             (Pads[chan].button & PAD_BUTTON_B) ? 'O' : '_',
76             (Pads[chan].button & PAD_BUTTON_X) ? 'O' : '_',
77             (Pads[chan].button & PAD_BUTTON_Y) ? 'O' : '_',
78             (Pads[chan].button & PAD_BUTTON_START) ? 'O' : '_',
79             (Pads[chan].button & PAD_TRIGGER_Z) ? 'O' : '_',
80             (Pads[chan].button & PAD_TRIGGER_L) ? 'O' : '_',
81             (Pads[chan].button & PAD_TRIGGER_R) ? 'O' : '_',
82 
83             (Pads[chan].button & PAD_BUTTON_LEFT)  ? '<' : '_',
84             (Pads[chan].button & PAD_BUTTON_RIGHT) ? '>' : '_',
85             (Pads[chan].button & PAD_BUTTON_UP)    ? '^' : '_',
86             (Pads[chan].button & PAD_BUTTON_DOWN)  ? 'v' : '_',
87 
88             Pads[chan].stickX,
89             Pads[chan].stickY,
90             Pads[chan].substickX,
91             Pads[chan].substickY,
92             Pads[chan].triggerLeft,
93             Pads[chan].triggerRight);
94     }
95 }
96 
main(void)97 void main(void)
98 {
99     u32 padBit;
100     u32 resetBits;
101     u32 connectedBits;
102     int chan;
103 
104     VIInit();
105     PADInit();
106     OSReport("\033c");   // Resets the terminal
107 
108     connectedBits = 0x0;
109 
110     for (;;)
111     {
112         PADRead(Pads);
113 
114         resetBits = 0x0;
115         for (chan = 0; chan < PAD_MAX_CONTROLLERS; ++chan)
116         {
117             padBit = PAD_CHAN0_BIT >> chan;
118             switch (Pads[chan].err)
119             {
120               case PAD_ERR_NONE:
121               case PAD_ERR_TRANSFER:
122                 connectedBits |= padBit;
123                 break;
124               case PAD_ERR_NO_CONTROLLER:
125                 connectedBits &= ~padBit;
126                 resetBits |= padBit;
127                 break;
128               case PAD_ERR_NOT_READY:
129               default:
130                 break;
131             }
132         }
133         if (connectedBits)
134         {
135             resetBits &= ~connectedBits;
136         }
137         if (resetBits)
138         {
139             PADReset(resetBits);
140         }
141 
142         OSReport("\033[H");   // Home
143         if (connectedBits)
144         {
145             PADStatus org[PAD_MAX_CONTROLLERS];
146 
147             OSReport("Attached Controllers: 0x%1x.\n", connectedBits);
148             PrintPads();
149 
150             memcpy(org, Pads, sizeof org);
151             PADClamp(Pads);
152             OSReport("\nClamped\n");
153             PrintPads();
154 
155             memcpy(Pads, org, sizeof org);
156             PADClampCircle(Pads);
157             OSReport("\nClamped (circle)\n");
158             PrintPads();
159         }
160         else
161         {
162             OSReport("Please connect controllers\n");
163         }
164 
165         VIWaitForRetrace();
166     }
167 }
168