14 - Interface and Application Programming
This week we will create an interface oriented toward our final project (an animatronic for childhood apraxia). The objective is to establish a link between a Graphical User Interface (GUI) and the animatronic that allows us to control our project in a friendly, intuitive, and easy-to-use way.
qt designer
Before starting, it is important to download " Qt Designer " .
- Widgets. Here we find all the elements that we can add to our main screen (buttons, labels, inputs, etc.).
- Work area. This is the place where we will design our interface. The widgets are placed here.
- Property. Each element that we place in the work area will have properties that can be configured in this section.
- background -color: Color can be expressed in RGB (rgb(R, G, B)) or hexadecimal form (e.g., #12F321).
- color: Font color.
- border-radius: Expressed in px; .
- QPushButton:hover: Changes the button and text color when you hover the mouse pointer over it.
- QPushButton:pressed: Changes the button and text color when the button is clicked.
Python
Once the designs are finished, we will notice that the file has a .ui extension (file.ui), in order to work with it,
it is necessary to convert it into a "file.py". For this, we will use Visual Studio Code.
From the Terminal terminal, we navigate to the folder where our files are located. From there, we follow the steps below.
- python -m venv venv Creates a virtual environment.
- source venv/bin/activate Activates the virtual environment.
- pip install PyQt6 Installs PyQt. PyQt is a set of Python bindings that allow you to use the Qt framework in Python.
PROGRAM STRUCTURE
The first thing we will do is create a new “file.py” in our project folder, where all the Qt-generated programs will be linked. This is done with the purpose of not altering the original design files.
XIAO RP2040
The objective of using the XIAO is to play audio using the " MP3-TF-16P " module. This module has the advantage of including a class D amplifier, allowing to connect a 4Ω and 3W speaker, enough for our needs. Below is a description of the program developed in the XIAO RP2040, which plays the corresponding track when receiving information through the serial port. It should be noted that communication between the module and the XIAO is done via UART.
PROGRAM STRUCTURE
The first thing to do is to declare our libraries.- #include
. Allows you to establish serial communication - #include "DFRobotDFPlayerMini.h" . Allows you to control the DFPlayer Mini / MP3-TF-16P module.
For its part
The while (!mp3.begin(mp3Serial)) instruction allows us to verify if the module is connected. If it is not, it enters a loop until the module works.
On the other hand, in a range from 0 (minimum) to 30 (maximum), mp3.volume(20); allow us to set the module volume to level 20.
Below is a video of how it works.
Results
Finally, we need to connect the interface generated in Python with the audio output using the
XIAO RP2040.
From Python, you need to add serial.Serial()
with the address of the port being used on the computer to communicate; all of this within the
class MainWindow.
Learning outcome
This week we learned how to develop an interface using “Qt” and Python, using a XIAO RP2040 to interact with the outside world.
It's important to consider certain things, such as who the app is intended for, so information from the internet is very useful, as it can
help us come up with design ideas to develop a customized prototype.