This weeks assigenment was very interessting, because i can use it often on my work or hobby-projects:
Build a GUI to interface with your microcontroller!
A few years ago i made a GUI with LabView, very straight forward, but not a c or c++ programming. It´s more a graphical and mathematical solution.
So this time i wanted to learn something new and choosed QT.
Basicly QT is a framework written in C++ for generating and interfacing a GUI. It is Open-Source and available
for almost every system for free. You can even build iOS apps with it.
You CAN programe QT in a text-editor if you like, but i used the "QT-Creator" IDE. The IDE is also free
and very comfortable. Especialy you can drag and drop your "Widgets" (your graphics and buttons) on the place you want it to be.
This is much easier than program the coordinates of the widgets by Hand.
You can find everything you need on www.qt.io
If you generate a new project, you see on the left side five important files:
void MainWindow::serial_connect()
{
serial->setPortName(ui->port_box->currentText());
serial->setBaudRate(QSerialPort::Baud115200);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
// serial->write("Hello World!\n");
}
void MainWindow::serial_rescan()
{
ui->port_box->clear();
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
ui->port_box->addItem(serialPortInfo.portName());
}
}
void MainWindow::slider_changed()
{
QString buffer;
buffer.sprintf("%03i;%03i;%03i\n",ui->slider_1->value(),ui->slider_2->value(),ui->slider_3->value());
serial->write( buffer.toStdString().c_str(), buffer.size() );
serial->QSerialPort::waitForBytesWritten(-1);
ui->lcd_1->display(ui->slider_1->value());
ui->lcd_2->display(ui->slider_2->value());
ui->lcd_3->display(ui->slider_3->value());
}
void MainWindow::serial_received()
{
QString received;
while (serial->canReadLine()){
received = serial->readLine(); //reads in data line by line, separated by \n or \r characters
}
QStringList splitted = received.split(";");
if (splitted.value(0) == "A"){
ui->adc_1->display(splitted.value(1));
ui->adc_2->display(splitted.value(2));
ui->adc_3->display(splitted.value(3));
}
}
{
ui->setupUi(this);
serial = new QSerialPort(this);
connect(ui->rescan_Button, SIGNAL(clicked()), this, SLOT(serial_rescan()));
connect(ui->connect_button, SIGNAL(clicked()), this, SLOT (serial_connect()));
connect(ui->slider_1, SIGNAL(valueChanged(int)), this, SLOT(slider_changed()));
connect(ui->slider_2, SIGNAL(valueChanged(int)), this, SLOT(slider_changed()));
connect(ui->slider_3, SIGNAL(valueChanged(int)), this, SLOT(slider_changed()));
connect(serial,SIGNAL(readyRead()),this,SLOT(serial_received()));
serial_rescan();
}