style refactor

This commit is contained in:
Ivan I. Ovchinnikov
2026-07-27 21:29:50 +03:00
parent 47b1e70663
commit 5a702fe0f8
9 changed files with 119 additions and 119 deletions
+49 -49
View File
@@ -3,29 +3,29 @@
#include <cstdio>
ConnectionWindow::ConnectionWindow()
: selected_port_idx(0), is_connected(false), active_port(nullptr) {
: selectedPortIdx(0), connected(false), activePort(nullptr) {
// Заполняем массив стандартными скоростями UART
baud_rates = { 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 };
baudRates = { 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 };
// По умолчанию выбираем 9600 (индекс 0 в векторе)
selected_baud_idx = 0;
selectedBaudIdx = 0;
}
ConnectionWindow::~ConnectionWindow() {
if (is_connected && active_port) {
sp_close(active_port);
sp_free_port(active_port);
if (connected && activePort) {
sp_close(activePort);
sp_free_port(activePort);
}
}
void ConnectionWindow::Initialize() {
RefreshPorts();
void ConnectionWindow::initialize() {
refreshPorts();
}
void ConnectionWindow::RefreshPorts() {
void ConnectionWindow::refreshPorts() {
ports.clear();
selected_port_idx = 0;
selectedPortIdx = 0;
struct sp_port** port_list;
if (sp_list_ports(&port_list) == SP_OK) {
@@ -33,7 +33,7 @@ void ConnectionWindow::RefreshPorts() {
struct sp_port* port = port_list[i];
SerialPortInfo info;
const char* name = sp_get_port_name(port);
info.port_name = name ? name : "Unknown";
info.portName = name ? name : "Unknown";
const char* desc = sp_get_port_description(port);
info.description = desc ? desc : "No Description";
ports.push_back(info);
@@ -46,48 +46,48 @@ void ConnectionWindow::RefreshPorts() {
}
}
void ConnectionWindow::ForceLogMessage(const std::string& msg) {
tx_log += msg + "\n";
void ConnectionWindow::forceLogMessage(const std::string& msg) {
txLog += msg + "\n";
}
void ConnectionWindow::Render(bool* p_open) {
void ConnectionWindow::render(bool* p_open) {
if (!ImGui::Begin("Connection Settings", p_open)) {
ImGui::End();
return;
}
if (ImGui::Button("Refresh Ports") && !is_connected) {
RefreshPorts();
if (ImGui::Button("Refresh Ports") && !connected) {
refreshPorts();
}
ImGui::SameLine();
// 1. Выбор COM-порта
std::string combo_preview = ports[selected_port_idx].port_name;
if (ports[selected_port_idx].port_name != "None") {
combo_preview += " (" + ports[selected_port_idx].description + ")";
std::string combo_preview = ports[selectedPortIdx].portName;
if (ports[selectedPortIdx].portName != "None") {
combo_preview += " (" + ports[selectedPortIdx].description + ")";
}
ImGui::BeginDisabled(is_connected);
ImGui::BeginDisabled(connected);
if (ImGui::BeginCombo("Serial Port", combo_preview.c_str())) {
for (int n = 0; n < ports.size(); n++) {
const bool is_selected = (selected_port_idx == n);
std::string item_text = ports[n].port_name + " - " + ports[n].description;
const bool is_selected = (selectedPortIdx == n);
std::string item_text = ports[n].portName + " - " + ports[n].description;
if (ImGui::Selectable(item_text.c_str(), is_selected)) {
selected_port_idx = n;
selectedPortIdx = n;
}
}
ImGui::EndCombo();
}
// 2. Выбор Baud Rate через выпадающий список
std::string baud_preview = std::to_string(baud_rates[selected_baud_idx]);
std::string baud_preview = std::to_string(baudRates[selectedBaudIdx]);
if (ImGui::BeginCombo("Baud Rate", baud_preview.c_str())) {
for (int b = 0; b < baud_rates.size(); b++) {
const bool is_selected = (selected_baud_idx == b);
std::string baud_item_text = std::to_string(baud_rates[b]);
for (int b = 0; b < baudRates.size(); b++) {
const bool is_selected = (selectedBaudIdx == b);
std::string baud_item_text = std::to_string(baudRates[b]);
if (ImGui::Selectable(baud_item_text.c_str(), is_selected)) {
selected_baud_idx = b;
selectedBaudIdx = b;
}
}
ImGui::EndCombo();
@@ -95,26 +95,26 @@ void ConnectionWindow::Render(bool* p_open) {
ImGui::EndDisabled();
// 3. Логика подключения
if (!is_connected) {
ImGui::BeginDisabled(ports[selected_port_idx].port_name == "None");
if (!connected) {
ImGui::BeginDisabled(ports[selectedPortIdx].portName == "None");
if (ImGui::Button("Connect", ImVec2(120, 0))) {
if (sp_get_port_by_name(ports[selected_port_idx].port_name.c_str(), &active_port) == SP_OK) {
if (sp_open(active_port, SP_MODE_READ_WRITE) == SP_OK) {
if (sp_get_port_by_name(ports[selectedPortIdx].portName.c_str(), &activePort) == SP_OK) {
if (sp_open(activePort, SP_MODE_READ_WRITE) == SP_OK) {
// Передаем реальное значение скорости из выбранного индекса массива
int real_baud = baud_rates[selected_baud_idx];
sp_set_baudrate(active_port, real_baud);
int real_baud = baudRates[selectedBaudIdx];
sp_set_baudrate(activePort, real_baud);
sp_set_bits(active_port, 8);
sp_set_parity(active_port, SP_PARITY_NONE);
sp_set_stopbits(active_port, 1);
sp_set_bits(activePort, 8);
sp_set_parity(activePort, SP_PARITY_NONE);
sp_set_stopbits(activePort, 1);
is_connected = true;
ForceLogMessage("[SYSTEM] Connected to " + ports[selected_port_idx].port_name + " at " + std::to_string(real_baud) + " baud.");
connected = true;
forceLogMessage("[SYSTEM] Connected to " + ports[selectedPortIdx].portName + " at " + std::to_string(real_baud) + " baud.");
} else {
ForceLogMessage("[SYSTEM ERROR] Could not open port " + ports[selected_port_idx].port_name);
sp_free_port(active_port);
active_port = nullptr;
forceLogMessage("[SYSTEM ERROR] Could not open port " + ports[selectedPortIdx].portName);
sp_free_port(activePort);
activePort = nullptr;
}
}
}
@@ -122,19 +122,19 @@ void ConnectionWindow::Render(bool* p_open) {
} else {
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.8f, 0.1f, 0.1f, 1.0f));
if (ImGui::Button("Disconnect", ImVec2(120, 0))) {
if (active_port) {
sp_close(active_port);
sp_free_port(active_port);
active_port = nullptr;
if (activePort) {
sp_close(activePort);
sp_free_port(activePort);
activePort = nullptr;
}
is_connected = false;
ForceLogMessage("[SYSTEM] Disconnected.");
connected = false;
forceLogMessage("[SYSTEM] Disconnected.");
}
ImGui::PopStyleColor();
}
ImGui::SameLine();
ImGui::Text("Status: %s", is_connected ? "CONNECTED" : "DISCONNECTED");
ImGui::Text("Status: %s", connected ? "CONNECTED" : "DISCONNECTED");
ImGui::End();
}