ó
Ñ+]Yc           @   sx   d  Z  d d l Z d d l Z d d l Z d d l Z d d	 d „  ƒ  YZ d „  Z d „  Z e	 d k rt e d ƒ n  d S(
   sU   
This is a module that communicates with the ESKPLA laser in the lab via RS232 port.
iÿÿÿÿNt   LaserSerialInterfacec           B   se   e  Z d  Z e d „ Z d „  Z d „  Z d „  Z d „  Z e	 d „ Z
 e d d „ ƒ Z d	 „  Z RS(
   s]   
    This class is for communicating with the laser through the RS232 serial interface.

    c         C   s¶   d  |  _ d  |  _ i d d g d 6d d g d 6d d g d	 6d d
 g d 6|  _ | rj t j d ƒ |  _ n! t j d d d d d d ƒ |  _ i d d 6|  _ i d d 6d d 6|  _ d  S(   Nt   readys   The device is readys   [PC:READY=0\NL]t   busys   The device is busys   [PC:BUSY=0\NL]t   offs   The device is offs   [PC:OFF=0\NL]s+   The device is ready but with cooling error.s   [PC:READY=2048\NL]s   loop://t   ports   /dev/ttyUSB.LASERt   baudratei K  t   timeouti   s   [NL:SAY\PC]t   say_state_msgs   Unrecognized strings   [NL:What\PC]s   Unrecognized commands   [NL:Ignored\PC](	   t   Nonet   statet   errort   statest   serialt   serial_for_urlt   Serialt   commandst	   responses(   t   selft   loop(    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   __init__   s    		!c         C   sz   d d d d g } xa | D]Y } |  j  d } |  j j | ƒ | |  j k r |  j | d |  _ |  j | d GHq q Wd S(   ss   
        This is a method for testing the state of the laser in a loop-back environment.

        :return:
        s   [PC:OFF=0\NL]s   [PC:BUSY=0\NL]s   [PC:READY=0\NL]R   i    i   N(   R   R   t   writeR   R	   (   R   R   t   responseR   (    (    s    /home/pi/Code/cbp_2/bin/laser.pyt
   test_state   s    c         C   sq   |  j  d } |  j j | ƒ |  j j d d ƒ } | |  j k rh |  j | d |  _ |  j | d GHn d GHd S(   sˆ   
        This method checks the status of the laser and then sets the state of the laser inside of the class.

        :return:
        R   t   sizei   i    i   s
   Not found.N(   R   R   R   t   readR   R	   (   R   R   R   (    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   check_state+   s    c         C   s$   x |  j  d k r |  j ƒ  q Wd S(   sy   
        This method calls the :py:meth:`check_state` until a ready state is set by the class.

        :return:
        R   N(   R	   R   (   R   (    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   get_ready_state:   s    c         C   sd   |  j  ƒ  d j t | ƒ ƒ } |  j j | ƒ |  j d t ƒ } t | ƒ | k r[ d GHn d GHd S(   sõ   
        This method changes the wavelength of the laser.

        :param wavelength: This is the value of the wavelength to be set. Units are in nanometers and limits are 355nm
                            to 2300 nm.

        :return:

        s	   [W0/S{0}]t
   comparisons   Wavelength set correctlys   Something went wrongN(   R   t   formatt   strR   R   t   check_wavelengtht   Truet   int(   R   t
   wavelengtht   wavelength_change_msgt   check_response(    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   change_wavelengthC   s    

c         C   s^   d } |  j  j | ƒ |  j  j d d ƒ } | GH| rI |  j | ƒ } | S|  j ƒ  } | GHd  S(   Ns   [W0/?]R   i   (   R   R   R   t   parse_wavelength(   R   R   t   wavelength_check_msgR   R!   (    (    s    /home/pi/Code/cbp_2/bin/laser.pyR   V   s    s   [MS:W0/S520\NL]c         C   s}   |  } g  } x t  j D] } | j | ƒ q Wx t  j D] } | j | ƒ q7 W| j d  d j | ƒ ƒ } | d } t | ƒ S(   Nt    i   (   t   stringt   letterst   appendt   punctuationt	   translateR   t   joinR    (   t   msgt	   msg_parset   remove_charst   chart	   split_msg(    (    s    /home/pi/Code/cbp_2/bin/laser.pyR%   b   s    
c         C   s=   t  j | | d ƒ } x | D] } |  j | ƒ q Wd GHd  S(   Ni   s   done.(   t   npt   arangeR$   (   R   t   mint   maxt   np_arrayt   item(    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   loop_change_wavelengthn   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   R$   t   FalseR   t   staticmethodR%   R9   (    (    (    s    /home/pi/Code/cbp_2/bin/laser.pyR       s   					c          C   s/   t  j d d ƒ }  |  j d d d d d ƒ|  S(   Nt   descriptionsD   Program to change the wavelength of the laser using rs232 interface.R!   t   nargsi   t   helps=   This is for setting the value of the wavelength of the laser.(   t   argparset   ArgumentParsert   add_argument(   t   parser(    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   create_parseru   s    c         C   s#   t  d t ƒ } | j d d ƒ d S(   sV   
    This creates a command line and arguments for the script.

    :return: None
    R   iô  i  N(   R    R   R9   (   R!   t   laser_interface(    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   main{   s    t   __main__iô  (    (
   R<   R   RB   R(   t   numpyR3   R    RF   RH   R:   (    (    (    s    /home/pi/Code/cbp_2/bin/laser.pyt   <module>   s   j		