2023-01-15 16:56:46 +01:00
|
|
|
import sys
|
|
|
|
import random
|
|
|
|
import matplotlib
|
|
|
|
matplotlib.use('Qt5Agg')
|
|
|
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
|
|
|
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
|
|
|
|
from matplotlib.figure import Figure
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# twin_plot.py
|
|
|
|
# @title: Visualisation des données
|
|
|
|
# @project: Blender-EduTech
|
|
|
|
# @lang: fr
|
|
|
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
|
|
|
# @copyright: Copyright (C) 2023 Philippe Roy
|
|
|
|
# @license: GNU GPL
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# UPBGE scene
|
|
|
|
# scene = bge.logic.getCurrentScene()
|
|
|
|
|
|
|
|
# Récupérer le brochage du jumeau réel
|
|
|
|
# system=importlib.import_module(scene.objects['Doc']['system']) # Système
|
|
|
|
# pin_config = system.get_pin_config()
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Fenêtre dynamique
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
class MplCanvas(FigureCanvasQTAgg):
|
|
|
|
|
|
|
|
def __init__(self, parent=None, width=5, height=4, dpi=100):
|
|
|
|
fig = Figure(figsize=(width, height), dpi=dpi)
|
|
|
|
self.subplot = fig.add_subplot(111)
|
|
|
|
super(MplCanvas, self).__init__(fig)
|
|
|
|
|
|
|
|
class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
|
|
|
# Création du graphique
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(MainWindow, self).__init__(*args, **kwargs)
|
|
|
|
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
|
|
|
|
self.setCentralWidget(self.canvas)
|
|
|
|
n_data = 50
|
|
|
|
# self.xdata = list(range(n_data))
|
|
|
|
# self.ydata = [random.randint(0, 10) for i in range(n_data)]
|
|
|
|
self.xdata = [0]
|
|
|
|
self.ydata = [0]
|
|
|
|
self.update_plot()
|
|
|
|
self.show()
|
|
|
|
|
|
|
|
# Timer pour le update_plot
|
2023-01-17 01:24:21 +01:00
|
|
|
fps = 120 # Blender est cadencé à 60 fps
|
2023-01-15 16:56:46 +01:00
|
|
|
self.timer = QtCore.QTimer()
|
2023-01-17 01:24:21 +01:00
|
|
|
# self.timer.setInterval(int(1000/fps))
|
|
|
|
self.timer.setInterval(1)
|
2023-01-15 16:56:46 +01:00
|
|
|
self.timer.timeout.connect(self.update_plot)
|
|
|
|
self.timer.start()
|
2023-01-17 01:24:21 +01:00
|
|
|
print("Qt : Canvas ready\n")
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
# Lecture des données et mise à jour du graphique
|
|
|
|
def update_plot(self):
|
|
|
|
plt = self.canvas.subplot
|
|
|
|
|
|
|
|
# Données
|
2023-01-18 06:06:15 +01:00
|
|
|
msg=""
|
|
|
|
for line in sys.stdin:
|
|
|
|
msg = line.rstrip()
|
|
|
|
break
|
|
|
|
print("Qt : ", msg)
|
2023-01-15 16:56:46 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
# Essai
|
2023-01-18 06:06:15 +01:00
|
|
|
new_x=self.xdata[len(self.xdata)-1]+1
|
|
|
|
self.xdata.append(new_x)
|
|
|
|
self.ydata.append(random.randint(0, 10))
|
2023-01-15 16:56:46 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
# Lecture du Pipe simple
|
2023-01-18 06:06:15 +01:00
|
|
|
# msg=""
|
2023-01-17 01:24:21 +01:00
|
|
|
# print (sys.stdin)
|
|
|
|
# contents = sys.stdin.read(1)
|
|
|
|
# # contents = sys.stdin.buffer
|
|
|
|
# print ("contents :", contents)
|
|
|
|
|
2023-01-18 06:06:15 +01:00
|
|
|
# for line in sys.stdin:
|
|
|
|
# msg = line.rstrip()
|
|
|
|
# break
|
|
|
|
# print(msg)
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
# X et Y
|
|
|
|
# FIXME : temps et une valeur
|
2023-01-17 01:24:21 +01:00
|
|
|
# msg_list=msg.split(',')
|
|
|
|
# print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Lecture du Pipe
|
|
|
|
# # i=0
|
|
|
|
# # lines = sys.stdin.readlines()
|
|
|
|
# # print (lines)
|
|
|
|
# # print ("b")
|
|
|
|
|
|
|
|
# msg_line=""
|
|
|
|
# # msg_lines=[]
|
|
|
|
# for line in sys.stdin:
|
|
|
|
# # i+=1
|
|
|
|
# # print (i)
|
|
|
|
# msg_line = line.rstrip()
|
|
|
|
# # msg_lines.append(msg_line)
|
|
|
|
# # print("Qt msg_lines :", msg)
|
|
|
|
# # if i==2:
|
|
|
|
# # break
|
|
|
|
# break
|
|
|
|
# print("Qt :", msg_line)
|
|
|
|
# # print("Qt msg_lines :", msg_lines)
|
|
|
|
|
|
|
|
# # X et Y
|
|
|
|
# # FIXME : temps msg_list[0] et une seule valeur msg_list[1]
|
|
|
|
# msg_list=msg_line.split(',')
|
|
|
|
# # print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# for line in msg_lines:
|
|
|
|
# msg_list=line.split(',')
|
|
|
|
# # print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
|
|
|
|
|
|
|
|
|
|
|
# for line in sys.stdin:
|
|
|
|
# msg_list=msg.split(',')
|
|
|
|
# print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
|
|
|
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
# self.ydata = self.ydata + [random.randint(0, 10)]
|
|
|
|
# Drop off the first y element, append a new one.
|
|
|
|
# self.ydata = self.ydata[1:] + [random.randint(0, 10)]
|
|
|
|
|
|
|
|
# Redraw
|
|
|
|
plt.cla()
|
|
|
|
plt.plot(self.xdata, self.ydata, 'r')
|
|
|
|
self.canvas.draw()
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Application
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
app = QtWidgets.QApplication(sys.argv)
|
|
|
|
w = MainWindow()
|
|
|
|
app.exec_()
|