From 1189c8c2671b2463dbca7842a81adfe00c35549c Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Sun, 26 Feb 2017 11:34:01 +1030 Subject: Created a console based version of the RGB controller using Qt. --- qt/console/main.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 qt/console/main.cpp (limited to 'qt/console/main.cpp') diff --git a/qt/console/main.cpp b/qt/console/main.cpp new file mode 100644 index 0000000..ed986ce --- /dev/null +++ b/qt/console/main.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include + +void show_ports(); +bool serial_connect(QString port); +bool serial_disconnect(); +void send(QString com); +void check(); +QString port_name; +QSerialPort serial; +QByteArray data; +QSerialPortInfo s; + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + if (argc < 2) + { + qDebug() << QString("Usage: %1 [port] [commands]").arg(argv[0]); + qDebug() << "Available serial ports."; + show_ports(); + return 0; + } + else + { + if (serial_connect(QString(argv[1]))) + { + for (int x = 0; x < argc - 2; x++) + { + send(QString(argv[x + 2])); + } + QTimer::singleShot(100, check); + } + else + qDebug() << "nope." << argv[2]; + } + return a.exec(); +} + +void check() +{ + if (s.isBusy()) + qDebug() << "An error occured."; + QCoreApplication::quit(); +} + +void show_ports() +{ + for (int x = 0; x < QSerialPortInfo::availablePorts().size(); x++) + { + if (!s.availablePorts().at(x).description().isEmpty()) + qDebug() << QString("Port: %1").arg(QSerialPortInfo::availablePorts().at(x).portName()); + } +} + +bool serial_connect(QString port) +{ + /* this function will attempt a serial connection if we are not already connected */ + if (!serial.isOpen()) + { + serial.setPortName(port); + serial.setBaudRate(QSerialPort::Baud9600); + serial.setDataBits(QSerialPort::Data8); + serial.setParity(QSerialPort::NoParity); + serial.setStopBits(QSerialPort::OneStop); + serial.setFlowControl(QSerialPort::NoFlowControl); + serial.open(QIODevice::ReadWrite); + serial.waitForBytesWritten(9000); + if (serial.isWritable()) { + return true; + } + } + return false; +} + +bool serial_disconnect() +{ + /* this function disconnects from the serial port if it is connected already */ + if (serial.isOpen()) + { + serial.close(); + return true; + } + return false; +} + +void send(QString com) +{ + data = ""; + data.append(com + "\n"); + //qDebug() << "sending:" << data; + if (serial.isOpen()) + { + serial.write(data); + } + else + qDebug() << "Cannot send, device not open."; +} + -- cgit v1.2.3