Commit 5d1d0418 authored by Pedro Henrique Kopper's avatar Pedro Henrique Kopper

Adicionar cronometro

parent 94590b72
...@@ -8,9 +8,10 @@ import sys ...@@ -8,9 +8,10 @@ import sys
from PyQt5 import QtWidgets, QtCore from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QTimer from PyQt5.QtCore import QTimer
from util.Graph import Graph from util.Graph import Graph
from util.Stopwatch import Stopwatch
from ui.MainWindow import Ui_MainWindow from ui.MainWindow import Ui_MainWindow
UPDATE_INTERVAL = 250 # milisegundos UPDATE_INTERVAL = 100 # milisegundos
class App(object): class App(object):
def __init__(self): def __init__(self):
...@@ -23,10 +24,10 @@ class App(object): ...@@ -23,10 +24,10 @@ class App(object):
self.mainWindow = QtWidgets.QMainWindow() self.mainWindow = QtWidgets.QMainWindow()
self.dialog = QtWidgets.QDialog(self.mainWindow) self.dialog = QtWidgets.QDialog(self.mainWindow)
self.timer = QTimer(self.app) self.timer = QTimer(self.app)
self.stopwatch = Stopwatch()
# Seta as variáveis de estado da aplicação # Seta as variáveis de estado da aplicação
self.running = False self.running = False
self.elapsedTime = 0
# Cria a interface principal # Cria a interface principal
self.ui = Ui_MainWindow() self.ui = Ui_MainWindow()
...@@ -35,7 +36,7 @@ class App(object): ...@@ -35,7 +36,7 @@ class App(object):
# Inicializa os dois gráficos # Inicializa os dois gráficos
self.pressurePlot = Graph(self.ui.pressaoGraph, "Pressão", "bar") self.pressurePlot = Graph(self.ui.pressaoGraph, "Pressão", "bar")
self.temperaturaPlot = Graph(self.ui.temperaturaGraph, "Temperatura", "ºC") self.temperaturePlot = Graph(self.ui.temperaturaGraph, "Temperatura", "ºC")
# Exibe a tela maximizada # Exibe a tela maximizada
self.mainWindow.showMaximized() self.mainWindow.showMaximized()
...@@ -52,14 +53,16 @@ class App(object): ...@@ -52,14 +53,16 @@ class App(object):
self.timer.stop() self.timer.stop()
self.running = False self.running = False
self.ui.controlBtn.setText("COMEÇAR") self.ui.controlBtn.setText("COMEÇAR")
self.stopwatch.stop()
else: else:
self.timer.start() self.timer.start(UPDATE_INTERVAL)
self.running = True self.running = True
self.ui.controlBtn.setText("PARAR") self.ui.controlBtn.setText("PARAR")
self.stopwatch.start()
# É chamada a cada UPDATE_INTERVAL milisegundos e é responsável por atualizar as informações da tela # É chamada a cada UPDATE_INTERVAL milisegundos e é responsável por atualizar as informações da tela
def updateData(self): def updateData(self):
self.elapsedTime += UPDATE_INTERVAL * 0.001 self.ui.infos.setText(self.stopwatch.readFormatted())
if __name__ == '__main__': if __name__ == '__main__':
......
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Classe Stopwatch (Cronometro) #
# Autores: Pedro Henrique Kopper (pedro.kopper@ufrgs.br) -> https://lief.if.ufrgs.br/~phckopper #
# Licença: GNU GPLv3 #
# Propósito: Implementa um cronometro #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from math import floor
from time import time
class Stopwatch(object):
def __init__(self):
self.running = False
self.lastUpdatedTime = 0
self.elapsedTime = 0
def start(self):
self.running = True
self.lastUpdatedTime = time()
def stop(self):
self.running = False
def reset(self):
self.elapsedTime = 0
# Deve ser rodado periodicamente
def update(self):
if self.running:
self.elapsedTime += time() - self.lastUpdatedTime
self.lastUpdatedTime = time()
def readSeconds(self):
self.update()
return self.elapsedTime
def readFormatted(self):
elapsedTime = self.readSeconds()
seconds = floor(elapsedTime % 60)
minutes = floor((elapsedTime / 60) % 60)
hours = floor((elapsedTime / (60 * 60)))
return "{:0>2d}:{:0>2d}:{:0>2d}".format(hours, minutes, seconds)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment