FEAT : Classe Bouton
This commit is contained in:
parent
f4cf814c7b
commit
1260dce8fa
61
main.py
61
main.py
@ -8,11 +8,49 @@ import urequests
|
|||||||
import localtime_fr # TODO : Lire info HP.. HC.. sur HA > sensor.compteur_linky_ptec
|
import localtime_fr # TODO : Lire info HP.. HC.. sur HA > sensor.compteur_linky_ptec
|
||||||
import webrepl
|
import webrepl
|
||||||
|
|
||||||
|
class Bouton:
|
||||||
|
|
||||||
|
def __init__(self, nom, pin, todo = None, pin_led = None):
|
||||||
|
self.dernier_appui = 0
|
||||||
|
self.nom = nom
|
||||||
|
self.pin = Pin(pin, Pin.IN, Pin.PULL_UP)
|
||||||
|
self.pin.irq(trigger = Pin.IRQ_FALLING, handler = self.bouton_presse)
|
||||||
|
self.todo = todo
|
||||||
|
if pin_led is None:
|
||||||
|
self.avec_led = False
|
||||||
|
else:
|
||||||
|
self.avec_led = True
|
||||||
|
self.pin_led = Pin(pin_led, Pin.OUT)
|
||||||
|
|
||||||
|
def led_on(self):
|
||||||
|
if self.avec_led:
|
||||||
|
self.pin_led.on()
|
||||||
|
|
||||||
|
def led_off(self):
|
||||||
|
if self.avec_led:
|
||||||
|
self.pin_led.off()
|
||||||
|
|
||||||
|
def bouton_presse(self, pin):
|
||||||
|
self.maintenant = time.ticks_ms()
|
||||||
|
if time.ticks_diff(self.maintenant, self.dernier_appui) > DEBOUNCE_MS:
|
||||||
|
self.dernier_appui = self.maintenant
|
||||||
|
print("Bouton " + self.nom + " pressé !")
|
||||||
|
if self.todo is not None:
|
||||||
|
self.todo()
|
||||||
|
|
||||||
# Capter exception pour éviter les sorties intempestives (comme timeout sur ntp)
|
# Capter exception pour éviter les sorties intempestives (comme timeout sur ntp)
|
||||||
# Regarder https://docs.micropython.org/en/latest/esp32/quickref.html#timers
|
# Regarder https://docs.micropython.org/en/latest/esp32/quickref.html#timers
|
||||||
|
|
||||||
led_jaune = Pin(0, Pin.OUT) # D0
|
# led_jaune = Pin(0, Pin.OUT) # D0
|
||||||
button_jaune = Pin(1, Pin.IN, Pin.PULL_UP) # D1
|
# button_jaune = Pin(1, Pin.IN, Pin.PULL_UP) # D1
|
||||||
|
|
||||||
|
def action_jaune():
|
||||||
|
bouton_jaune.led_on()
|
||||||
|
led_bleue.off()
|
||||||
|
led_rouge.off()
|
||||||
|
set_relays(0b0011)
|
||||||
|
|
||||||
|
bouton_jaune = Bouton("jaune", 1, action_jaune, 0)
|
||||||
|
|
||||||
led_bleue = Pin(2, Pin.OUT) # D2
|
led_bleue = Pin(2, Pin.OUT) # D2
|
||||||
button_bleu = Pin(21, Pin.IN, Pin.PULL_UP) # D3
|
button_bleu = Pin(21, Pin.IN, Pin.PULL_UP) # D3
|
||||||
@ -53,26 +91,13 @@ dernier_appui = 0
|
|||||||
|
|
||||||
token_ha = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkNGY4MDdmYWYzNDQ0NTc0ODY4MmFmNzA4NDdmMTE0MyIsImlhdCI6MTc2NDQ1Mzk0NSwiZXhwIjoyMDc5ODEzOTQ1fQ.DJgSqeTKPHWbKEFH3HuFih4QKt3CSqLqot34_vhCOQU"
|
token_ha = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkNGY4MDdmYWYzNDQ0NTc0ODY4MmFmNzA4NDdmMTE0MyIsImlhdCI6MTc2NDQ1Mzk0NSwiZXhwIjoyMDc5ODEzOTQ1fQ.DJgSqeTKPHWbKEFH3HuFih4QKt3CSqLqot34_vhCOQU"
|
||||||
|
|
||||||
def button_jaune_presse(pin):
|
|
||||||
global dernier_appui
|
|
||||||
maintenant = time.ticks_ms()
|
|
||||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
|
||||||
dernier_appui = maintenant
|
|
||||||
print("Bouton jaune pressé !")
|
|
||||||
led_jaune.on()
|
|
||||||
led_bleue.off()
|
|
||||||
led_rouge.off()
|
|
||||||
set_relays(0b0011)
|
|
||||||
|
|
||||||
button_jaune.irq(trigger=Pin.IRQ_FALLING, handler=button_jaune_presse)
|
|
||||||
|
|
||||||
def button_bleu_presse(pin):
|
def button_bleu_presse(pin):
|
||||||
global dernier_appui
|
global dernier_appui
|
||||||
maintenant = time.ticks_ms()
|
maintenant = time.ticks_ms()
|
||||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
||||||
dernier_appui = maintenant
|
dernier_appui = maintenant
|
||||||
print("Bouton bleu !")
|
print("Bouton bleu !")
|
||||||
led_jaune.off()
|
bouton_jaune.led_off()
|
||||||
led_bleue.on()
|
led_bleue.on()
|
||||||
led_rouge.off()
|
led_rouge.off()
|
||||||
set_relays(0b1100)
|
set_relays(0b1100)
|
||||||
@ -85,7 +110,7 @@ def button_rouge_presse(pin):
|
|||||||
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
if time.ticks_diff(maintenant, dernier_appui) > DEBOUNCE_MS:
|
||||||
dernier_appui = maintenant
|
dernier_appui = maintenant
|
||||||
print("Bouton rouge pressé !")
|
print("Bouton rouge pressé !")
|
||||||
led_jaune.off()
|
bouton_jaune.led_off()
|
||||||
led_bleue.off()
|
led_bleue.off()
|
||||||
led_rouge.on()
|
led_rouge.on()
|
||||||
set_relays(0b1100)
|
set_relays(0b1100)
|
||||||
@ -149,7 +174,7 @@ def charge() -> int:
|
|||||||
|
|
||||||
# Init
|
# Init
|
||||||
set_relays(0b0000)
|
set_relays(0b0000)
|
||||||
led_jaune.off()
|
bouton_jaune.led_off()
|
||||||
led_bleue.off()
|
led_bleue.off()
|
||||||
led_rouge.off()
|
led_rouge.off()
|
||||||
wifi = network.WLAN(network.STA_IF)
|
wifi = network.WLAN(network.STA_IF)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user