Files
grbl-sender/grbl_communication.h
T

56 lines
1.3 KiB
C++
Raw Normal View History

2023-04-27 15:53:44 +03:00
#pragma once
#include <string>
#include <thread>
#include <queue>
namespace grbl {
struct transport;
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;
2023-04-27 15:53:44 +03:00
};
struct transport {
2023-04-28 14:50:58 +03:00
virtual ~transport() = default;
2023-04-27 15:53:44 +03:00
virtual void open(transport_callbacks& cb) = 0;
virtual void close() = 0;
virtual void send(std::string line) = 0;
virtual void send_single_char_command(uint8_t data) const = 0;
2023-04-27 15:53:44 +03:00
};
struct tcp_transport : public transport {
tcp_transport(std::string address, uint16_t port);
virtual ~tcp_transport();
2023-04-27 15:53:44 +03:00
void open(transport_callbacks& cb) override;
void close() override;
void send(std::string line) override;
void send_single_char_command(uint8_t data) const override;
2023-04-27 15:53:44 +03:00
2023-04-27 18:10:48 +03:00
void request_realtime_report();
void request_cycle_start();
void request_feed_hold();
void parser_state_report();
2023-04-27 15:53:44 +03:00
private:
void worker();
std::string ip;
uint16_t port;
transport_callbacks *listener = nullptr;
int fd = -1;
volatile bool is_connected = false;
std::thread live_check_thread;
std::queue<std::string> send_queue;
volatile bool should_quit = false;
2023-04-27 18:10:48 +03:00
static bool is_empty_line(const std::string& line);
2023-04-27 15:53:44 +03:00
};
}