Skip to content

Commit 97855d6

Browse files
committed
Move vending code to library
Resolves #1
1 parent 0d0f083 commit 97855d6

File tree

2 files changed

+149
-179
lines changed

2 files changed

+149
-179
lines changed

machine_controller/app.py

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -33,87 +33,21 @@
3333
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
3434
# THE SOFTWARE.
3535
from flask import Flask, request, abort, jsonify
36-
import RPi.GPIO as GPIO
37-
import time
38-
import sys
39-
40-
CLOCK = 21
41-
ROW = [26, 16, 20]
42-
COL = [19, 13]
43-
44-
GPIO.setmode(GPIO.BCM)
36+
import json
37+
from vend import Merch
4538

4639
app = Flask(__name__)
40+
merch = Merch()
41+
4742

4843
@app.route('/vend', methods=['POST'])
4944
def hello_world():
5045
if 'item' not in request.args:
5146
abort(400)
5247
item = request.args['item']
53-
vend(item[0], item[1])
54-
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
55-
56-
def setup():
57-
GPIO.setup(CLOCK, GPIO.OUT, initial=GPIO.LOW)
58-
for row in ROW:
59-
GPIO.setup(row, GPIO.OUT, initial=GPIO.LOW)
60-
for col in COL:
61-
GPIO.setup(col, GPIO.OUT, initial=GPIO.LOW)
62-
63-
def LOW():
64-
GPIO.output(CLOCK, GPIO.LOW)
65-
66-
for row in ROW:
67-
GPIO.output(row, GPIO.LOW)
68-
for col in COL:
69-
GPIO.output(col, GPIO.LOW)
70-
71-
def commit():
72-
GPIO.output(CLOCK, GPIO.HIGH)
73-
time.sleep(0.5)
74-
GPIO.output(CLOCK, GPIO.LOW)
75-
LOW()
76-
77-
def vend(letter, number):
78-
# Set the letter
79-
binary = ord(letter) - ord('A')
80-
if(binary & 0b100):
81-
GPIO.output(ROW[0], GPIO.HIGH)
82-
if(binary & 0b010):
83-
GPIO.output(ROW[1], GPIO.HIGH)
84-
if(binary & 0b001):
85-
GPIO.output(ROW[2], GPIO.HIGH)
86-
87-
GPIO.output(COL[0], GPIO.HIGH)
88-
GPIO.output(COL[1], GPIO.HIGH)
89-
90-
commit()
91-
92-
93-
# Set the number
94-
row = (number-1) / 2
95-
col = (number+1) % 2+1
96-
97-
if(col & 0b01):
98-
GPIO.output(COL[1], GPIO.HIGH)
99-
if(col & 0b10):
100-
GPIO.output(COL[0], GPIO.HIGH)
101-
102-
if(row & 0x100):
103-
GPIO.output(ROW[0], GPIO.HIGH)
104-
if(row & 0b010):
105-
GPIO.output(ROW[1], GPIO.HIGH)
106-
if(row & 0b001):
107-
GPIO.output(ROW[2], GPIO.HIGH)
108-
109-
commit()
110-
commit()
48+
merch.vend(item[0], item[1])
49+
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
11150

112-
def reject(bad_position):
113-
print("%s is not a valid character" % bad_position)
11451

11552
if __name__ == '__main__':
116-
setup()
117-
LOW()
118-
commit()
119-
app.run(debug=True)
53+
app.run(debug=True)

machine_controller/vend.py

Lines changed: 142 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -34,109 +34,145 @@
3434
# THE SOFTWARE.
3535
import RPi.GPIO as GPIO
3636
import time
37-
import sys
38-
39-
GPIO.setmode(GPIO.BCM)
40-
41-
CLOCK = 26
42-
ROW = [21, 20, 16]
43-
COL = [19, 13]
44-
45-
def setup():
46-
GPIO.setup(CLOCK, GPIO.OUT, initial=GPIO.LOW)
47-
for row in ROW:
48-
GPIO.setup(row, GPIO.OUT, initial=GPIO.LOW)
49-
for col in COL:
50-
GPIO.setup(col, GPIO.OUT, initial=GPIO.LOW)
51-
52-
def LOW():
53-
GPIO.output(CLOCK, GPIO.LOW)
54-
55-
for row in ROW:
56-
GPIO.output(row, GPIO.LOW)
57-
for col in COL:
58-
GPIO.output(col, GPIO.LOW)
59-
60-
def commit():
61-
GPIO.output(CLOCK, GPIO.HIGH)
62-
time.sleep(0.5)
63-
GPIO.output(CLOCK, GPIO.LOW)
64-
LOW()
65-
66-
67-
def reject(bad_position):
68-
print("%s is not a valid character" % bad_position)
69-
70-
def main():
71-
char = sys.argv[1][0]
72-
num = sys.argv[1][1]
73-
74-
setup()
75-
LOW()
76-
commit()
77-
78-
vend(char, int(num))
79-
GPIO.cleanup()
80-
81-
def vend(letter, number):
82-
# Set the letter
83-
binary = ord(letter) - ord('A')
84-
if(binary & 0b100):
85-
GPIO.output(ROW[0], GPIO.HIGH)
86-
if(binary & 0b010):
87-
GPIO.output(ROW[1], GPIO.HIGH)
88-
if(binary & 0b001):
89-
GPIO.output(ROW[2], GPIO.HIGH)
90-
91-
GPIO.output(COL[0], GPIO.HIGH)
92-
GPIO.output(COL[1], GPIO.HIGH)
93-
94-
commit()
95-
96-
# Set the number
97-
row = int((number-1) / 2)
98-
col = (number+1) % 2+1
99-
100-
if(col & 0b01):
101-
GPIO.output(COL[1], GPIO.HIGH)
102-
if(col & 0b10):
103-
GPIO.output(COL[0], GPIO.HIGH)
104-
105-
print(row, col)
106-
107-
if(row & 0x100):
108-
GPIO.output(ROW[0], GPIO.HIGH)
109-
if(row & 0b010):
110-
GPIO.output(ROW[1], GPIO.HIGH)
111-
if(row & 0b001):
112-
GPIO.output(ROW[2], GPIO.HIGH)
113-
114-
commit()
115-
commit()
116-
117-
118-
# TABLE OF OUTPUTS
119-
# ROW = {ROW[0],ROW[1],ROW[2]}
120-
# COL = {COL[0], COL[1]}
121-
# ROW COL OUTPUT
122-
# 000 11 A
123-
# 000 01 1
124-
# 000 10 2
125-
# 001 11 B
126-
# 001 01 3
127-
# 001 10 4
128-
# 010 11 C
129-
# 010 01 5
130-
# 010 10 6
131-
# 011 11 D
132-
# 011 01 7
133-
# 011 10 8
134-
# 100 11 E
135-
# 100 01 9
136-
# 100 10 0
137-
# 101 11 F
138-
# 101 01 *
139-
# 101 10 CLR
140-
141-
if __name__ == "__main__":
142-
main()
37+
38+
39+
class Merch:
40+
'''Merch Hardware Controller'''
41+
CLOCK = 26
42+
ROW = [21, 20, 16]
43+
COL = [19, 13]
44+
MAX_LETTER = 'F'
45+
MAX_NUMBER = '0'
46+
47+
def __init__(self, debug=False):
48+
self.debug = debug
49+
50+
self.__setup()
51+
self.__low()
52+
self.__commit()
53+
54+
def __del__(self):
55+
self.__cleanup()
56+
57+
def __cleanup(self):
58+
''' Clean up all of the GPIO pins '''
59+
GPIO.cleanup()
60+
61+
def __setup(self):
62+
''' Setup all of the GPIO pins '''
63+
GPIO.setmode(GPIO.BCM)
64+
65+
GPIO.setup(self.CLOCK, GPIO.OUT, initial=GPIO.LOW)
66+
for row in self.ROW:
67+
GPIO.setup(row, GPIO.OUT, initial=GPIO.LOW)
68+
for col in self.COL:
69+
GPIO.setup(col, GPIO.OUT, initial=GPIO.LOW)
70+
71+
def __low(self):
72+
''' Writes all outputs to low. Does not commit them '''
73+
GPIO.output(self.CLOCK, GPIO.LOW)
74+
75+
for row in self.ROW:
76+
GPIO.output(row, GPIO.LOW)
77+
for col in self.COL:
78+
GPIO.output(col, GPIO.LOW)
79+
80+
def __commit(self):
81+
'''
82+
Clocks the flip flop that gates the output
83+
'''
84+
GPIO.output(self.CLOCK, GPIO.HIGH)
85+
time.sleep(0.5)
86+
GPIO.output(self.CLOCK, GPIO.LOW)
87+
self.__low()
88+
89+
# Wrap the base _vend function, which doesn't check arguments
90+
def vend(self, letter, number):
91+
''' Presses the keypad with letter, number'''
92+
char = 0
93+
try:
94+
char = ord(letter)
95+
except TypeError:
96+
raise TypeError('Letter %s does not represent a character' %
97+
str(letter))
98+
99+
# Maybe we should use the actual keypad value?
100+
if char < ord('A') or char > ord('Z'):
101+
raise ValueError('Invalid Letter: %s' % str(letter))
102+
103+
num = 0
104+
try:
105+
num = int(number)
106+
except TypeError:
107+
raise TypeError('Number %s is not convertible to an integer' %
108+
str(num))
109+
110+
if num < 0 or num > 10:
111+
raise ValueError('Number %d is not in the range 1-10' % num)
112+
113+
self.__vend(letter, number)
114+
115+
def __vend(self, letter, number):
116+
''' Base vending function that handles GPIO's
117+
118+
Arguments:
119+
letter -- the letter as a length 1 string
120+
number -- the number as an integer between 1-10
121+
'''
122+
# TABLE OF OUTPUTS
123+
# ROW = {ROW[0],ROW[1],ROW[2]}
124+
# COL = {COL[0], COL[1]}
125+
# ROW COL OUTPUT
126+
# 000 11 A
127+
# 000 01 1
128+
# 000 10 2
129+
# 001 11 B
130+
# 001 01 3
131+
# 001 10 4
132+
# 010 11 C
133+
# 010 01 5
134+
# 010 10 6
135+
# 011 11 D
136+
# 011 01 7
137+
# 011 10 8
138+
# 100 11 E
139+
# 100 01 9
140+
# 100 10 0
141+
# 101 11 F
142+
# 101 01 *
143+
# 101 10 CLR
144+
145+
binary = ord(letter) - ord('A')
146+
if binary & 0b100:
147+
GPIO.output(self.ROW[0], GPIO.HIGH)
148+
if binary & 0b010:
149+
GPIO.output(self.ROW[1], GPIO.HIGH)
150+
if binary & 0b001:
151+
GPIO.output(self.ROW[2], GPIO.HIGH)
152+
153+
GPIO.output(self.ROW[0], GPIO.HIGH)
154+
GPIO.output(self.ROW[1], GPIO.HIGH)
155+
156+
self.__commit()
157+
158+
# Set the number
159+
row = int((number-1) / 2)
160+
col = (number+1) % 2+1
161+
162+
if self.debug:
163+
print("Vending", row, col)
164+
165+
if col & 0b01:
166+
GPIO.output(self.ROW[1], GPIO.HIGH)
167+
if col & 0b10:
168+
GPIO.output(self.ROW[0], GPIO.HIGH)
169+
170+
if row & 0x100:
171+
GPIO.output(self.ROW[0], GPIO.HIGH)
172+
if row & 0b010:
173+
GPIO.output(self.ROW[1], GPIO.HIGH)
174+
if row & 0b001:
175+
GPIO.output(self.ROW[2], GPIO.HIGH)
176+
177+
self.__commit()
178+
self.__commit()

0 commit comments

Comments
 (0)