1 /*---------------------------------------------------------------------------*
2   Project:  Clamp demo
3   File:     clamp2.c
4 
5   Copyright (C) 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: clamp2.c,v $
14   Revision 1.3  2006/10/24 12:36:12  yasuh-to
15   Added error handling.
16 
17   Revision 1.2  2006/09/06 16:10:53  yasuh-to
18   Fixed the following bug.
19   "This program supports controller hot-swap only for the controller ports where a controller was connected to when booted."
20 
21   Revision 1.1  2006/09/06 14:33:05  yasuh-to
22   Clamp2 demo.
23   Clamp2 uses new paramaters these are more widly than GC's.
24 
25   $NoKeywords: $
26  *---------------------------------------------------------------------------*/
27 
28 #include <string.h>
29 #include <revolution.h>
30 
31 PADStatus Pads[PAD_MAX_CONTROLLERS];
32 
PrintPads(void)33 static void PrintPads(void)
34 {
35     int chan;
36 
37     OSReport("Port  AB XY S ZLR +Pad Left         Right        Trigger\n");
38     //        1[-2] AB XY S ZLR <>^v (1234, 1234) (1234, 1234) (123, 123)
39     for (chan = 0; chan < PAD_MAX_CONTROLLERS; ++chan)
40     {
41         OSReport("%d[%-2d] %c%c %c%c %c %c%c%c %c%c%c%c (%4d, %4d) (%4d, %4d) (%3d, %3d)\n",
42             chan,
43             Pads[chan].err,
44             (Pads[chan].button & PAD_BUTTON_A) ? 'O' : '_',
45             (Pads[chan].button & PAD_BUTTON_B) ? 'O' : '_',
46             (Pads[chan].button & PAD_BUTTON_X) ? 'O' : '_',
47             (Pads[chan].button & PAD_BUTTON_Y) ? 'O' : '_',
48             (Pads[chan].button & PAD_BUTTON_START) ? 'O' : '_',
49             (Pads[chan].button & PAD_TRIGGER_Z) ? 'O' : '_',
50             (Pads[chan].button & PAD_TRIGGER_L) ? 'O' : '_',
51             (Pads[chan].button & PAD_TRIGGER_R) ? 'O' : '_',
52 
53             (Pads[chan].button & PAD_BUTTON_LEFT)  ? '<' : '_',
54             (Pads[chan].button & PAD_BUTTON_RIGHT) ? '>' : '_',
55             (Pads[chan].button & PAD_BUTTON_UP)    ? '^' : '_',
56             (Pads[chan].button & PAD_BUTTON_DOWN)  ? 'v' : '_',
57 
58             Pads[chan].stickX,
59             Pads[chan].stickY,
60             Pads[chan].substickX,
61             Pads[chan].substickY,
62             Pads[chan].triggerLeft,
63             Pads[chan].triggerRight);
64     }
65 }
66 
main(void)67 void main(void)
68 {
69     u32 padBit;
70     u32 resetBits;
71     u32 connectedBits;
72     int chan;
73     static BOOL s_clampTypeSwitch=FALSE;
74 
75     VIInit();
76     PADInit();
77     OSReport("\033c");   // Resets the terminal
78 
79     connectedBits = 0x0;
80 
81     for (;;)
82     {
83         PADRead(Pads);
84 
85         resetBits = 0x0;
86         for (chan = 0; chan < PAD_MAX_CONTROLLERS; ++chan)
87         {
88             padBit = PAD_CHAN0_BIT >> chan;
89             switch (Pads[chan].err)
90             {
91               case PAD_ERR_NONE:
92                 if ( Pads[chan].button & PAD_BUTTON_START)
93                 {
94                     s_clampTypeSwitch = !s_clampTypeSwitch;
95                 }
96               case PAD_ERR_TRANSFER:
97                 connectedBits |= padBit;
98                 break;
99               case PAD_ERR_NO_CONTROLLER:
100                 connectedBits &= ~padBit;
101                 resetBits |= padBit;
102                 break;
103               case PAD_ERR_NOT_READY:
104               default:
105                 break;
106             }
107         }
108 
109         if (connectedBits)
110         {
111             resetBits &= ~connectedBits;
112         }
113         if (resetBits)
114         {
115             PADReset(resetBits);
116         }
117 
118 
119         OSReport("\033[H");   // Home
120         if (connectedBits)
121         {
122             PADStatus org[PAD_MAX_CONTROLLERS];
123 
124             OSReport("Attached Controllers: 0x%1x.\n", connectedBits);
125             PrintPads();
126 
127             memcpy(org, Pads, sizeof org);
128             if (!s_clampTypeSwitch)
129             {
130                 PADClamp2(Pads, PAD_STICK_CLAMP_OCTA_WITH_MARGIN);
131                 PADClampTrigger(Pads, PAD_TRIGGER_FIXED_BASE);
132                 OSReport("\nClamped WITH_MARGIN   \n");
133             }
134             else
135             {
136                 PADClamp2(Pads, PAD_STICK_CLAMP_OCTA_WITHOUT_MARGIN);
137                 PADClampTrigger(Pads, PAD_TRIGGER_INDIVIDUAL_BASE);
138                 OSReport("\nClamped WITHOUT_MARGIN\n");
139             }
140             PrintPads();
141 
142             memcpy(Pads, org, sizeof org);
143             if (!s_clampTypeSwitch)
144             {
145                 PADClampCircle2(Pads, PAD_STICK_CLAMP_CIRCLE_WITH_MARGIN);
146                 PADClampTrigger(Pads, PAD_TRIGGER_FIXED_BASE);
147                 OSReport("\nClamped (circle) WITH_MARGIN  \n");
148             }
149             else
150             {
151                 PADClampCircle2(Pads, PAD_STICK_CLAMP_CIRCLE_WITHOUT_MARGIN);
152                 PADClampTrigger(Pads, PAD_TRIGGER_INDIVIDUAL_BASE);
153                 OSReport("\nClamped (circle) WITHOUT_MARGIN\n");
154             }
155             PrintPads();
156         }
157         else
158         {
159             OSReport("Please connect controllers\n");
160         }
161 
162         VIWaitForRetrace();
163     }
164 }
165