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);
|
||||
}
|
||||
|
||||
+12
-1
@@ -12,6 +12,7 @@ struct transport_callbacks {
|
||||
virtual void on_connected(transport*) = 0;
|
||||
virtual void on_disconnected(transport*) = 0;
|
||||
virtual void on_line_received(std::string line, transport* ) = 0;
|
||||
virtual void on_banner(std::string version, transport* ) = 0;
|
||||
};
|
||||
|
||||
struct transport {
|
||||
@@ -28,6 +29,14 @@ struct tcp_transport : public transport {
|
||||
void close() override;
|
||||
void send(std::string line) override;
|
||||
|
||||
void request_realtime_report();
|
||||
|
||||
void request_cycle_start();
|
||||
|
||||
void request_feed_hold();
|
||||
|
||||
void parser_state_report();
|
||||
|
||||
private:
|
||||
|
||||
void worker();
|
||||
@@ -40,7 +49,9 @@ private:
|
||||
std::thread live_check_thread;
|
||||
std::queue<std::string> send_queue;
|
||||
volatile bool should_quit = false;
|
||||
bool is_empty_line(std::string line);
|
||||
static bool is_empty_line(const std::string& line);
|
||||
|
||||
void send_single_char_command(uint8_t data) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -553,7 +553,7 @@ public:
|
||||
m_shader->set_uniform("intensity", 0.5f);
|
||||
}
|
||||
|
||||
bool resize_event(const Vector2i& size) override {
|
||||
bool resize_event(const Vector2i &size) override {
|
||||
window->set_size(size);
|
||||
Screen::resize_event(size);
|
||||
return true;
|
||||
@@ -616,7 +616,9 @@ private:
|
||||
struct grbl_listener : public grbl::transport_callbacks {
|
||||
void on_connected(grbl::transport *t) override {
|
||||
std::cout << "Listener: connected!" << std::endl;
|
||||
t->send("$$");
|
||||
// telnet handshake so that we get the banner. banner won't be coming otherwise
|
||||
// t->send("\xff\xfd\x18\xff\xfd\x20\xff\xfd\x23\xff\xfd\x27");
|
||||
t->send("\xff\xfd\x18");
|
||||
}
|
||||
|
||||
void on_disconnected(grbl::transport *t) override {
|
||||
@@ -626,6 +628,11 @@ struct grbl_listener : public grbl::transport_callbacks {
|
||||
void on_line_received(std::string line, grbl::transport *t) override {
|
||||
std::cout << "Listener: -> " << line << std::endl;
|
||||
}
|
||||
|
||||
void on_banner(std::string version, grbl::transport *t) override {
|
||||
std::cout << "Banner: " << version << std::endl;
|
||||
t->send("$$");
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -641,6 +648,11 @@ int main(int argc, char **argv) {
|
||||
exit(result);
|
||||
}
|
||||
|
||||
transport.request_realtime_report();
|
||||
transport.request_cycle_start();
|
||||
transport.request_feed_hold();
|
||||
transport.parser_state_report();
|
||||
|
||||
try {
|
||||
|
||||
// grbl::program pgm;
|
||||
@@ -659,7 +671,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
nanogui::shutdown();
|
||||
} catch (const std::exception& e) {
|
||||
} catch (const std::exception &e) {
|
||||
std::string error_msg = std::string("Caught a fatal error: ") + std::string(e.what());
|
||||
std::cerr << error_msg << std::endl;
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user