-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIO.py
137 lines (109 loc) · 3.25 KB
/
GPIO.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
"""This is test code for interacting with GPIO
This is intented to mock up an interface into the RPi GPIO until hardware
is accquired
"""
import json
import threading
import time
try:
import RPi.GPIO as GPIO
"""This is trapped so you can still run without RPi.GPIO
GPIO will be checked before use
"""
pi_interface = True
except:
pi_interface = False
pass
blink_active = False
"""bool: to controll weather blink continues to be active
This will not start the blinking it will only be important once blinking
has started. This is currently only used in testing.
"""
GPIO_STATE = {}
""" This is an object of tracking GPIO"""
GPIO_ON = {}
""" This maps functions to gpio activit
These functions will be called when the GPIO is changed in any way.
"""
is_imported = True
"""Used as a flag to show that GPIO was imported"""
"""This is the setup for the RPi GPIO"""
if pi_interface:
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
def set(key,value):
"""This is used to set values for GPIO
This function is used to set values for each of GPIO's Will also call
the GPIO's function if it exisits.
Args:
key(int or str): Used to identify the gpio to interface
value(int or str): The value to set the gpio to.
"""
GPIO_STATE[key] = value
if (key=="GPIO1") and pi_interface:
if value.upper()=="ON":
GPIO.output(11,True)
GPIO.output(12,True)
else:
GPIO.output(11,False)
GPIO.output(12,False)
if (key=="GPIO2") and pi_interface:
if value.upper()=="ON":
GPIO.output(15,True)
GPIO.output(16,True)
else:
GPIO.output(15,False)
GPIO.output(16,False)
if key in GPIO_ON:
GPIO_ON[key]()
def get(key):
""" Returns the value of the givien GPIO
Args:
key(int or str): Used to identify the gpio to interface
"""
return GPIO_STATE[key]
def on(key,function):
"""Used to set the function for the GPIO interface
Args:
key(int or str): Used to identify the gpio to interface
function(function): The function for the gpio
"""
GPIO_ON[key]= function
def json():
"""Returns a json object of the GPIO info"""
return json.dumps(GPIO_STATE)
if __name__=="__main__":
"""This is console testing of the GPIO. Not ment for automated testing
This should show that everything is working to the developer and allow
for changes going forward
"""
def printgpio():
print(GPIO_STATE)
def blink_led():
if blink_active:
threading.Timer(0.5, blink_led).start()
if get("GPIO1")!="On":
set("GPIO1","On")
else:
set("GPIO1","Off")
def blink_buzzer():
if blink_active:
threading.Timer(0.5, blink_buzzer).start()
if get("GPIO2")!="On":
set("GPIO2","On")
else:
set("GPIO2","Off")
on("GPIO1",printgpio)
on("GPIO2",printgpio)
set("GPIO1","On")
set("GPIO2","On")
blink_active = True
blink_led()
blink_buzzer()
time.sleep(10)
blink_active = False
if pi_interface:
print("GPIO is valid")