-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathals.py
46 lines (39 loc) · 1.33 KB
/
als.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
from time import sleep
from collections import deque
from stoppable_thread import StoppableThread
class AmbientLightSensor(StoppableThread):
def __init__(self):
super(AmbientLightSensor, self).__init__()
self._max_value = 7880
self._filter_depth = 10
self._filter = deque(self._filter_depth * [50.0], self._filter_depth)
self._value = None
self._sensor_data_file = '/sys/bus/acpi/devices/ACPI0008:00/ali'
def run(self):
with open(self._sensor_data_file) as f:
raw = f.read()
value = 100.0 - 1000.0 / (float(raw)/3 + 10.0)
self._filter.append(value)
self._filter.popleft()
self._value = int(round(sum(self._filter) / len(self._filter)))
sleep(2)
def get_raw(self):
with open(self._sensor_data_file) as f:
value = f.read()
return int(value)
def get_value(self):
return self._value
if __name__ == "__main__":
als = AmbientLightSensor()
try:
als.start()
while True:
value = als.get_value()
if value is not None:
print("%d : %d" % (als.get_raw(), als.get_value()))
else:
print('waiting...')
sleep(0.5)
except KeyboardInterrupt:
als.stop()