1 /*===================================================================================================================================================
2
3 USB Remote : Input Device Driver Modules
4
5 - device driver modules are responsible for monitoring the state of USB power and translating
6 that into a "virtual switch" state
7 - the "virtual switch" state is monitored by the current control logic module for processing - there is no response expected back from the module
8 - the active device driver module is selected from a menu entry in the USB Remote menu
9 - to add a device driver, create a new function here, add name string to gui_USB_switch_types[]
10 in gui.c and add ptr below to structure (*usb_driver[10])()
11
12 ===================================================================================================================================================*/
13
14 #include "camera_info.h"
15 #include "clock.h"
16 #include "conf.h"
17 #include "usb_remote.h"
18
19
20 /*===================================================================================================
21 Debug and error handling
22 ===================================================================================================*/
23
24 #ifdef USB_REMOTE_DEBUGGING
25
26 extern int debug_errors[] ;
27 extern void debug_error(int ) ;
28
29 #else
30
31 extern void debug_error(int ) ;
32
33 #endif
34
35
36
37 /*---------------------------------------------------------------------------------------------------
38 Device Driver : "one press" switch
39 - goes to HALF_PRESS when USB power applied and then pulses FULL_PRESS when USB power removed
40 ---------------------------------------------------------------------------------------------------*/
41 void usb_one_press_switch(int usb_state)
42 {
43 static int time_stamp = 0 ;
44 int current_time ;
45
46 current_time = get_tick_count();
47
48 switch( driver_state )
49 {
50 case SW_RESET :
51 virtual_remote_state = REMOTE_RELEASE ;
52 driver_state = SW_IDLE ;
53 break ;
54 case SW_IDLE :
55 virtual_remote_state = REMOTE_RELEASE ;
56 if ( usb_state == USB_POWER_ON )
57 {
58 driver_state = SW_POWER_CONNECT ;
59 time_stamp = current_time ;
60 }
61 break ;
62 case SW_POWER_CONNECT :
63 if ( usb_state == USB_POWER_ON )
64 {
65 if ( current_time - time_stamp > DEBOUNCE_TIME ) // debounce
66 {
67 driver_state = SW_POWER_ON ;
68 virtual_remote_state = REMOTE_HALF_PRESS ;
69 time_stamp = current_time ;
70 }
71 }
72 else
73 {
74 driver_state = SW_IDLE ;
75 }
76 break ;
77 case SW_POWER_ON :
78 if ( usb_state != USB_POWER_ON )
79 {
80 if ( current_time - time_stamp > DEBOUNCE_TIME ) // debounce
81 {
82 driver_state = SW_POWER_DISCONNECT ;
83 virtual_remote_state = REMOTE_FULL_PRESS ;
84 time_stamp = current_time ;
85 }
86 }
87 break ;
88 case SW_POWER_DISCONNECT :
89 if ( current_time - time_stamp > FULL_PRESS_PULSE_TIME )
90 {
91 driver_state = SW_IDLE ;
92 virtual_remote_state = REMOTE_RELEASE ;
93 }
94 break ;
95 default :
96 debug_error(INVALID_STATE) ;
97 break ;
98 }
99
100 };
101
102
103 /*---------------------------------------------------------------------------------------------------
104 Device Driver : "two press" switch
105 - goes to HALF_PRESS when USB power applied and then FULL_PRESS if USB power removed and quickly reapplied
106 ---------------------------------------------------------------------------------------------------*/
107
108 void usb_two_press_switch(int usb_state)
109 {
110 static int time_stamp = 0 ;
111 int current_time ;
112
113 current_time = get_tick_count() ;
114
115 switch( driver_state )
116 {
117 case SW_RESET :
118 virtual_remote_state = REMOTE_RELEASE ;
119 driver_state = SW_IDLE ;
120 break ;
121 case SW_IDLE :
122 if ( usb_state == USB_POWER_ON )
123 {
124 driver_state = SW_POWER_CONNECT ;
125 time_stamp = current_time ;
126 }
127 break ;
128 case SW_POWER_CONNECT :
129 if ( usb_state == USB_POWER_ON )
130 {
131 if ( current_time - time_stamp > DEBOUNCE_TIME ) // debounce
132 {
133 driver_state = SW_POWER_ON ;
134 virtual_remote_state = REMOTE_HALF_PRESS ;
135 time_stamp = current_time ;
136 }
137 }
138 else
139 {
140 driver_state = SW_IDLE ;
141 }
142 break ;
143 case SW_POWER_ON :
144 if ( usb_state == USB_POWER_OFF )
145 {
146 if ( current_time - time_stamp > DEBOUNCE_TIME ) // debounce
147 {
148 driver_state = SW_POWER_DISCONNECT ;
149 time_stamp = current_time ;
150 }
151 }
152 break ;
153 case SW_POWER_DISCONNECT :
154 if ( usb_state == USB_POWER_ON )
155 {
156 if ( current_time - time_stamp > DEBOUNCE_TIME ) // debounce
157 {
158 driver_state = SW_FULL_PRESS_ACTIVE;
159 virtual_remote_state = REMOTE_FULL_PRESS ;
160 time_stamp = current_time ;
161 }
162 }
163 else
164 {
165 if ( current_time - time_stamp > MAX_FULL_PRESS_DELAY ) // timeout if no second press
166 {
167 virtual_remote_state = REMOTE_RELEASE ;
168 driver_state = SW_IDLE ;
169 }
170 }
171 break ;
172 case SW_FULL_PRESS_ACTIVE :
173 if ( usb_state == USB_POWER_OFF )
174 {
175 virtual_remote_state = REMOTE_RELEASE ;
176 driver_state = SW_IDLE ;
177 }
178 break ;
179 default :
180 debug_error(INVALID_STATE) ;
181 break ;
182 }
183 } ;
184
185 /*---------------------------------------------------------------------------------------------------
186 Device Driver : Ricoh CA-1 switch
187 - measures USB pulse widths to detect RICOH CA-1 half press, full press and cancel sequences
188 and sets virtual switch state appropriately
189 ---------------------------------------------------------------------------------------------------*/
190
191 #define CA1_MAX_SHORT_PULSE_TIME 30
192 #define CA1_MAX_GAP_TIME 50
193
194 /*
195
196 |30|30|30| ms
197 _/~~\_____________ half-press 100
198
199 _/~~~~~~~~~~~~~~\_ full-press 111
200
201 _/~~\__/~~\_______ release 101
202
203 */
204
205 void usb_ricoh_ca1_switch(int usb_state)
206 {
207 static int time_stamp = 0 ;
208 int current_time ;
209
210 current_time = get_tick_count() ;
211
212 switch( driver_state )
213 {
214 case SW_RESET :
215 virtual_remote_state = REMOTE_RELEASE ;
216 driver_state = SW_IDLE ;
217 break ;
218 case SW_IDLE : // wait for USB power to be applied
219 if ( usb_state == USB_POWER_ON )
220 {
221 driver_state = SW_CA1_1XX ;
222 time_stamp = current_time ;
223 }
224 break ;
225 case SW_CA1_1XX : // decide between CA-1 30 mSec / 150 mSec pulse
226 if ( usb_state == USB_POWER_OFF )
227 {
228 driver_state = SW_CA1_10X ;
229 time_stamp = current_time ;
230 }
231 else if ( current_time - time_stamp > CA1_MAX_SHORT_PULSE_TIME )
232 {
233 driver_state = SW_CA1_11 ; // we can set FULL_PRESS either here or wait for USB_POWER_OFF
234 virtual_remote_state = REMOTE_FULL_PRESS ; // note : setting FULL_PRESS here means that we can use the end of the current 150 mSec pulse for synch
235 }
236 break ;
237 case SW_CA1_10X :
238 if ( usb_state == USB_POWER_ON) // is the CA-1 30 mSec pulse followed by another one ?
239 {
240 driver_state = SW_CA1_101 ;
241 }
242 else if ( current_time - time_stamp > CA1_MAX_GAP_TIME)
243 {
244 driver_state = SW_CA1_100 ;
245 }
246 break ;
247 case SW_CA1_101 : // CA-1 release
248 if ( usb_state == USB_POWER_OFF ) // wait for end of pulse
249 {
250 driver_state = SW_IDLE ;
251 virtual_remote_state = REMOTE_RELEASE ;
252 }
253 break ;
254 case SW_CA1_100 : // CA-1 half-press
255 driver_state = SW_IDLE ;
256 virtual_remote_state = REMOTE_HALF_PRESS ;
257 break ;
258 case SW_CA1_11 : // CA-1 full press
259 if ( usb_state == USB_POWER_OFF )
260 {
261 driver_state = SW_IDLE ; // this is always followed by the release pulse
262 virtual_remote_state = REMOTE_RELEASE ; // so we can set the state already here
263 }
264 break ;
265 default :
266 debug_error(INVALID_STATE) ;
267 break ;
268 }
269 } ;
270
271
272 /*===================================================================================================
273
274 Input Driver Jump Table
275
276 ===================================================================================================*/
277
278
279 void usb_null_driver(__attribute__ ((unused))int i) { return ; } ;
280
281 void (*usb_driver[NUM_USB_INPUT_DRV])(int) = // jump table for input drivers - must match gui_USB_switch_types[] in gui.c
282 {
283 usb_null_driver ,
284 usb_one_press_switch ,
285 usb_two_press_switch ,
286 usb_ricoh_ca1_switch ,
287 usb_null_driver // <- insert new devices here - update NUM_USB_INPUT_DRV if necessary
288 };