Skip to content

Commit

Permalink
QT testing GUI!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
krismaz committed Oct 27, 2016
1 parent ecad28b commit ff38b3f
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 57 deletions.
64 changes: 64 additions & 0 deletions Controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from time import sleep, time
import datetime
from pymsgbox import *
import threading
from pubsub import pub


class Controller:
def __init__(self, settings, coms):
self.settings = settings
self.coms = coms
thread = threading.Thread(target=self.brew_loop)
thread.start()

def name_sensors(self, raw):
return dict((self.settings['names'].get(k) or k, v) for k, v in raw.items())

def evaluate(self, command, index):
if not command:
return
print(command, '----')
if command[0] == '#':
return
op, *args = command.split(' ')
if op == 'TARGET':
print('TARGET command deprecated, use settings file.')
if op == 'HEAT':
self.coms.set_temperature(float(args[0]))
while True:
temps = self.coms.get_temperatures()
print(index, '-', self.name_sensors(temps))
try:
if temps[self.coms.sensor] >= float(args[0]):
break
except KeyError:
print('Unknown target sensor {}!'.format(self.coms.sensor))
print('Connected sensors are:')
print('\n'.join(*temps.keys()))
exit(1)
pub.sendMessage('MainTemp', arg1=temps[self.coms.sensor])
sleep(5)
if op == 'COOK':
start = time()
coms.set_temperature(float(args[0]))
while True:
temps = self.coms.get_temperatures()
print(index, '-', self.name_sensors(temps))
remaining = start + float(args[1]) * 60.0 - time()
if remaining < 0:
break
print('Time remaining:', datetime.timedelta(seconds=remaining))
pub.sendMessage('MainTemp', arg1=temps[self.coms.sensor])
sleep(5)
if op == 'PAUSE':
alert(text=' '.join(args), title='', button='OK')
if op == 'DONE':
self.coms.set_temperature(-100000000.0)
self.coms.set_target('0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0')

def brew_loop(self):
i = 1
while(True):
self.evaluate(input().strip(), i)
i += 1
83 changes: 26 additions & 57 deletions SerialCom.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,38 @@
from time import sleep, time
from pymsgbox import *
import json
import datetime
import argparse
from ArduinoCommunicator import ArduinoCommunicator
from DebugCommunicator import DebugCommunicator
import gui
from PyQt5 import QtWidgets
from Controller import Controller
import sys
from pubsub import pub

parser = argparse.ArgumentParser(description='Brewing process runner.')
parser.add_argument('-s', dest="settings", default="settings.json",
help="Settings file")
args = parser.parse_args()

with open(args.settings, 'r') as settingsfile:
settings = json.load(settingsfile)
def update_temp(arg1):
ui.progressBar.setProperty('value', arg1)

if __name__ == "__main__":
global ui
parser = argparse.ArgumentParser(description='Brewing process runner.')
parser.add_argument('-s', dest="settings", default="settings.json",
help="Settings file")
args = parser.parse_args()

def name_sensors(raw):
return dict((settings['names'].get(k) or k, v) for k, v in raw.items())
with open(args.settings, 'r') as settingsfile:
settings = json.load(settingsfile)

# coms = ArduinoCommunicator(settings['COM'], settings['sensor'])
coms = DebugCommunicator(settings['COM'], settings['sensor'])

def evaluate(command, index, coms):
if not command:
return
print(command, '----')
if command[0] == '#':
return
op, *args = command.split(' ')
if op == 'TARGET':
print('TARGET command deprecated, use settings file.')
if op == 'HEAT':
coms.set_temperature(float(args[0]))
while True:
temps = coms.get_temperatures()
print(index, '-', name_sensors(temps))
try:
if temps[coms.sensor] >= float(args[0]):
break
except KeyError:
print('Unknown target sensor {}!'.format(coms.sensor))
print('Connected sensors are:')
print('\n'.join(*temps.keys()))
exit(1)
sleep(5)
if op == 'COOK':
start = time()
coms.set_temperature(float(args[0]))
while True:
temps = coms.get_temperatures()
print(index, '-', name_sensors(temps))
remaining = start + float(args[1]) * 60.0 - time()
if remaining < 0:
break
print('Time remaining:', datetime.timedelta(seconds=remaining))
sleep(5)
if op == 'PAUSE':
alert(text=' '.join(args), title='', button='OK')
if op == 'DONE':
coms.set_temperature(-100000000.0)
coms.set_target('0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0')
control = Controller(settings, coms)

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = gui.Ui_MainWindow()
ui.setupUi(MainWindow)

if __name__ == "__main__":
i = 1
coms = ArduinoCommunicator(settings['COM'], settings['sensor'])
# coms = DebugCommunicator(settings['COM'], settings['sensor'])
while(True):
evaluate(input().strip(), i, coms)
i += 1
pub.subscribe(update_temp, 'MainTemp')

MainWindow.show()
sys.exit(app.exec_())
54 changes: 54 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(620, 470, 161, 81))
self.pushButton.setObjectName("pushButton")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(0, 530, 611, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName("progressBar")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(0, 0, 591, 371))
self.textBrowser.setObjectName("textBrowser")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.progressBar.setFormat(_translate("MainWindow", "%p"))


if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

71 changes: 71 additions & 0 deletions gui.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>620</x>
<y>470</y>
<width>161</width>
<height>81</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QProgressBar" name="progressBar">
<property name="geometry">
<rect>
<x>0</x>
<y>530</y>
<width>611</width>
<height>23</height>
</rect>
</property>
<property name="value">
<number>24</number>
</property>
<property name="format">
<string>%p</string>
</property>
</widget>
<widget class="QTextBrowser" name="textBrowser">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>591</width>
<height>371</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

0 comments on commit ff38b3f

Please sign in to comment.