source: trunk/platform/a430/kbd.c @ 1376

Revision 1376, 10.1 KB checked in by philmoz, 19 months ago (diff)

Cleanup SVN properties using reyalp's platprops.bash script (part 3).

  • Property svn:eol-style set to native
Line 
1#include "lolevel.h"
2#include "platform.h"
3#include "conf.h"
4#include "core.h"
5#include "keyboard.h"
6
7typedef struct {
8        long hackkey;
9        long canonkey;
10} KeyMap;
11
12
13static long kbd_new_state[3];
14static long kbd_prev_state[3];
15static long kbd_mod_state;
16static KeyMap keymap[];
17static long last_kbd_key = 0;
18static int usb_power=0;
19static int remote_key, remote_count;
20static int shoot_counter=0;
21#define DELAY_TIMEOUT 10000
22
23#define NEW_SS (0x2000)
24#define SD_READONLY_FLAG (0x20000)
25
26#define USB_MASK 0x40000
27#define USB_REG 2
28
29#ifndef MALLOCD_STACK
30static char kbd_stack[NEW_SS];
31#endif
32
33long __attribute__((naked)) wrap_kbd_p1_f() ;
34
35void wait_until_remote_button_is_released(void)
36{
37
38long x[3];
39int count1;
40int count2;
41int tick,tick2,tick3;
42int nSW;
43int prev_usb_power,cur_usb_power;
44 // ------ add by Masuji SUTO (start) --------------
45    static int nMode;
46 // ------ add by Masuji SUTO (end)   --------------
47
48asm volatile ("STMFD SP!, {R0-R11,LR}\n"); // store R0-R11 and LR in stack
49debug_led(1);
50tick = get_tick_count();
51tick2 = tick;
52static long usb_physw[3];
53 if (conf.synch_enable && conf.ricoh_ca1_mode && conf.remote_enable && (!shooting_get_drive_mode()|| (shooting_get_drive_mode()==1) || ((shooting_get_drive_mode()==2) && state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)))
54//   if (conf.synch_enable && conf.ricoh_ca1_mode && conf.remote_enable && (!shooting_get_drive_mode()|| ((shooting_get_drive_mode()==2) && state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)))                                      // synch mode enable so wait for USB to disconnect
55  {
56// ------ add by Masuji SUTO (start) --------------
57        nMode=0;
58        usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
59        _kbd_read_keys_r2(usb_physw);
60        if((usb_physw[2] & USB_MASK)==USB_MASK) nMode=1;
61// ------ add by Masuji SUTO (end)   --------------
62if(conf.ricoh_ca1_mode && conf.remote_enable)
63  {
64        if(shooting_get_drive_mode()==1 && state_shooting_progress == SHOOTING_PROGRESS_PROCESSING){                    //continuous-shooting mode
65                if(conf.bracket_type>2){
66                        if(shoot_counter<2) shutter_int=3;
67                        shoot_counter--;
68                        }
69                else{
70                        prev_usb_power=0;
71                        nSW = 0;
72                        do
73                                {     
74                                usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
75                                _kbd_read_keys_r2(usb_physw);
76                                cur_usb_power = (usb_physw[2] & USB_MASK)==USB_MASK;
77                                if(cur_usb_power){
78                                        if(!prev_usb_power){
79                                                tick2 = get_tick_count();
80                                                prev_usb_power=cur_usb_power;
81                                                }
82                                        else{
83                                                if((int)get_tick_count()-tick2>1000) {debug_led(0);}
84                                                }
85                                        }
86                                else{
87                                        if(prev_usb_power){
88                                                tick3 = (int)get_tick_count()-tick2;
89                                                if(nSW==10) {
90                                                        if(tick3>50) shutter_int=1;
91                                                        nSW=20;
92                                                        }
93                                                if(nSW==0 && tick3>0) {
94                                                        if(tick3<50) {
95                                                        nSW=10;
96                                                        }
97                                                else{
98                                                        if(tick3>1000) shutter_int=1;
99                                                                nSW=20;
100                                                        }
101                                                }
102                                                prev_usb_power=cur_usb_power;
103                                                }
104                                        }
105                                if((int)get_tick_count()-tick >= DELAY_TIMEOUT) {nSW=20;shutter_int=2;}
106                                }
107                         while(nSW<20);
108                         }
109                }               //continuous-shooting mode
110                else{           // normal mode
111                        shoot_counter=0;
112                        if(conf.bracket_type>2){
113                                shoot_counter=(conf.bracket_type-2)*2;
114                                }
115     do
116          {
117            usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
118           _kbd_read_keys_r2(usb_physw);
119             
120           }
121
122// ------ modif by Masuji SUTO (start) --------------
123        while(((((usb_physw[2] & USB_MASK)!=USB_MASK) && (nMode==0)) || (((usb_physw[2] & USB_MASK)==USB_MASK) && (nMode==1))) && ((int)get_tick_count()-tick < DELAY_TIMEOUT));
124// ------ modif by Masuji SUTO (end)   --------------
125        }
126  }
127
128else
129   {
130
131      do
132          {
133            usb_physw[2] = 0;                                             // makes sure USB bit is cleared.
134           _kbd_read_keys_r2(usb_physw);
135             
136           }
137      while((usb_physw[2]&USB_MASK) &&  ((int)get_tick_count()-tick < DELAY_TIMEOUT));
138    }
139 } // synch enable
140
141
142if (conf.synch_delay_enable && conf.synch_delay_value>0)       // if delay is switched on and greater than 0
143  {
144    for (count1=0;count1<conf.synch_delay_value+(conf.synch_delay_coarse_value*1000);count1++) // wait delay_value * 0.1ms
145    {
146      for (count2=0;count2<1400;count2++)            // delay approx. 0.1ms
147        {
148        }
149     }
150  }
151
152debug_led(0);
153
154asm volatile ("LDMFD SP!, {R0-R11,LR}\n"); // restore R0-R11 and LR from stack
155}
156
157static void __attribute__((noinline)) mykbd_task_proceed()
158{
159    while (physw_run){
160        _SleepTask(10);
161
162        if (wrap_kbd_p1_f() == 1){ // autorepeat ?
163            _kbd_p2_f();
164        }
165    }
166}
167
168void __attribute__((naked,noinline)) mykbd_task()
169{
170    /* WARNING
171     * Stack pointer manipulation performed here!
172     * This means (but not limited to):
173     *  function arguments destroyed;
174     *  function CAN NOT return properly;
175     *  MUST NOT call or use stack variables before stack
176     *  is setup properly;
177     *
178     */
179
180    register int i;
181    register long *newstack;
182
183#ifndef MALLOCD_STACK
184    newstack = (void*)kbd_stack;
185#else
186    newstack = malloc(NEW_SS);
187#endif
188
189    for (i=0;i<NEW_SS/4;i++)
190        newstack[i]=0xdededede;
191
192    asm volatile (
193        "MOV    SP, %0"
194        :: "r"(((char*)newstack)+NEW_SS)
195        : "memory"
196    );
197
198    mykbd_task_proceed();
199
200    /* function can be modified to restore SP here...
201     */
202
203    _ExitTask();
204}
205
206
207long __attribute__((naked,noinline)) wrap_kbd_p1_f()
208{
209
210    asm volatile(
211                "STMFD   SP!, {R4-R7,LR}\n"
212                "SUB     SP, SP, #0xC\n"
213                "BL      my_kbd_read_keys\n"
214                "B       _kbd_p1_f_cont\n"
215    );
216    return 0; // shut up the compiler
217}
218
219
220void my_kbd_read_keys()
221{
222    kbd_prev_state[0] = kbd_new_state[0];
223    kbd_prev_state[1] = kbd_new_state[1];
224    kbd_prev_state[2] = kbd_new_state[2];
225
226//    _kbd_pwr_on();
227
228    kbd_fetch_data(kbd_new_state);
229
230#if 0
231    if ((new_state[2] & 0x00001000 /* print button */) == 0 )
232        started();
233    else
234        finished();
235#endif
236
237
238    if (kbd_process() == 0){
239        // leave it alone...
240        physw_status[0] = kbd_new_state[0];
241        physw_status[1] = kbd_new_state[1];
242        physw_status[2] = kbd_new_state[2];
243#if 0
244        kbd_mod_state = kbd_new_state[2];
245#endif
246    } else {
247        // override keys
248#if 0
249        physw_status[2] = kbd_mod_state;
250#else
251        physw_status[0] = kbd_new_state[0];
252        physw_status[1] = kbd_new_state[1];
253        physw_status[2] = (kbd_new_state[2] & (~0x1fff)) |
254                          (kbd_mod_state & 0x1fff);
255#endif
256    }
257
258    _kbd_read_keys_r2(physw_status);
259      remote_key = (physw_status[2] & USB_MASK)==USB_MASK;
260
261      if (remote_key)
262        remote_count += 1;
263      else if (remote_count) {
264        usb_power = remote_count;
265        remote_count = 0;
266      }
267
268    if (conf.remote_enable) {
269
270      physw_status[2] = physw_status[2] & ~(SD_READONLY_FLAG | USB_MASK);
271    }
272    else     
273      physw_status[2] = physw_status[2] & ~SD_READONLY_FLAG;
274
275//    _kbd_pwr_off();
276
277}
278int get_usb_power(int edge)
279{
280        int x;
281
282        if (edge) return remote_key;
283        x = usb_power;
284        usb_power = 0;
285        return x;
286}
287/****************/
288
289
290void kbd_key_press(long key)
291{
292    int i;
293    for (i=0;keymap[i].hackkey;i++){
294        if (keymap[i].hackkey == key){
295            kbd_mod_state &= ~keymap[i].canonkey;
296            return;
297        }
298    }
299}
300
301void kbd_key_release(long key)
302{
303    int i;
304    for (i=0;keymap[i].hackkey;i++){
305        if (keymap[i].hackkey == key){
306            kbd_mod_state |= keymap[i].canonkey;
307            return;
308        }
309    }
310}
311
312void kbd_key_release_all()
313{
314    kbd_mod_state |= 0x1fff;
315}
316
317long kbd_is_key_pressed(long key)
318{
319    int i;
320    for (i=0;keymap[i].hackkey;i++){
321        if (keymap[i].hackkey == key){
322            return ((kbd_new_state[2] & keymap[i].canonkey) == 0) ? 1:0;
323        }
324    }
325    return 0;
326}
327
328long kbd_is_key_clicked(long key)
329{
330    int i;
331    for (i=0;keymap[i].hackkey;i++){
332        if (keymap[i].hackkey == key){
333            return ((kbd_prev_state[2] & keymap[i].canonkey) != 0) &&
334                    ((kbd_new_state[2] & keymap[i].canonkey) == 0);
335        }
336    }
337    return 0;
338}
339
340long kbd_get_pressed_key()
341{
342    int i;
343    for (i=0;keymap[i].hackkey;i++){
344        if ((kbd_new_state[2] & keymap[i].canonkey) == 0){
345            return keymap[i].hackkey;
346        }
347    }
348    return 0;
349}
350
351long kbd_get_clicked_key()
352{
353    int i;
354    for (i=0;keymap[i].hackkey;i++){
355        if (((kbd_prev_state[2] & keymap[i].canonkey) != 0) &&
356            ((kbd_new_state[2] & keymap[i].canonkey) == 0)){
357            return keymap[i].hackkey;
358        }
359    }
360    return 0;
361}
362
363void kbd_reset_autoclicked_key() {
364    last_kbd_key = 0;
365}
366
367long kbd_get_autoclicked_key() {
368    static long last_kbd_time = 0, press_count = 0;
369    register long key, t;
370
371    key=kbd_get_clicked_key();
372    if (key) {
373        last_kbd_key = key;
374        press_count = 0;
375        last_kbd_time = get_tick_count();
376        return key;
377    } else {
378        if (last_kbd_key && kbd_is_key_pressed(last_kbd_key)) {
379            t = get_tick_count();
380            if (t-last_kbd_time>((press_count)?175:500)) {
381                ++press_count;
382                last_kbd_time = t;
383                return last_kbd_key;
384            } else {
385                return 0;
386            }
387        } else {
388            last_kbd_key = 0;
389            return 0;
390        }
391    }
392}
393
394long kbd_use_zoom_as_mf() {
395    return 0;
396}
397static KeyMap keymap[] = {
398    /* tiny bug: key order matters. see kbd_get_pressed_key()
399     * for example
400     */
401    { KEY_UP        , 0x00000001 }, // KEY_UP/KEY_ZOOM_IN
402    { KEY_DOWN      , 0x00000002 }, // KEY_DOWN/KEY_ZOOM_OUT/KEY_ERASE
403    { KEY_LEFT      , 0x00000008 },
404    { KEY_RIGHT     , 0x00000004 },
405    { KEY_SET       , 0x00000100 },
406    { KEY_SHOOT_FULL, 0x00000030 }, // note 3 here!
407    { KEY_SHOOT_FULL_ONLY, 0x00000020 },
408    { KEY_SHOOT_HALF, 0x00000010 },
409//  { KEY_ZOOM_IN   , 0x00000001 }, // KEY_UP/KEY_ZOOM_IN
410//  { KEY_ZOOM_OUT  , 0x00000002 }, // KEY_DOWN/KEY_ZOOM_OUT/KEY_ERASE
411    { KEY_MENU      , 0x00000400 },
412    { KEY_DISPLAY   , 0x00000200 },
413    { KEY_PRINT     , 0x00001000 }, // KEY_DOWN/KEY_ZOOM_OUT/KEY_ERASE
414//  { KEY_ERASE     , 0x00000002 },
415//  { KEY_DUMMY     , 0x00001000 },
416    { 0, 0 }
417};
418
419void kbd_fetch_data(long *dst)
420{
421    volatile long *mmio0 = (void*)0xc0220200;
422    volatile long *mmio1 = (void*)0xc0220204;
423    volatile long *mmio2 = (void*)0xc0220208;
424
425    dst[0] = *mmio0;
426    dst[1] = *mmio1;
427    dst[2] = *mmio2 & 0xffff;
428}
429
Note: See TracBrowser for help on using the repository browser.