-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
168 lines (138 loc) · 4.59 KB
/
control.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python3
import RPi.GPIO as GPIO
import sys
import os
from time import sleep
raw_input = input # Python 3
bounce = 25
path = (os.path.dirname(os.path.abspath(__file__)))
if len(sys.argv) > 1:
cmd = sys.argv[1].lower()
GPIO.setmode(GPIO.BCM)# utilizamos la numeracion BCM de la placa
GPIO.setwarnings(False)
if cmd == "lout":
des = int(sys.argv[2])
def write_txt_led(data):
with open(path+'/TXT_Files/Leds/led'+str(des)+'.txt', 'w') as bit:
bit.write(str(data))
write_txt_led(0)
while True:
try:
data = raw_input()
if 'close' in data:
write_txt_led(0)
sys.exit(0)
data = int(data)
except (EOFError, SystemExit):
sys.exit(0)
if data != 0:
data = 1
write_txt_led(data)
elif cmd == "di":
pin = int(sys.argv[2])
def handle_callback(chan):
sleep(bounce/1000.0)
print(GPIO.input(chan))
GPIO.setup(pin,GPIO.IN,GPIO.PUD_DOWN)
GPIO.add_event_detect(pin, GPIO.BOTH, callback=handle_callback, bouncetime=bounce)
sleep(0.1)
print(GPIO.input(pin))
while True:
try:
data = raw_input()
if 'close' in data:
sys.exit(0)
except (EOFError, SystemExit):
GPIO.cleanup(pin)
sys.exit(0)
elif cmd == "s":
des = int(sys.argv[2])
datos = []
def read_buttons():
with open(path+'/TXT_Files/Buttons/buttons.txt') as archivo:
for linea in archivo:
datos.append(linea.strip('\n'))
button = 0
for i in range(len(datos)):
if i == des:
button = datos[i]
datos.clear()
sleep(0.004)
return button
pin_1 = read_buttons()
print(pin_1)
while True:
try:
pin_read = read_buttons()
if pin_read != pin_1:
print(pin_read)
pin_1 = pin_read
except:
sys.exit(0)
elif cmd == "do":
des = int(sys.argv[2])
def write_txt_out(data):
with open(path+'/TXT_Files/DOs/DO'+str(des)+'.txt', 'w') as bit:
bit.write(str(data))
write_txt_out(0)
while True:
try:
data = raw_input()
if 'close' in data:
write_txt_out(0)
sys.exit(0)
data = int(data)
except (EOFError, SystemExit):
sys.exit(0)
if data != 0:
data = 1
write_txt_out(data)
elif cmd == "adc":
des = sys.argv[2]
prom = []
S_K = 0
alpha = 0.05
mean = 0.0
def read_adcs():
with open(path+'/TXT_Files/ADC/adc'+des+'.txt') as archivo:
dato = archivo.read()
if not dato:
dato = str(adc_1);
adc = float(dato)
return adc
adc_1 = read_adcs()
prom.append(adc_1)
print(adc_1)
while True:
try:
adc_read = read_adcs()
if len(prom) < 10:
S_K = alpha*adc_read+(1-alpha)*S_K
prom.append(round(S_K, 1))
else:
mean = round(sum(prom)/float(len(prom)), 1)
prom.clear()
if mean != adc_1:
print(mean)
sleep(0.2)
adc_1 = mean
except:
sys.exit(0)
elif cmd == "dac":
des = sys.argv[2]
def write_txt_dac(data):
with open(path+'/TXT_Files/DACs/dac'+str(des)+'.txt', 'w') as bit:
bit.write(data)
write_txt_dac('0')
while True:
try:
data = raw_input()
if 'close' in data:
write_txt_dac('0')
sys.exit(0)
except (EOFError, SystemExit):
data = '0'
sys.exit(0)
write_txt_dac(data)
else:
print("Parametros Incorrectos - Revisar que se cumpla con los requerimientos y argumentos")