
    j                         d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
 dZdZdZd	Z G d
 d          ZdZdZdeddfdZddee         dee         fdZdS )a{  PCsensor FootSwitch integration.

Runs a background thread that reads key-press events from the footpedal and
invokes registered callbacks with the key code.

Usage::

    pedal = FootPedal()   # auto-detects the device via /sys
    pedal.on_press(lambda code: print(f"pedal pressed: {code}"))
    pedal.open()
    pedal.start()
    ...
    pedal.close()

The device path is resolved from /sys/class/input without needing to open any
/dev/input file, so auto-detection works before the udev rule is installed.
Opening the device itself does require the udev rule (or sudo).  Run once::

    sudo bash scripts/install_footpedal_udev.sh
    N)Path)CallableListOptional)InputDeviceecodeszPCsensor FootSwitch Keyboard   0   .   c                       e Zd ZdZddee         fdZedefd            ZddZ	ddZ
dd	Zd
eegdf         ddfdZddZdS )	FootPedalz@Reads PCsensor FootSwitch button presses in a background thread.Ndevice_pathc                     |p|                                  | _        d| _        d| _        t	          j                    | _        g | _        dS )z\Args:
        device_path: explicit /dev/input/eventN path.  Auto-detected if None.
        N)_find_device_path_device_path_device_thread	threadingEvent_stop_event
_callbacks)selfr   s     =/home/robot-lab/cameron/raiden_fork/raiden/robot/footpedal.py__init__zFootPedal.__init__(   sF     (C4+A+A+C+C.237$?,,79    returnc                  j   t          t          d                              d          d           D ]i} | dz  dz  }|                                rKt                                          |                                                                v rd| j         c S jt          dt          d	          )
zBScan /sys/class/input for a matching device name (no open needed).z/sys/class/inputzevent*c                 :    t          | j        dd                    S )N   )intname)ps    r   <lambda>z-FootPedal._find_device_path.<locals>.<lambda>;   s    #afQRRj// r   )keydevicer!   z/dev/input/zFootPedal (z) not found. Make sure it is plugged in. If this is the first run, install the udev rule first:
  sudo bash scripts/install_footpedal_udev.sh)	sortedr   globexistsDEVICE_NAMElower	read_textr!   RuntimeError)	event_dir	name_files     r   r   zFootPedal._find_device_path6   s      #$$))(33))
 
 
 		6 		6I "H,v5I  ""6%%''9+>+>+@+@+F+F+H+HHH5Y^55555<+ < < <
 
 	
r   c                     t          | j                  | _        t          d| j        j         d| j         d           d S )Nu     ✓ FootPedal opened: z ())r   r   r   printr!   r   s    r   openzFootPedal.openN   sD    "4#455R):RRd>ORRRSSSSSr   c                     | j                                          | j        | j                            d           | j        "| j                                         d | _        d S d S )Ng       @)timeout)r   setr   joinr   closer2   s    r   r8   zFootPedal.closeR   sg    <#Lc***<#L   DLLL $#r   c                     | j                                          t          j        | j        dd          | _        | j                                         dS )z&Start background event-reading thread.	footpedalT)targetr!   daemonN)r   clearr   Thread
_read_loopr   startr2   s    r   r@   zFootPedal.startZ   sW        '?
 
 

 	r   callbackc                 :    | j                             |           dS )u   Register *callback(key_code)* — called on every pedal press.

        The callback is invoked from the footpedal thread; keep it short or
        hand off to another thread if needed.
        N)r   append)r   rA   s     r   on_presszFootPedal.on_pressh   s      	x(((((r   c                    	 | j                                         D ]}| j                                        r d S |j        t
          j        k    rO|j        dk    rD| j        D ]<}	  ||j	                   # t          $ r}t          d|            Y d }~5d }~ww xY wd S # t          $ r<}| j                                        st          d|            Y d }~d S Y d }~d S d }~ww xY w)N   z  FootPedal callback error: z  FootPedal read error: )r   	read_loopr   is_settyper   EV_KEYvaluer   code	Exceptionr1   OSError)r   eventcbes       r   r?   zFootPedal._read_loopt   s^   	6//11 F F#**,, EE:..5;!3C3C"o F FFBuzNNNN( F F F!"D"D"DEEEEEEEEFF F  	6 	6 	6#**,, 64445555555556 6 6 6 6 6	6sF   5B  )B  #A43B  4
B>BB  BB   
C&*+C!!C&N)r   N)__name__
__module____qualname____doc__r   strr   staticmethodr   r3   r8   r@   r   r    rD   r?    r   r   r   r   %   s        JJ: :HSM : : : : 
s 
 
 
 \
.T T T T          )3%+!6 )4 ) ) ) )6 6 6 6 6 6r   r   z[1;33mz[0mmsgr   c                     t          t           d t                      |                                 D ]"}t          t           d| t                      #t          t           d t           d           d S )Nz<!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!z  
)r1   _YELLOW_RESET
splitlines)rZ   lines     r   _warnra      s    	W
(h
(
(
()))   , ,**D*&**++++	W
*h
*
*
*
*+++++r   r   c                     	 t          |           }|                                 |S # t          $ r}t          d| d           Y d}~dS d}~wt          $ r}t          d| d           Y d}~dS d}~ww xY w)u   Create, open, and return a FootPedal, or return None with a warning.

    Intended for callers that treat the footpedal as optional — recording and
    teleoperation continue normally without it.
    u   FootPedal not available — z 
Continuing WITHOUT soft e-stop.NzFootPedal permission denied (z_)
Run once to fix:  sudo bash scripts/install_footpedal_udev.sh
Continuing WITHOUT soft e-stop.)r   r3   r,   ra   PermissionError)r   pedalrQ   s      r   try_open_footpedalre      s    +&&

   QQQQQRRRttttt   .A . . .	
 	
 	

 ttttts    $' 
A5A

A5A00A5rR   )rV   r   pathlibr   typingr   r   r   evdevr   r   r)   
PEDAL_LEFTPEDAL_MIDDLEPEDAL_RIGHTr   r]   r^   rW   ra   re   rY   r   r   <module>rl      s   *           + + + + + + + + + + % % % % % % % %, 
\6 \6 \6 \6 \6 \6 \6 \6H 	,s ,t , , , , HSM Xi=P      r   