ESP8266 Interrupt Handling

From ProjectPages
Jump to: navigation, search
from machine import Timer, Pin

import micropython
micropython.alloc_emergency_exception_buf(100)

class led_toggle(object):
	# set up a timer interrupt to toggle an led
	def __init__(self, led):
		self.led = led
		# this is used to remember the led state
		self._value = False
		# set up the timer
		self.timer = Timer(-1)
		# start the timer
		# 2 second period
		# PERIODIC mode means that it repeats
		# uses the cb function when timer triggers
		self.timer.init(period=2000, mode=Timer.PERIODIC, callback=self.cb)
		
	def cb(self, timer):
		# timer callback function
		
		# set the led
		self.led.value(self._value)
		
		# toggle the value
		self._value = not self._value

# use the ESP8266-12 onboard LED
led = Pin(2, Pin.OUT)

# call the Class to start the routine
blue = led_toggle(led)