#Ecran ili9341 Touch IRQ simulé
# M_A # 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
# on peut visualiser (ON) ou non (OFF) chaque diode
# simulé sur WorWI sur un esp32-devkit-v1
"""
import gc
print("Mémoire libre :", gc.mem_free(), "octets")
"""
import machine, onewire, ds18x20, time, binascii
from machine import Pin, SPI, Timer
timer_0 = Timer(0)
timer_1 = Timer(1)
from ili9341new import ILI9341
from xpt2046 import Touch
import framebuf
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]
from couleursdebase import *
# 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()
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 ----------------------------
cs_pinTouch=Pin(ILI_CS_PINTouch,Pin.OUT)
cs_pinTouch.on()
touch = SPI(3, baudrate=2000000, polarity=0, phase=0,
sck=Pin(ILI_CLK_PINTouch), mosi=Pin(ILI_MOSI_PIN), miso=Pin(ILI_MISO_PINTouch))
irq_pinTouch = Pin(ILI_IRQ_PINTouch, Pin.IN)
# Initialisation tactile XPT2046
touch = TOUCH(spi, cs=cs_pinTouch, int_pin=irq_pinTouch)
# 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_ecran = machine.Pin(LED_ECRAN, Pin.OUT)
#------------------------------------------------
def Interrup_dectectee(pin):
global interruption_a_traiter
interruption_a_traiter = True
def AcquisitionTemperatures(timer):
global temp_motoconvecteur
global temp_pièce
global temperatures_a_afficher
temperatures_a_afficher=True
Temp_radiateur.convert_temp()
time.sleep_ms(750)
Temp_Pièce.convert_temp()
time.sleep_ms(750)
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) # arrodi au 1/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) # arrodi au 1/10
temp_pièce += 0.3
#print(" temp de la piéce ", temp_pièce )
def affichage_températures ():
global ecran_allumé
if ecran_allumé : #affichage des températures
afficher_bouton(222,5,93,45,noir, cyan, ": "+ str(temp_pièce), 9 , 10, tt32)
afficher_bouton(222,51,93,45,noir, cyan, ": "+ str(temp_motoconvecteur), 9 , 10, tt32)
def calculEtatTurbine(): # traitement des temperatures et LED turbine
global temp_motoconvecteur
global temp_pièce
global etat_relai_turbine
global delta_temperature
if temp_motoconvecteur < temp_pièce : # ne pas ventiler de l'air plus froid que la piéce!
relai_turbine.off()
led_turbine.off()
etat_relai_turbine=False
else :
if temp_motoconvecteur - delta_temperature >= temp_pièce :
etat_relai_turbine=True
relai_turbine.on()
allumerLED_turbine() #allumer si param visualisation= ON
def clignoterLED_ECRAN (timer): # clignotement led pour contrôle etat de marche
global etat_Ecran , led_ecran
if etat_Ecran :
led_ecran.value(not led_ecran.value()) # clignote à chaque boucle
else :
led_ecran.off()
def allumerLED_turbine() :
global voirLED1
if voirLED1:
led_turbine.on()
else :
led_turbine.off()
def afficher_bouton (x, y, w, h, color1, color2, texte, dxt , dyt, font):
ecran.fill_rectangle(x, y, w, h, color=color2)
if len(texte) != 0:
ecran.set_font(font)
ecran.set_color(color1,color2)
ecran.set_pos(x+dxt,y+dyt)
ecran.print(texte)
def afficher_Zfixes ():
ecran.set_color(blanc,blue)
ecran.erase() #init ecran blanc/bleu
afficher_bouton(5,5,310,45,noir, vert, "", 0 , 0, tt32)
ecran.afficher_sprite(fb_sprite_T_piece , 15, 23, w_sprite_T_piece , h_sprite)
afficher_bouton(5,51,310,45,noir, vert, "", 0 , 0, tt32)
ecran.afficher_sprite(fb_sprite_T_radiateur , 15, 65, w_sprite_T_radiateur , h_sprite)
afficher_bouton (5, 100, 190, 43, noir, vert, "" , 9 , 12, tt24)
ecran.afficher_sprite(fb_sprite_Ventilo , 15, 113, w_sprite_Ventilo , h_sprite)
afficher_bouton (5, 148, 190, 43, noir, vert, "", 9 , 12, tt24)
ecran.afficher_sprite(fb_sprite_MA , 15, 161, w_sprite_MA , h_sprite)
afficher_bouton (xB3, yB3, wB3, hB3, noir, grisC, "", 10 , 12, tt24)
ecran.afficher_sprite(fb_sprite_E_N, xB3+20, yB3+13, w_sprite_E_N, h_sprite)
def AffBtonLED1_OFF():
afficher_bouton (xB1, yB1, wB1, hB1, grisF, grisC, "OFF", 32 , 14, tt24)
def AffBtonLED1_ON():
afficher_bouton (xB1, yB1, wB1, hB1, noir, cyan, "ON", 36 , 12, tt24)
def AffBtonLED2_OFF():
afficher_bouton (xB2, yB2, wB2, hB2, grisF, grisC, "OFF", 32 , 14, tt24)
def AffBtonLED2_ON():
afficher_bouton (xB2, yB2, wB2, hB2, noir, cyan, "ON", 36 , 12, tt24)
def reveiller(): #si écran noir touché
global ecran_allumé
global voirLED1
global voirLED2
global etat_Ecran
ecran_allumé = True
afficher_Zfixes()
AcquisitionTemperatures(timer_1)
calculEtatTurbine()
if voirLED1 :
AffBtonLED1_ON()
else :
AffBtonLED1_OFF()
if voirLED2 :
AffBtonLED2_ON()
etat_Ecran = True
else :
AffBtonLED2_OFF()
etat_Ecran = False
def maj_affichage ():
global ecran_allumé
global ZonTactile_touchée
global voirLED1
global voirLED2
global etat_Ecran
if ecran_allumé :
if ZonTactile_touchée==1 :
voirLED1 = not voirLED1 #flip flop
if voirLED1 :
AffBtonLED1_ON()
else :
AffBtonLED1_OFF()
#print(" mise a jour 1")
if ZonTactile_touchée==2 :
voirLED2 = not voirLED2
if voirLED2 :
AffBtonLED2_ON()
etat_Ecran = True
led_ecran.on()
else :
AffBtonLED2_OFF()
etat_Ecran= False
led_ecran.off()
#print(" mise a jour 2")
if ZonTactile_touchée == 3 :
ecran.set_color(noir,blanc)
ecran.erase()
ecran_allumé=False
def determine_zone_touchée():
# determine quelle zone à été touchée
#ou allumer l'ecran
global ZonTactile_touchée, pos, allumerEcran
global CPTboucle
global x, y ,xB1 , xB2, xB3, yB1, yB2, yB3
global w, wB1, wB2, wB3
global h, hB1, hB2, hB3
ZonTactile_touchée=0
if ecran_allumé :
if (xB1 < x < xB1 + wB1) and (yB1 < y < yB1 + hB1) :
ZonTactile_touchée= 1
if (xB2 < x < xB2 + wB2) and (yB2 < y < yB2 + hB2) :
ZonTactile_touchée= 2
if (xB3 < x < xB3 + wB3) and (yB3 < y < yB3 + hB3) :
ZonTactile_touchée= 3
else :
allumerEcran = True
def creer_sprite(sprite_txt, sprite_nom ,spritecolor_txt ,spritecolor_fond):
w_E = len(sprite_txt*8)
h_E = 8
buf_Entree = bytearray(w_E * h_E * 2)
fb_buf_Entree = framebuf.FrameBuffer(buf_Entree, w_E, h_E, framebuf.RGB565)
fb_buf_Entree.fill(spritecolor_fond) # FOND gris clair
fb_buf_Entree.text(sprite_txt, 0, 0, spritecolor_txt) # CARACTERES
#continuer = input("enter 3 pour continuer")
buf_X2 = bytearray(w_E*2 * h_E*2 * 2)
fb_buf_X2 = framebuf.FrameBuffer(buf_X2, w_E*2, h_E*2, framebuf.RGB565)
ligne_O = bytearray(w_E*2) # ligne O à traiter
ligne_D = bytearray(w_E*2*2) #w_E*4 ligne D à creer
index_D = 0
for ih in range(h_E): #pour la ligne ih
ligne_O[:] = buf_Entree[ih*2*w_E:ih*2*w_E + 2*w_E] # extraction ligne ih
index_LD = 0
for iw in range(0,2*w_E,2): # parcourir les w_E*2 codcol
ligne_D[index_LD:index_LD+2] = ligne_O[iw:iw+2]
ligne_D[index_LD+2:index_LD+4] = ligne_O[iw:iw+2]
index_LD += 4
buf_X2[index_D:index_D+2*w_E*2] = ligne_D[:]
buf_X2[index_D+2*w_E*2:index_D+4*w_E*2] = ligne_D[:]
index_D += 4*w_E*2
long_sprite = len(buf_X2)
nom_sprite = "sprite_" + sprite_nom
w_sprite = "w_sprite_" + sprite_nom
fb_sprite = "fb_sprite_" + sprite_nom
globals()[nom_sprite] = buf_X2
globals()[w_sprite] = w_E*2
globals()[fb_sprite] = framebuf.FrameBuffer(globals()[nom_sprite], globals()[w_sprite], h_sprite, framebuf.RGB565)
#print("___FIN DES DEFINITIONS des routines--debut boucle____")
#creer zone Ecran Noir
sprite_txt ="ETEINDRE L'ECRAN"
sprite_nom ="E_N"
spritecolor_fond=0xEFBD
spritecolor_txt=0xFFFF
creer_sprite(sprite_txt, sprite_nom ,spritecolor_txt ,spritecolor_fond )
#creer zone MA
sprite_txt="LED M/A"
sprite_nom = "MA"
spritecolor_fond=0xE007
spritecolor_txt=0x0000
creer_sprite(sprite_txt, sprite_nom, spritecolor_txt ,spritecolor_fond )
#creer zone Ventilo
sprite_txt="LED Ventil."
sprite_nom = "Ventilo"
spritecolor_fond=0xE007
spritecolor_txt=0x0000
creer_sprite(sprite_txt, sprite_nom, spritecolor_txt ,spritecolor_fond )
#creer zone Temp. pièce
sprite_txt="TEMP. Piece"
sprite_nom = "T_piece"
spritecolor_fond=0xE007
spritecolor_txt=0x0000
creer_sprite(sprite_txt, sprite_nom, spritecolor_txt ,spritecolor_fond )
#creer zone Temp. radiateur
sprite_txt="TEMP. Radiat."
sprite_nom = "T_radiateur"
spritecolor_fond=0xE007
spritecolor_txt=0x0000
creer_sprite(sprite_txt, sprite_nom, spritecolor_txt ,spritecolor_fond )
################################################################
# Configuration sur ESP32 de la broche IRQ (T_IRQ sur GPIO xx)
# 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)
etat_relai_turbine=True
etat_Ecran = 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_ecran.on()
etat_Ecran = True
ecran_allumé=True
afficher_Zfixes()
AcquisitionTemperatures(timer_1)
AffBtonLED1_ON()
AffBtonLED2_ON()
allumerEcran = False
voirLED1=True
voirLED2=True
ZonTactile_touchée=0
interruption_a_traiter = False
temperatures_a_afficher=True
CPTboucle = 0
timer_0.init(mode=Timer.PERIODIC, period=300, callback=clignoterLED_ECRAN)
timer_1.init(mode=Timer.PERIODIC, period=4000, callback=AcquisitionTemperatures)
while(True):
CPTboucle += 1
# print("BBBBBBBBBBBBB ",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))
# delai Anti-rebond
#time.sleep_ms(200)
determine_zone_touchée() # pour determiner quelle zone a ete touché
if allumerEcran :
reveiller()
allumerEcran = False
else :
maj_affichage()
interruption_a_traiter=False
else :
calculEtatTurbine()
if temperatures_a_afficher :
affichage_températures()
temperatures_a_afficher =not (temperatures_a_afficher)