-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvocoreGPIO.py
108 lines (83 loc) · 5.39 KB
/
vocoreGPIO.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
'''
library is public domain. Enjoy!
Based on the original C++ commands for pin control in Arduino (https://www.arduino.cc/)
Made by Bryan Barbosa (Github: BryanCMBarbosa) from Federal University of Juiz de Fora and CREA Lab - UC Berkeley
'''
import time
def pinMode(num_pin, mode): #Defines pin mode as OUTPUT or
config = file('/sys/class/gpio/export', 'w') #exporting the GPIO%num_pin
config.write(str(num_pin))
config.close()
pin_path = "/sys/class/gpio/gpio%d/" % num_pin #directory where all the files for controlling the pin <num_pin> are stored
file_direc = file(pin_path + "direction", "w") #Opens the file to write data. If we write "out", the pin goes to OUTPUT mode, if we write "in", the pin goes to INPUT mode.
value = "out"
'''
If mode is setted as True, "true", "1", "output", "out", 1 or 1.0, the pin will be setted as output.
Else, pin will be setted as input.
'''
if type(mode) is bool: #Any of this parameters leads to put the pin as OUTPUT
if mode:
value = "out"
else:
value = "in"
elif type(mode) is str:
mode = mode.lower()
if mode == "true" or mode == "1" or mode == "output" or mode == "out":
value = "out"
else:
value = "in"
elif type(mode) is int or type(mode) is float:
if mode == 1 or mode == 1.0:
value = "out"
else:
value = "in"
else:
value = "in"
file_direc.write(value)
file_direc.close()
def digitalWrite(num_pin, state):
pin_path = "/sys/class/gpio/gpio%d/" % num_pin
file_direc = file(pin_path + "direction", "r") #Gets the info if the pin is in OUTPUT mode. We can only use digitalWrite on pins in OUTPUT mode.
pin_mode = file_direc.read()
file_direc.close()
'''
If state is setted as True, "true", "1", "high", "on", 1 or 1.0, the pin will be setted in high state.
Else, the pin will be setted in low state.
'''
if pin_mode == "out" or pin_mode == "out\n" or pin_mode == "out \n":
value = "0"
file_value = file(pin_path + "value", "w") #Opens the file that determinates if the pin is setted as ON or OFF.
if type(state) is bool:
if state:
value = "1"
else:
value = "0"
elif type(state) is str:
state = state.lower()
if state == "true" or state == "1" or state == "high" or state == "on":
value = "1"
else:
value = "0"
elif type(state) is int or type(state) is float:
if state == 1 or state == 1.0:
value = "1"
else:
value = "0"
else:
value = "0"
file_value.write(value)
file_value.close()
else:
print("Pin {} is not in OUTPUT mode.".format(num_pin))
def digitalRead(num_pin):
pin_path = "/sys/class/gpio/gpio%d/" % num_pin
file_value = file(pin_path + "value", "r") #Gets info about the pin state
state = file_value.read()
file_value.close()
if state == "1" or state == "1\n" or state == "1 \n": #If the pin is setted as ON or is receiving a reading, the return will be 1
return 1
return 0
def delay(t): #Delay in Milliseconds
time.sleep((t/1000))
def delayMicroseconds(t): #Delay in Microseconds
time.sleep((t/1000000))