forked from directive0/picorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpiobasics.py
141 lines (106 loc) · 3.65 KB
/
gpiobasics.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
#!/usr/bin/python
# External module imports
import RPi.GPIO as GPIO
import pygame
import sys
import time
# Pin Definitons:
led1 = 4 # Broadcom pin 4 (Pi0 pin 7)
led2 = 17 # Broadcom pin 17 (Pi0 pin 11)
led3 = 27 # Broadcom pin 27 (P1 pin 13)
# The following pins are my three buttons.
buta = 5
butb = 6
butc = 13
# Pin Setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(led1, GPIO.OUT) # LED pin set as output
GPIO.setup(led2, GPIO.OUT) # LED pin set as output
GPIO.setup(led3, GPIO.OUT) # LED pin set as output
GPIO.setup(buta, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Circle Button for GPIO23
GPIO.setup(butb, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Square Button for GPIO22
GPIO.setup(butc, GPIO.IN, pull_up_down=GPIO.PUD_UP) #R Button for GPIO4
# a function to clear the gpio
def cleangpio():
GPIO.cleanup() # cleanup all GPIO
# a function to clear the LEDs
def resetleds():
GPIO.output(led1, GPIO.LOW)
GPIO.output(led2, GPIO.LOW)
GPIO.output(led3, GPIO.LOW)
# The following set of functions are for activating each LED individually. I figured it was easier than having different functions for different combinations. This way you can just manually set them as you please.
def leda_on():
GPIO.output(led1, GPIO.HIGH)
def ledb_on():
GPIO.output(led2, GPIO.HIGH)
def ledc_on():
GPIO.output(led3, GPIO.HIGH)
def leda_off():
GPIO.output(led1, GPIO.LOW)
def ledb_off():
GPIO.output(led2, GPIO.LOW)
def ledc_off():
GPIO.output(led3, GPIO.LOW)
# The following function returns the instantanious state of each button.
def buttonget():
buttondict = {'buta':False, 'butb':False, 'butc':False}
buttondict['buta'] = GPIO.input(buta)
buttondict['butb'] = GPIO.input(butb)
buttondict['butc'] = GPIO.input(butc)
return buttondict
# The following function returns the debounced activation for each button, much more elegant for use in my program.
class debounce(object):
def __init__(self):
self.awaspressed = 0
self.bwaspressed = 0
self.cwaspressed = 0
self.afire = False
self.bfire = False
self.cfire = False
def read(self):
button_readings = buttonget()
if (button_readings['buta']==False):
self.awaspressed = 1
if (button_readings['butb']==False):
self.bwaspressed = 1
if (button_readings['butc']==False):
self.cwaspressed = 1
if (self.awaspressed == 1):
if (button_readings['buta']==True):
self.afire = True
self.awaspressed = 0
if (self.bwaspressed == 1):
if (button_readings['butb']==True):
self.bfire = True
self.bwaspressed = 0
if (self.cwaspressed == 1):
if (button_readings['butc']==True):
self.cfire = True
self.cwaspressed = 0
buttondict = {'buta':False, 'butb':False, 'butc':False}
buttondict['buta'] = self.afire
buttondict['butb'] = self.bfire
buttondict['butc'] = self.cfire
self.afire = False
self.bfire = False
self.cfire = False
return buttondict
# The following function is merely a hardware tool so I can ensure my LED's were wired correctly.
def cycleloop():
while True:
try:
leda_on()
time.sleep(0.2)
leda_off()
ledb_on()
time.sleep(0.2)
ledb_off()
ledc_on()
time.sleep(0.2)
ledc_off()
except KeyboardInterrupt:
print("cleaning up")
cleangpio()
print("shutting down")
sys.exit()
resetleds()