-
Notifications
You must be signed in to change notification settings - Fork 0
/
potentiometer.py
82 lines (52 loc) · 1.95 KB
/
potentiometer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import time
from threading import Thread, Lock
from random import randint
from helper_funcs import is_raspberrypi
######################################################
# ADS1115 chip, each value will be 16 bit int solution
######################################################
class PotentiometerSampling:
__GAIN = 1
__poti_brightness = 200
def __init__(self) -> None:
self.__is_raspberry = is_raspberrypi()
if self.__is_raspberry == True:
import Adafruit_ADS1x15
self.__adc = Adafruit_ADS1x15.ADS1115()
self.__lock = Lock()
__t = Thread(target=self.__sample_potentiometer_thread, daemon=True)
__t.start()
def get_poti_brightness(self):
return self.__poti_brightness
def __read_poti_val(self):
if self.__is_raspberry:
return self.__adc.read_adc(0, gain=self.__GAIN, data_rate = 128)
else:
return randint(3,32558)
def __get_poti_brightness(self, current_value):
...
min_value = 3
max_value = 32558
val_percent = (current_value - min_value) / (max_value - min_value)
if val_percent > 1:
val_percent = 1
if val_percent < 0:
val_percent = 0
brightnes = int(round(255 * val_percent, 0))
return brightnes
def __sample_potentiometer_thread(self):
...
while True:
# value = self.adc.read_adc(0, gain=self.GAIN, data_rate = 128)
value = self.__read_poti_val()
# print("reading poti val:", value)
brightness = self.__get_poti_brightness(value)
with self.__lock:
self.__poti_brightness = brightness
time.sleep(0.2)
if __name__ == "__main__":
...
poti = PotentiometerSampling()
while True:
# print("Poti val = ", poti.get_poti_brightness())
time.sleep(1)