-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
170 lines (121 loc) · 5.11 KB
/
main.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
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
# compliments of: https://www.baldengineer.com/raspberry-pi-gui-tutorial.html
import sys
import threading
#Library for reading COM port details
import serial.tools.list_ports
# This gets the Qt stuff
import PyQt5
from PyQt5.QtWidgets import *
from serial_stuff import SerialCom
# This is our window from QtCreator
import mainwindow_auto
from serialThread import serialThreadClass
# create class for our Raspberry Pi GUI
class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow):
# access variables inside of the UI's file
### functions for the buttons to call
def pressedInfuseCloseButton(self):
print ("Pressed Infuse Close")
#INSERT CODE
#function should send a 'C' to the Arduino identified as 'I'
self.mySerial1.sendSerial('C')
def pressedInfuseOpenButton(self):
print ("Pressed Infuse Open")
#INSERT CODE
#function should send a 'O' to the Arduino identified as 'I'
self.mySerial1.sendSerial('O')
def pressedDrainOpenButton(self):
print ("Pressed Drain Open")
#INSERT CODE
#function should send a 'O' to the Arduino identified as 'D'
self.mySerial2.sendSerial('O')
def pressedDrainCloseButton(self):
print ("Pressed Drain Close")
#INSERT CODE
#function should send a 'C' to the Arduino identified as 'D'
self.mySerial2.sendSerial('C')
def infuseData(self,data):
print(data[2:9])
if data[4] == 'C':
self.updateInfuseValveState('Closed')
elif data[4] == 'O':
self.updateInfuseValveState('Open')
self.updateInfuseWeight(str(int(data[5:9])))
def drainData(self,data):
print(data[2:9])
if data[4] == 'C':
self.updateDrainValveState('Closed')
elif data[4] == 'O':
self.updateDrainValveState('Open')
self.updateDrainWeight(str(int(data[5:9])))
def updateDrainValveState(self, state):
#after receiving string from Drain-Arduino this should be called
#to update the drain valve state
self.lblDrainValveState.setText(state)
def updateInfuseValveState(self, state):
#after receiving string from Infuse-Arduino this should be called
#to update the infuse valve state
self.lblInfuseValveState.setText(state)
def updateDrainWeight(self, weight):
#after receiving string from Drain-Arduino this should be called
#to update the drain weight
self.lblDrainWeight.setText(weight)
def updateInfuseWeight(self, weight):
#after receiving string from Infuse-Arduino this should be called
#to update the infuse weight
self.lblInfuseWeight.setText(weight)
def __init__(self,IF,DR):
super(self.__class__, self).__init__()
self.setupUi(self) # gets defined in the UI file
self.infuse = IF
self.drain = DR
### Hooks for buttons
self.btnInfuseClose.clicked.connect(lambda: self.pressedInfuseCloseButton())
self.btnInfuseOpen.clicked.connect(lambda: self.pressedInfuseOpenButton())
self.btnDrainOpen.clicked.connect(lambda: self.pressedDrainOpenButton())
self.btnDrainClose.clicked.connect(lambda: self.pressedDrainCloseButton()) # I feel better having one of these
self.mySerial1 = serialThreadClass(IF)
self.mySerial2 = serialThreadClass(DR)
# INSERT CODE
# upon receipt of string from an Arduino, the string needs to be
# parsed and the relevant data needs to be displayed to the GUI
self.mySerial1.msg.connect(self.infuseData)
self.mySerial2.msg.connect(self.drainData)
self.mySerial1.start() #startingInfuseSerialThread
self.mySerial2.start() #startingDrainSerialThread
def main():
#INSERT CODE
#need to search available COM ports and automatically connect
#will be able to identify the Infuse-Arduino and Drain-Arduion
#by sending an 'X' and expecting a reply of either 'I' or 'D'
ports = list(serial.tools.list_ports.comports())
COM = []
for p in ports:
if 'USB' in p[2]:
print('Arduino may connected to : ' + p[0])
COM.append(p[0])
if len(COM) < 2 :
print('Two arduinos are not connected properly')
return
for u in COM:
SER = SerialCom(u)
SER.writeData('X')
kind = SER.readLine()
if 'I' in kind :
infuse = u
print(u + ' is Infuse Arduino')
elif 'D' in kind :
drain = u
print(u + ' is Drain Arduino')
#INSERT CODE
#once arduino connections are identified and made, need to set
#monitoring for incoming strings of data from each Arduino.
# a new app instance
app = QApplication(sys.argv)
form = MainWindow(infuse,drain)
form.show()
# without this, the script exits immediately.
sys.exit(app.exec_())
# python bit to figure how who started This
if __name__ == "__main__":
main()