Communicator banner + single char commands
This commit is contained in:
+40
-6
@@ -12,7 +12,7 @@ grbl::tcp_transport::tcp_transport(std::string address, uint16_t p)
|
||||
live_check_thread = std::thread(&grbl::tcp_transport::worker, this);
|
||||
}
|
||||
|
||||
void grbl::tcp_transport::open(grbl::transport_callbacks& cb) {
|
||||
void grbl::tcp_transport::open(grbl::transport_callbacks &cb) {
|
||||
if (is_connected) {
|
||||
close();
|
||||
}
|
||||
@@ -52,14 +52,21 @@ void grbl::tcp_transport::close() {
|
||||
is_connected = false;
|
||||
send_queue = std::queue<std::string>{};
|
||||
|
||||
if (listener != nullptr)
|
||||
if (listener != nullptr) {
|
||||
listener->on_disconnected(this);
|
||||
listener = nullptr;
|
||||
}
|
||||
|
||||
fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void grbl::tcp_transport::send(std::string line) {
|
||||
if (is_connected) {
|
||||
send_queue.push(line + "\r");
|
||||
if (line.at(0) == (char) 0xff)
|
||||
send_queue.push(line);
|
||||
else
|
||||
send_queue.push(line + "\r");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +100,13 @@ void grbl::tcp_transport::worker() {
|
||||
auto is_eol = buffer[i] == '\r' || buffer[i] == '\n';
|
||||
if (is_eol) {
|
||||
if (!is_empty_line(received)) {
|
||||
listener->on_line_received(received, this);
|
||||
// starts with? https://stackoverflow.com/questions/1878001/how-do-i-check-if-a-c-stdstring-starts-with-a-certain-string-and-convert-a
|
||||
if (received.rfind("Grbl", 0) == 0) {
|
||||
listener->on_banner(received, this);
|
||||
|
||||
} else {
|
||||
listener->on_line_received(received, this);
|
||||
}
|
||||
}
|
||||
received.clear();
|
||||
} else {
|
||||
@@ -114,11 +127,32 @@ grbl::tcp_transport::~tcp_transport() {
|
||||
live_check_thread.join();
|
||||
}
|
||||
|
||||
bool grbl::tcp_transport::is_empty_line(std::string line) {
|
||||
for (auto& c: line) {
|
||||
bool grbl::tcp_transport::is_empty_line(const std::string &line) {
|
||||
for (auto &c: line) {
|
||||
if (c != '\r' && c != '\n' && c != ' ') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void grbl::tcp_transport::send_single_char_command(uint8_t data) const {
|
||||
if (is_connected)
|
||||
write(fd, &data, 1);
|
||||
}
|
||||
|
||||
void grbl::tcp_transport::request_realtime_report() {
|
||||
send_single_char_command(0x80);
|
||||
}
|
||||
|
||||
void grbl::tcp_transport::request_cycle_start() {
|
||||
send_single_char_command(0x81);
|
||||
}
|
||||
|
||||
void grbl::tcp_transport::request_feed_hold() {
|
||||
send_single_char_command(0x82);
|
||||
}
|
||||
|
||||
void grbl::tcp_transport::parser_state_report() {
|
||||
send_single_char_command(0x83);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user