Files
serial-imgui/sources/ConnectionWindow.cpp
Ivan I. Ovchinnikov 8c4d40b66a protobuf server
2026-07-28 20:46:41 +03:00

140 lines
4.8 KiB
C++

#include "../include/ConnectionWindow.h"
#include "imgui.h"
ConnectionWindow::ConnectionWindow()
: selectedPortIdx(0), connected(false), activePort(nullptr) {
// Заполняем массив стандартными скоростями UART
baudRates = { 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 };
// По умолчанию выбираем 9600 (индекс 0 в векторе)
selectedBaudIdx = 0;
}
ConnectionWindow::~ConnectionWindow() {
if (connected && activePort) {
sp_close(activePort);
sp_free_port(activePort);
}
}
void ConnectionWindow::initialize() {
refreshPorts();
}
void ConnectionWindow::refreshPorts() {
ports.clear();
selectedPortIdx = 0;
sp_port** portList;
if (sp_list_ports(&portList) == SP_OK) {
for (int i = 0; portList[i] != nullptr; i++) {
const sp_port* port = portList[i];
SerialPortInfo info;
const char* name = sp_get_port_name(port);
info.portName = name ? name : "Unknown";
const char* desc = sp_get_port_description(port);
info.description = desc ? desc : "No Description";
ports.push_back(info);
}
sp_free_port_list(portList);
}
if (ports.empty()) {
ports.push_back({"None", "No serial devices detected"});
}
}
void ConnectionWindow::forceLogMessage(const std::string& msg) {
txLog += msg + "\n";
}
void ConnectionWindow::render(bool* paramOpen) {
if (!ImGui::Begin("Connection Settings", paramOpen)) {
ImGui::End();
return;
}
if (ImGui::Button("Refresh Ports") && !connected) {
refreshPorts();
}
ImGui::SameLine();
// 1. Выбор COM-порта
std::string preview = ports[selectedPortIdx].portName;
if (ports[selectedPortIdx].portName != "None") {
preview += " (" + ports[selectedPortIdx].description + ")";
}
ImGui::BeginDisabled(connected);
if (ImGui::BeginCombo("Serial Port", preview.c_str())) {
for (int n = 0; n < ports.size(); n++) {
const bool isSelected = (selectedPortIdx == n);
std::string item_text = ports[n].portName + " - " + ports[n].description;
if (ImGui::Selectable(item_text.c_str(), isSelected)) {
selectedPortIdx = n;
}
}
ImGui::EndCombo();
}
// 2. Выбор Baud Rate через выпадающий список
const std::string baudPreview = std::to_string(baudRates[selectedBaudIdx]);
if (ImGui::BeginCombo("Baud Rate", baudPreview.c_str())) {
for (int b = 0; b < baudRates.size(); b++) {
const bool isSelected = (selectedBaudIdx == b);
std::string baud_item_text = std::to_string(baudRates[b]);
if (ImGui::Selectable(baud_item_text.c_str(), isSelected)) {
selectedBaudIdx = b;
}
}
ImGui::EndCombo();
}
ImGui::EndDisabled();
// 3. Логика подключения
if (!connected) {
ImGui::BeginDisabled(ports[selectedPortIdx].portName == "None");
if (ImGui::Button("Connect", ImVec2(120, 0))) {
if (sp_get_port_by_name(ports[selectedPortIdx].portName.c_str(), &activePort) == SP_OK) {
if (sp_open(activePort, SP_MODE_READ_WRITE) == SP_OK) {
// Передаем реальное значение скорости из выбранного индекса массива
const int selectedBaud = baudRates[selectedBaudIdx];
sp_set_baudrate(activePort, selectedBaud);
sp_set_bits(activePort, 8);
sp_set_parity(activePort, SP_PARITY_NONE);
sp_set_stopbits(activePort, 1);
connected = true;
forceLogMessage("[SYSTEM] Connected to " + ports[selectedPortIdx].portName + " at " + std::to_string(selectedBaud) + " baud.");
} else {
forceLogMessage("[SYSTEM ERROR] Could not open port " + ports[selectedPortIdx].portName);
sp_free_port(activePort);
activePort = nullptr;
}
}
}
ImGui::EndDisabled();
} else {
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.8f, 0.1f, 0.1f, 1.0f));
if (ImGui::Button("Disconnect", ImVec2(120, 0))) {
if (activePort) {
sp_close(activePort);
sp_free_port(activePort);
activePort = nullptr;
}
connected = false;
forceLogMessage("[SYSTEM] Disconnected.");
}
ImGui::PopStyleColor();
}
ImGui::SameLine();
ImGui::Text("Status: %s", connected ? "CONNECTED" : "DISCONNECTED");
ImGui::End();
}