-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrateCan.py
131 lines (102 loc) · 3.74 KB
/
calibrateCan.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
from time import sleep
from coolerClass.filecfg import filecfg
import serial
import pymysql
import datetime
#get configuration info from cfg file
cfg = filecfg('config.cfg')
#initialise the serial connection
ser = serial.Serial(cfg.ser_interface,9600)
class calibrateCan():
def __init__(self,db,cursor,productName,redScan,blueScan,greenScan):
self.rbScan = 0
self.rgScan = 0
self.bgScan = 0
self.wScan = 0
self.productName = productName
self.redScan = redScan
self.blueScan = blueScan
self.greenScan = greenScan
self.db = db
self.cursor = cursor
self.calibrateCanA()
def calibrateCanA(self):
sql = '''SELECT productID FROM coolerhack.producttable WHERE productName = %s'''
c = self.cursor.execute(sql,(self.productName))
if c<1:
sql = '''INSERT INTO coolerhack.producttable (productName) VALUES (%s)'''
self.cursor.execute(sql,(self.productName))
self.db.commit()
self.calibrateCanA()
else:
line = cursor.fetchone()
self.productID = line[0]
self.calibrateCanB()
def calibrateCanB(self):
sql = '''INSERT INTO coolerhack.knownproducts (productID,productName,
redScan,blueScan,greenScan,rbScan,bgScan,rgScan,wScan) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)'''
#print(self.cursor.fetchone())
self.cursor.execute(sql,(int(self.productID),self.productName,self.redScan,self.blueScan,self.greenScan,self.rbScan,self.bgScan,self.rgScan,self.wScan))
self.db.commit()
def formatStream(val):
plotStream = (float(val-plotStreamRange[0])/float(plotStreamRangeW))
plotStream = int(plotStream * plotStreamWidth)
return plotStream
#config data for formatStream
plotStreamRange = [950,1024]
plotStreamRangeW = plotStreamRange[1] - plotStreamRange[0]
plotStreamWidth = 30
plotStreamChar = "|"
#set up database connection
db = pymysql.connect(db=cfg.db_db,user=cfg.db_user,passwd=cfg.db_passwd,host=db_host)
cursor = db.cursor()
#empty read to let the serial synchronize
ser.readline()
ser.flushInput()
ser.flushOutput()
#wait for serial
sleep(0.5)
#say hey
print("starts runing")
dataList = []
productName = raw_input("What is this product called?\n")
print("\n")
for i in range(50):
#serial line reading limits the rate
ser.flushInput()
ser.flushOutput()
line = ser.readline()
red = int(line[line.index("r: ")+3:line.index("g: ")])
green = int(line[line.index("g: ")+3:line.index("b: ")])
blue = int(line[line.index("b: ")+3:line.index("row: ")])
rowData = int(line[line.index("row: ")+5:line.index("\r\n")])
# print(str(rowData).ljust(5)+"* "+toPlotStream(red)+toPlotStream(green)+toPlotStream(blue))
#exaggerate like the visualization
red = formatStream(red)
green = formatStream(green)
blue = formatStream(blue)
print(red,green,blue)
redScan = red
blueScan = blue
greenScan = green
#throw the janky initial data
if i > 25:
dataList.append([red, green, blue])
# calibrateCan(db,cursor,productName,redScan,blueScan,greenScan)
# print("sample " + str(i) + " taken")
sleep(.05)
#send the data
aBool = raw_input("Save Data for " + productName+ "? y/n\n")
aBool = aBool == "y"
print(aBool)
if aBool:
print("sending fingerprint data for " + productName + "...")
for i in dataList:
calibrateCan(db,cursor,productName,i[0],i[2],i[1])
print("fingerprint data sent!")
else:
print("Well ... No data sent")
#close connection
db.commit()
cursor.close()
db.close()