-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattendance_pi.py
More file actions
292 lines (228 loc) · 8.31 KB
/
attendance_pi.py
File metadata and controls
292 lines (228 loc) · 8.31 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import RPi.GPIO as GPIO
import SimpleMFRC522
from RPLCD.gpio import CharLCD
from check_closing_time import check
import subprocess
#import sys
from threading import Thread
import time
import datetime
import os
import csv
global cycle
cycle = 0.0
class Primary:
#from datetime import datetime, time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#Screen Setup--------------------------------------
lcd = CharLCD(pin_rs=22, pin_rw=24, pin_e=23, pins_data=[21, 16, 12, 20],
numbering_mode=GPIO.BCM,
cols=16, rows=2, dotsize=8,
charmap='A02',
auto_linebreaks=True)
#-end-----------------------------------------------
button1= 6 #gray1
button2= 13 #blue
button3= 19 #red
button4= 26 # grey 2
menu_options = ["1) Sign in", "2) Sign out", "3) Quit"]
#testing-----------------------------------------------------------
global reader
reader = SimpleMFRC522.SimpleMFRC522()
GPIO.setwarnings(False)
#reading/writing
'''def write():
try:
text = raw_input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")
finally:
GPIO.cleanup()
'''
def read(self):
try:
#check_closingtime()
id, text = reader.read()
print(id)
print(text)
return text.strip() + "\n"
finally:
GPIO.cleanup()
def compress_name(self, name):
name_list = name.split("_")
formatted_name = str(name_list[0] + " " + name_list[1][0])
return formatted_name
def buzz(self):
buzz_pin = 27
GPIO.setup(buzz_pin, GPIO.OUT)
GPIO.output(buzz_pin, GPIO.HIGH)
print("buzzing")
time.sleep(0.5)
GPIO.output(buzz_pin, GPIO.LOW)
def pressButton(self):
button1= 6 #gray1
button2= 13 #blue
button3= 19 #red
button4= 26 # grey 2
GPIO.setup(button1, GPIO.IN)
GPIO.setup(button2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(button3, GPIO.IN)
GPIO.setup(button4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
previous_input = 1
while True:
input1 = GPIO.input(button2)
input2 = GPIO.input(button3)
input3 = GPIO.input(button4)
input4 = GPIO.input(button1)
if((True) and ((not input1) or (not input2) or (not input3) or (not input4))):
if(not input1):
print("Button 1 Pressed!")
return 1
elif(not input2):
print("Button 2 Pressed!")
return 2
elif(not input3):
print("Button 3 Pressed!")
return 3
elif(not input4):
print("Button 4 Pressed!")
return 4
def display(self, message):
lcd = CharLCD(pin_rs=22, pin_rw=24, pin_e=23, pins_data=[21, 16, 12, 20],
numbering_mode=GPIO.BCM,
cols=16, rows=2, dotsize=8,
charmap='A02',
auto_linebreaks=True)
lcd.write_string(message)
def clear(self):
lcd = CharLCD(pin_rs=22, pin_rw=24, pin_e=23, pins_data=[21, 16, 12, 20],
numbering_mode=GPIO.BCM,
cols=16, rows=2, dotsize=8,
charmap='A02',
auto_linebreaks=True)
lcd.clear()
def interrupt1():
print("interrupt1")
def main(self):
Primary.clear(self)
#time.sleep(1)
#check_closingtime()
#see if it's time to send the log to Mr. P
#send_log()
#buzz()
display_now = Thread(target=Primary.display(self, "Please sign in"))
display_now.start()
ids = []
with open("/home/pi/Documents/AttendanceMachine/ids.csv", "r") as ins:
for line in ins:
#(key, val) = line.split(',')
#ids[key] = int(val)
ids.append(line)
print(ids)
global name
name = Primary.read(self)
if name in ids:
display_now.join
Primary.clear(self)
short_name = Primary.compress_name(self, name)
print(short_name)
display_now = Thread(target=Primary.display(self, "Select command, " + short_name))
display_now.start()
#Primary.display(self, "Select command, " + short_name)
#time.sleep(0.5)
choice = Primary.pressButton(self)
Primary.clear(self)
#time.sleep(0.05)
#choice = input("Select your command:\n" + " ".join(menu_options) + "\n")
#choice = int(choice)
#open csv log
print("Logging file...")
log_file = open("/home/pi/Documents/AttendanceMachine/logs/log-" + datetime.datetime.strptime(str(datetime.date.today()), '%Y-%m-%d').strftime('%m-%d-%y') + ".csv","ab")
writer = csv.writer(log_file, delimiter=',', quoting=csv.QUOTE_MINIMAL)
#time.sleep(0.5)
#print(formatted_name)
if choice == 1:
display_now.join()
Primary.clear(self)
writer.writerow([name,str(datetime.datetime.now()),'SIGNED IN'])
display_second = Thread(target=Primary.display(self," Greetings, \r\n " + short_name))
display_second.start()
time.sleep(1.25)
print("Greetings, " + short_name + ". You are now signed in!")
log_file.close()
print("clearing...")
Primary.clear(self)
print("done clearing")
#time.sleep(2.5)
display_second.join()
Primary.main(self)
elif choice == 2:
display_now.join()
Primary.clear(self)
#time.sleep(0.5)
writer.writerow([name,str(datetime.datetime.now()),'SIGNED OUT'])
display_third = Thread(target=Primary.display(self," Goodbye, \r\n " + short_name))
display_third.start()
print("You are now signed out! Thanks for coming!")
log_file.close()
time.sleep(2.25)
# Primary.clear(self)
display_third.join()
Primary.main(self)
elif choice == 3:
display_now.join()
Primary.buzz(self)
time.sleep(0.2)
Primary.buzz(self)
time.sleep(0.2)
Primary.buzz(self)
time.sleep(1)
Primary.clear(self)
time.sleep(1)
Primary.main(self)
elif choice == 4:
display_now.join()
Primary.clear(self)
Primary.main(self)
else:
display_now.join()
time.sleep(0.1)
Primary.main(self)
else:
#interrupt1()
time.sleep(1.5)
Primary.display(self, "Sorry. Please scan a valid ID")
print("Sorry. Please scan a valid ID")
Primary.main(self)
'''if __name__ == "__main__":
check()
main()'''
def __init__(self):
self._running = True
def terminate(self):
self._running = False
#def run(self):
# main()
class Closing:
def main(self):
check() #see if it's closing time
try:
Primary_Protocol = Primary()
Primary_Protocol_Thread = Thread(target=Primary_Protocol.main)
Primary_Protocol_Thread.start()
Closing_Protocol = Closing()
Closing_Protocol_Thread = Thread(target=Closing_Protocol.main)
Closing_Protocol_Thread.start()
Exit = False
while Exit == False:
cycle = cycle + 0.1
time.sleep(1)
if (cycle > 5): Exit = True
Primary_Protocol.terminate()
except KeyboardInterrupt:
GPIO.cleanup()
Primary_Protocol_Thread.join()
Closing_Protocol_Thread.join()
sys.exit()