#!/usr/bin/python3 # -*- coding: utf-8 -* #Bertrand Vandeportaele 2019 import os import sys from PyQt4.QtGui import * from PyQt4.QtNetwork import * from PyQt4.QtCore import * from PyQt4 import * import re #pour découpage chaine comme sur https://stackoverflow.com/questions/2175080/sscanf-in-python global numeroPremiereDonneeCapteur #TODO: donner une valeur à cette variable en fonction de votre numéro d'étudiant numeroPremiereDonneeCapteur=100 global etatBouton etatBouton=0 global slider1Value slider1Value=0 global slider2Value slider2Value=0 global slider3Value slider3Value=0 global slider4Value slider4Value=0 global slider5Value slider5Value=0 global slider6Value slider6Value=0 global led0Button ################################################################################ def close(): print('close') exit(0) ################################################################################ def commutTimer(): global timer print('commutTimer') if timer.isActive(): timer.stop() commutTimerButton.setText('start stream sensor') else: timer.start() commutTimerButton.setText('stop stream sensor') ################################################################################ def slider1ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider1Value global slider1Value slider1Value=value #labelSlider1Value.setText( "J1: "+str(slider1Value) ) #https://mkaz.blog/code/python-string-format-cookbook/ labelSlider1Value.setText( str( numeroPremiereDonneeCapteur+0)+ " -> J1: {:6d}".format(slider1Value) ) print('changed') ################################################################################ def slider2ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider2Value global slider2Value slider2Value=value labelSlider2Value.setText( str( numeroPremiereDonneeCapteur+1)+ " -> J2: {:6d}".format(slider2Value) ) print('changed') ################################################################################ def slider3ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider3Value global slider3Value slider3Value=value labelSlider3Value.setText( str( numeroPremiereDonneeCapteur+2)+ " -> J3: {:6d}".format(slider3Value) ) print('changed') ################################################################################ def slider4ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider4Value global slider4Value slider4Value=value labelSlider4Value.setText( str( numeroPremiereDonneeCapteur+3)+ " -> J4: {:6d}".format(slider4Value) ) print('changed') ################################################################################ def slider5ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider5Value global slider5Value slider5Value=value labelSlider5Value.setText( str( numeroPremiereDonneeCapteur+4)+ " -> J5: {:6d}".format(slider5Value) ) print('changed') ################################################################################ def slider6ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider6Value global slider6Value slider6Value=value labelSlider6Value.setText( str( numeroPremiereDonneeCapteur+5)+ " -> J6: {:6d}".format(slider6Value) ) print('changed') ################################################################################ def bouton0(): global numeroPremiereDonneeCapteur global led0Button global etatBouton global labelButtonValue etatBouton=(etatBouton+1)%2 if etatBouton==0: led0Button.setText('Activer la sortie TOR') else: led0Button.setText('Desactiver la sortie TOR') labelButtonValue.setText( str( numeroPremiereDonneeCapteur+6)+" : {:6d}".format(etatBouton) ) print('etatBouton: '+ str(etatBouton) +" \n"); # led0Button.setText('Appuyer sur le bouton') # else: # led0Button.setText('Relacher le bouton') ################################################################################ def sendUDP(i): global numeroPremiereDonneeCapteur global udpSocket global destIP global portOut DestIP = QHostAddress(destIP); global slider1Value global slider2Value global slider3Value global slider4Value global slider5Value global slider6Value global etatBouton # chaine=str(slider1Value)+ " " + str(slider2Value) #ajout @MAC bidon # chaine="00:00:00:00:00:00 "+str(slider1Value)+ " " + str(slider2Value) #ajout champs vides pour être compatible avec le joystick wifi #@MAC: BC:DD:C2:FE:6F:F0 num: 0 0 -> 529.0 , 1 -> 534.0 , 2 -> 0.0 , 3 -> 0.0 , 4 -> 0.0 , 5 -> -73.0 , 6 -> 63.0 , chaine="00:00:00:00:00:00 "+str(numeroPremiereDonneeCapteur)+ " " +str(slider1Value)+ " " + str(slider2Value) + " " + str(slider3Value) + " " + str(slider4Value) + " " + str(slider5Value) + " " + str(slider6Value)+" " +str(etatBouton) #chaines bidons pour générer erreur de parsing sur le serveur # chaine="00:00:00:00:00:00 0 0 0 "+"687f"+" 0 0 "+str(slider1Value)+ " " + str(slider2Value) chaine=chaine+chr(13)+chr(10) print("la chaine envoyée est: "+chaine) udpSocket.writeDatagram(chaine,DestIP, portOut); ################################################################################ def timerUpdate(): sendUDP(0) ################################################################################ global led0Button #destIP='192.168.1.50' #pc simulateur #destIP='192.168.0.112' #pc simulateur #destIP='192.168.1.49' #pc serveur rapid connecté en filaire à réseau AIP destIP='127.0.0.1' #pc serveur sur la boucle locale portOut=10000 posx=10 posy=10 sizex=500 sizey=150 ################################################################################ #par défaut: numeroPremiereDonneeCapteur= 100 #option -n pour choisir numeroPremiereDonneeCapteur if len(sys.argv)==3: if sys.argv[1]=='-n': numeroPremiereDonneeCapteur= int(sys.argv[2]) print("réglage de numeroPremiereDonneeCapteur à: ",str(numeroPremiereDonneeCapteur)); app = QApplication(sys.argv) w=QDialog() statusLabel =QLabel('En attente de datagrammes UDP depuis le PIC32') commutTimerButton=QPushButton('stop stream sensor') quitButton = QPushButton('&Quit') led0Button = QPushButton('Activer la sortie TOR') udpSocket =QUdpSocket() udpSocket.bind(portOut, QUdpSocket.ShareAddress) app.connect(quitButton,QtCore.SIGNAL('clicked()'), close) app.connect(commutTimerButton, QtCore.SIGNAL('clicked()'), commutTimer) app.connect(led0Button,QtCore.SIGNAL('clicked()'), bouton0) buttonLayout =QHBoxLayout() buttonLayout.addStretch(1) buttonLayout.addWidget(quitButton) buttonLayout.addWidget(commutTimerButton) buttonLayout.addStretch(1) #button1Layout =QHBoxLayout() #button1Layout.addStretch(1) #button1Layout.addWidget(led0Button) #button1Layout.addStretch(1) global labelButtonValue labelButtonValue=QLabel( ) labelButtonValue.setGeometry(250, 50, 50, 35) #labelButtonValue.setText( "x: "+str(0 ) ) labelButtonValue.setText( str( numeroPremiereDonneeCapteur+6)+" : {:6d}".format(0) ) button1Layout =QHBoxLayout() button1Layout.addStretch(1) button1Layout.addWidget(labelButtonValue) button1Layout.addStretch(1) button1Layout.addWidget(led0Button) button1Layout.addStretch(1) #http://koor.fr/Python/CodeSamplesQt/PyQtSlider.wp ################################### slider1 = QSlider(Qt.Horizontal); slider1.setMinimum(-160) slider1.setMaximum(160) slider1.setGeometry( 10, 10, 600, 40 ) slider1.valueChanged.connect(slider1ValueChanged ) slider1.setValue( 0 ) global labelSlider1Value labelSlider1Value=QLabel( ) labelSlider1Value.setGeometry(250, 50, 50, 35) labelSlider1Value.setText( str( numeroPremiereDonneeCapteur+0)+ " -> J1: "+str(0 ) ) slider1Layout =QHBoxLayout() slider1Layout.addWidget(labelSlider1Value) slider1Layout.addWidget(slider1) ################################### slider2 = QSlider(Qt.Horizontal); slider2.setMinimum(-127.5) slider2.setMaximum(127.5) slider2.setGeometry( 10, 10, 600, 40 ) slider2.valueChanged.connect(slider2ValueChanged ) slider2.setValue( 0 ) global labelSlider2Value labelSlider2Value=QLabel( ) labelSlider2Value.setGeometry(250, 50, 50, 35) labelSlider2Value.setText( str( numeroPremiereDonneeCapteur+1)+ " -> J2: "+str(0 ) ) slider2Layout =QHBoxLayout() slider2Layout.addWidget(labelSlider2Value) slider2Layout.addWidget(slider2) ################################### slider3 = QSlider(Qt.Horizontal); slider3.setMinimum(-134.5) slider3.setMaximum(134.5) slider3.setGeometry( 10, 10, 600, 40 ) slider3.valueChanged.connect(slider3ValueChanged ) slider3.setValue( 0 ) global labelSlider3Value labelSlider3Value=QLabel( ) labelSlider3Value.setGeometry(250, 50, 50, 35) labelSlider3Value.setText( str( numeroPremiereDonneeCapteur+2)+ " -> J3: "+str(0 ) ) slider3Layout =QHBoxLayout() slider3Layout.addWidget(labelSlider3Value) slider3Layout.addWidget(slider3) ################################### slider4 = QSlider(Qt.Horizontal); slider4.setMinimum(-270) slider4.setMaximum(270) slider4.setGeometry( 10, 10, 600, 40 ) slider4.valueChanged.connect(slider4ValueChanged ) slider4.setValue( 0 ) global labelSlider4Value labelSlider4Value=QLabel( ) labelSlider4Value.setGeometry(250, 50, 50, 35) labelSlider4Value.setText( str( numeroPremiereDonneeCapteur+3)+ " -> J4: "+str(0 ) ) slider4Layout =QHBoxLayout() slider4Layout.addWidget(labelSlider4Value) slider4Layout.addWidget(slider4) ################################### slider5 = QSlider(Qt.Horizontal); slider5.setMinimum(-109.5) slider5.setMaximum(+120.5) slider5.setGeometry( 10, 10, 600, 40 ) slider5.valueChanged.connect(slider5ValueChanged ) slider5.setValue( 0 ) global labelSlider5Value labelSlider5Value=QLabel( ) labelSlider5Value.setGeometry(250, 50, 50, 35) labelSlider5Value.setText( str( numeroPremiereDonneeCapteur+4)+ " -> J5: "+str(0 ) ) slider5Layout =QHBoxLayout() slider5Layout.addWidget(labelSlider5Value) slider5Layout.addWidget(slider5) ################################### slider6 = QSlider(Qt.Horizontal); slider6.setMinimum(-270) slider6.setMaximum(270) slider6.setGeometry( 10, 10, 600, 40 ) slider6.valueChanged.connect(slider6ValueChanged ) slider6.setValue( 0 ) global labelSlider6Value labelSlider6Value=QLabel( ) labelSlider6Value.setGeometry(250, 50, 50, 35) labelSlider6Value.setText( str( numeroPremiereDonneeCapteur+5)+ " -> J6: "+str(0 ) ) slider6Layout =QHBoxLayout() slider6Layout.addWidget(labelSlider6Value) slider6Layout.addWidget(slider6) ################################### sliderLayout =QVBoxLayout() sliderLayout.addStretch(1) sliderLayout.addLayout(slider1Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider2Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider3Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider4Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider5Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider6Layout) sliderLayout.addStretch(1) mainLayout = QVBoxLayout() mainLayout.addLayout(buttonLayout) mainLayout.addLayout(sliderLayout) mainLayout.addLayout(button1Layout) mainLayout.addWidget(statusLabel) w.setLayout(mainLayout) chaine='@BVDP2021: Port Em '+str(portOut) +' vers IP: '+str(destIP) w.setWindowTitle(chaine) #timer adapted from example 1 of https://www.programcreek.com/python/example/52106/PyQt4.QtCore.QTimer timer = QtCore.QTimer() app.connect(timer, QtCore.SIGNAL('timeout()'), timerUpdate) timer.setInterval(100) timer.start() w.setGeometry(posx,posy,sizex,sizey) #donne le focus au bouton 1 led0Button.setDefault(True) w.show() app.exec_()