Page 1 of 1

PIN - Interup Handler - return Value

Posted: Wed Dec 04, 2019 11:47 am
by Jerremy09
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.
------------------------------------------------------------------------------------------------------------------------------------------------------

Re: PIN - Interup Handler - return Value

Posted: Wed Dec 04, 2019 3:44 pm
by Roberthh
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.

Re: PIN - Interup Handler - return Value

Posted: Thu Dec 05, 2019 9:04 am
by Jerremy09
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

Re: PIN - Interup Handler - return Value

Posted: Thu Dec 05, 2019 9:47 am
by pythoncoder
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.