ó
Nš–Zc           @   s'  d  d l  Z  d  d l Z d  d l Z d e j f d „  ƒ  YZ e d k r#d  d l Z d  d l Z d  d l Z d Z d Z	 d Z
 d „  Z e j ƒ  Z e j e d	 d
 d d d d ƒ Z e j e e j ƒ e j d ƒ d Z e j e ƒ e d ƒ x2 e d ƒ D]$ Z e  j e ƒ e j ƒ  Z e GHqÿ We j e ƒ e d ƒ x2 e d ƒ D]$ Z e  j e ƒ e j ƒ  Z e GHqKWe j e ƒ y} xv e rûe j ƒ  Z e j e e d ƒ e j e	 e d ƒ e j e
 e d ƒ e e j ƒ  e j f GHe  j e ƒ q†WWn  d GHe j ƒ  e j  ƒ  n Xn  d S(   iÿÿÿÿNt   sensorc           B   sÑ   e  Z d  Z d d d d d „ Z d „  Z d „  Z d d „ Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sü  
   This class reads RGB values from a TCS3200 colour sensor.

   GND   Ground.
   VDD   Supply Voltage (2.7-5.5V)
   LED   1: LEDs on, 0: LEDs off
   /OE   Output enable, active low. When OE is high OUT is disabled
         allowing multiple sensors to share the same OUT line.
   OUT   Output frequency square wave.
   S0/S1 Output frequency scale selection.
   S2/S3 Colour filter selection.

   OUT is a square wave whose frequency is proprtional to the
   intensity of the selected filter colour.

   S2/S3 selects between red, green, blue, and no filter.

   S0/S1 scales the frequency at 100%, 20%, 2% or off.

   To take a reading the colour filters are selected in turn for a
   fraction of a second and the frequency is read and converted to
   Hz.  
   c	   	      C   sÏ  t  j j |  ƒ | |  _ | |  _ | |  _ | |  _ | j | ƒ |  _ | j | ƒ |  _	 | j | ƒ |  _
 | j | d ƒ | j | t j ƒ | j | t j ƒ | |  _ | |  _ | |  _ | |  _ | d k	 r)| d k	 r)| j | ƒ |  _ | j | ƒ |  _ | j | t j ƒ | j | t j ƒ n  | d k	 rm| j | ƒ |  _ | j | t j ƒ | j | d ƒ n  | d k	 r±| j | ƒ |  _ | j | t j ƒ | j | d ƒ n  |  j d ƒ |  j d ƒ |  j d ƒ |  j d ƒ d g d |  _ d g d |  _ d g d |  _ d g d |  _ d g d |  _  d g d |  _! d g d |  _" d |  _# | j$ | t j% |  j& ƒ |  _' | j$ | t j( |  j& ƒ |  _) | j$ | t j( |  j& ƒ |  _* t+ |  _, |  j- ƒ  d S(	   s¦   
      The gpios connected to the sensor OUT, S2, and S3 pins must
      be specified.  The S0, S1 (frequency) and OE (output enable)
      gpios are optional.
      i    i   gš™™™™™É?i   i   i'  gš™™™™™¹?N(.   t	   threadingt   Threadt   __init__t   _pit   _OUTt   _S2t   _S3t   get_modet	   _mode_OUTt   _mode_S2t   _mode_S3t   writet   set_modet   pigpiot   OUTPUTt   _S0t   _S1t   _OEt   _LEDt   Nonet   _mode_S0t   _mode_S1t	   _mode_LEDt   _mode_OEt   set_sample_sizet   set_update_intervalt   set_frequencyt   _set_filtert
   _rgb_blackt
   _rgb_whitet   hertzt   _hertzt   tallyt   _tallyt   _delayt   _cyclet   callbackt   RISING_EDGEt   _cbft   _cb_OUTt   EITHER_EDGEt   _cb_S2t   _cb_S3t   Truet   daemont   start(	   t   selft   pit   OUTt   S2t   S3t   S0t   S1t   LEDt   OE(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR   $   sZ    										c         C   s3   | r | j  |  j d ƒ n | j  |  j d ƒ d  S(   Ni   i    (   R   R   (   R/   R0   t   onOff(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   ledk   s    c         C   s$  |  j  j ƒ  |  j j ƒ  |  j j ƒ  |  j d ƒ |  j d ƒ |  j j |  j |  j	 ƒ |  j j |  j
 |  j ƒ |  j j |  j |  j ƒ |  j d k	 rß |  j d k	 rß |  j j |  j |  j ƒ |  j j |  j |  j ƒ n  |  j d k	 r |  j j |  j d ƒ |  j j |  j |  j ƒ n  d S(   s6   
      Cancel the sensor and release resources.
      i    i   i   N(   R+   t   cancelR*   R(   R   R   R   R   R   R	   R   R
   R   R   R   R   R   R   R   R   R   R   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR:   r   s    iÿ   c         C   s›   d g d } x† t  d ƒ D]x } |  j | |  j | } |  j | |  j | } | | | } | d k  rs d } n | | k rˆ | } n  | | | <q W| S(   s¯  
      Get the latest RGB reading.

      The raw colour hertz readings are converted to RGB values
      as follows.

      RGB = 255 * (Fv - Fb) / (Fw - Fb)

      Where Fv is the sampled hertz, Fw is the calibrated
      white hertz, and Fb is the calibrated black hertz.

      By default the RGB values are constrained to be between
      0 and 255.  A different upper limit can be set by using
      the top parameter.
      i    i   (   t   rangeR   R   R   (   R/   t   topt   rgbt   ct   vt   st   p(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   get_rgbŠ   s    		c         C   s   |  j  S(   s+   
      Get the latest hertz reading.
      (   R   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt	   get_hertz¦   s    c         C   s,   x% t  d ƒ D] } | | |  j | <q Wd S(   s.   
      Set the black level calibration.
      i   N(   R;   R   (   R/   R=   t   i(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   set_black_level¬   s    c         C   s   |  j  S(   s.   
      Get the black level calibration.
      (   R   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   get_black_level³   s    c         C   s,   x% t  d ƒ D] } | | |  j | <q Wd S(   s.   
      Set the white level calibration.
      i   N(   R;   R   (   R/   R=   RD   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   set_white_level¹   s    c         C   s   |  j  S(   s.   
      Get the white level calibration.
      (   R   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   get_white_levelÀ   s    c         C   sÀ   | d k r d } d } nB | d k r6 d } d } n' | d k rQ d } d } n d } d } |  j  d k	 r³ |  j d k	 r³ | |  _ |  j j |  j  | ƒ |  j j |  j | ƒ n	 d |  _ d S(   s    
      Set the frequency scaling.

      f  S0  S1  Frequency scaling
      0  L   L   Off
      1  L   H   2%
      2  H   L   20%
      3  H   H   100%
      i    i   i   N(   R   R   R   t
   _frequencyR   R   (   R/   t   fR4   R5   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR   Æ   s     
 	 	 	 	c         C   s   |  j  S(   s0   
      Get the current frequency scaling.
      (   RI   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   get_frequencyà   s    c         C   s(   | d k r$ | d k  r$ | |  _  n  d S(   s3   
      Set the interval between RGB updates.
      gš™™™™™¹?g       @N(   t	   _interval(   R/   t   t(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR   æ   s    c         C   s   |  j  S(   s3   
      Get the interval between RGB updates.
      (   RL   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   get_update_intervalí   s    c         C   s7   | d k  r d } n | d k r* d } n  | |  _  d S(   sM   
      Set the sample size (number of frequency cycles to accumulate).
      i
   id   N(   t   _samples(   R/   t   samples(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR   ó   s
    		c         C   s   |  j  S(   s"   
      Get the sample size.
      (   RO   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   get_sample_sizeþ   s    c         C   s   t  |  _ d S(   s5   
      Pause reading (until a call to resume).
      N(   t   Falset   _read(   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   pause  s    c         C   s   t  |  _ d S(   s5   
      Resume reading (after a call to pause).
      N(   R,   RS   (   R/   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   resume
  s    c         C   s   | d k r d } d } nB | d k r6 d } d } n' | d k rQ d } d } n d } d } |  j  j |  j | ƒ |  j  j |  j | ƒ d S(   s­   
      Set the colour to be sampled.

      f  S2  S3  Photodiode
      0  L   L   Red
      1  H   H   Green
      2  L   H   Blue
      3  H   L   Clear (no filter)
      i    i   i   N(   R   R   R   R   (   R/   RJ   R2   R3   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR     s    
 	 	 	  c         C   sg  | |  j  k rE |  j d k r* | |  _ n	 | |  _ |  j d 7_ n| |  j k rv | d k rm d |  _ d  Sd } n | d k r‹ d } n d } |  j d k rò |  j d 8_ t j |  j |  j ƒ } d |  j | |  j | <|  j |  j | <n d |  j | <d |  j | <d |  _ | d k rcx? t	 d ƒ D]. } |  j | |  j
 | <|  j | |  j | <q.Wn  d  S(   Ni    i   i   i@B i   (   R   R$   t   _start_tickt
   _last_tickR   R   t   tickDiffR    R"   R;   R   R!   (   R/   t   gt   lRM   t   colourt   tdRD   (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR'   %  s2    					c         C   sš  t  |  _ xŠt  r•|  j r…t j ƒ  |  j } |  j j |  j t j ƒ |  j	 d ƒ t j
 |  j d ƒ |  j	 d ƒ t j
 |  j d ƒ |  j	 d ƒ t j
 |  j d ƒ |  j j |  j d ƒ |  j	 d ƒ | t j ƒ  } | d k rù t j
 | ƒ n  x– t d ƒ D]x } |  j | r6|  j t |  j | ƒ } n |  j | d } | d k  r\d } n | d k rqd } n  | |  j | <qWq t j
 d ƒ q Wd  S(	   Ni    i   i   i   g        gš™™™™™¹?gü©ñÒMbP?g      à?(   R,   RS   t   timeRL   R   R   R   R   t   INPUTR   t   sleepR#   R   R;   R   RO   t   float(   R/   t	   next_timet   delayR>   t   dly(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   runL  s4    					N(   t   __name__t
   __module__t   __doc__R   R   R9   R:   RB   RC   RE   RF   RG   RH   R   RK   R   RN   R   RQ   RT   RU   R   R'   Rd   (    (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyR       s*   G																	't   __main__i   i   i   c         C   s*   t  j d k  r t |  ƒ n
 t |  ƒ d  S(   Ni   (   t   syst
   hexversiont	   raw_inputt   input(   t   str(    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   wait_for_return  s    i   i   i   i   i   i   i   gš™™™™™É?s/   Calibrating black object, press RETURN to starti   s/   Calibrating white object, press RETURN to starti    i   t
   cancelling(!   R]   R   R   R   R    Re   Ri   t   TCS3200t   REDt   GREENt   BLUERn   R0   R@   R9   t   ONR   t   intervalR   R;   RD   R_   RC   t   hzRE   RG   R,   RB   R=   t   set_PWM_dutycycleR!   R:   t   stop(    (    (    s,   /home/uli/afnog/workspace/vPython/TCS3200.pyt   <module>   sT   ÿ y	!
	
		
