diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-12-11 16:46:57 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-12-11 16:46:57 +0100 |
commit | 28cd1a0d195637f8f55faf711a66a3e0249843dc (patch) | |
tree | 43d5f1597b5a0a23d9703ffc3b4feb1e30de44fe /confui | |
parent | ae3970ad5e1b3495726b70d5653c2424fccfba07 (diff) |
more serial worky
Diffstat (limited to 'confui')
-rw-r--r-- | confui/mainwindow.cpp | 21 | ||||
-rw-r--r-- | confui/mainwindow.h | 2 | ||||
-rw-r--r-- | confui/serial.cpp | 31 | ||||
-rw-r--r-- | confui/serial.h | 5 |
4 files changed, 52 insertions, 7 deletions
diff --git a/confui/mainwindow.cpp b/confui/mainwindow.cpp index 8dcf077..b0a1857 100644 --- a/confui/mainwindow.cpp +++ b/confui/mainwindow.cpp @@ -49,11 +49,20 @@ void CDMainWindow::update() { QAction *menu_options_add_automation = menu_options->addAction("add automation"); connect(menu_options_add_automation, &QAction::triggered, this, &CDMainWindow::menu_add_automation); - QMenu *menu_options_serialport = menu_options->addMenu("serial port (FIXME)"); + QString serial_port_menu_label = "serial port"; + string port_name = g_cd_serial->get_port(); + if (port_name.size() > 0) { + serial_port_menu_label.append(" ("); + serial_port_menu_label.append(QString::fromStdString(port_name)); + serial_port_menu_label.append(")"); + } + QMenu *menu_options_serialport = menu_options->addMenu(serial_port_menu_label); vector<string> ports = CDSerialConnector::get_ports(); - for (string port : ports) - menu_options_serialport->addAction(QString::fromStdString(port)); + for (string port : ports) { + QAction* menu_port = menu_options_serialport->addAction(QString::fromStdString(port)); + connect(menu_port, &QAction::triggered, this, [this, port](){ menu_set_serial_port(port); }); + } } void CDMainWindow::menu_refresh() { update(); } @@ -62,3 +71,9 @@ void CDMainWindow::menu_add_automation() { g_cd_mesh_connector->create_link(); update(); } + +void CDMainWindow::menu_set_serial_port(string new_port) { + g_cd_serial->disconnect(); + g_cd_serial->connect(new_port); + update(); +} diff --git a/confui/mainwindow.h b/confui/mainwindow.h index 41a0eac..587c447 100644 --- a/confui/mainwindow.h +++ b/confui/mainwindow.h @@ -37,4 +37,6 @@ public: virtual void menu_refresh(); /** @brief menu bar add automation action handler */ virtual void menu_add_automation(); + /** @brief menu bar set serial port action handler */ + void menu_set_serial_port(string new_port); }; diff --git a/confui/serial.cpp b/confui/serial.cpp index 96c5c0f..93adf28 100644 --- a/confui/serial.cpp +++ b/confui/serial.cpp @@ -1,5 +1,7 @@ #include "serial.h" +#include "../shared/serial_parse.h" +#include <iostream> #include <QDebug> #include <QSerialPort> #include <QSerialPortInfo> @@ -7,7 +9,6 @@ CDSerialConnector::CDSerialConnector() { this->_serial = new QSerialPort; - _serial->setPortName("/Users/jregnier/ttypx"); if (!_serial->setBaudRate(QSerialPort::Baud9600)) qDebug() << _serial->errorString(); if (!_serial->setDataBits(QSerialPort::Data8)) // data is 8 bits qDebug() << _serial->errorString(); @@ -28,6 +29,12 @@ void CDSerialConnector::action() { _msg = _serial->readAll(); qDebug() << _msg; } + + // string std_string = _msg.toStdString(); + // size_t size = std_string.size(); + // const char* data = std_string.c_str(); + // for (size_t i = 0; i < size; i++) + // cd_serial_parse(data[i]); } void CDSerialConnector::write(QByteArray msg) { @@ -35,7 +42,8 @@ void CDSerialConnector::write(QByteArray msg) { qDebug() << _serial->errorString(); } -void CDSerialConnector::connect() { +void CDSerialConnector::connect(string port) { + _serial->setPortName(QString::fromStdString(port)); // QIODevice::ReadOnly if (!_serial->open(QIODevice::ReadWrite)) qDebug() << _serial->errorString(); @@ -47,7 +55,8 @@ void CDSerialConnector::connect() { } void CDSerialConnector::disconnect() { - // WIP? + if (_serial->isOpen() == false) return; + _serial->disconnect(); } QByteArray CDSerialConnector::get_data() { return _msg; } @@ -58,3 +67,19 @@ vector<string> CDSerialConnector::get_ports() { ports.push_back(port.portName().toStdString()); return ports; } + +string CDSerialConnector::get_port() { + return _serial->portName().toStdString(); +} + +extern "C" { + +void cd_cmd_response(cd_s_bin* data) { + (void) data; + + std::cout << "received!" << std::endl; + + return; +} + +} diff --git a/confui/serial.h b/confui/serial.h index 9645227..22f2542 100644 --- a/confui/serial.h +++ b/confui/serial.h @@ -17,7 +17,7 @@ public: static vector<string> get_ports(); /** @brief open serial port */ - virtual void connect(); + virtual void connect(string port); /** @brief close serial port */ virtual void disconnect(); @@ -26,6 +26,9 @@ public: /** @brief get last message */ virtual QByteArray get_data(); + /** @brief get current port name */ + virtual string get_port(); + private: /** @brief executed when new data is received */ virtual void action(); |