Skip to content

Instantly share code, notes, and snippets.

@beezly
Created January 13, 2019 18:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beezly/5cf0d53f7d46d82428771548b34ad0ed to your computer and use it in GitHub Desktop.
Save beezly/5cf0d53f7d46d82428771548b34ad0ed to your computer and use it in GitHub Desktop.
ESP32 Hardware Interrupts in micropython
import machine
import sys
import utime
# Error buffer for inside ISRs
import micropython
micropython.alloc_emergency_exception_buf(100)
class Run():
def __init__(self):
self.values = [ 125, 1000 ]
self.iter = 0
self.curr_val = self.values[self.iter & len(self.values)]
self.incr_set_ref = self.incr_set
self.repl_button = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
self.led = machine.Pin(5, machine.Pin.OUT)
self.repl_button.irq(trigger=machine.Pin.IRQ_RISING, handler=self.incr_handler)
def incr_handler(self, p):
print("Handling Pin ",p)
micropython.schedule(self.incr_set_ref,0)
def incr_set(self,v):
print("Setting new iter")
self.iter+=1
self.curr_val = self.values[self.iter % len(self.values)]
print('Iter: {}, curr_val: {}'.format(self.iter, self.curr_val))
def run(self):
while 1 != 0:
self.led.value(1)
print("On")
utime.sleep_ms(self.curr_val)
self.led.value(0)
print("Off")
utime.sleep_ms(self.curr_val)
sys.exit(0)
Run().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment