mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 16:19:19 +02:00
git changes
This commit is contained in:
14
Globals.cpp
14
Globals.cpp
@@ -749,7 +749,6 @@ void Global::ConfigParse(cParser &Parser)
|
|||||||
Global::uart_conf.enable = true;
|
Global::uart_conf.enable = true;
|
||||||
Parser >> Global::uart_conf.port;
|
Parser >> Global::uart_conf.port;
|
||||||
Parser >> Global::uart_conf.baud;
|
Parser >> Global::uart_conf.baud;
|
||||||
Parser >> Global::uart_conf.interval;
|
|
||||||
Parser >> Global::uart_conf.updatetime;
|
Parser >> Global::uart_conf.updatetime;
|
||||||
}
|
}
|
||||||
else if (token == "uarttune")
|
else if (token == "uarttune")
|
||||||
@@ -770,6 +769,19 @@ void Global::ConfigParse(cParser &Parser)
|
|||||||
>> Global::uart_conf.currentmax
|
>> Global::uart_conf.currentmax
|
||||||
>> Global::uart_conf.currentuart;
|
>> Global::uart_conf.currentuart;
|
||||||
}
|
}
|
||||||
|
else if (token == "uartfeature")
|
||||||
|
{
|
||||||
|
Parser.getTokens(4);
|
||||||
|
Parser >> Global::uart_conf.mainenable
|
||||||
|
>> Global::uart_conf.scndenable
|
||||||
|
>> Global::uart_conf.trainenable
|
||||||
|
>> Global::uart_conf.localenable;
|
||||||
|
}
|
||||||
|
else if (token == "uartdebug")
|
||||||
|
{
|
||||||
|
Parser.getTokens(1);
|
||||||
|
Parser >> Global::uart_conf.debug;
|
||||||
|
}
|
||||||
} while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile)
|
} while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile)
|
||||||
// na koniec trochę zależności
|
// na koniec trochę zależności
|
||||||
if (!bLoadTraction) // wczytywanie drutów i słupów
|
if (!bLoadTraction) // wczytywanie drutów i słupów
|
||||||
|
|||||||
@@ -298,7 +298,6 @@ class Global
|
|||||||
bool enable = false;
|
bool enable = false;
|
||||||
std::string port;
|
std::string port;
|
||||||
int baud;
|
int baud;
|
||||||
int interval;
|
|
||||||
float updatetime;
|
float updatetime;
|
||||||
|
|
||||||
float mainbrakemin = 0.0f;
|
float mainbrakemin = 0.0f;
|
||||||
@@ -315,6 +314,13 @@ class Global
|
|||||||
float hvuart = 65535.0f;
|
float hvuart = 65535.0f;
|
||||||
float currentmax = 10000.0f;
|
float currentmax = 10000.0f;
|
||||||
float currentuart = 65535.0f;
|
float currentuart = 65535.0f;
|
||||||
|
|
||||||
|
bool mainenable = true;
|
||||||
|
bool scndenable = true;
|
||||||
|
bool trainenable = true;
|
||||||
|
bool localenable = true;
|
||||||
|
|
||||||
|
bool debug = false;
|
||||||
};
|
};
|
||||||
static uart_conf_t uart_conf;
|
static uart_conf_t uart_conf;
|
||||||
|
|
||||||
|
|||||||
57
uart.cpp
57
uart.cpp
@@ -15,11 +15,18 @@ uart_input::uart_input()
|
|||||||
if (sp_open(port, SP_MODE_READ_WRITE) != SP_OK)
|
if (sp_open(port, SP_MODE_READ_WRITE) != SP_OK)
|
||||||
throw std::runtime_error("uart: cannot open port");
|
throw std::runtime_error("uart: cannot open port");
|
||||||
|
|
||||||
if (sp_set_baudrate(port, conf.baud) != SP_OK)
|
sp_port_config *config;
|
||||||
throw std::runtime_error("uart: cannot set baudrate");
|
|
||||||
|
|
||||||
if (sp_set_flowcontrol(port, SP_FLOWCONTROL_NONE) != SP_OK)
|
if (sp_new_config(&config) != SP_OK ||
|
||||||
throw std::runtime_error("uart: cannot set flowcontrol");
|
sp_set_config_baudrate(config, conf.baud) != SP_OK ||
|
||||||
|
sp_set_config_flowcontrol(config, SP_FLOWCONTROL_NONE) != SP_OK ||
|
||||||
|
sp_set_config_bits(config, 8) != SP_OK ||
|
||||||
|
sp_set_config_stopbits(config, 1) != SP_OK ||
|
||||||
|
sp_set_config_parity(config, SP_PARITY_NONE) != SP_OK ||
|
||||||
|
sp_set_config(port, config) != SP_OK)
|
||||||
|
throw std::runtime_error("uart: cannot set config");
|
||||||
|
|
||||||
|
sp_free_config(config);
|
||||||
|
|
||||||
if (sp_flush(port, SP_BUF_BOTH) != SP_OK)
|
if (sp_flush(port, SP_BUF_BOTH) != SP_OK)
|
||||||
throw std::runtime_error("uart: cannot flush");
|
throw std::runtime_error("uart: cannot flush");
|
||||||
@@ -44,6 +51,8 @@ void uart_input::poll()
|
|||||||
last_update = now;
|
last_update = now;
|
||||||
|
|
||||||
TTrain *t = Global::pWorld->train();
|
TTrain *t = Global::pWorld->train();
|
||||||
|
if (!t)
|
||||||
|
return;
|
||||||
|
|
||||||
sp_return ret;
|
sp_return ret;
|
||||||
|
|
||||||
@@ -54,8 +63,15 @@ void uart_input::poll()
|
|||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
throw std::runtime_error("uart: failed to read from port");
|
throw std::runtime_error("uart: failed to read from port");
|
||||||
|
|
||||||
WriteLog("uart: recv");
|
if (conf.debug)
|
||||||
sp_drain(port);
|
{
|
||||||
|
char buf[buffer.size() * 3 + 1];
|
||||||
|
size_t pos = 0;
|
||||||
|
for (uint8_t b : buffer)
|
||||||
|
pos += sprintf(&buf[pos], "%02X ", b);
|
||||||
|
WriteLog("uart: rx: " + std::string(buf));
|
||||||
|
}
|
||||||
|
|
||||||
data_pending = false;
|
data_pending = false;
|
||||||
|
|
||||||
for (auto entry : input_bits)
|
for (auto entry : input_bits)
|
||||||
@@ -103,21 +119,18 @@ void uart_input::poll()
|
|||||||
relay.post(std::get<1>(entry), 0, 0, action, 0, desired_state);
|
relay.post(std::get<1>(entry), 0, 0, action, 0, desired_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mainctrl = buffer[6];
|
if (conf.mainenable)
|
||||||
int scndctrl = buffer[7];
|
t->set_mainctrl(buffer[6]);
|
||||||
float trainbrake = (float)(((uint16_t)buffer[8] | ((uint16_t)buffer[9] << 8)) - conf.mainbrakemin) / (conf.mainbrakemax - conf.mainbrakemin);
|
if (conf.scndenable)
|
||||||
float localbrake = (float)(((uint16_t)buffer[10] | ((uint16_t)buffer[11] << 8)) - conf.mainbrakemin) / (conf.localbrakemax - conf.localbrakemin);
|
t->set_scndctrl(buffer[7]);
|
||||||
|
if (conf.trainenable)
|
||||||
t->set_mainctrl(mainctrl);
|
t->set_trainbrake((float)(((uint16_t)buffer[8] | ((uint16_t)buffer[9] << 8)) - conf.mainbrakemin) / (conf.mainbrakemax - conf.mainbrakemin));
|
||||||
t->set_scndctrl(scndctrl);
|
if (conf.localenable)
|
||||||
t->set_trainbrake(trainbrake);
|
t->set_localbrake((float)(((uint16_t)buffer[10] | ((uint16_t)buffer[11] << 8)) - conf.mainbrakemin) / (conf.localbrakemax - conf.localbrakemin));
|
||||||
t->set_localbrake(localbrake);
|
|
||||||
|
|
||||||
old_packet = buffer;
|
old_packet = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteLog("uart, available input: " + std::to_string(ret));
|
|
||||||
|
|
||||||
if (!data_pending && sp_output_waiting(port) == 0)
|
if (!data_pending && sp_output_waiting(port) == 0)
|
||||||
{
|
{
|
||||||
// TODO: ugly! move it into structure like input_bits
|
// TODO: ugly! move it into structure like input_bits
|
||||||
@@ -165,9 +178,15 @@ void uart_input::poll()
|
|||||||
|
|
||||||
buffer[8] |= buzzer << 7;
|
buffer[8] |= buzzer << 7;
|
||||||
|
|
||||||
// sp_flush(port, SP_BUF_INPUT); // flush input buffer in preparation for reply packet
|
if (conf.debug)
|
||||||
|
{
|
||||||
|
char buf[buffer.size() * 3 + 1];
|
||||||
|
size_t pos = 0;
|
||||||
|
for (uint8_t b : buffer)
|
||||||
|
pos += sprintf(&buf[pos], "%02X ", b);
|
||||||
|
WriteLog("uart: tx: " + std::string(buf));
|
||||||
|
}
|
||||||
|
|
||||||
WriteLog("uart: send");
|
|
||||||
ret = sp_blocking_write(port, (void*)buffer.data(), buffer.size(), 0);
|
ret = sp_blocking_write(port, (void*)buffer.data(), buffer.size(), 0);
|
||||||
if (ret != buffer.size())
|
if (ret != buffer.size())
|
||||||
throw std::runtime_error("uart: failed to write to port");
|
throw std::runtime_error("uart: failed to write to port");
|
||||||
|
|||||||
Reference in New Issue
Block a user