PIN - Interup Handler - return Value

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Jerremy09
Posts: 11
Joined: Wed May 08, 2019 7:40 am

PIN - Interup Handler - return Value

Post by Jerremy09 » Wed Dec 04, 2019 11:47 am

Helllo,

I would like to ask if this is even possbble. I have Lolin 2.4 TFT display connected to PIN (0) and I use it as Interup handler in the case of touch (XPT2046 modul). Once Micropython receive this interup it runs define Function. In that function I have a code to read a touch coordinates and base on these coordinates I call another Module to "Open new page" (use ILI9341). Problem is that I run Interup handled on define page. This page is also value I need to retunr back from define Module into Function (this is correct - this works), but I cannot return value from define Function into Main program (always return "<IRQ>"):

Questions:
1) is there something wrong?
2) Is it even possible to return value of actual "page" from function which is triggered by IRQ?

Here is my program:
boot.py
------------------------------------------------------------------------------------------------------------------------------------------------------
import sys
sys.path.insert(1, '/Display')

import _1_Home_Page_mod
_1_Home_Page_mod._1_Home_Page()

page = 1
------------------------------------------------------------------------------------------------------------------------------------------------------

Main.py:
------------------------------------------------------------------------------------------------------------------------------------------------------
def touch_ON(page):
ts_cs.value(0)
stisk = ts_touch.get_touch(initial=False, wait=True, raw=True, timeout=3000)
ts_cs.value(1)
ts_spi.deinit()
import Find_Page_mod
actual_page = Find_Page_mod._1_Find_Page(stisk,page)
return actual_page

#Setup
import machine
import xpt2046
import time
import gc

ts_spi = machine.SPI(1, baudrate=1000000, miso=machine.Pin(19), mosi=machine.Pin(23), sck=machine.Pin(18))
ts_touch = xpt2046.XPT2046(spi = ts_spi, confidence=5, margin=50, delay=10)
ts_cs = machine.Pin(12, machine.Pin.OUT)
ts_bussy = machine.Pin(0, machine.Pin.IN)

page = ts_bussy.irq(touch_ON(page),trigger=machine.Pin.IRQ_FALLING)

gc.collect()
------------------------------------------------------------------------------------------------------------------------------------------------------

Find_Page_Mod:
------------------------------------------------------------------------------------------------------------------------------------------------------
def _1_Find_Page(coordinates, page1):
import sys
sys.path.insert(1, '/Display')

#/-----------------------------Home Page-----------------------------/#
if page1 == 1:
#Menu
if ((300<coordinates[0]<1100)and(350<coordinates[1]<850)):
import _2_Main_Menu_mod
_2_Main_Menu_mod._2_Main_Menu()
return 2

#/-----------------------------Main Menu-----------------------------/#
if page1 == 2:
#Back
if ((230<coordinates[0]<930)and(350<coordinates[1]<850)):
import _1_Home_Page_mod
_1_Home_Page_mod._1_Home_Page()
return 1
...... - there is more lines I just dont want to add all of them here.
------------------------------------------------------------------------------------------------------------------------------------------------------

User avatar
Roberthh
Posts: 1744
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: PIN - Interup Handler - return Value

Post by Roberthh » Wed Dec 04, 2019 3:44 pm

You cannot return a value form an Interrupt or callback function, because it runs asynchrously. What you can do is to set global variables, which are visible both in the main code and our interrupt service. You have to define these as global in the interrupt function.

Jerremy09
Posts: 11
Joined: Wed May 08, 2019 7:40 am

Re: PIN - Interup Handler - return Value

Post by Jerremy09 » Thu Dec 05, 2019 9:04 am

Hi Roberthh,

thank you for answer. I dont know if I can use it because that variable is already used in boot.py, then reuse in main.py (there cannot be definition just use) so I dont know if I can provide this variable into sub-Function called by IRQ.

Nevertheless I think I found solution. I created Actual_Page.txt file where I write current page after displaying it and I use the page into local variable to see current page inside IRQ.

I realize, that may be a little bit more time consuming, but it is easier for me to implement.

Thank you again

Jan

User avatar
pythoncoder
Posts: 3720
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PIN - Interup Handler - return Value

Post by pythoncoder » Thu Dec 05, 2019 9:47 am

An alternative to globals is for the interrupt handler to be a bound method of a class. The handler can then assign values to bound variables.
Peter Hinch

Post Reply