Module LightSensor
[frames] | no frames]

Source Code for Module LightSensor

 1  # LightSensor.py 
 2   
 3  ''' 
 4   This software is part of the EV3BrickPi library. 
 5   It is Open Source Free Software, so you may 
 6   - run the code for any purpose 
 7   - study how the code works and adapt it to your needs 
 8   - integrate all or parts of the code in your own programs 
 9   - redistribute copies of the code 
10   - improve the code and release your improvements to the public 
11   However the use of the code is entirely your responsibility. 
12  ''' 
13   
14  from GenericSensor import GenericSensor 
15  import brickpi3 
16  import time 
17  from Tools import * 
18   
19 -class LightSensor(GenericSensor):
20 ''' 21 Abstraction of the EV3 color sensor used in light detection mode. 22 '''
23 - def __init__(self, port):
24 GenericSensor.__init__(self, port) 25 self.activate(True)
26
27 - def activate(self, enable):
28 if enable: 29 self.bp.set_sensor_type(self._getPort(), self.bp.SENSOR_TYPE.EV3_COLOR_REFLECTED) 30 else: 31 self.bp.set_sensor_type(self._getPort(), self.bp.SENSOR_TYPE.EV3_COLOR_AMBIENT) 32 time.sleep(2) # wait until setup is complete
33
34 - def getValue(self):
35 ''' 36 Returns the reflected light intensity (turns red LED on) or -1, if detection fails. 37 ''' 38 try: 39 value = self.bp.get_sensor(self._getPort()) 40 except brickpi3.SensorError as error: 41 return -1 42 Tools.debug("LightSensor.getValue() returned: " + str(value)) 43 return value
44