2 Button and the click event

2.1 experimental goals

In this lesson, you will learn how to draw buttons and click buttons.

The reference code path for this experiment :CanMV\04-GUI\button.py

 

2.2 experimental procedure

The factory firmware of the module has been integrated with the lvgl graphical library. If you have downloaded other firmware, please burn it back to the factory firmware before experimenting.

  1. Import the relevant libraries.

  1. Initialize the lcd, touchpad, and lvgl.

  1. Connect K210's screen driver to lvgl's display registration interface.

  1. Connect the K210 touchpad driver pair to the lvgl input registration interface.

  1. Create a new Button and display the name "Button".

  1. Button presses are handled as follows: Each time a button press is detected, btn_count is automatically incremented and displayed on the button.

  1. Since the images of lvgl need to be updated in real time, it is necessary to refresh the tasks of lvgl every 5 ms.

 

2.3 experimental results

Connect the K210 module to the computer through the microUSB data cable, CanMV IDE click the connect button, after the connection is completed click the Run button to run the routine code. You can also download the code as main.py and run it in the K210 module.

You can see a blue "Button" in the middle of the LCD display, and each time the button is touched, the number on the button will automatically increase by 1.

image-20230214153723835

image-20230214153746876

If you need to run the program again, reconnect to avoid issues with buttons that add a number overlay.

 

2.4 experiment summary

Using CanMV IDE, with MicroPython syntax written in the factory firmware, it is very convenient to draw buttons and handle button touch events. Button touch event handling is best done without printing data, but it is included here for illustrative purposes only.