From bff5348031dbb6ba211dc24428e1e07e38c598e0 Mon Sep 17 00:00:00 2001 From: Raymond Bourges Date: Tue, 30 Dec 2025 17:09:24 +0100 Subject: [PATCH] FEAT : Classe Leds avec gestion de la consigne --- main.py | 77 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/main.py b/main.py index 3ef5081..d44d1ab 100644 --- a/main.py +++ b/main.py @@ -110,12 +110,43 @@ class Cloud: print("Erreur :", e) return False +class Leds: + brightness = 0.01 # 1 % + pin_leds = Pin(Broche[8], Pin.OUT) + np = NeoPixel(pin_leds, 10) + vert = (0, 255, 0) + noir = (0, 0, 0) + orange = (255, 165, 0) + bascule = False + + def _led(self, i, r, g, b): + self.np[i] = ( + int(r * self.brightness), + int(g * self.brightness), + int(b * self.brightness) + ) + + def afficher(self, charge:int, consigne): + self.bascule = not self.bascule + nb_dizaine = charge // 10 + led_consigne = (consigne // 10) - 1 + for led in range(0, 10): + self._led(led, *self.noir) + for led in range(0, nb_dizaine): + self._led(led, *self.vert) + if self.bascule: + self._led(nb_dizaine, *self.vert) + else: + self._led(led_consigne, *self.orange) + self.np.write() + + # x = charge // 10 + # print(f"--> {x}") + # pass + # Capter exception pour éviter les sorties intempestives (comme timeout sur ntp) # Regarder https://docs.micropython.org/en/latest/esp32/quickref.html#timers -# led_jaune = Pin(0, Pin.OUT) # D0 -# button_jaune = Pin(1, Pin.IN, Pin.PULL_UP) # D1 - relais = Relais() cloud = Cloud() @@ -144,41 +175,31 @@ def action_rouge(): bouton_rouge = Bouton("bleu", Broche[10], action_rouge, Broche[9]) -button_moins = Pin(16, Pin.IN, Pin.PULL_UP) # D6 (et masse) -button_plus = Pin(17, Pin.IN, Pin.PULL_UP) # D7 (et masse) +def action_moins(): + global consigne + consigne = max(10, consigne - 10) -bouton_moins = Bouton("moins", Broche[7]) +bouton_moins = Bouton("moins", Broche[7], action_moins) -bouton_plus = Bouton("plus", Broche[6]) +def action_plus(): + global consigne + consigne = min(100, consigne + 10) + +bouton_plus = Bouton("plus", Broche[6], action_plus) + +leds = Leds() #######################################################"" -brightness = 0.01 # 1 % - -def set_pixel(i, r, g, b): - np[i] = ( - int(r * brightness), - int(g * brightness), - int(b * brightness) - ) - -pin_leds = Pin(19, Pin.OUT) # D8 -np = NeoPixel(pin_leds, 10) -set_pixel(0, 0, 255, 0) -set_pixel(7, 0, 255, 0) -np.write() -# np.brightness(50) - pas_temps = 1 i = 0 +consigne = 80 while True: - # now = datetime.now() - # print("Date et heure :", now) - i += 1 + i = (i + 1) % 10 print(f"-------------- {i}") print(f"Charge : {cloud.zoe()}") print(f"HC : {cloud.HC()}") + print(f"Consigne : {consigne}") + leds.afficher(charge=cloud.zoe(), consigne=consigne) time.sleep(pas_temps) - # set_pixel(0, 0, 0, 0) # OFF - # np.write() \ No newline at end of file