From 5ffdc6e63c5775acf53ea6c7962baef6dfadc305 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Tue, 24 Dec 2019 15:53:18 -0500 Subject: [PATCH] Adding interrupt example --- interrupts/README.md | 36 ++++++++++++++++++++++++++++++++++++ interrupts/printer.py | 15 +++++++++++++++ threading/hello_threading.py | 2 +- webserver/README.md | 11 +++++++++-- 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 interrupts/README.md create mode 100644 interrupts/printer.py diff --git a/interrupts/README.md b/interrupts/README.md new file mode 100644 index 0000000..bd69957 --- /dev/null +++ b/interrupts/README.md @@ -0,0 +1,36 @@ +# Interrupts + +While a great amount of logic can be handled within our code, `while` loops are not very conducive to event driven + behavior. [Enter interrupts](https://randomnerdtutorials.com/micropython-interrupts-esp32-esp8266/). When a change is detected interrupts trigger a handler function. + +When an interrupt occurs the processor halts execution of the main program and executes the interrupt handling routine +then returns control back to the main program. + +On the ESP32 you can use all pins as interrupts with the except of pins 6 - 11. + +## Basics + +Let's use a button and an LED to demonstrate the basics of interrupts. + +Your interrupt handler must accept a single parameter, a `Pin` object, which will be set to the pin triggering the interrupt. + +```` +def hangle_interrupt(pin): + pass +```` + +Set the GPIO that will be used as the input pin. +``` +pin = Pin(14, Pin.IN) +``` + +Attach an interrupt to pin. +``` +pin.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt) +``` + +There are 3 different trigger modes you can use: +* IRQ_RISING - Trigger on the rising edge +* IRQ_FALLING - Trigger on the falling edge. For example when a button is released. +* 3 - Trigger the interrupt on both edges. + diff --git a/interrupts/printer.py b/interrupts/printer.py new file mode 100644 index 0000000..c49195d --- /dev/null +++ b/interrupts/printer.py @@ -0,0 +1,15 @@ +from machine import Pin + +# Enable input pin. We will put a button on this pin +pir = Pin(34, Pin.IN) + + +# Create handler +def handle_interrupt(pin): + print("You triggered the interrupt") + + +# attach interrupt to pin +pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt) + +# You don't need a While loop here. If you leave it off you will get a REPL with your objects still set. \ No newline at end of file diff --git a/threading/hello_threading.py b/threading/hello_threading.py index 255bd3f..281e77b 100644 --- a/threading/hello_threading.py +++ b/threading/hello_threading.py @@ -5,7 +5,7 @@ import time def testThread(): while True: print("Hello threading") - time.sleep(15) # Time is so high so that we can actually get a repl. + time.sleep(15) # Time is so high so that we can actually get a repl. _thread.start_new_thread(testThread, ()) diff --git a/webserver/README.md b/webserver/README.md index eaa93fc..70a698c 100644 --- a/webserver/README.md +++ b/webserver/README.md @@ -1,3 +1,10 @@ -# ESP32 Webserver +# ESP32 Web server -This project makes use of the ESP32's wifi capabilities and the `usocket` library to respond to a simple get request and return and HTML response. \ No newline at end of file +This project makes use of the ESP32's wifi capabilities and the `usocket` library to respond to a simple get request and return and HTML response. + +### Web page auto refresh + +You can make use the of the meta tag to tell the web browser to refresh the page on a certain interval: +``` + +```