-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathACDmain.py
More file actions
55 lines (45 loc) · 1.2 KB
/
ACDmain.py
File metadata and controls
55 lines (45 loc) · 1.2 KB
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
import RPi.GPIO as GPIO
import time
D = [10, 9, 11, 5, 6, 13, 19, 26]
comp = 4
V_pin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(D, GPIO.OUT)
GPIO.setup(comp, GPIO.IN)
GPIO.setup(V_pin, GPIO.OUT)
voltage_step = 3.3 / 255
rounded = 2
bin_depth = 8
time_sleep = 0.001
def num2dac(val):
binary_string = bin(val)[2:].zfill(bin_depth)
binary_array = [int(binary_string[bin_depth - 1 - i]) for i in range(bin_depth)]
GPIO.output(D, binary_array)
def all_out():
GPIO.output(D, 0)
def guess():
for i in range(2 ** bin_depth):
num2dac(i)
time.sleep(time_sleep)
if GPIO.input(comp) == 0:
return f'{0.15 * i}V'
if __name__ == '__main__':
try:
GPIO.output(V_pin, 1)
while True:
value = int(input('Enter value (-1 to exit) > '))
all_out()
if value == -1:
break
if value < 0 or value > 255:
raise Exception
num2dac(value)
print(f'{value} = ' + '{0:.{1}f}'.format(value * voltage_step, rounded))
# except TypeError:
# pass
# except Exception:
# pass
finally:
all_out()
print('Quit')