Welcome to mirror list, hosted at ThFree Co, Russian Federation.

avrdude-slic3r.cpp « avrdude « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a859200fb442fc8bae098c44b94648006901a956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "avrdude-slic3r.hpp"

#include <thread>

extern "C" {
#include "ac_cfg.h"
#include "avrdude.h"
}


namespace Slic3r {


// C callbacks

// Used by our custom code in avrdude to receive messages that avrdude normally outputs on stdout (see avrdude_message())
static void avrdude_message_handler_closure(const char *msg, unsigned size, void *user_p)
{
	auto *message_fn = reinterpret_cast<AvrDude::MessageFn*>(user_p);
	(*message_fn)(msg, size);
}

// Used by our custom code in avrdude to report progress in the GUI
static void avrdude_progress_handler_closure(const char *task, unsigned progress, void *user_p)
{
	auto *progress_fn = reinterpret_cast<AvrDude::ProgressFn*>(user_p);
	(*progress_fn)(task, progress);
}


// Private

struct AvrDude::priv
{
	std::string sys_config;
	std::vector<std::string> args;
	MessageFn message_fn;
	ProgressFn progress_fn;
	CompleteFn complete_fn;

	std::thread avrdude_thread;

	int run();
};

int AvrDude::priv::run() {
	std::vector<char*> c_args {{ const_cast<char*>(PACKAGE_NAME) }};
	for (const auto &arg : args) {
		c_args.push_back(const_cast<char*>(arg.data()));
	}

	if (message_fn) {
		::avrdude_message_handler_set(avrdude_message_handler_closure, reinterpret_cast<void*>(&message_fn));
	} else {
		::avrdude_message_handler_set(nullptr, nullptr);
	}

	if (progress_fn) {
		::avrdude_progress_handler_set(avrdude_progress_handler_closure, reinterpret_cast<void*>(&progress_fn));
	} else {
		::avrdude_progress_handler_set(nullptr, nullptr);
	}

	const auto res = ::avrdude_main(static_cast<int>(c_args.size()), c_args.data(), sys_config.c_str());

	::avrdude_message_handler_set(nullptr, nullptr);
	::avrdude_progress_handler_set(nullptr, nullptr);
	return res;
}


// Public

AvrDude::AvrDude() : p(new priv()) {}

AvrDude::AvrDude(AvrDude &&other) : p(std::move(other.p)) {}

AvrDude::~AvrDude()
{
	if (p && p->avrdude_thread.joinable()) {
		p->avrdude_thread.detach();
	}
}

AvrDude& AvrDude::sys_config(std::string sys_config)
{
	if (p) { p->sys_config = std::move(sys_config); }
	return *this;
}

AvrDude& AvrDude::args(std::vector<std::string> args)
{
	if (p) { p->args = std::move(args); }
	return *this;
}

AvrDude& AvrDude::on_message(MessageFn fn)
{
	if (p) { p->message_fn = std::move(fn); }
	return *this;
}

AvrDude& AvrDude::on_progress(ProgressFn fn)
{
	if (p) { p->progress_fn = std::move(fn); }
	return *this;
}

AvrDude& AvrDude::on_complete(CompleteFn fn)
{
	if (p) { p->complete_fn = std::move(fn); }
	return *this;
}

int AvrDude::run_sync()
{
	return p->run();
}

AvrDude::Ptr AvrDude::run()
{
	auto self = std::make_shared<AvrDude>(std::move(*this));

	if (self->p) {
		auto avrdude_thread = std::thread([self]() {
				auto res = self->p->run();
				if (self->p->complete_fn) {
					self->p->complete_fn(res);
				}
			});
		self->p->avrdude_thread = std::move(avrdude_thread);
	}

	return self;
}

void AvrDude::cancel()
{
	::avrdude_cancel();
}

void AvrDude::join()
{
	if (p && p->avrdude_thread.joinable()) {
		p->avrdude_thread.join();
	}
}


}