jumeaux-numeriques/portail_coulissant/porcou_lib.py

195 lines
5.9 KiB
Python
Raw Normal View History

2022-12-11 08:40:31 +01:00
import bge # Bibliothèque Blender Game Engine (UPBGE)
import threading # Multithreading
import trace
import sys
import time
2022-12-11 15:50:38 +01:00
# import serial # Liaison série (jumeau numérique)
# from serial.tools.list_ports import comports # Détection du port automatique
2022-12-11 08:40:31 +01:00
###############################################################################
# porcou_lib.py
2022-12-13 02:42:31 +01:00
# @title: Bibliothèque utilisateur du portail coulissant (pc_*)
2022-12-11 08:40:31 +01:00
# @project: Blender-EduTech
# @lang: fr
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
# @copyright: Copyright (C) 2020-2022 Philippe Roy
# @license: GNU GPL
2022-12-11 15:50:38 +01:00
#
2022-12-11 08:40:31 +01:00
###############################################################################
scene = bge.logic.getCurrentScene()
2022-12-11 15:50:38 +01:00
debug_mvt = scene.objects['System']['debug_mvt']
# Threads
threads_cmd=[]
threads_gostore=[]
debug_thread = scene.objects['System']['debug_thread']
# Jumeau numérique
# twins_serial = None
2022-12-11 08:40:31 +01:00
2022-12-11 15:50:38 +01:00
# UPBGE constants
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED
ACTIVATE = bge.logic.KX_INPUT_ACTIVE
# JUST_DEACTIVATED = bge.logic.KX_SENSOR_JUST_DEACTIVATED
###############################################################################
2022-12-11 08:40:31 +01:00
# 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
2022-12-11 15:50:38 +01:00
2022-12-11 08:40:31 +01:00
def start(self):
self.__run_backup = self.run
2022-12-11 15:50:38 +01:00
self.run = self.__run
2022-12-11 08:40:31 +01:00
threading.Thread.start(self)
2022-12-11 15:50:38 +01:00
2022-12-11 08:40:31 +01:00
def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup
2022-12-11 15:50:38 +01:00
2022-12-11 08:40:31 +01:00
def globaltrace(self, frame, event, arg):
if event == 'call':
return self.localtrace
else:
return None
2022-12-11 15:50:38 +01:00
2022-12-11 08:40:31 +01:00
def localtrace(self, frame, event, arg):
if self.killed:
if event == 'line':
raise SystemExit()
return self.localtrace
2022-12-11 15:50:38 +01:00
2022-12-11 08:40:31 +01:00
def kill(self):
self.killed = True
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-11 08:40:31 +01:00
# Start et stop des tâches (threads)
###############################################################################
2022-12-11 15:50:38 +01:00
def thread_start(threads, type_txt, fct):
threads.append(thread_with_trace(target = fct))
2022-12-11 08:40:31 +01:00
threads[len(threads)-1].start()
2022-12-11 15:50:38 +01:00
if (debug_thread):
print ("Thread",type_txt, "#", len(threads)-1, "open.")
2022-12-11 08:40:31 +01:00
2022-12-11 15:50:38 +01:00
def thread_stop(threads, type_txt):
2022-12-11 08:40:31 +01:00
i=0
zombie_flag=False
for t in threads:
if not t.is_alive():
2022-12-11 15:50:38 +01:00
if (debug_thread):
print ("Thread",type_txt, "#",i,"closed.")
2022-12-11 08:40:31 +01:00
else:
2022-12-11 15:50:38 +01:00
if (debug_thread):
print ("Thread",type_txt, "#",i,"still open ...")
2022-12-11 08:40:31 +01:00
t.kill()
t.join()
if not t.is_alive():
2022-12-11 15:50:38 +01:00
if (debug_thread):
print ("Thread",type_txt, "#",i,"killed.")
2022-12-11 08:40:31 +01:00
else:
2022-12-11 15:50:38 +01:00
if (debug_thread):
print ("Thread",type_txt, "#",i,"zombie...")
2022-12-11 08:40:31 +01:00
zombie_flag=True
i +=1
if zombie_flag==False:
2022-12-11 15:50:38 +01:00
if (debug_thread):
print ("All threads",type_txt, "are closed.")
scene.objects['System']['thread_cmd']=False
return True
2022-12-11 08:40:31 +01:00
else:
2022-12-11 15:50:38 +01:00
if (debug_thread):
print ("There are zombies threads",type_txt, ".")
return False
2022-12-11 08:40:31 +01:00
2022-12-11 15:50:38 +01:00
def thread_cmd_start(fct):
thread_start(threads_cmd, "commands", fct)
def thread_cmd_stop():
thread_stop(threads_cmd, "commands")
2022-12-13 02:42:31 +01:00
def end():
2022-12-11 15:50:38 +01:00
# Jumeau numérique
# if scene.objects['Commands']['twins']:
# serial_msg = "FI\n"
# twins_serial.write(serial_msg.encode()) # Communication série : modele 3d -> carte communication ( arduino | micro:bit )
# rp_jumeau_close()
# Thread
if (debug_thread):
print ("Thread commands is arrived.")
time.sleep(0.125)
scene.objects['System']['thread_cmd']=False
time.sleep(0.125)
2022-12-13 02:42:31 +01:00
def fin():
end()
2022-12-11 15:50:38 +01:00
2022-12-13 02:42:31 +01:00
def quit():
end()
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-11 08:40:31 +01:00
# 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
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-11 08:40:31 +01:00
# Capteurs
###############################################################################
# Compte-rendu utilisateur du capteur fin de course portail ouvert
2022-12-13 02:42:31 +01:00
def fdc_o ():
2022-12-11 08:40:31 +01:00
return scene.objects['Capteur fdc ouvert']['actif']
# Compte-rendu utilisateur du capteur fin de course portail ouvert
2022-12-13 02:42:31 +01:00
def fdc_f ():
2022-12-11 08:40:31 +01:00
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
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-11 08:40:31 +01:00
# 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']
2022-12-11 15:50:38 +01:00
###############################################################################
2022-12-11 08:40:31 +01:00
# Temporisation
###############################################################################
def tempo (duree):
time.sleep(duree)