Adding interrupt example
							parent
							
								
									07bcb4f05b
								
							
						
					
					
						commit
						5ffdc6e63c
					
				| @ -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. | ||||
| 
 | ||||
| @ -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. | ||||
| @ -1,3 +1,10 @@ | ||||
| # 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. | ||||
| 
 | ||||
| ### 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: | ||||
| ``` | ||||
| <meta http-equiv="refresh" content="10"> | ||||
| ``` | ||||
|  | ||||
					Loading…
					
					
				
		Reference in New Issue