legoEv3/ev3dev2/sensor/__pycache__/lego.cpython-35.pyc




[t@sddlZejddfkr-edddlZddlmZddlmZGdddeZGd    d
d
eZ    GdddeZ
Gd
ddeZGdddeeZGdddeZ
GdddeZdS)Nz"Must be using Python 3.4 or higher)
ButtonBase)SensorcseZdZdZejZejZdZefZdedfddZ    e
ddZe
d    d
ZddZ
dd
ddZdd
ddZdd
ddZS)TouchSensorz
    Touch Sensor
    ZTOUCHNFcs/tt|j|||dddg|dS)Ndriver_namezlego-ev3-touchzlego-nxt-touch)superr__init__)selfaddressname_pattern
name_exactkwargs)    __class__5/usr/lib/python3/dist-packages/ev3dev2/sensor/lego.pyr    0szTouchSensor.__init__cCs|j|j|jdS)za
        A boolean indicating whether the current touch sensor is being
        pressed.
        r)_ensure_mode
MODE_TOUCHvalue)r
rrr
is_pressed3szTouchSensor.is_pressedcCs|jS)N)r)r
rrris_released<szTouchSensor.is_releasedcCs}tj}|r"t|d}xT|j|kr8dS|dk    rbtj||dkrbdS|r%tj|q%WdS)NiTF)timefloatrsleep)r
Zwait_for_press
timeout_mssleep_msZticrrr_wait@s&zTouchSensor._wait
cCs|jd||S)NT)r)r
rrrrrwait_for_pressedSszTouchSensor.wait_for_pressedcCs|jd||S)NF)r)r
rrrrrwait_for_releasedVszTouchSensor.wait_for_releasedcCs\tj}|j||rX|dk    rH|ttj|d8}|j||SdS)z
        Wait for the touch sensor to be pressed down and then released.
        Both actions must happen within timeout_ms.
        NiF)rrintr)r
rrZ
start_timerrr
wait_for_bumpYszTouchSensor.wait_for_bump)__name__
__module____qualname____doc__rSYSTEM_CLASS_NAMESYSTEM_DEVICE_NAME_CONVENTIONrMODESr    propertyrrrrrr!rr)rrr$s                rcseZdZdZdddgZejZejZdZdZ    dZ
dZd    Zd
Z
dZdZd
ZdZdZdZdZee    e
eefZd8ZdedfddZeddZed d!Zed"d#Zed$d%Zed&d'Zd(d)Zed*d+Zed,d-Z ed.d/Z!ed0d1Z"ed2d3Z#ed4d5Z$ed6d7Z%S)9ColorSensorz 
    LEGO EV3 color sensor.
    red_max    green_maxblue_maxzCOL-REFLECTzCOL-AMBIENTz    COL-COLORzREF-RAWzRGB-RAWrrrNoColorBlackBlueGreenYellowRedWhiteBrownNFcsDtt|j|||dd|d|_d|_d|_dS)Nrzlego-ev3-colori,)rr*r    r+r,r-)r
rrr
r)rrrr    s%        zColorSensor.__init__cCs|j|j|jdS)zT
        Reflected light intensity as a percentage. Light on sensor is red.
        r)rMODE_COL_REFLECTr)r
rrrreflected_light_intensitysz%ColorSensor.reflected_light_intensitycCs|j|j|jdS)zM
        Ambient light intensity. Light on sensor is dimly lit blue.
        r)rMODE_COL_AMBIENTr)r
rrrambient_light_intensitysz#ColorSensor.ambient_light_intensitycCs|j|j|jdS)z
        Color detected by the sensor, categorized by overall value.
          - 0: No color
          - 1: Black
          - 2: Blue
          - 3: Green
          - 4: Yellow
          - 5: Red
          - 6: White
          - 7: Brown
        r)rMODE_COL_COLORr)r
rrrcolors
zColorSensor.colorcCs|j|jS)z3
        Returns NoColor, Black, Blue, etc
        )COLORSr@)r
rrr
color_nameszColorSensor.color_namecCs8|j|j|jd|jd|jdfS)a
        Red, green, and blue components of the detected color, officially in the
        range 0-1020 but the values returned will never be that high. We do not
        yet know why the values returned are low, but pointing the color sensor
        at a well lit sheet of white paper will return values in the 250-400 range.

        If this is an issue, check out the rgb() and calibrate_white() methods.
        rr.r/)rMODE_RGB_RAWr)r
rrrraws
zColorSensor.rawcCs|j\|_|_|_dS)a
        The RGB raw values are on a scale of 0-1020 but you never see a value
        anywhere close to 1020.  This function is designed to be called when
        the sensor is placed over a white object in order to figure out what
        are the maximum RGB values the robot can expect to see.  We will use
        these maximum values to scale future raw values to a 0-255 range in
        rgb().

        If you never call this function red_max, green_max, and blue_max will
        use a default value of 300.  This default was selected by measuring
        the RGB values of a white sheet of paper in a well lit room.

        Note that there are several variables that influence the maximum RGB
        values detected by the color sensor
        - the distance of the color sensor to the white object
        - the amount of light in the room
        - shadows that the robot casts on the sensor
        N)rDr+r,r-)r
rrrcalibrate_whiteszColorSensor.calibrate_whitecCsm|j\}}}tt|d|jdtt|d|jdtt|d|jdfS)zB
        Same as raw() but RGB values are scaled to 0-255
        )rDminr r+r,r-)r
redgreenbluerrrrgbszColorSensor.rgbcCsdddg}dddg}x[t|jD]J\}}|dkr`t|ddd}n
|d}|d||<q.W|dd|d    d
|dd}|dd
|d    d|dd}|dd|d    d|dd}|d|d<|d|d    <|d|d<xPt|D]B\}}|dkrOt|d}nd|d}|||<q%Wd|d    d}d|d|d    }    d|d    |d}
t|d}t|    d}    t|
d}
||    |
fS) z2
        Return colors in Lab color space
        rg?ܵ?g)\(?gzG?g333333@gףp=
)@gY@gAAe?r.g2݆?r/gjt?gk8?g2݆?gz?gD̓?g]?g&$h?g^IW@gn8[@g2#?g?g@gS%@g]@g@@gi@rgUUUUUU?g{a?)    enumeraterKpowround)r
ZRGBZXYZZnumrXYZLabrrrlabs2
&&&zColorSensor.labcCs|j\}}}t|||}t|||}|}||krUdd|fS|||}||||}||||}    ||||}
||kr|
|    }n+||krd||
}nd|    |}|dd}|||fS)z
        HSV: Hue, Saturation, Value
        H: position in the spectrum
        S: color saturation ("purity")
        V: color brightness
        gg@g@g@g?)rKmaxrG)r
rgrUmaxcmincvsrcgcbchrrrhsv7s"

zColorSensor.hsvcCsF|j\}}}t|||}t|||}||d}||kr]d|dfS|dkr~||||}n3d||dkrd}n||d||}||||}||||}    ||||}
||kr|
|    }n+||krd||
}nd|    |}|dd}|||fS)z
        HLS: Hue, Luminance, Saturation
        H: position in the spectrum
        L: color lightness
        S: color saturation
        g@gg?rg@g@g?)rKrWrG)r
rXrYrUrZr[lr]r^r_r`rarrrhlsWs*
    
zColorSensor.hlscCs|j|j|jdS)zK
        Red component of the detected color, in the range 0-1020.
        r)rrCr)r
rrrrH~szColorSensor.redcCs|j|j|jdS)zM
        Green component of the detected color, in the range 0-1020.
        r.)rrCr)r
rrrrIszColorSensor.greencCs|j|j|jdS)zL
        Blue component of the detected color, in the range 0-1020.
        r/)rrCr)r
rrrrJszColorSensor.blue)r3r4r5r6r7r8r9r:)&r"r#r$r%    __slots__rr&r'r;r=r?ZMODE_REF_RAWrCZ
COLOR_NOCOLORZCOLOR_BLACKZ
COLOR_BLUEZCOLOR_GREENZCOLOR_YELLOWZ    COLOR_REDZCOLOR_WHITEZCOLOR_BROWNr(rAr    r)r<r>r@rBrDrErKrVrbrdrHrIrJrr)rrr*hsX            
/ 'r*cseZdZdZejZejZdZdZdZ    dZ
dZeee    e
efZdedfd    d
Z
eddZed
dZeddZeddZeddZeddZeddZS)UltrasonicSensorz%
    LEGO EV3 ultrasonic sensor.
    z
US-DIST-CMz
US-DIST-INz    US-LISTENzUS-SI-CMzUS-SI-INNFcs/tt|j|||dddg|dS)Nrzlego-ev3-uszlego-nxt-us)rrfr    )r
rrr
r)rrrr    szUltrasonicSensor.__init__cCs*|j|j|jd|jdS)a!
        Measurement of the distance detected by the sensor,
        in centimeters.

        The sensor will continue to take measurements so
        they are available for future reads.

        Prefer using the equivalent :meth:`UltrasonicSensor.distance_centimeters` property.
        r
US_DIST_CM)rMODE_US_DIST_CMr_scale)r
rrrdistance_centimeters_continuoussz0UltrasonicSensor.distance_centimeters_continuouscCs&|j|_|jd|jdS)a
        Measurement of the distance detected by the sensor,
        in centimeters.

        The sensor will take a single measurement then stop
        broadcasting.

        If you use this property too frequently (e.g. every
        100msec), the sensor will sometimes lock up and writing
        to the mode attribute will return an error. A delay of
        250msec between each usage seems sufficient to keep the
        sensor from locking up.
        rrg)
MODE_US_SI_CMmoderri)r
rrrdistance_centimeters_pingsz*UltrasonicSensor.distance_centimeters_pingcCs|jS)z
        Measurement of the distance detected by the sensor,
        in centimeters.

        Equivalent to :meth:`UltrasonicSensor.distance_centimeters_continuous`.
        )rj)r
rrrdistance_centimeterssz%UltrasonicSensor.distance_centimeterscCs*|j|j|jd|jdS)a
        Measurement of the distance detected by the sensor,
        in inches.

        The sensor will continue to take measurements so
        they are available for future reads.

        Prefer using the equivalent :meth:`UltrasonicSensor.distance_inches` property.
        r
US_DIST_IN)rMODE_US_DIST_INrri)r
rrrdistance_inches_continuoussz+UltrasonicSensor.distance_inches_continuouscCs&|j|_|jd|jdS)a
        Measurement of the distance detected by the sensor,
        in inches.

        The sensor will take a single measurement then stop
        broadcasting.

        If you use this property too frequently (e.g. every
        100msec), the sensor will sometimes lock up and writing
        to the mode attribute will return an error. A delay of
        250msec between each usage seems sufficient to keep the
        sensor from locking up.
        rro)
MODE_US_SI_INrlrri)r
rrrdistance_inches_pingsz%UltrasonicSensor.distance_inches_pingcCs|jS)z
        Measurement of the distance detected by the sensor,
        in inches.

        Equivalent to :meth:`UltrasonicSensor.distance_inches_continuous`.
        )rq)r
rrrdistance_inchessz UltrasonicSensor.distance_inchescCs#|j|jt|jdS)ze
        Boolean indicating whether another ultrasonic sensor could
        be heard nearby.
        r)rMODE_US_LISTENboolr)r
rrrother_sensor_presentsz%UltrasonicSensor.other_sensor_present)r"r#r$r%rr&r'rhrprurkrrr(r    r)rjrmrnrqrsrtrwrr)rrrfs*            

rfcseZdZdZejZejZdZdZdZ    dZ
dZdZdZ
eee    e
eee
fZd    ed
fddZed
dZeddZeddZeddZeddZddZddZS)
GyroSensorz
    LEGO EV3 gyro sensor.
    zGYRO-ANGz    GYRO-RATEzGYRO-FASzGYRO-G&AzGYRO-CALz
TILT-ANGLEz    TILT-RATENFcs2tt|j|||dd|d|_dS)Nrz
lego-ev3-gyro)rrxr    _direct)r
rrr
r)rrrr    Bs%zGyroSensor.__init__cCs|j|j|jdS)zq
        The number of degrees that the sensor has been rotated
        since it was put into this mode.
        r)r
MODE_GYRO_ANGr)r
rrrangleFszGyroSensor.anglecCs|j|j|jdS)zN
        The rate at which the sensor is rotating, in degrees/second.
        r)rMODE_GYRO_RATEr)r
rrrrateOszGyroSensor.ratecCs,|j|j|jd|jdfS)zH
        Angle (degrees) and Rotational Speed (degrees/second).
        rr.)r
MODE_GYRO_G_Ar)r
rrrrate_and_angleWszGyroSensor.rate_and_anglecCs|j|j|jdS)Nr)r
MODE_TILT_ANGr)r
rrr
tilt_angle_szGyroSensor.tilt_anglecCs|j|j|jdS)Nr)rMODE_TILT_RATEr)r
rrr    tilt_ratedszGyroSensor.tilt_ratecCs/|j|j|j|jdd|_dS)NZdirect)rrzZset_attr_rawry)r
rrrresetiszGyroSensor.resetcCss|j|j|j|jfks-td|jd}x0t||jd|krntjdq?WdS)zC
        Wait until angle has changed by specified amount.
        zAGyro mode should be MODE_GYRO_ANG, MODE_GYRO_G_A or MODE_TILT_ANGrg{Gz?N)    rlr~rzrAssertionErrorrabsrr)r
ZdeltaZstart_anglerrrwait_until_angle_changed_byms    "z&GyroSensor.wait_until_angle_changed_by)r"r#r$r%rr&r'rzr|Z
MODE_GYRO_FASr~Z
MODE_GYRO_CALrrr(r    r)r{r}rrrrrrr)rrrxs2                rxcsneZdZdZejZejZdZdZdZ    dZ
dZeee    e
efZdgdd    gd
dgdd
gddgdd    d
gdd    dgddd
gdddgddgdd    dgdd
dgiZ
d4ZdZdZd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dedfddZ/ddZ0e1ddZ2dd d!Z3dd"d#Z4dd$d%Z5dd&d'Z6dd(d)Z7dd*d+Z8dd,d-Z9dd.d/Z:dd0d1Z;d2d3Z<S)5InfraredSensorz#
    LEGO EV3 infrared sensor.
    zIR-PROXzIR-SEEKz    IR-REMOTEzIR-REM-AzIR-CALrr.top_leftr/bottom_leftr    top_rightrbottom_rightr0r1r2    beaconrNFcs)tt|j|||dd|dS)Nrzlego-ev3-ir)rrr    )r
rrr
r)rrrr    szInfraredSensor.__init__cCsH|dkr|dks(td|tdtd|d}|S)Nr.rz'channel is %s, it must be 1, 2, 3, or 4)rrWrG)r
channelrrr_normalize_channels(z!InfraredSensor._normalize_channelcCs|j|j|jdS)z
        A measurement of the distance between the sensor and the remote,
        as a percentage. 100% is approximately 70cm/27in.
        r)rMODE_IR_PROXr)r
rrr    proximityszInfraredSensor.proximitycCs0|j|j|j|}|j|dS)zO
        Returns heading (-25, 25) to the beacon on the given channel.
        r/)rMODE_IR_SEEKrr)r
rrrrheadingszInfraredSensor.headingcCsJ|j|j|j|}|j|dd}|dkrFdS|S)z~
        Returns distance (0, 100) to the beacon on the given channel.
        Returns None when beacon is not found.
        r/r.Ni)rrrr)r
rZ    ret_valuerrrdistanceszInfraredSensor.distancecCs|j||j|fS)ze
        Returns heading and distance to the beacon on the given channel as a
        tuple.
        )rr)r
rrrrheading_and_distancesz#InfraredSensor.heading_and_distancecCsd|j|kS)z;
        Checks if ``top_left`` button is pressed.
        r)buttons_pressed)r
rrrrrszInfraredSensor.top_leftcCsd|j|kS)z>
        Checks if ``bottom_left`` button is pressed.
        r)r)r
rrrrrszInfraredSensor.bottom_leftcCsd|j|kS)z<
        Checks if ``top_right`` button is pressed.
        r)r)r
rrrrrszInfraredSensor.top_rightcCsd|j|kS)z?
        Checks if ``bottom_right`` button is pressed.
        r)r)r
rrrrr%szInfraredSensor.bottom_rightcCsd|j|kS)z9
        Checks if ``beacon`` button is pressed.
        r)r)r
rrrrr+szInfraredSensor.beaconcCs;|j|j|j|}|jj|j|gS)z<
        Returns list of currently pressed buttons.
        )rMODE_IR_REMOTEr_BUTTON_VALUESgetr)r
rrrrr1szInfraredSensor.buttons_pressedcsngg}xtddD]}xR|j|D]A}j||f||f|jkr2|j||fq2WxK|jD]@}||fkr||f|jkr|j||fqWqW|j}|_xW|D]O\}}t|dt|d|}|dk    r|||fkqW|jdk    rj|rj|jfdd|DdS)a
        Check for currenly pressed buttons. If the new state differs from the
        old state, call the appropriate button event handlers.

        To use the on_channel1_top_left, etc handlers your program would do something like:
        
        .. code:: python

            def top_left_channel_1_action(state):
                print("top left on channel 1: %s" % state)

            def bottom_right_channel_4_action(state):
                print("bottom right on channel 4: %s" % state)

            ir = InfraredSensor()
            ir.on_channel1_top_left = top_left_channel_1_action
            ir.on_channel4_bottom_right = bottom_right_channel_4_action

            while True:
                ir.process()
                time.sleep(0.01)
        
        r.r0Z
on_channel_Ncs+g|]!\}}|||kfqSrr).0buttonr)    new_staterr
<listcomp>ls    z*InfraredSensor.process.<locals>.<listcomp>)rangerappendZ_state_BUTTONSgetattrstrZ    on_change)r
Z
state_diffrrZ    old_stateZhandlerr)rrprocess9s$'        !zInfraredSensor.process)rrrrr)=r"r#r$r%rr&r'rrrZ
MODE_IR_REM_AZMODE_IR_CALr(rrZ    NO_BUTTONZTOP_LEFTZBOTTOM_LEFTZ    TOP_RIGHTZBOTTOM_RIGHTZTOP_LEFT_TOP_RIGHTZTOP_LEFT_BOTTOM_RIGHTZBOTTOM_LEFT_TOP_RIGHTZBOTTOM_LEFT_BOTTOM_RIGHTZBEACONZTOP_LEFT_BOTTOM_LEFTZTOP_RIGHT_BOTTOM_RIGHTZon_channel1_top_leftZon_channel1_bottom_leftZon_channel1_top_rightZon_channel1_bottom_rightZon_channel1_beaconZon_channel2_top_leftZon_channel2_bottom_leftZon_channel2_top_rightZon_channel2_bottom_rightZon_channel2_beaconZon_channel3_top_leftZon_channel3_bottom_leftZon_channel3_top_rightZon_channel3_bottom_rightZon_channel3_beaconZon_channel4_top_leftZon_channel4_bottom_leftZon_channel4_top_rightZon_channel4_bottom_rightZon_channel4_beaconr    rr)rrrrrrrrrrrrr)rrrys                                    rcseZdZdZejZejZdZdZeefZ    dedfddZ
edd    Zed
dZ
S)SoundSensorz
    LEGO NXT Sound Sensor
    DBDBANFcs)tt|j|||dd|dS)Nrzlego-nxt-sound)rrr    )r
rrr
r)rrrr    szSoundSensor.__init__cCs*|j|j|jd|jdS)zr
        A measurement of the measured sound pressure level, as a
        percent. Uses a flat weighting.
        rr)rMODE_DBrri)r
rrrsound_pressureszSoundSensor.sound_pressurecCs*|j|j|jd|jdS)z
        A measurement of the measured sound pressure level, as a
        percent. Uses A-weighting, which focuses on levels up to 55 dB.
        rr)rMODE_DBArri)r
rrrsound_pressure_lowszSoundSensor.sound_pressure_low)r"r#r$r%rr&r'rrr(r    r)rrrr)rrros                rcseZdZdZejZejZdZdZeefZ    dedfddZ
edd    Zed
dZ
S)LightSensorz
    LEGO NXT Light Sensor
    REFLECTAMBIENTNFcs)tt|j|||dd|dS)Nrzlego-nxt-light)rrr    )r
rrr
r)rrrr    szLightSensor.__init__cCs*|j|j|jd|jdS)zR
        A measurement of the reflected light intensity, as a percentage.
        rr)rMODE_REFLECTrri)r
rrrr<sz%LightSensor.reflected_light_intensitycCs*|j|j|jd|jdS)zP
        A measurement of the ambient light intensity, as a percentage.
        rr)rMODE_AMBIENTrri)r
rrrr>sz#LightSensor.ambient_light_intensity)r"r#r$r%rr&r'rrr(r    r)r<r>rr)rrrs            r)sysversion_infoSystemErrorrZev3dev2.buttonrZev3dev2.sensorrrr*rfrxrrrrrrr<module>sD0^)