B
    :^                 @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
G dd dZdd Zddd	Zed
kre Zejredd ejredejd dS )z
.. module:: shutter
    :platform: unix
    :synopsis: This module is for communicating with the shutter instrument.

.. codeauthor:: Michael Coughlin, Eric Coughlin
    Nc               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )Shutterz?
    This is the class for communicating with the shutter

    c             C   s   d | _ |  | _d | _d S )N)statuscreate_connectionshutterstate)self r   /home/cbp/cbp/cbp/shutter.py__init__   s    
zShutter.__init__c          
   C   sN   yd}t |}d| _|S  tk
rH } zt| d| _W dd}~X Y nX dS )z^
        This method creates a connection by using a child process.

        :return:
        z%picocom -b 57600 /dev/ttyACM.SHUTTER2	connectedznot connectedN)pexpectspawnr   	Exceptionprint)r   shutter_commandchilder   r   r	   r      s    
zShutter.create_connectionc          
   C   sH   yd}t |}W n0 tk
rB } zt| d| _W dd}~X Y nX dS )zP
        This method checks the status of the shutter

        :return:
        z%picocom -b 57600 /dev/ttyACM.SHUTTER2znot connectedN)r   r   r   r   r   )r   r   r   r   r   r   r	   check_status*   s    zShutter.check_statusc             C   s   | j dkr| j  dS )zB
        This method closes the shutter

        :return:
        znot connectedN)r   r   close)r   r   r   r	   close_connection7   s    
zShutter.close_connectionc             C   s`   | j dkr\d}xL|sX| jjtjdgdd}|dkrLd|}| j| d}|d	krqqW n d
S )z
        This method is a duration run of the shutter, -1 can be used to keep shutter open indefinitely or 0 to close it.

        :param shutter: This is the length that the shutter will stay open in milliseconds.
        :return:
        znot connectedF
   )timeoutr   zargs {0:d}T   N)r   r   expectr   ZTIMEOUTformatZsendline)r   r   doneiZ	argstringr   r   r	   run_shutter@   s    

zShutter.run_shutterc             C   s   | j dkrd}t| n dS )zR
        This method compiles the shutter arduino code.

        :return:
        znot connectedz/cd /home/pi/Code/arduino/shutter/; ./compile.shN)r   ossystem)r   Zsteps_commandr   r   r	   
do_compileV   s    
zShutter.do_compileN)
__name__
__module____qualname____doc__r
   r   r   r   r   r!   r   r   r   r	   r      s   	r   c              C   sL   t  } | jdddtd | jddddd	 | jd
ddd	 |  \}}|S )z6
    Parse the options given on the command-line.
    z-sz	--shutter)defaulttypez-cz--doCompile
store_trueF)actionr'   z--doShutter)optparseOptionParser
add_optionint
parse_args)parseroptsargsr   r   r	   parse_commandlinec   s    r3   compilec             C   s.   t  }| dkr|  n| dkr*|| d S )Nr4   r   )r   r!   r   )runtypevalr   r   r   r	   mainr   s
    
r7   __main__)r5   r   )r5   r6   )r4   r   )r%   serialsystimeglobstructr   numpynpr+   r   r   r3   r7   r"   r1   Z	doCompileZ	doShutterr   r   r   r   r	   <module>	   s   0R
	
