1
2
3 '''
4 Class that represents a light sensor.
5
6 This software is part of the raspibrick module.
7 It is Open Source Free Software, so you may
8 - run the code for any purpose
9 - study how the code works and adapt it to your needs
10 - integrate all or parts of the code in your own programs
11 - redistribute copies of the code777
12 - improve the code and release your improvements to the public
13 However the use of the code is entirely your responsibility.
14 '''
15
16 from Tools import Tools
17 from RobotInstance import RobotInstance
18 import SharedConstants
19
21 '''
22 Class that represents an light sensor.
23 '''
25 '''
26 Creates a light sensor instance with given id.
27 IDs: 0: front left, 1: front right, 2: rear left, 3: rear right
28 The following global constants are defined:
29 LS_FRONT_LEFT = 0, LS_FRONT_RIGHT = 1, LS_REAR_LEFT = 2, LS_REAR_RIGHT = 3.
30 @param id: the LightSensor identifier
31 '''
32 self.id = id
33 self.sensorState = "DARK"
34 self.sensorType = "LightSensor"
35 self.triggerLevel = 500
36 self.brightCallback = None
37 self.darkCallback = None
38 self.isRegistered = False
39 for key in kwargs:
40 if key == "bright":
41 self.brightCallback = kwargs[key]
42 elif key == "dark":
43 self.darkCallback = kwargs[key]
44 robot = RobotInstance.getRobot()
45 if robot == None:
46 RobotInstance._sensorsToRegister.append(self)
47 else:
48 if self.brightCallback != None or self.darkCallback != None:
49 robot.registerSensor(self)
50 Tools.debug("LightSensor instance with ID " + str(id) + " created")
51
53 '''
54 Returns the current intensity value (0..1000).
55 @return: the measured light intensity
56 @rtype: int
57 '''
58 self._checkRobot()
59 Tools.delay(1)
60 nb = self.id
61 if nb == 0:
62 nb = 1
63 elif nb == 1:
64 nb = 0
65 robot = RobotInstance.getRobot()
66 return int(self._readADC(nb) / 255.0 * 1000 + 0.5)
67
69 return self.triggerLevel
70
72 self.triggerLevel = level
73
75 return self.sensorState
76
78 self.sensorState = state
79
81 return self.sensorType
82
84 if self.brightCallback != None:
85 self.brightCallback(self.id, v)
86
88 if self.darkCallback != None:
89 self.darkCallback(self.id, v)
90
101
105