Interface and Application Programming
On this week assigment we have to write and application that interfaces a user with and input and/or output device on a board.
QT DESIGNER
Qt designer is a Qt tool that provides you with a what-you-see-is-what-you-get (WYSIWYG) user interface to create GUIs for your PyQt applications productively and efficiently. With this tool, you can create GUIs and it is used for mobile, desktop and embedded systems. It is used with drag and drop design with buttons, sliders, text, lables, etc.
The first think that appears when you open the software. Select Main Window and create it. In this case we do not specify the size because we can modify it later.
Now that we created our file we will see a lot of items for our interface. To use them we just need to drag them into the canva.
What I wanted to create was an interface where you can write a sentence and it will translate into a morse code and, it will "teach" you morse code by showing each letter and how you can do it in morse. It will also contain an slider to change the speed of how fast the letters\ change and the LED status. The input will be your keaboard and the output will be a LED on a board.
Coding
Before we start coding first we have to turn our .ui into a python file. To do that we go into our cmd and first opened the folder you have your interface and install PyQt6 y pyserial.
pip install PyQt6 pyserial
The convert you .ui file into a .py file. To do that we use the command pyuic6
pyuic6 -x interfaz.ui -o interfaz.py
To see how your interface looks, exxecute:
python interfaz.py
We have to made another file inside the same folder called app.py and paste the following code on it.
from interface import *
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
# from here write you code logic
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec()
To execute your application use the following command.
python app.py
App Code
Once you have everything is set, I started making the logic of my code. I you can sea I try to replicate the morse traductor I did on week 6.
from Interface import *
from PyQt6 import QtTest
import serial
speed=1000
slider_speed = 0
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def _init_(self, *args, **kwargs):
QtWidgets.QMainWindow._init_(self, *args, **kwargs)
self.setupUi(self)
self.serial = serial.Serial('COM4',9600)
self.lbl_letter.setText(" ")
self.lbl_morse.setText(" ")
self.lbl_speednumber.setText("1000 ms")
self.btn_start.clicked.connect(self.start)
self.Sld_speed.sliderMoved.connect(self.slider)
def dot(self):
print("dot")
self.Sld_speed.sliderMoved.connect(self.slider)
self.LED_ON()
QtTest.QTest.qWait(speed)
self.LED_OFF()
QtTest.QTest.qWait(speed)
def line(self):
print("line")
self.Sld_speed.sliderMoved.connect(self.slider)
self.LED_ON()
QtTest.QTest.qWait(speed * 2)
self.LED_OFF()
QtTest.QTest.qWait(speed)
def LED_ON(self):
self.lbl_LED.setText("ON")
self.lbl_LED.setStyleSheet('font: 81 12pt "Rockwell Extra Bold"; background-color: red;border-radius:30%')
self.serial.write(b'1')
def LED_OFF(self):
self.lbl_LED.setText("OFF")
self.lbl_LED.setStyleSheet('font: 81 12pt "Rockwell Extra Bold"; background-color: rgb(182, 182, 182);border-radius:30%')
self.serial.write(b'0')
def slider(self):
slider_speed = self.Sld_speed.value()
speed = (((900/19)*slider_speed)+(1900/19))
self.lbl_speednumber.setText(str(int(speed)) + "ms")
print(slider_speed)
print(speed)
def start(self):
sentence = self.TxtEdit_Sentence.toPlainText()
print(sentence)
print("Button Start pressed")
for character in sentence:
if character == "A" or character == "a": #Letter num 1 - A
print("A = . _") #Prints the letter and the morse code
self.lbl_letter.setText("A")
self.lbl_morse.setText(". _")
self.dot()
self.line()
elif character == "B" or character == "b": #Letter num 2 - B
print("B = _ . . . ") #Prints the letter and the morse code
self.lbl_letter.setText("B")
self.lbl_morse.setText("_ . . .")
self.line()
self.dot()
self.dot()
self.dot()
elif character == "C" or character == "c": #Letter num 3 - C
print("C = _ . _ . ")
self.lbl_letter.setText("C")
self.lbl_morse.setText("_ . _ .")
self.line()
self.dot()
self.line()
self.dot()
elif character == "D" or character == "d": #Letter num 4 - D
print("D = _ . . ")
self.lbl_letter.setText("D")
self.lbl_morse.setText("_ . .")
self.line()
self.dot()
self.dot()
elif character == "E" or character == "e": #Letter num 5 - E
print("E = . ")
self.lbl_letter.setText("E")
self.lbl_morse.setText(".")
self.dot()
elif character == "F" or character == "f": #Letter num 6 - F
print("F = . . _ . ")
self.lbl_letter.setText("F")
self.lbl_morse.setText(". . _ .")
self.dot()
self.dot()
self.line()
self.dot()
elif character == "G" or character == "g": #Letter num 7 - G
print("G = _ _ . ")
self.lbl_letter.setText("G")
self.lbl_morse.setText("_ _ .")
self.line()
self.line()
self.dot()
elif character == "H" or character == "h": #Letter num 8 - H
print("H = . . . . ")
self.lbl_letter.setText("H")
self.lbl_morse.setText(". . . .")
self.dot()
self.dot()
self.dot()
self.dot()
elif character == "I" or character == "i": #Letter num 9 - I
print("I = . . ")
self.lbl_letter.setText("I")
self.lbl_morse.setText(". .")
self.dot()
self.dot()
elif character == "J" or character == "j": #Letter num 10 - J
print("J = . - - - ")
self.lbl_letter.setText("G")
self.lbl_morse.setText(". _ _ _")
self.dot()
self.line()
self.line()
self.line()
elif character == "K" or character == "k": #Letter num 11 - K
print("K = - . - ")
self.lbl_letter.setText("K")
self.lbl_morse.setText("_ . _")
self.line()
self.dot()
self.line()
elif character == "L" or character == "l": #Letter num 12 - L
print("L = . - . . ")
self.lbl_letter.setText("L")
self.lbl_morse.setText(". _ . .")
self.dot()
self.line()
self.dot()
self.dot()
elif character == "M" or character == "m": #Letter num 13 - M
print("M = - - ")
self.lbl_letter.setText("M")
self.lbl_morse.setText("_ _")
self.line()
self.line()
elif character == "N" or character == "n": #Letter num 14 - N
print("N = - . ")
self.lbl_letter.setText("N")
self.lbl_morse.setText("_ .")
self.line()
self.dot()
elif character == "O" or character == "o": #Letter num 15 - O
print("O = - - - ")
self.lbl_letter.setText("O")
self.lbl_morse.setText("_ _ _ _")
self.line()
self.line()
self.line()
elif character == "P" or character == "p": #Letter num 16 - P
print("P = . - - . ")
self.lbl_letter.setText("P")
self.lbl_morse.setText(". _ _ .")
self.dot()
self.line()
self.line()
self.dot()
elif character == "Q" or character == "p": #Letter num 17 - Q
print("Q = - - . - ")
self.lbl_letter.setText("Q")
self.lbl_morse.setText("_ _ . _")
self.line()
self.line()
self.dot()
self.line()
elif character == "R" or character == "r": #Letter num 18 - R
print("R = . - . ")
self.lbl_letter.setText("R")
self.lbl_morse.setText(". _ .")
self.line()
self.dot()
self.line()
elif character == "S" or character == "s": #Letter num 19 - S
print("S = . . . ")
self.lbl_letter.setText("S")
self.lbl_morse.setText(". . .")
self.dot()
self.dot()
self.dot()
elif character == "T" or character == "t": #Letter num 20 - T
print("T = - ")
self.lbl_letter.setText("T")
self.lbl_morse.setText("_")
self.line()
elif character == "U" or character == "u": #Letter num 21 - U
print("U = . . - ")
self.lbl_letter.setText("U")
self.lbl_morse.setText(". . _")
self.dot()
self.dot()
self.line()
elif character == "V" or character == "v": #Letter num 22 - V
print("V = . . . - ")
self.lbl_letter.setText("V")
self.lbl_morse.setText(". . . _")
self.dot()
self.dot()
self.dot()
self.line()
elif character == "W" or character == "w": #Letter num 23 - W
print("W = . - - ")
self.lbl_letter.setText("W")
self.lbl_morse.setText(". _ _")
self.dot()
self.line()
self.line()
elif character == "X" or character == "x": #Letter num 24 - X
print("X = - . . - ")
self.lbl_letter.setText("X")
self.lbl_morse.setText("_ . . _")
self.line()
self.dot()
self.dot()
self.line()
elif character == "Y" or character == "y": #Letter num 25 - Y
print("Y = - . - - ")
self.lbl_letter.setText("Y")
self.lbl_morse.setText("_ _ . _ _")
self.line()
self.dot()
self.line()
self.line()
elif character == "Z" or character == "z": #Letter num 26 - Z
print("Z = - - . . ")
self.lbl_letter.setText("Z")
self.lbl_morse.setText("_ _ . .")
self.line()
self.line()
self.dot()
self.dot()
#Special Characters
elif character == " ": #Space
print("Space")
self.lbl_letter.setText(" ")
self.lbl_morse.setText("SPACE")
QtTest.QTest.qWait(speed)
self.TxtEdit_Sentence.clear()
self.lbl_letter.setText(" ")
self.lbl_morse.setText(" ")
if _name_ == '_main_':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec()
Xiao RP2040
The rp2040 helped me to controlled the led that I wanted to used. This is because Python can not be run on hardware so this works as a communication with the app.
void setup() {
// put your setup code here, to run once:
pinMode(D6, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0){
char dato = Serial.read();
if (dato == '1'){
digitalWrite(D6, HIGH);
}
else if (dato == '0'){
digitalWrite(D6, LOW);
}
}
}
Result
Here is the result of both codes working together. On the left you can se the app where you write the sentence you want to transale and on the right is the XIAO with the LED that makes the morse code.