import bge # Bibliothèque Blender Game Engine (UPBGE) import threading # Multithreading import trace import sys import time ############################################################################### # porcou_lib.py # @title: Bibliothèque utilisateur du portail coulissant (pcl_*) # @project: Blender-EduTech # @lang: fr # @authors: Philippe Roy # @copyright: Copyright (C) 2020-2022 Philippe Roy # @license: GNU GPL # # Ce simulateur est un environnement 3D programmable en Python. # Il est destiné à la découverte de la programmation de système pluritechnique. # ############################################################################### # Récupérer l'objet portail coulissant scene = bge.logic.getCurrentScene() # print("Objets de la scene : ", scene.objects) ############################################################################### # Méthode kill pour les tâches (threads) ############################################################################### class thread_with_trace(threading.Thread): def __init__(self, *args, **keywords): threading.Thread.__init__(self, *args, **keywords) self.killed = False def start(self): self.__run_backup = self.run self.run = self.__run threading.Thread.start(self) def __run(self): sys.settrace(self.globaltrace) self.__run_backup() self.run = self.__run_backup def globaltrace(self, frame, event, arg): if event == 'call': return self.localtrace else: return None def localtrace(self, frame, event, arg): if self.killed: if event == 'line': raise SystemExit() return self.localtrace def kill(self): self.killed = True ############################################################################### # Start et stop des tâches (threads) ############################################################################### def thread_start(threads, commandes): threads.append(thread_with_trace(target = commandes)) threads[len(threads)-1].start() scene.objects['Systeme']['thread_run']=True scene.objects['Systeme']['thread_alive']=True print ("Thread #", len(threads)-1, "ouvert.") def thread_stop(threads): i=0 zombie_flag=False for t in threads: if not t.is_alive(): print ("Thread #",i,"fermé.") else: print ("Thread #",i,"encore ouvert ...") t.kill() t.join() if not t.is_alive(): print ("Thread #",i,"tué.") else: print ("Thread #",i,"zombie ...") zombie_flag=True i +=1 if zombie_flag==False: scene.objects['Systeme']['thread_alive']=False print ("Tous les threads sont fermés.") else: print ("Il reste des threads zombies.") ############################################################################### # Actionneurs ############################################################################### # Ordres utilisateur du clignotant def gyr (ordre): scene.objects['Module led']['actif']=ordre # Ordres utilisateur du moteur def mot_o (ordre): scene.objects['Ensemble moteur']['actif_ouvrir']=ordre def mot_f (ordre): scene.objects['Ensemble moteur']['actif_fermer']=ordre # Ordre utilisateur du capteur barrage IR def ir_emet(ordre): scene.objects['Module emetteur IR']['actif']=ordre ############################################################################### # Capteurs ############################################################################### # Compte-rendu utilisateur du capteur fin de course portail ouvert def fc_o (): return scene.objects['Capteur fdc ouvert']['actif'] # Compte-rendu utilisateur du capteur fin de course portail ouvert def fc_f (): return scene.objects['Capteur fdc ferme']['actif'] # Compte-rendu utilisateur du capteur barrage IR def ir_recep (): if scene.objects['Module recepteur IR']['actif'] : return False else: return True ############################################################################### # Boutons poussoirs ############################################################################### # Compte-rendu utilisateur du bouton pousssoir coté rue def bp_ext (): return scene.objects['Module bouton cote rue']['actif'] # Compte-rendu utilisateur du bouton pousssoir coté cour def bp_int (): return scene.objects['Module bouton cote cour']['actif'] ############################################################################### # Temporisation ############################################################################### def tempo (duree): time.sleep(duree)