Solved inter-thread communication issues by feeding commands over a queue.

This commit is contained in:
2023-05-10 16:14:57 +03:00
parent 5748e3d5af
commit 91d438353d
4 changed files with 356 additions and 174 deletions
+99 -16
View File
@@ -2,6 +2,8 @@
#include <map>
#include <unordered_map>
#include <memory>
#include <mutex>
#include "grbl_communication.h"
#include "grbl.h"
#include "heightmap.h"
@@ -116,20 +118,92 @@ static bool operator!=(const jog_state& a, const jog_state& b) {
return !(a == b);
}
struct machine_listener {
virtual void on_connected() = 0;
virtual void on_disconnected() = 0;
virtual void on_realtime_status_report(realtime_status_report) = 0;
virtual void on_banner(std::string line) = 0;
virtual void on_message(std::string message) = 0;
virtual void on_alarm(int alarm) = 0;
virtual void on_init_completed() = 0;
virtual void on_run_completed(bool success, size_t failed_index, size_t error) = 0;
virtual void on_check_completed(bool success, size_t failed_index, size_t error) = 0;
virtual void on_settings_reloaded() = 0;
virtual void on_parameters_reloaded() = 0;
virtual void on_probe_result(bool probe_touched, float probe_coords[3]) = 0;
virtual void on_heightmap_probe_acquired(heightmap* grid) = 0;
enum class machine_event_type {
connected,
disconnected,
report_received,
banner,
message,
alarm,
init_completed,
run_completed,
check_completed,
settings_reloaded,
parameters_reloaded,
probe_result,
heightmap_probe_acquired,
};
struct machine_event {
machine_event_type type;
// TODO: ewwww
virtual void omg() {}
};
struct machine_event_connect : public machine_event {
machine_event_connect();
};
struct machine_event_disconnect : public machine_event {
machine_event_disconnect();
};
struct machine_event_report_received : public machine_event {
explicit machine_event_report_received(const realtime_status_report& report);
realtime_status_report report;
};
struct machine_event_banner : public machine_event {
explicit machine_event_banner(const std::string& banner);
std::string banner;
};
struct machine_event_message : public machine_event {
explicit machine_event_message(const std::string& message);
std::string message;
};
struct machine_event_alarm : public machine_event {
explicit machine_event_alarm(int code);
int alarm;
};
struct machine_event_init_completed : public machine_event {
machine_event_init_completed();
};
struct machine_event_run_completed : public machine_event {
machine_event_run_completed(bool success, size_t failed_index, size_t error);
bool success;
size_t failed_index;
size_t error;
};
struct machine_event_check_completed : public machine_event {
machine_event_check_completed(bool success, size_t failed_idx, size_t error);
bool success;
size_t failed_index;
size_t error;
};
struct machine_event_settings_reloaded : public machine_event {
machine_event_settings_reloaded();
};
struct machine_event_parameters_reloaded : public machine_event {
machine_event_parameters_reloaded();
};
struct machine_event_probe_result : public machine_event {
machine_event_probe_result(bool touched, const float *coords);
bool probe_touched;
float probe_coords[3];
};
struct machine_event_heightmap_probe_acquired : public machine_event {
machine_event_heightmap_probe_acquired(heightmap *g, size_t location);
grbl::heightmap *grid;
size_t probed_location;
};
@@ -278,7 +352,6 @@ struct machine : public transport_callbacks {
machine();
~machine();
void set_listener(grbl::machine_listener *listener);
void connect();
void set_work_offset(std::string work_offset);
@@ -311,7 +384,18 @@ struct machine : public transport_callbacks {
void start_z_probe(float min_z, float feed_rate);
void probe_heightmap(grbl::heightmap& grid);
void push_event(std::shared_ptr<machine_event> event);
// pops nullptr if queue is empty
std::shared_ptr<machine_event> pop_event();
protected:
std::mutex event_mutex;
std::deque<std::shared_ptr<machine_event>> events;
void on_connected(transport *transport) override;
void on_disconnected(transport *transport) override;
void on_line_received(std::string line, transport *transport) override;
@@ -337,7 +421,6 @@ protected:
volatile size_t awaiting_responses = 0;
realtime_status_report last_report{};
machine_listener *listener = nullptr;
transport *pipe = nullptr;
grbl_machine_state state = grbl_machine_state::disconnected;
program running_program;