Hello,
By any luck someone could tell me how i could fix my thread problem.
In the while loop at the begging there is a thread. Its link to a function delai and sub function decompte or (counter in english)
While the counter is in action nothing on my board will be of any use its lock.
The thread() should let the other function run without disrupting the rest. The counter with the thread become eratic. Anyone know how to use the thread in that context?
Thank you
By any luck someone could tell me how i could fix my thread problem.
In the while loop at the begging there is a thread. Its link to a function delai and sub function decompte or (counter in english)
While the counter is in action nothing on my board will be of any use its lock.
The thread() should let the other function run without disrupting the rest. The counter with the thread become eratic. Anyone know how to use the thread in that context?
Thank you
""" """ from time import sleep from threading import Thread import time import smtplib import RPi.GPIO as GPIO import pygame from pygame.locals import * import sys import time #Pygame code clock = pygame.time.Clock() pygame.init() pygame.display.set_caption('Panneau de contrôle') FPS_CLOCK = pygame.time.Clock() BLANC = (255, 255, 255) ROUGE = (255, 0, 0) GREEN = (0, 255, 0) GRIS = (200, 200, 200) NOIR = (0, 0, 0) JAUNE = (255, 255, 0) screen = pygame.display.set_mode([700, 500]) sysfont = pygame.font.get_default_font() font = pygame.font.SysFont(None, 48) def bouton_A(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, GREEN, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_D(screen, pos, texte): rayon = 55 pygame.draw.circle(screen, ROUGE, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) def bouton_nb(screen, pos, texte): rayon = 25 pygame.draw.circle(screen, GRIS, pos, rayon) txtBtn = font.render(texte, True, NOIR) rectBtn = txtBtn.get_rect() rectBtn.center = (pos) screen.blit(txtBtn, rectBtn) class Etat(): def horloge(self): theFont=pygame.font.Font(None,42) theTime=time.strftime("%H:%M:%S", time.localtime()) timeText=theFont.render(str(theTime), True,(255,255,255),(0,0,0)) screen.blit(timeText, (448,428)) pygame.display.update() def image(self): image = pygame.image.load('3.png') screen.blit(image, (110, 406)) def Alert_image(self): image = pygame.image.load('Master-Warning-Light.png') image = pygame.transform.scale(image, (96, 96)) screen.blit(image, (600, -10)) #pygame.display.update() img_intrus = font.render("Alerte Intrus", True, JAUNE) screen.blit(img_intrus, (450, 20)) sysfont = pygame.font.get_default_font() font = pygame.font.SysFont(None, 36) #Rapsberry Pi code GPIO.setmode(GPIO.BOARD) senseur_Del2 = 12 DEL1 = 18 DEL2 = 22 GPIO.setwarnings(False) GPIO.setup(senseur_Del2,GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.setup(DEL1,GPIO.OUT,) GPIO.setup(DEL2,GPIO.OUT) led_off1=False led_off2=False def decompte(t): while t: min , sec = divmod(t,60) timer = '{:02d}:{:02d}'.format(min, sec) print(timer, end='\r') time.sleep(1) t -= 1 if t == 0: print('Délai terminé!') t = 10 class Etat_ent: def delai(self): #délai d'entrée print('Vous avez 10 secondes avant l`enclenchement de l`alarme!') GPIO.output(DEL1,True) time.sleep(.1) decompte(int(t)) GPIO.output(DEL1,True) time.sleep(1) GPIO.output(DEL1,False) time.sleep(1) GPIO.output(DEL1,True) ent = Etat_ent() e = Etat() while(True): if led_off1==False and GPIO.input(senseur_Del2)==1: thread = Thread(target=ent.delai, args=()) <-----maybe at the wrong place or stop at the wrong time thread.start() GPIO.output(DEL1,True) time.sleep(.2) GPIO.output(DEL1,False) time.sleep(.2) led_off2=True sleep(.5) if GPIO.input(senseur_Del2)==1: GPIO.output(DEL2,True) led_off2=True e.Alert_image() sleep(.5) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False x , y = pygame.mouse.get_pos() #liste des boutons cliqués Bouton_A = x > 104 and x < 204 and y > 196 and y < 301 Bouton_D = x < 549 and x > 444 and y > 192 and y < 309 Chiffre_1 = x > 248 and x < 285 and y > 182 and y < 218 Chiffre_2 = x > 301 and x < 339 and y > 182 and y < 220 Chiffre_3 = x > 358 and x < 390 and y > 180 and y < 218 Chiffre_4 = x > 244 and x < 282 and y > 237 and y < 269 Chiffre_5 = x > 303 and x < 341 and y > 235 and y < 268 Chiffre_6 = x > 357 and x < 394 and y > 238 and y < 288 Chiffre_7 = x > 241 and x < 287 and y > 294 and y < 329 Chiffre_8 = x > 301 and x < 339 and y > 295 and y < 330 Chiffre_9 = x > 353 and x < 395 and y > 291 and y < 327 if event.type == pygame.MOUSEBUTTONDOWN: #print(x,y) if Bouton_A: img = font.render("Entrez votre code pour désarmer le systeme d'alarme!", True, NOIR) screen.blit(img, (20, 100)) img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC) screen.blit(img, (20, 100)) GPIO.output(DEL1,True) led_off1=True sleep(.5) else: screen.fill(NOIR) pygame.draw.rect(screen, NOIR, (21, 75, 677, 51), 2) GPIO.output(DEL1,False) led_off1= False sleep(.5) if Bouton_D: img = font.render("Entrez votre code pour désarmer le systeme d'alarme!", True, BLANC) screen.blit(img, (20, 100)) GPIO.output(DEL1,False) led_off1=False sleep(.5) GPIO.output(DEL2,False) led_off2=False sleep(.5) thread.stop() if Chiffre_1: img = font.render("*", True, BLANC) screen.blit(img, (258, 131)) if Chiffre_2: img = font.render("*", True, BLANC) screen.blit(img, (298, 131)) if Chiffre_3: img = font.render("*", True, BLANC) screen.blit(img, (335, 131)) if Chiffre_4: img = font.render("*", True, BLANC) screen.blit(img, (370, 131)) screen.fill(NOIR) pygame.draw.rect(screen, NOIR, (21, 125, 677, 51), 2) img = font.render("Merci", True, BLANC) screen.blit(img, (295, 350)) if Chiffre_5: img = font.render("*", True, BLANC) screen.blit(img, (258, 131)) e.Alert_image() GPIO.output(DEL2,True) led_off1=True sleep(.5) if Chiffre_6: img = font.render("*", True, BLANC) screen.blit(img, (258, 131)) e.Alert_image() GPIO.output(DEL2,True) led_off1=True sleep(.5) if Chiffre_7: img = font.render("*", True, BLANC) screen.blit(img, (258, 131)) e.Alert_image() GPIO.output(DEL2,True) led_off1=True sleep(.5) if Chiffre_8: img = font.render("*", True, BLANC) screen.blit(img, (258, 131)) e.Alert_image() GPIO.output(DEL2,True) led_off1=True sleep(.5) if Chiffre_9: img = font.render("*", True, BLANC) screen.blit(img, (258, 131)) e.Alert_image() GPIO.output(DEL2,True) led_off1=True sleep(.5) #listes des boutons bouton_A(screen, (150, 250), "Activé") bouton_D(screen, (500, 250), "Désactivé") bouton_nb(screen, (265, 200), "1") bouton_nb(screen, (320, 200), "2") bouton_nb(screen, (375, 200), "3") bouton_nb(screen, (265, 255), "4") bouton_nb(screen, (320, 255), "5") bouton_nb(screen, (375, 255), "6") bouton_nb(screen, (265, 310), "7") bouton_nb(screen, (320, 310), "8") bouton_nb(screen, (375, 310), "9") img = font.render("Bienvenue", True, ROUGE) screen.blit(img, (20, 20)) pygame.display.flip() e.image() e.horloge() FPS_CLOCK.tick(30) pygame.quit() sys.exit()