#Ecran ili9341 Touch IRQ simulé
#
# Pilotage motoconvecteur
# si T° du motoconvecteur < T° pièce arrêter la turbine
# le relai turbine est simulé par la diode blue
# la diode verte allumée la turbine tourne
# la diode rouge clignote si le programme tourne
# simulé sur WorWI sur un esp32-devkit-v1
# la simu ne marche pas pour l'ecran tactile
# d'ou les 3 poussoirs
import machine, onewire, ds18x20, time, binascii
from machine import Pin, SPI, Timer
timer_0 = Timer(0)
from ili9341new import ILI9341, color565
from xpt2046 import Touch
import ESP32
from ESP32 import *
import ZonesTactiles
from ZonesTactiles import *
import random
#fontes____________________________
import glcdfont
import tt14
import tt24
import tt32
fonts = [glcdfont, tt14, tt24, tt32]
blanc=color565(255,255,255)
noir=color565(0,0,0)
rouge=color565(255,0,0)
blue=color565(0,0,255)
vert=color565(0,255,0)
cyan = color565(0, 255, 255)
pourpre = color565(255, 0, 255)
grisF=color565(64,64,64)
grisM=color565(128,128,128)
grisC=color565(192,192,192)
temp_pièce=20.1
temp_motoconvecteur=26.7
# INIT ECRAN ili9341
power = Pin(ILI_LED_PIN, Pin.OUT)
power.value(1) # obligatoire sur certains modele
#---------------------------------------------
cs_pin=Pin(ILI_CS_PIN,Pin.OUT)
cs_pin.on()
cs_pinTouch=Pin(ILI_CS_PINTouch,Pin.OUT)
irq_pinTouch=Pin(ILI_IRQ_PINTouch,Pin.IN)
cs_pinTouch.on()
spi = SPI(2,
baudrate=40000000,
miso=Pin(ILI_MISO_PIN),
mosi=Pin(ILI_MOSI_PIN),
sck=Pin(ILI_CLK_PIN)
)
ecran = ILI9341(
spi,
cs=Pin(ILI_CS_PIN),
dc=Pin(ILI_DC_PIN),
rst=Pin(ILI_RST_PIN),
w=ILI_largeur,
h=ILI_hauteur,
r=ILI_rotation
)
# capteur tactile
touch = Touch(spi, cs=Pin(Pin(ILI_CS_PINTouch)))
# Variable pour stocker l'état
interruption_a_traiter = False
# Fonction de rappel (Callback) pour l'interruption
def Interrup_dectectee(pin):
#print("interruption detecteexxxxxxxxxxxxTROis xxxxxxxxxxxxx 3")
global interruption_a_traiter ,pos
pos = [20,205] #LIGNES TEST A SUPPRIMER8888888888888888888888888888888888888888888
interruption_a_traiter = True
#DEBUT LIGNES TEST A SUPPRIMER 888888888888888888888888888888888888888888888
def Interrup_dectectee1(pin):
#print("interruption detecteexxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1")
global interruption_a_traiter ,pos
pos = [205, 105 ]
interruption_a_traiter = True
def Interrup_dectectee2(pin):
#print("interruption detecteexxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2")
global interruption_a_traiter ,pos
pos = [205,153]
interruption_a_traiter = True
#FIN DES LIGNES TEST A SUPPRIMER8888888888888888888888888888888888888888888
# Configuration de la broche IRQ (T_IRQ sur GPIO 14)
# On détecte le front descendant (Falling edge) car IRQ passe à 0 au contact
irq_pin = Pin(ILI_IRQ_PINTouch, Pin.IN, Pin.PULL_UP)
irq_pin.irq(trigger=Pin.IRQ_FALLING, handler=Interrup_dectectee)
#DEBUT LIGNES TEST A SUPPRIMER 888888888888888888888888888888888888888888888
irq_pin = Pin(ILI_IRQ_PINTouch, Pin.IN, Pin.PULL_UP)
irq_pin.irq(trigger=Pin.IRQ_RISING, handler=Interrup_dectectee)
irq_pin1 = Pin(26, Pin.IN, Pin.PULL_UP)
irq_pin1.irq(trigger=Pin.IRQ_RISING, handler=Interrup_dectectee1)
irq_pin2 = Pin(27, Pin.IN, Pin.PULL_UP)
irq_pin2.irq(trigger=Pin.IRQ_RISING, handler=Interrup_dectectee2)
#FIN DES LIGNES TEST A SUPPRIMER8888888888888888888888888888888888888888888
# réseaux des capteurs de température ------------
ow1 = onewire.OneWire(machine.Pin(ds18B20Rad)) #
Temp_radiateur = ds18x20.DS18X20(ow1) #
#
ow2 = onewire.OneWire(machine.Pin(ds18B20Pièce)) #
Temp_Pièce = ds18x20.DS18X20(ow2) #
#-----------------------------------------------
relai_turbine = machine.Pin(RELAI_TURBINE, Pin.OUT)
led_turbine = machine.Pin(LED_TURBINE, Pin.OUT)
led_M_A = machine.Pin(LED_M_A, Pin.OUT)
état_relai_turbine=True
etat_MarcheArret = True
# initialisations au démarrage
roms1 = Temp_radiateur.scan() # recherche nb sondes
roms2 = Temp_Pièce.scan() # recherche nb sondes
delta_temperature = 0.5 # hystérésis on/off
relai_turbine.on()
led_turbine.on()
led_M_A.on()
#etat_led_turbine = True
etat_led_M_A = True
def lectureTemperatures():
global temp_motoconvecteur
global temp_pièce
global ecran_allumé
Temp_radiateur.convert_temp()
Temp_Pièce.convert_temp()
time.sleep_ms(750)
#print("---acquisition des 2 temperatues------")
for rom1 in roms1:
#print(f'Capteur {i} : {binascii.hexlify(rom1)}')
temp_motoconvecteur = Temp_radiateur.read_temp(rom1)
temp_motoconvecteur = ((((temp_motoconvecteur*100)+5)//10)/10)
#print(" temp du motoconvecteur", temp_motoconvecteur)
for rom2 in roms2:
#print(f'Capteur {j} : {binascii.hexlify(rom2)}')
temp_pièce = Temp_Pièce.read_temp(rom2)
temp_pièce = ((((temp_pièce*100)+5)//10)/10)
#print(" temp de la piéce ", temp_pièce )
if ecran_allumé : #affichage des températures
afficher_bouton(222,5,90,45,noir, vert, ": "+ str(temp_pièce), 9 , 12, tt32)
afficher_bouton(222,46,88,45,noir, vert, ": "+ str(temp_motoconvecteur), 9 , 12, tt32)
def calculEtatTurbine():
# traitement des temperatures
global temp_motoconvecteur
global temp_pièce
global état_relai_turbine
global delta_temperature
global etatB1
if temp_motoconvecteur < temp_pièce : # ne pas ventiler de l'air plus froid que la piéce!
relai_turbine.off()
led_turbine.off()
état_relai_turbine=False
else :
if temp_motoconvecteur - delta_temperature >= temp_pièce :
relai_turbine.on()
if etatB1 :
led_turbine.on()
else :
led_turbine.off()
état_relai_turbine=True
def allumerLED_MA (timer): # clignotement led pour contrôle etat de marche
global etat_MarcheArret , led_M_A
if etat_MarcheArret :
led_M_A.value(not led_M_A.value()) # clignote à chaque boucle
else :
led_M_A.off()
def allumerLED_turbine() :
#global etat_led_turbine
global etatB1
if etatB1 :
led_turbine.on()
else :
led_turbine.off()
def afficher_bouton (x, y, w, h, color1, color2, texte, dxt , dyt, font):
ecran.set_font(font)
ecran.fill_rectangle(x, y, w, h, color=color2)
ecran.set_color(color1,color2)
ecran.set_pos(x+dxt,y+dyt)
ecran.print(texte)
def afficher_libelleles ():
ecran.set_color(blanc,blue)
ecran.erase() #init ecran blanc/bleu
afficher_bouton(5,5,310,45,noir, vert, "Temp. local", 9 , 12, tt32)
afficher_bouton(5,46,310,45,noir, vert, "Temp. radiateur", 9 , 12, tt32)
afficher_bouton (5, 100, 190, 43, noir, vert, "LED Ventilateur" , 9 , 12, tt24)
afficher_bouton (5, 148, 190, 43, noir, vert, "LED M/A", 9 , 12, tt24)
afficher_bouton (xB3, yB3, wB3, hB3, noir, grisC, "Ecran noir", 10 , 12, tt24)
def afficherB1_OFF():
global etatB1
afficher_bouton (xB1, yB1, wB1, hB1, grisF, grisC, "OFF", 32 , 14, tt24)
etatB1=False
def afficherB1_ON():
global etatB1
afficher_bouton (xB1, yB1, wB1, hB1, noir, cyan, "ON", 36 , 12, tt24)
etatB1=True
def afficherB2_OFF():
global etatB2
afficher_bouton (xB2, yB2, wB2, hB2, grisF, grisC, "OFF", 32 , 14, tt24)
etatB2=False
def afficherB2_ON():
global etatB2
afficher_bouton (xB2, yB2, wB2, hB2, noir, cyan, "ON", 36 , 12, tt24)
etatB2=True
def eteindre(): #activer si touche eteindre appuyée
global ecran_allumé
ecran.set_color(noir,noir)
ecran.erase()
ecran_allumé=False
def reveiller(): #si écran éteint touché
global ecran_allumé
global etatB1
global etatB2
global etat_MarcheArret
ecran_allumé = True
afficher_libelleles ()
lectureTemperatures()
calculEtatTurbine()
#allumerLED_MA ()
allumerLED_turbine()
#print(" REVEIL B1 ",etatB1," B2 ",etatB2)
if etatB1 :
afficherB1_ON()
else :
afficherB1_OFF()
if etatB2 :
afficherB2_ON()
etat_MarcheArret = True
else :
afficherB2_OFF()
etat_MarcheArret = False
def maj_affichage ():
global ecran_allumé
global Bx_touch
global etatB1
global etatB2
global etat_MarcheArret
#global etat_led_turbine
if ecran_allumé :
if Bx_touch==1 :
etatB1 = not etatB1 #flip flop
if etatB1 :
afficherB1_ON()
#etat_led_turbine = True
else :
afficherB1_OFF()
#etat_led_turbine = False
#print(" mise a jour 1")
if Bx_touch==2 :
etatB2 = not etatB2
if etatB2 :
afficherB2_ON()
etat_MarcheArret = True
led_M_A.on()
else :
afficherB2_OFF()
etat_MarcheArret= False
led_M_A.off()
#print(" mise a jour 2")
if Bx_touch == 3 :
eteindre()
#print("ecran ETEINT")
def Bx_touché():
# determine quel bouton à été touché
global Bx_touch, pos, allumage_seul
global CPTboucle
global x, y ,xB1 , xB2, xB3, yB1, yB2, yB3
global w, wB1, wB2, wB3
global h, hB1, hB2, hB3
Bx_touch=0
if ecran_allumé :
if (xB1 < x < xB1 + wB1) and (yB1 < y < yB1 + hB1) :
Bx_touch= 1
if (xB2 < x < xB2 + wB2) and (yB2 < y < yB2 + hB2) :
Bx_touch= 2
if (xB3 < x < xB3 + wB3) and (yB3 < y < yB3 + hB3) :
Bx_touch= 3
else :
allumage_seul = True
#print("___FIN DES DEFINITIONS des routines--debut boucle____")
afficher_libelleles ()
afficherB1_ON()
afficherB2_ON()
allumage_seul = False
ecran_allumé=True
etatB1=True
etatB2=True
B1detecte=False
B2detecte=False
B3detecte=False
Bx_touch=0
interruption_a_traiter = False
################################################################
CPTboucle = 0
if __name__ == "__main__":
timer_0.init(mode=Timer.PERIODIC, period=300, callback=allumerLED_MA)
while(True):
CPTboucle += 1
print("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD ",CPTboucle)
if interruption_a_traiter :
# Lecture des coordonnées
#pos = touch.get_touch()
#print("pos ",pos) # SIMU IRQ
if pos:
x, y = pos
#print("Touché détecté ! X: {}, Y: {}".format(x, y))
# Anti-rebond simple et réinitialisation du flag
time.sleep_ms(200)
Bx_touché() # pour determiner quel bouton a ete touché
#print(" boucle ",CPTboucle, "Bouton à traiter ", Bx_touch,"/ ",allumage_seul)
if allumage_seul :
reveiller()
allumage_seul = False
#print ("rallumage ecran termine")
else :
maj_affichage()
interruption_a_traiter=False
else :
lectureTemperatures()
calculEtatTurbine()
#allumerLED_MA () # appelé à chaque tour
allumerLED_turbine()