Sfoglia il codice sorgente

Set up sample IRQ handler for button press

DarkMorford 6 anni fa
parent
commit
bae546ea8f
1 ha cambiato i file con 22 aggiunte e 1 eliminazioni
  1. 22 1
      code/main.py

+ 22 - 1
code/main.py

@@ -1,6 +1,11 @@
 from machine import Pin, Signal
 LED_PIN = Signal(13, Pin.OUT, invert=False)
-BUTTON_PIN = Signal(0, Pin.IN, invert=True)
+BUTTON_PIN = Pin(0, Pin.IN)
+
+button_triggered = False
+def button_handler(pin):
+    global button_triggered
+    button_triggered = True
 
 # Turn off "ready" LED
 LED_PIN.off()
@@ -23,5 +28,21 @@ if not wifi_guest.isconnected():
         pass
 print(wifi_guest.ifconfig())
 
+# Set up interrupt handler
+BUTTON_PIN.irq(button_handler, Pin.IRQ_RISING)
+
 # Turn on "ready" LED
 LED_PIN.on()
+
+# Enter main loop
+import time
+while True:
+    if button_triggered:
+        import urequests as requests
+
+        LED_PIN.off()
+        time.sleep(5)
+        button_triggered = False
+        LED_PIN.on()
+    else:
+        time.sleep(1)