-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.py.save.1
101 lines (85 loc) · 2.95 KB
/
arduino.py.save.1
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
# Reads data from the serial monitor without interrupting the main thread
from serial import Serial, serialutil
import time
from threading import Thread
from sql import SQLServer
class SerialListener:
def __init__(self, baudrate=9600, timeout=0.5):
try:
self.ser = Serial('/dev/ttyACM0', baudrate, timeout=timeout)
except serialutil.SerialException:
try:
self.ser = Serial('/dev/ttyACM1', baudrate, timeout=timeout)
except serialutil.SerialException:
self.ser = Serial('/dev/
self.stopped = False
self.paused = False
self.stream = ''
time.sleep(1) # Wait for serial buffer to reset
print("Serial Started")
def start(self):
Thread(target=self.update, args=()).start()
return self
def update(self):
while True:
if not self.paused:
if self.stopped:
self.ser.close()
print("Serial Thread Stopped")
print("Serial Port Closed")
self.paused = True
try:
self.stream = self.ser.readline().decode('utf-8')
except:
self.stream = self.ser.readline().decode('ascii')
self.stream = self.stream.rstrip()
def stop(self):
self.paused = False
self.stopped = True
def resume(self):
self.paused = False
def pause(self):
self.paused = True
def flush(self):
self.ser.flush()
def SerialAvailable(self):
return self.ser.inWaiting()
def readDistance(self): # Deprecated as distance is now read within Arduino
try:
return float(self.stream)
except:
return -1 # Returns -1 if there is an error in reading
def read(self):
return self.stream
def write(self, msg):
self.ser.write(msg.encode())
if __name__ == "__main__": # FOR DEBUGGING ONLY
uno = SerialListener().start()
sql = SQLServer()
uno.flush()
name = None
pts = None
try:
while True:
uid = ''
while uid is '':
uid = uno.readRFID()
time.sleep(0.1)
user = sql.findUid(int(uid, 16))
if len(user) > 0:
(_, name, _, pts), = user
print("Welcome, " + name)
print("Current Incentives: " + str(pts))
uno.write('O')
else:
uno.write('X')
print("There is no record of UID: " + uid + " in our database")
cr = input("Would you like to create a new one? (Y/N): ")
if cr == 'Y' or cr == 'y':
name = input("Enter your name: ")
sql.insert(name, int(uid, 16), 0)
print("Please scan your ID again")
time.sleep(1)
except KeyboardInterrupt:
uno.stop()
sql.close()