reading of events on gpio pins for switch

Moderators: BertB, Voyager, grovkillen, Stuntteam, LisaM

Post Reply
Normal user
Posts: 5
Joined: 20 Aug 2018, 16:43

reading of events on gpio pins for switch

#1 Post by android12 » 26 Sep 2018, 22:46


I was searching in source code of uPyEasy in branch "master" for a place, when interrupt (irq / external even listener / isr ... however you call it) is configured on GPIO, if such GPIO is configured for switch.
However i didn't find any. I only found with these lines:
loop = asyncio.get_event_loop()
loop.create_task(self.switchcheck()) # Thread runs forever

Did i overlook it, or is there really no interrupt registered and only relying on fact, that one of these reads will detect pressed state (specially if i have push button)? Cannot it then happen, that button press was in between of two checks and hence not detected at all?

How often is event loop running? Isn't its run actually dealyed, if i do network communication?

thank you
best regards

Normal user
Posts: 513
Joined: 11 Apr 2017, 18:29

Re: reading of events on gpio pins for switch

#2 Post by LisaM » 07 Oct 2018, 21:28

It's interrupt based, but not as you're used to. Using the right libraries there is no need to program any interrupt routine.

Just add this to the init function of the plugin (for a push button):

Code: Select all

	from aswitch import Switch, Pushbutton
        from machine import Pin
        # init  button
        pbpin = Pin(0, Pin.IN, Pin.PULL_UP)
        pb = Pushbutton(pbpin)
The available push button functions: press_func, release_func, double_func and long_func.
The function names explain their own function enough.

The togglebutton function (add in the custom plugin section):

Code: Select all

    def togglebutton(self):
         # plugin specific section
        self._log.debug("Plugin: toggle button")
	# do something... :-)
And presto, every time you push the pushbutton, the togglebutton function will be called instantly. The button library together with the async library and MicroPython does all the work for you. No need to program your own interrupt function, everything is already in place (the libraries together with Micropython will do all the interrupting for you).
The button check function is running forever in it's own loop and will be called asynchronously every milliseconds or so, any pushbutton action (including push contact) will take a lot longer then that. In fact it's so fast it has a bounce function, when you push the button it might bounce a few times on the contact giving false push events. Therefor it waits for 50 ms for accepting new push contacts after the initial push contact, so that no false push events are generated...

How i know this works? I'm using this exact code in my top-secret project... ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests