From 410fbd473ad039dc815bc450f3b26732b13ac4dc Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Mon, 30 May 2016 10:08:26 +0930 Subject: First code commit --- qt/RGBController/RGBController.pro | 22 ++ qt/RGBController/RGBController.pro.user | 336 ++++++++++++++++++++++++++++++ qt/RGBController/controllerwindow.cpp | 255 +++++++++++++++++++++++ qt/RGBController/controllerwindow.h | 75 +++++++ qt/RGBController/controllerwindow.ui | 210 +++++++++++++++++++ qt/RGBController/main.cpp | 11 + qt/RGBController/serial_communication.cpp | 47 +++++ qt/RGBController/serial_communication.h | 23 ++ 8 files changed, 979 insertions(+) create mode 100755 qt/RGBController/RGBController.pro create mode 100755 qt/RGBController/RGBController.pro.user create mode 100755 qt/RGBController/controllerwindow.cpp create mode 100755 qt/RGBController/controllerwindow.h create mode 100755 qt/RGBController/controllerwindow.ui create mode 100755 qt/RGBController/main.cpp create mode 100644 qt/RGBController/serial_communication.cpp create mode 100644 qt/RGBController/serial_communication.h (limited to 'qt/RGBController') diff --git a/qt/RGBController/RGBController.pro b/qt/RGBController/RGBController.pro new file mode 100755 index 0000000..d075466 --- /dev/null +++ b/qt/RGBController/RGBController.pro @@ -0,0 +1,22 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-07T11:25:55 +# +#------------------------------------------------- + +QT += core gui serialport network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = RGBController +TEMPLATE = app + + +SOURCES += main.cpp\ + controllerwindow.cpp \ + serial_communication.cpp + +HEADERS += controllerwindow.h \ + serial_communication.h + +FORMS += controllerwindow.ui diff --git a/qt/RGBController/RGBController.pro.user b/qt/RGBController/RGBController.pro.user new file mode 100755 index 0000000..1b89dcd --- /dev/null +++ b/qt/RGBController/RGBController.pro.user @@ -0,0 +1,336 @@ + + + + + + EnvironmentId + {c148e485-3cf0-4847-b5fd-56246b2688e8} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + {a6071b8b-a567-46c9-babb-33312c5773b5} + 0 + 0 + 0 + + /home/daniel_j/documents/school/2016 research project/RGBController/qt/build/degub + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/daniel_j/documents/school/research_project/2016 research project/RGBController/qt/build-RGBController-Desktop-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + /home/daniel_j/documents/school/research_project/2016 research project/RGBController/qt/build-RGBController-Desktop-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + false + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + -w + -r + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy locally + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + RGBController + RGBController2 + Qt4ProjectManager.Qt4RunConfiguration:/home/daniel_j/documents/school/2016 research project/RGBController/qt/RGBController/RGBController.pro + true + + RGBController.pro + false + + /home/daniel_j/documents/school/2016 research project/RGBController/qt/build/degub + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/qt/RGBController/controllerwindow.cpp b/qt/RGBController/controllerwindow.cpp new file mode 100755 index 0000000..302e3d2 --- /dev/null +++ b/qt/RGBController/controllerwindow.cpp @@ -0,0 +1,255 @@ +#include "controllerwindow.h" + +controllerWindow::controllerWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::controllerWindow) +{ + /* this is our setup function, we set things up here */ + ui->setupUi(this); + info_log("RGB Controller started"); + /* disable buttons and widgets that should not be enabled yet, set slider values to 0 */ + ui->disconnect_button->setEnabled(false); + ui->connect_button->setEnabled(false); + ui->red_button->setEnabled(false); + ui->green_button->setEnabled(false); + ui->blue_button->setEnabled(false); + ui->off_button->setEnabled(false); + ui->reload_preset_button->setEnabled(false); + ui->set_preset_button->setEnabled(false); + ui->presets_dropdown->setEnabled(false); + ui->r_slider->setEnabled(false); + ui->g_slider->setEnabled(false); + ui->b_slider->setEnabled(false); + ui->r_slider->setValue(0); + ui->g_slider->setValue(0); + ui->b_slider->setValue(0); + /* set some crucial ints */ + preset_index = 0, r = 0, g = 0, b = 0; + /* populate our serial port dropdown box */ + populate_serial_list(); + /* load our presets file */ + load_presets(); +} + +controllerWindow::~controllerWindow() +{ + /* this function will end our program and clean up */ + delete ui; +} + +void controllerWindow::info_log(QString text) +{ + /* this function is used to append logging information to our text area */ + ui->info_log_textarea->append(text); +} + +void controllerWindow::populate_serial_list() +{ + /* + * things to be done here: + * - clear anything inside our serial dropdown box + * - re-populate the serial dropdown box + */ + + /* remove anything in the list */ + ui->arduino_port_dropdown->clear(); + /* populate the list */ + for (int x = 0; x < QSerialPortInfo::availablePorts().size(); x++) + { + if (!QSerialPortInfo::availablePorts().at(x).description().isEmpty()) + ui->arduino_port_dropdown->addItem(QSerialPortInfo::availablePorts().at(x).portName()); + } + if (!ui->arduino_port_dropdown->itemText(0).isEmpty()) + { + ui->connect_button->setEnabled(true); + info_log("Serial port list populated"); + } else + { + info_log("No serial port found!"); + ui->arduino_port_dropdown->addItem("None"); + } +} + +void controllerWindow::load_presets() +{ + /* + * this function will load presets from file and put them into a drop down box + * How i plan for it to work: + * -> load file + * -> parse line by line + * -> split line at delimeter + * -> add [0] to dropdown (name), add [1] to an array that stores all the values + */ + QFile inputFile("presets.txt"); + if (inputFile.open(QIODevice::ReadOnly)) + { + QTextStream in(&inputFile); + while (!in.atEnd()) + { + QString line = in.readLine(); + if (line.contains("=")) + { + ui->presets_dropdown->addItem(line.split("=")[0]); + presets.append(line.split("=")[1]); + } + } + inputFile.close(); + info_log("Presets loaded"); + } else + info_log("Presets file not found."); +} + +void controllerWindow::serial_rgb_change(int r, int g, int b) +{ + portf.rgb_change(r, g, b); +} + + +/* + * + * button and widget functions below + * + */ + +void controllerWindow::on_connect_button_clicked() +{ + /* + * here we need to call our serial port connect function + * if our connection is sucessful we need to enable and disable a few buttons and widgets + * we must also change our rgb sliders etc + */ + port = ui->arduino_port_dropdown->currentText(); + if (portf.serial_connect(port)) + { // if true we are connected + info_log("Connection established"); + ui->arduino_status_label->setText("Connected"); + /* now we need to disabled and enable some buttons and objects */ + ui->disconnect_button->setEnabled(true); + ui->connect_button->setEnabled(false); + ui->red_button->setEnabled(true); + ui->green_button->setEnabled(true); + ui->blue_button->setEnabled(true); + ui->off_button->setEnabled(true); + ui->reload_preset_button->setEnabled(true); + ui->set_preset_button->setEnabled(true); + ui->presets_dropdown->setEnabled(true); + ui->r_slider->setEnabled(true); + ui->g_slider->setEnabled(true); + ui->b_slider->setEnabled(true); + ui->refresh_port_button->setEnabled(false); + ui->arduino_port_dropdown->setEnabled(false); + } else + { + info_log("Unable to connect (port in use?)"); + } + +} + +void controllerWindow::on_disconnect_button_clicked() +{ + /* + * here we will attempt to disconnect from our serial port + * we will also enable/disable some buttons and widgets and set slider values to 0 + */ + if (portf.serial_disconnect()) + { + info_log("Disconnected from serial port"); + ui->arduino_status_label->setText("Disconnected"); + ui->disconnect_button->setEnabled(false); + ui->connect_button->setEnabled(true); + ui->red_button->setEnabled(false); + ui->green_button->setEnabled(false); + ui->blue_button->setEnabled(false); + ui->off_button->setEnabled(false); + ui->reload_preset_button->setEnabled(false); + ui->set_preset_button->setEnabled(false); + ui->presets_dropdown->setEnabled(false); + ui->refresh_port_button->setEnabled(true); + ui->arduino_port_dropdown->setEnabled(true); + ui->r_slider->setEnabled(false); + ui->g_slider->setEnabled(false); + ui->b_slider->setEnabled(false); + ui->r_slider->setValue(0); + ui->g_slider->setValue(0); + ui->b_slider->setValue(0); + + } else + { + /* I have no clue how we'd get here */ + info_log("Cannot disconnect"); + } +} + +void controllerWindow::on_refresh_port_button_clicked() +{ + populate_serial_list(); +} + +void controllerWindow::on_reload_preset_button_clicked() +{ + ui->presets_dropdown->clear(); + presets.clear(); + preset_index = 0; + load_presets(); +} + +void controllerWindow::on_r_slider_valueChanged(int value) +{ + r = value; + serial_rgb_change(r, g, b); +} + +void controllerWindow::on_g_slider_valueChanged(int value) +{ + g = value; + serial_rgb_change(r, g, b); +} + +void controllerWindow::on_b_slider_valueChanged(int value) +{ + b = value; + serial_rgb_change(r, g, b); +} + +void controllerWindow::on_red_button_clicked() +{ + ui->r_slider->setValue(255); + ui->g_slider->setValue(0); + ui->b_slider->setValue(0); +} + +void controllerWindow::on_green_button_clicked() +{ + ui->r_slider->setValue(0); + ui->g_slider->setValue(255); + ui->b_slider->setValue(0); +} + +void controllerWindow::on_blue_button_clicked() +{ + ui->r_slider->setValue(0); + ui->g_slider->setValue(0); + ui->b_slider->setValue(255); +} + +void controllerWindow::on_off_button_clicked() +{ + ui->r_slider->setValue(0); + ui->g_slider->setValue(0); + ui->b_slider->setValue(0); +} + +void controllerWindow::on_set_preset_button_clicked() +{ + QString tempstore = presets.at(preset_index); + QStringList temparray = tempstore.split(","); + info_log("Preset selected: " + temparray[0] + " " + temparray[1] + " " + temparray[2]); + ui->r_slider->setValue(temparray[0].toInt()); + ui->g_slider->setValue(temparray[1].toInt()); + ui->b_slider->setValue(temparray[2].toInt()); +} + +void controllerWindow::on_presets_dropdown_currentIndexChanged(int index) +{ + preset_index = index; +} diff --git a/qt/RGBController/controllerwindow.h b/qt/RGBController/controllerwindow.h new file mode 100755 index 0000000..350a599 --- /dev/null +++ b/qt/RGBController/controllerwindow.h @@ -0,0 +1,75 @@ +#ifndef CONTROLLERWINDOW_H +#define CONTROLLERWINDOW_H + +/* includes */ + +#include +#include +#include +#include +#include +#include "serial_communication.h" +#include "ui_controllerwindow.h" + +namespace Ui { + class controllerWindow; +} + +class controllerWindow : public QMainWindow +{ + Q_OBJECT + + public: + explicit controllerWindow(QWidget *parent = 0); + ~controllerWindow(); + + /* public functions */ + void info_log(QString text); + void populate_serial_list(); + void load_presets(); + void serial_rgb_change(int r, int g, int b); + /*public variables */ + /* these three ints will hold the current value (0 - 255) of each slider */ + int r, g, b; + /* this list will contain all the presets loaded from file */ + QStringList presets; + /* this string will hold our selected serial port */ + QString port; + /* this int will contain the current preset index selected */ + int preset_index; + +private slots: + /* these slots are used to trigger button clicks and drop down items selections etc */ + void on_connect_button_clicked(); + + void on_disconnect_button_clicked(); + + void on_refresh_port_button_clicked(); + + void on_reload_preset_button_clicked(); + + void on_r_slider_valueChanged(int value); + + void on_g_slider_valueChanged(int value); + + void on_b_slider_valueChanged(int value); + + void on_red_button_clicked(); + + void on_green_button_clicked(); + + void on_blue_button_clicked(); + + void on_off_button_clicked(); + + void on_set_preset_button_clicked(); + + void on_presets_dropdown_currentIndexChanged(int index); + +private: + Ui::controllerWindow *ui; + /* serial communication object */ + serial_communication portf; +}; + +#endif // CONTROLLERWINDOW_H diff --git a/qt/RGBController/controllerwindow.ui b/qt/RGBController/controllerwindow.ui new file mode 100755 index 0000000..1907b33 --- /dev/null +++ b/qt/RGBController/controllerwindow.ui @@ -0,0 +1,210 @@ + + + controllerWindow + + + + 0 + 0 + 229 + 394 + + + + RGB Controller + + + + + + + Status + + + + + + + <font color = red>Disconnected</font> + + + + + + + + 120 + 0 + + + + Arduino port + + + + + + + + + + Refresh + + + + + + + Connect + + + + + + + Disconnect + + + + + + + RGB colors + + + + + + + Red + + + + + + + + 120 + 0 + + + + + 120 + 16777215 + + + + 255 + + + Qt::Horizontal + + + + + + + + 120 + 0 + + + + + 120 + 16777215 + + + + 255 + + + Qt::Horizontal + + + + + + + + 120 + 0 + + + + + 120 + 16777215 + + + + 255 + + + Qt::Horizontal + + + + + + + Green + + + + + + + Blue + + + + + + + Presets + + + + + + + Off + + + + + + + Information log + + + + + + + Reload + + + + + + + Set + + + + + + + + + + + + + + + + diff --git a/qt/RGBController/main.cpp b/qt/RGBController/main.cpp new file mode 100755 index 0000000..4d44399 --- /dev/null +++ b/qt/RGBController/main.cpp @@ -0,0 +1,11 @@ +#include "controllerwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + controllerWindow w; + w.show(); + + return a.exec(); +} diff --git a/qt/RGBController/serial_communication.cpp b/qt/RGBController/serial_communication.cpp new file mode 100644 index 0000000..94e75db --- /dev/null +++ b/qt/RGBController/serial_communication.cpp @@ -0,0 +1,47 @@ +#include "serial_communication.h" +#include "controllerwindow.h" +serial_communication::serial_communication() +{ + +} + +bool serial_communication::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_communication::serial_disconnect() +{ + /* this function disconnects from the serial port if it is connected already */ + if (serial.isOpen()) + { + serial.close(); + return true; + } + return false; +} + +void serial_communication::rgb_change(int r, int g, int b) +{ + /* here we send our rgb values to the serial port */ + data = ""; + /* the 0 is a hack, i need to look into it at some point */ + data.append("0" + QString::number(r) + "," + QString::number(g) + "," + QString::number(b) + "\n"); + if (serial.isOpen()) + serial.write(data); +} diff --git a/qt/RGBController/serial_communication.h b/qt/RGBController/serial_communication.h new file mode 100644 index 0000000..d037757 --- /dev/null +++ b/qt/RGBController/serial_communication.h @@ -0,0 +1,23 @@ +#ifndef SERIAL_COMMUNICATION_H +#define SERIAL_COMMUNICATION_H + +/* includes */ +#include +#include + + +class serial_communication +{ +public: + /* function declares */ + serial_communication(); + bool serial_connect(QString port); + bool serial_disconnect(); + void rgb_change(int r, int g, int b); + /* variables */ + QString port_name; + QSerialPort serial; + QByteArray data; +}; + +#endif // SERIAL_COMMUNICATION_H -- cgit v1.2.3