<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Core2] Touch event handlers in [MicroPython]?]]></title><description><![CDATA[<p dir="auto">Does Core2 support touch event handlers in MicroPython?</p>
<p dir="auto">I would like to define my own touch interface:</p>
<p dir="auto"><img src="https://imgpile.com/images/7Py5GS.jpg" alt="alt text" class=" img-fluid img-markdown" /></p>
<p dir="auto">So far I only found the M5Btn class, but I can not use it to define transparent touch tiles.</p>
<pre><code>from m5stack import *
from m5stack_ui import *
from uiflow import *

screen = M5Screen()
screen.clean_screen()
screen.set_screen_bg_color(0xFFFFFF)

touch_button0 = M5Btn(text=None, x=0, y=0, w=100, h=80, bg_c=None, text_c=None, font=None, parent=None)

def touch_button0_pressed():
  # global params
  pass
touch_button0.pressed(touch_button0_pressed)
</code></pre>
<p dir="auto"><img src="https://imgpile.com/images/7Pyw4C.png" alt="alt text" class=" img-fluid img-markdown" /></p>
<p dir="auto">I tried to overwrite the button with my touch tiles, but after press the button is shown.</p>
<pre><code>lcd.fillRect(   0,   0, 100, 80, 0xff0000 )
lcd.fillRect( 100,   0, 120, 80, 0x00ff00 )
lcd.fillRect( 220,   0, 100, 80, 0x0000ff )    
lcd.fillRect(   0,  80, 100, 80, 0xaa0000 )
lcd.fillRect( 100,  80, 120, 80, 0x00aa00 )
lcd.fillRect( 220,  80, 100, 80, 0x0000aa )    
lcd.fillRect(   0, 160, 100, 80, 0x550000 )
lcd.fillRect( 100, 160, 120, 80, 0x005500 )
lcd.fillRect( 220, 160, 100, 80, 0x000055 )    
</code></pre>
<p dir="auto"><img src="https://imgpile.com/images/7Pyhml.jpg" alt="alt text" class=" img-fluid img-markdown" /></p>
<p dir="auto">Or do I need to do this manually in a loop?</p>
<pre><code>import time
from m5stack import *


def tile(x,y):
    qx = 0
    qy = 0

    if x&gt;=0 and x&lt;100: qx = 1
    if x&gt;=100 and x&lt;220: qx = 2
    if x&gt;=220 and x&lt;320: qx = 3
    
    if y&gt;=0 and y&lt;80: qy = 0
    if y&gt;=80 and y&lt;160: qy = 3
    if y&gt;=160 and y&lt;240: qy = 6

    return qx + qy


def action(q):
    if q == 0: lcd.clear(0xFFFFFF)
    if q == 1: lcd.fillRect(   0,   0, 100, 80, 0xff0000 )
    if q == 2: lcd.fillRect( 100,   0, 120, 80, 0x00ff00 )
    if q == 3: lcd.fillRect( 220,   0, 100, 80, 0x0000ff )    
    if q == 4: lcd.fillRect(   0,  80, 100, 80, 0xaa0000 )
    if q == 5: lcd.fillRect( 100,  80, 120, 80, 0x00aa00 )
    if q == 6: lcd.fillRect( 220,  80, 100, 80, 0x0000aa )    
    if q == 7: lcd.fillRect(   0, 160, 100, 80, 0x550000 )
    if q == 8: lcd.fillRect( 100, 160, 120, 80, 0x005500 )
    if q == 9: lcd.fillRect( 220, 160, 100, 80, 0x000055 )    


def loop():
    x = -1
    y = -1
    while True:
        if touch.status() == True:
            t = touch.read()
            tx = t[0]
            ty = t[1]
            if x != tx and y != ty:
                x = tx
                y = ty
                q = tile(x, y)
                action(q);
        else:
            x = -1
            y = -1
            action(0)

        time.sleep(0.05)


loop()
</code></pre>
]]></description><link>https://community.m5stack.com/topic/2910/core2-touch-event-handlers-in-micropython</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 06:48:09 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/2910.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Feb 2021 10:29:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Core2] Touch event handlers in [MicroPython]? on Sun, 21 Mar 2021 16:13:25 GMT]]></title><description><![CDATA[<p dir="auto">hello,<br />
i use the nice lvgl Lib to do That.</p>
<ol>
<li>create personalized button</li>
<li>add event on the button</li>
</ol>
<p dir="auto">example :</p>
<pre><code>from m5stack import *
from m5stack_ui import *
from uiflow import *
import lvgl as lv

screen = M5Screen()
screen.clean_screen()
screen.set_screen_bg_color(0x00000)

lv.init()

# create screen
scr = lv.obj() 
# set Background screen 
scr.set_style_local_bg_color(scr.PART.MAIN, lv.STATE.DEFAULT, lv.color_hex(0x000000))

# create button
btn = lv.btn(scr) 
btn.set_size(80, 80)
# change style
btn.set_style_local_bg_color(scr.PART.MAIN,lv.STATE.DEFAULT, lv.color_hex(0x0000ff))
styleButton = lv.style_t() # create style
styleButton.set_bg_color(lv.STATE.DEFAULT, lv.color_hex(0x0000ff))
styleButton.set_radius(lv.STATE.DEFAULT, 0);
styleButton.set_border_color(lv.STATE.DEFAULT, lv.color_hex(0x0000ff))
btn.add_style(btn.PART.MAIN,styleButton) #define this style

# callback action
def action(btn, event):
  global src
  if(event == lv.EVENT.CLICKED):
    btn.set_style_local_bg_color(btn.PART.MAIN, lv.STATE.DEFAULT, lv.color_hex(0xffffff))
    btn.set_style_local_border_color(btn.PART.MAIN, lv.STATE.DEFAULT, lv.color_hex(0xffffff))
    
# define callback  
btn.set_event_cb(action)

# load the screen
lv.scr_load(scr)

</code></pre>
<p dir="auto">best regards<br />
Thomas</p>
]]></description><link>https://community.m5stack.com/post/13174</link><guid isPermaLink="true">https://community.m5stack.com/post/13174</guid><dc:creator><![CDATA[favnec5]]></dc:creator><pubDate>Sun, 21 Mar 2021 16:13:25 GMT</pubDate></item><item><title><![CDATA[Reply to [Core2] Touch event handlers in [MicroPython]? on Sat, 27 Feb 2021 19:43:41 GMT]]></title><description><![CDATA[<p dir="auto">Hello<br />
Sorry if this is not helping (i am <em>new</em> to m5stack),<br />
isn't faq #56 close to this issue ?</p>
<ul>
<li>Q56: I used UiFlow to write the Core2 drawing board program. Why did the drawing trace disappear after releasing the hand?<br />
Core2 has used LVGL and normal GUI drawing so far. Your drawing may use normal GUI API, but touch uses LVGL API, so LVGL touch triggers redraw and will overwrite the previous drawing traces. Use * <em>Advanced</em>*-&gt;<strong>Additional code</strong> Add the code "lv.obj.set_click(lv.scr_act(), False)" in the module to close the redraw event</li>
</ul>
<p dir="auto">Can i ask if you found Python documentation for M5Btn object ?</p>
<p dir="auto">Thank you</p>
]]></description><link>https://community.m5stack.com/post/12708</link><guid isPermaLink="true">https://community.m5stack.com/post/12708</guid><dc:creator><![CDATA[vince94]]></dc:creator><pubDate>Sat, 27 Feb 2021 19:43:41 GMT</pubDate></item></channel></rss>