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 | |
parent | ae3970ad5e1b3495726b70d5653c2424fccfba07 (diff) |
more serial worky
-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 | ||||
-rw-r--r-- | shared/protocol.h | 16 | ||||
-rw-r--r-- | shared/serial_parse.c | 2 |
6 files changed, 61 insertions, 16 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(); diff --git a/shared/protocol.h b/shared/protocol.h index e2b3b1c..c2001cf 100644 --- a/shared/protocol.h +++ b/shared/protocol.h @@ -27,7 +27,7 @@ typedef uint16_t cd_cmd_id_t; typedef uint8_t cd_cmd_bool_t; /** @brief cmd handler function signature */ -typedef void (cd_cmd_handler)(cd_s_bin *data); +typedef void (cd_cmd_handler_t)(cd_s_bin *data); #pragma pack(push, 1) @@ -121,12 +121,12 @@ typedef enum { CD_CMD_RESPONSE = 0x05, /** @brief response message */ } cd_e_scmds; -cd_cmd_handler cd_cmd_ping, - cd_cmd_get_node, - cd_cmd_post_led, - cd_cmd_post_link, - cd_cmd_post_net, - cd_cmd_response; +cd_cmd_handler_t cd_cmd_ping, + cd_cmd_get_node, + cd_cmd_post_led, + cd_cmd_post_link, + cd_cmd_post_net, + cd_cmd_response; /** @brief constant message sizes, 0 for dynamic size */ static const size_t CD_CMD_HANDLERS_SIZE[CD_CMD_COUNT] = { @@ -149,7 +149,7 @@ static size_t (* const CD_CMD_HANDLERS_SIZEOF[CD_CMD_COUNT])(cd_s_bin*) = { }; /** @brief stores message handlers in array with opcode as index */ -static cd_cmd_handler* const CD_CMD_HANDLERS[CD_CMD_COUNT] = { +static cd_cmd_handler_t* const CD_CMD_HANDLERS[CD_CMD_COUNT] = { [CD_CMD_PING] = &cd_cmd_ping, [CD_CMD_GET_NODE] = &cd_cmd_get_node, [CD_CMD_POST_LED] = &cd_cmd_post_led, diff --git a/shared/serial_parse.c b/shared/serial_parse.c index de48f60..bfc374a 100644 --- a/shared/serial_parse.c +++ b/shared/serial_parse.c @@ -47,7 +47,7 @@ void cd_cmd_handle(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_length cd_s_bin *copy = cd_bin_s_alloc(data_length, data); if (data[0] >= CD_CMD_COUNT) return; - cd_cmd_handler* handler = CD_CMD_HANDLERS[data[0]]; + cd_cmd_handler_t* handler = CD_CMD_HANDLERS[data[0]]; if (handler == NULL) return; (*handler)(copy); |