Hi All,
I've got an ESP12 running esp8266-20160917-v1.8.4-16-gb9672bc.bin (latest nightly) and I'm struggling to disable an IRQ to allow me to debounce a connected switch. Here what I'm seeing....
=== from machine import Pin
=== import esp
=== esp.osdebug(None)
===
=== def handle_tip(p):
=== print('tip')
===
=== bucket = Pin(0, Pin.IN, Pin.PULL_UP)
=== bucket_irq = bucket.irq(trigger=Pin.IRQ_RISING, handler=handle_tip)
===
>>>
>>> bucket_irq
<IRQ>
>>> bucket_irq.disable()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'IRQ' object has no attribute 'disable'
>>> bucket_irq.disable_irq()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'IRQ' object has no attribute 'disable_irq'
Firstly, is the sane, and additionally is this possible right now? I've seen https://github.com/micropython/micropython/pull/2090 but this seems to control all irqs not an individual. If so is something like this do-able?
def handle_tip(p):
self.state = machine.disable_irq()
print('tip')
tim = Timer(-1)
tim.init(period=DEBOUNCE_TIME, mode=Timer.ONE_SHOT, callback=lambda t:machine.enable_irq(self.state))
-Thanks Max.
Can I enable/disable IRQs?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Can I enable/disable IRQs?
Disabling interrupts is a little drastic and best done for very brief periods of time. Consider the following approach.
Have a timer running continuously (a virtual timer will be fine) with a period of, say, 50ms. The callback function compares the state of the pin with its previous state before saving its current state. If the states differ, it calls the pin change function. Note that the pin change function is called in an interrupt context with its rules about not using the heap. To avoid this have the function set a flag to be tested in the main program loop.
This approach works for most non-pathological switches. If you're interested in the topic of switch debouncing the following link, supplied by @dhylands, is worth a read http://www.ganssle.com/debouncing.htm.
Have a timer running continuously (a virtual timer will be fine) with a period of, say, 50ms. The callback function compares the state of the pin with its previous state before saving its current state. If the states differ, it calls the pin change function. Note that the pin change function is called in an interrupt context with its rules about not using the heap. To avoid this have the function set a flag to be tested in the main program loop.
This approach works for most non-pathological switches. If you're interested in the topic of switch debouncing the following link, supplied by @dhylands, is worth a read http://www.ganssle.com/debouncing.htm.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Can I enable/disable IRQs?
Thanks, thats very helpful, I was trying to think of another way of doing it, If I get something going I'll post a follow up here for future reference.
-Cheers Max.
-Cheers Max.
Re: Can I enable/disable IRQs?
ok, so this seems to do the trick just using a timer.
Thanks!
Code: Select all
def check_switch(p):
global switch_state
global switched
global last_switch_state
switch_state = switch.value()
if switch_state != last_switch_state:
switched = True
last_switch_state = switch_state
switch = Pin(0, Pin.IN, Pin.PULL_UP)
switch_state = switch.value()
last_switch_state = switch_state
switched = False
tim = Timer(-1)
tim.init(period=50, mode=Timer.PERIODIC, callback=check_switch)
while True:
if switched:
if switch_state == 1:
print('tipped')
switched = False
time.sleep_ms(20)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact: