[Core2] Touch event handlers in [MicroPython]?



  • Does Core2 support touch event handlers in MicroPython?

    I would like to define my own touch interface:

    alt text

    So far I only found the M5Btn class, but I can not use it to define transparent touch tiles.

    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)
    

    alt text

    I tried to overwrite the button with my touch tiles, but after press the button is shown.

    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 )    
    

    alt text

    Or do I need to do this manually in a loop?

    import time
    from m5stack import *
    
    
    def tile(x,y):
        qx = 0
        qy = 0
    
        if x>=0 and x<100: qx = 1
        if x>=100 and x<220: qx = 2
        if x>=220 and x<320: qx = 3
        
        if y>=0 and y<80: qy = 0
        if y>=80 and y<160: qy = 3
        if y>=160 and y<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()
    


  • Hello
    Sorry if this is not helping (i am new to m5stack),
    isn't faq #56 close to this issue ?

    • Q56: I used UiFlow to write the Core2 drawing board program. Why did the drawing trace disappear after releasing the hand?
      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 * Advanced*->Additional code Add the code "lv.obj.set_click(lv.scr_act(), False)" in the module to close the redraw event

    Can i ask if you found Python documentation for M5Btn object ?

    Thank you



  • hello,
    i use the nice lvgl Lib to do That.

    1. create personalized button
    2. add event on the button

    example :

    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)
    
    

    best regards
    Thomas