Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from sensor_adc import SensorADC 

2 

3 

4class LightSensor(SensorADC): 

5 

6 """LightSensor, measures amount of light""" 

7 

8 def __init__(self, pin_num, range_min=0, range_max=4000, average_converging_speed=1 / 2, threshold=3000): 

9 """constructor. 

10 :param pin_num: number pin that will read value, it has to be ADC, analog to digital converter 

11 :param range_min: min value of sensor (empty), 

12 :param range_max: max value of sensor (full), 

13 :param threshold: limit below is light, above it's dark """ 

14 super().__init__(pin_num, range_min, range_max, average_converging_speed) 

15 self.threshold = threshold 

16 

17 def __str__(self): 

18 """prints the object""" 

19 return "LightSensor: average: {}, last measure {}, percentage: {}, threshold: {}".format(self.get_average(), 

20 self.last_measure, 

21 self.get_percentage(), 

22 self.threshold) 

23 

24 def get_percentage(self): 

25 """actual percentage of light (0 is lighter)""" 

26 return super().get_percentage() 

27 

28 def is_light(self): 

29 """true if below threshold of light""" 

30 return self.measure() < self.threshold 

31 

32 def is_dark(self): 

33 """true if above threshold""" 

34 return not self.is_light()