B
    ¿ìg^6  ã               @   sB   d Z ddlZddlZddlZddlmZ ddlZG dd„ dƒZdS )z;Buttons on Adafruit GPIO Expander Bonnet (MCP23017 device).é    N)ÚMCP23017c               @   sŒ   e Zd ZdZdddddddd	d
ddddddddœZdZdZg Zd#dd„Zdd„ Z	dd„ Z
dd„ Zdd„ ZG dd „ d eƒZG d!d"„ d"eƒZdS )$ÚBonnetButtonzIA button provided by the Adafruit GPIO Expander Bonnet (MCP23017 device).r   é   é   é   é   é   é   é   é   é	   é
   é   é   é   é   é   )ZA0ÚA1ÚA2ZA3ZA4ZA5ZA6ZA7ÚB0ZB1ZB2ZB3ZB4ZB5ZB6ZB7Né    c             C   s   |t _t tjtj¡}yt|| ƒt _W n$ t	k
rJ   t  
d | ¡¡‚Y nX x>| ¡ D ]2\}}tj| }t |||ƒ}| ¡  t j |¡ qVW dS )zInitialize the buttons.z#Button Bonnet not found at 0x{0:x}.N)r   Úbutton_lookupÚbusioZI2CÚboardZSCLZSDAr   ÚmcpÚ
ValueErrorÚHardwareErrorÚformatÚitemsÚ	E2ConfigsÚ
button_mapÚconfigÚbuttonsÚappend)Zi2c_addrr   Zi2cÚlocnÚcmdÚnameÚbutton© r(   ú./home/pi/sexton-multicamera/e2/BonnetButton.pyÚinit_buttons    s    
zBonnetButton.init_buttonsc              C   s   xt jD ]} |  ¡  qW dS )z)Poll for button presses, execute handler.N)r   r"   Úupdate)Úbr(   r(   r)   ÚpollA   s    zBonnetButton.pollc             C   s*   || _ || _|| _tj tj| ¡| _dS )zConstructor.N)r$   r&   Úcommandr   r   Zget_pinÚlocn_to_pinÚpin)Úselfr$   r&   r%   r(   r(   r)   Ú__init__K   s    
zBonnetButton.__init__c             C   s.   t jj| j_t jj| j_| jj| _	| j	| _
dS )z<Configure the pin on the MCP device to be input and pull-up.N)Ú	digitalioZ	DirectionZINPUTr0   Ú	directionZPullZUPZpullÚvalueÚcurrentÚprevious)r1   r(   r(   r)   r!   Z   s    
zBonnetButton.configc             C   sF   | j | _| jj| _ | jdko$| j dk}|rBtd | j¡ƒ |  ¡  dS )z>Keep track of button state. Call command handler when pressed.r   r   z{0} pressedN)r6   r7   r0   r5   Úprintr   r&   r.   )r1   Zpressedr(   r(   r)   r+   d   s    
zBonnetButton.updatec               @   s   e Zd ZdZdS )zBonnetButton.Errorz*Base class for exceptions in BonnetButton.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r(   r(   r)   ÚErroro   s   r=   c               @   s   e Zd ZdZdd„ ZdS )zBonnetButton.HardwareErrorz%Exception raised for hardware errors.c             C   s
   || _ dS )zConstructor.N)Úmessage)r1   r>   r(   r(   r)   r2   {   s    z#BonnetButton.HardwareError.__init__N)r9   r:   r;   r<   r2   r(   r(   r(   r)   r   t   s   r   )r   N)r9   r:   r;   r<   r/   r   r   r"   r*   r-   r2   r!   r+   Ú	Exceptionr=   r   r(   r(   r(   r)   r      s   
!

r   )r<   r   r   r3   Zadafruit_mcp230xx.mcp23017r   r   r   r(   r(   r(   r)   Ú<module>   s   