1 /*---------------------------------------------------------------------------*
2   Project:     KPAD library version 2
3   File:        KPADinside.h
4   Programmers: Keizo Ohta
5                HIRATSU Daisuke
6 
7   Copyright 2005-2006 Nintendo. All rights reserved.
8 
9   These coded instructions, statements, and computer programs contain
10   proprietary information of Nintendo of America Inc. and/or Nintendo
11   Company Ltd., and are protected by Federal copyright law. They may
12   not be disclosed to third parties or copied or duplicated in any form,
13   in whole or in part, without the prior written consent of Nintendo.
14  *---------------------------------------------------------------------------*/
15 
16 #ifdef KPADOLD_INSIDE_H__
17 #error KPADinside.h of KPAD version 2 cannot be used with that of KPADOld
18 #endif
19 
20 #ifndef KPAD_INSIDE_H__
21 #define KPAD_INSIDE_H__
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <revolution/kpad.h>
28 
29 
30 /*******************************************************************************
31     DEFINE
32 *******************************************************************************/
33 //----- Definitions for calculation
34 #define KPAD_CMOS_FOV       WPAD_DPD_ANGLE  // CMOS lens angle of view (Field Of View)
35 #define KPAD_CMOS_HFOV_TAN  0.383864f   // Tangent of half angle of CMOS_FOV
36 
37 #define KPAD_CMOS_RESO_WX   128     // CMOS resolution
38 #define KPAD_CMOS_RESO_WY   96
39 #define KPAD_CMOS_RESO_CX   (KPAD_CMOS_RESO_WX / 2)
40 #define KPAD_CMOS_RESO_CY   (KPAD_CMOS_RESO_WY / 2)
41 
42 #define KPAD_DPD_RESO_WX    WPAD_DPD_IMG_RESO_WX    // DPD resolution
43 #define KPAD_DPD_RESO_WY    WPAD_DPD_IMG_RESO_WY
44 #define KPAD_DPD_RESO_CX    (KPAD_DPD_RESO_WX / 2)
45 #define KPAD_DPD_RESO_CY    (KPAD_DPD_RESO_WY / 2)
46 
47 #define KPAD_USE_OBJECTS    2   // Number of marks used for determining position
48 
49 #define KPAD_BTN_RPT_TIME_MAX   20000   // btn_repeat_time range (will not be this value)
50 #define KPAD_BTN_NO_RPT_DELAY   40000   // Delay setting when not repeating
51 
52 /*******************************************************************************
53     TYPEDEF
54 *******************************************************************************/
55 typedef struct {
56     Vec2    center ;    // Center of mass coordinate (coordinate system where the horizontal range of CMOS will be + or - 1)
57 
58     s8      error_fg ;  // -1: not displayed, 0: OK, 1: too far out, 2: same coordinate
59     s8      state_fg ;  // -1: expected value, 0: normal
60     s8      _fg_1 ;     // Extra
61     s8      _fg_2 ;     // Extra
62 } KPADObject ;
63 
64 typedef union {
65     KPADStatus              k;
66     KPADUnifiedWpadStatus   w;
67 } KPADTmpStatus;
68 
69 typedef struct {
70     KPADStatus  status ;
71 
72     //----- Variables that adjust how the controller responds
73     f32     pos_play_radius ;   // Tolerance radius (size at pos coordinate system)
74     f32     pos_sensitivity ;   // Tracking sensitivity (0: slow -> 1: sensitive)
75 
76     f32     hori_play_radius ;  // Adjust response of the 'horizon'
77     f32     hori_sensitivity ;  //
78 
79     f32     dist_play_radius ;  // Adjust response of the 'dist'
80     f32     dist_sensitivity ;  //
81 
82     f32     acc_play_radius ;   // Adjust the response of 'acc'
83     f32     acc_sensitivity ;   //
84 
85     //----- DPD calibration value
86     f32     dist_org ;          // Distance after calibration
87     Vec2    accXY_nrm_hori ;    // XY acceleration direction when the controller is placed horizontally
88     Vec2    sec_nrm_hori ;      // Direction from left mark to right mark when the controller is placed horizontally
89     Vec2    center_org ;        // Coordinates during calibration
90     f32     dpd2pos_scale ;     // Scale value where the left and right pointing edge in the shooting range will be + or - 1
91 
92     //----- Variable used for internal control
93     KPADObject  kobj_sample[ WPAD_DPD_MAX_OBJECTS ] ;   // All captured marks will be covered
94     KPADObject  kobj_regular[ KPAD_USE_OBJECTS ] ;      // Left to right order (top to bottom order for straight vertical)
95 
96     s16     valid_objs ;        // Valid object count
97     u8      bufIdx ;
98     u8      bufCount ;
99 
100     KPADUnifiedWpadStatus        uniRingBuf[ KPAD_RING_BUFS ] ;
101     KPADUnifiedWpadStatus       *uniRingBufEx ;         // Pointer to additional ring buffer
102     u32                          uniRingBufExLen ;      // Length of additional ring buffer
103 
104     f32     sec_length ;        // Distance between left and right marks
105     Vec2    sec_nrm ;           // Direction from the left to right mark
106     f32     sec_dist ;          // Raw distance from the TV (meter)
107     f32     trust_sec_length ;  // Trusted interval distance stored here
108 
109     Vec     hard_acc ;          // Raw acceleration
110 
111     Vec2    obj_horizon ;       // Tilt calculated by the object
112     Vec2    acc_horizon ;       // Tilt calculated by the acceleration (either side)
113 
114     Vec2    ah_circle_pos ;     // Point chasing the acc_horizon
115     u16     ah_circle_ct ;      // Countdown if in a certain range (stopped if zero)
116 
117     u8      dpd_valid2_ct ;     // Number of successful consecutive two point recognition
118     u8      readLocked ;        // KPADRead() is in progress
119 
120     //----- Button control
121     u16     btn_repeat_time ;   // Time to be counted
122     u16     btn_repeat_next ;   // Time of setting next flag
123     u16     btn_repeat_delay ;  // Repeat start time
124     u16     btn_repeat_pulse ;  // Repeat interval time
125     //----- For Classic Controller unit
126     u16     btn_cl_repeat_time ;   // Time to be counted
127     u16     btn_cl_repeat_next ;   // Time of setting next flag
128 
129     KPADControlDpdCallback  dpd_ctrl_callback ; // User callback when DPD setting change becomes necessary
130 
131     f32        acc_scale_x ;
132     f32        acc_scale_y ;
133     f32        acc_scale_z ;
134     f32     fs_acc_scale_x ;
135     f32     fs_acc_scale_y ;
136     f32     fs_acc_scale_z ;
137 
138     Vec2    kobj_frame_min;     // Range where the center of mass coordinate is valid
139     Vec2    kobj_frame_max;     //
140     f32     err_dist_speed_1 ;  // Reciprocal
141     f32     err_dist_speedM_1 ; // Reciprocal of negative value
142     f32     ah_circle_radius2 ; // Second power
143     f32     dist_vv1 ;          // Constant
144     f32     err_dist_min ;      // Automatic calculation of minimum operational distance (in meters)
145 
146     WPADSamplingCallback    appSamplingCallback ;    // For application
147     WPADConnectCallback     appConnectCallback ;     // For application
148 
149     u8      resetReq ;
150     u8      exResetReq ;
151     u8      dpdNextState ;
152     u8      dpdCurrState ;
153 
154     u8      dpdIssued ;
155     u8      dpdCmd ;
156     u8      dpdPreCallbackDone ;
157     u8      dpdPostCallbackDone ;
158 
159     u8      aimReq ;
160     u8      aimEnabled ;
161 
162     u8      fsAccRevise ;
163     u8      btnProcMode ;
164 
165     KPADPlayMode    pos_play_mode ;
166     KPADPlayMode    hori_play_mode ;
167     KPADPlayMode    dist_play_mode ;
168     KPADPlayMode    acc_play_mode ;
169 
170     u8      wbcIssued ;
171     u8      wbcEnabled ;
172     u8      wbcSetup ;
173     u8      wbcZeroPointWaitCount ;
174 
175     u8      wbcSetZeroPoint1Done ;
176     u8      wbcSetZeroPoint2Done ;
177     u8      wbcSetZeroPoint3Done ;
178     u8      wbcGetWeightAveDone ;
179 
180     u16     wbcZeroPointSampleCount ;
181     u16     wbcWeightAveSampleCount ;
182 
183     u32     paddings ;
184 
185     double  wbcZeroPoint[ WPAD_PRESS_UNITS ] ;
186     double  wbcWeightAve[ WPAD_PRESS_UNITS ] ;
187 
188 } KPADInsideStatus;
189 
190 
191 /*******************************************************************************
192     EXTERNAL WORK
193 *******************************************************************************/
194 //----- Various adjustments
195 extern f32  kp_obj_interval ;       // Separation between marks at each extremes (in meters)
196 extern f32  kp_acc_horizon_pw ;     // Calculating twist from acceleration
197 extern f32  kp_ah_circle_radius ;   // Stationary state determination radius
198 extern f32  kp_ah_circle_pw ;       // Stationary state determination tracking level
199 extern u16  kp_ah_circle_ct ;       // Stationary state determination count
200 extern BOOL kp_stick_clamp_cross ;  // Perform cross clamping
201 
202 
203 //----- Values determined to be an error.
204 extern f32  kp_err_outside_frame ;  // Surrounding area width where center of mass coordinate will be invalid (not all surrounding lights are necessarily shown)
205 extern f32  kp_err_dist_min ;       // Minimum operational distance (in meters)
206 extern f32  kp_err_dist_max ;       // Maximum operational distance (in meters)
207 extern f32  kp_err_dist_speed ;     // Accepted range of distance change (in meters)
208 extern f32  kp_err_first_inpr ;     // Internal product of acceleration tilt and object tilt when selecting two points for the first time
209 extern f32  kp_err_next_inpr ;      // Accepted range of tilt change (internal product value)
210 extern f32  kp_err_acc_inpr ;       // Accepted range of acceleration tilt and internal product value at stationary state
211 extern f32  kp_err_up_inpr ;        // Accepted internal product range with controller pointed upwards
212 extern f32  kp_err_near_pos ;       // Distance from the previous point when selecting one point as a continuation
213 
214 //----- KPAD
215 extern KPADInsideStatus inside_kpads[] ;
216 
217 
218 /*******************************************************************************
219     EXTERNAL FUNCTION
220 *******************************************************************************/
221 
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif // KPAD_INSIDE_H__
228