#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
widget = new QWidget();
grid = new QGridLayout();
button_show = new QPushButton("Show");
button_hide = new QPushButton("Hide");
button_blink = new QPushButton("Blink");
button_blink_n = new QPushButton("Blink 0");
editLine = new QLineEdit("0");
/* connect signals to slots */
connect(editLine, SIGNAL(textChanged(QString)), this, SLOT(setButtonBlinkN(QString)));
connect(button_show, SIGNAL(clicked()), this, SLOT(ShowLigth()));
connect(button_hide, SIGNAL(clicked()), this, SLOT(HideLigth()));
connect(button_blink, SIGNAL(clicked()), this, SLOT(Blink()));
connect(button_blink_n, SIGNAL(clicked()), this, SLOT(BlinkN()));
/* only 0..9 number */
editLine->setInputMask("9");
grid->addWidget(button_show, 0, 0);
grid->addWidget(button_hide, 0, 1);
grid->addWidget(button_blink, 0, 2);
grid->addWidget(editLine, 0, 3);
grid->addWidget(button_blink_n, 0, 4);
widget->setLayout(grid);
setCentralWidget(widget);
/* init QByteArray */
ba.clear();
ba.resize(1);
if(initSerial(tr("/dev/ttyUSB0"), 9600) == false)
{
qDebug() << "Error initSerial";
exit(-1);
}
}
void MainWindow::setButtonBlinkN(QString n)
{
button_blink_n->setText("Blink " + n);
}
/* init serial port, set baud rate */
bool MainWindow::initSerial(QString dev, int baudRate)
{
serial = new AbstractSerial();
serial->setDeviceName(dev);
/* open or return false */
if(serial->open(AbstractSerial::ReadWrite))
{
serial->setBaudRate(baudRate);
/* if ready read call ReadData() */
connect(serial, SIGNAL(readyRead()), this, SLOT(ReadData()));
}
else
{
return false;
}
return true;
}
/* Send data */
void MainWindow::send(QByteArray &ba)
{
serial->write(ba);
}
/* Not light
*/
void MainWindow::ShowLigth()
{
ba[0] = 'S';
send(ba);
}
void MainWindow::HideLigth()
{
ba[0] = 'H';
send(ba);
}
void MainWindow::Blink()
{
ba[0] = 'B';
send(ba);
}
void MainWindow::BlinkN()
{
ba[0] = 'N';
send(ba);
ba[0] = (char)editLine->text().toLocal8Bit().at(0);
send(ba);
}
/* Read data */
void MainWindow::ReadData()
{
qDebug() << serial->readAll();
}
MainWindow::~MainWindow()
{
serial->close();
delete serial;
delete widget;
delete grid;
delete button_show;
delete button_hide;
delete button_blink;
delete button_blink_n;
delete editLine;
}