Firmware updater: Make the GUI less scary

This commit is contained in:
Vojtech Kral 2018-05-18 15:38:33 +02:00
parent c5f1acfbfb
commit a54672fb54
5 changed files with 211 additions and 68 deletions

View file

@ -8,37 +8,67 @@ extern "C" {
namespace Slic3r {
namespace AvrDude {
// 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<MessageFn*>(user_p);
auto *message_fn = reinterpret_cast<AvrDude::MessageFn*>(user_p);
(*message_fn)(msg, size);
}
int main(std::vector<std::string> args, std::string sys_config, MessageFn message_fn)
// 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);
}
AvrDude::AvrDude() {}
AvrDude::~AvrDude() {}
AvrDude& AvrDude::sys_config(std::string sys_config)
{
m_sys_config = std::move(sys_config);
return *this;
}
AvrDude& AvrDude::on_message(MessageFn fn)
{
m_message_fn = std::move(fn);
return *this;
}
AvrDude& AvrDude::on_progress(MessageFn fn)
{
m_progress_fn = std::move(fn);
return *this;
}
int AvrDude::run(std::vector<std::string> args)
{
std::vector<char *> c_args {{ const_cast<char*>(PACKAGE_NAME) }};
for (const auto &arg : args) {
c_args.push_back(const_cast<char*>(arg.data()));
}
::avrdude_message_handler_set(avrdude_message_handler_closure, reinterpret_cast<void*>(&message_fn));
const auto res = ::avrdude_main(static_cast<int>(c_args.size()), c_args.data(), sys_config.c_str());
if (m_message_fn) {
::avrdude_message_handler_set(avrdude_message_handler_closure, reinterpret_cast<void*>(&m_message_fn));
} else {
::avrdude_message_handler_set(nullptr, nullptr);
}
if (m_progress_fn) {
::avrdude_progress_handler_set(avrdude_progress_handler_closure, reinterpret_cast<void*>(&m_progress_fn));
} else {
::avrdude_progress_handler_set(nullptr, nullptr);
}
const auto res = ::avrdude_main(static_cast<int>(c_args.size()), c_args.data(), m_sys_config.c_str());
::avrdude_message_handler_set(nullptr, nullptr);
::avrdude_progress_handler_set(nullptr, nullptr);
return res;
}
int main(std::vector<std::string> args, std::string sys_config, std::ostream &os)
{
return main(std::move(args), std::move(sys_config), std::move([&os](const char *msg, unsigned /* size */) {
os << msg;
}));
}
}
}