root/core/usb_input.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. usb_one_press_switch
  2. usb_two_press_switch
  3. usb_ricoh_ca1_switch
  4. usb_null_driver

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

/* [<][>][^][v][top][bottom][index][help] */