FirmwareDialog: Fix progress display

This commit is contained in:
Vojtech Kral 2018-06-05 11:55:23 +02:00 committed by bubnikv
parent 2a07f3a0d5
commit 5414f7379d
4 changed files with 43 additions and 13 deletions

View file

@ -34,6 +34,7 @@ struct AvrDude::priv
{ {
std::string sys_config; std::string sys_config;
std::vector<std::string> args; std::vector<std::string> args;
RunFn run_fn;
MessageFn message_fn; MessageFn message_fn;
ProgressFn progress_fn; ProgressFn progress_fn;
CompleteFn complete_fn; CompleteFn complete_fn;
@ -94,6 +95,12 @@ AvrDude& AvrDude::args(std::vector<std::string> args)
return *this; return *this;
} }
AvrDude& AvrDude::on_run(RunFn fn)
{
if (p) { p->run_fn = std::move(fn); }
return *this;
}
AvrDude& AvrDude::on_message(MessageFn fn) AvrDude& AvrDude::on_message(MessageFn fn)
{ {
if (p) { p->message_fn = std::move(fn); } if (p) { p->message_fn = std::move(fn); }
@ -123,11 +130,17 @@ AvrDude::Ptr AvrDude::run()
if (self->p) { if (self->p) {
auto avrdude_thread = std::thread([self]() { auto avrdude_thread = std::thread([self]() {
auto res = self->p->run(); if (self->p->run_fn) {
if (self->p->complete_fn) { self->p->run_fn();
self->p->complete_fn(res); }
}
}); auto res = self->p->run();
if (self->p->complete_fn) {
self->p->complete_fn(res);
}
});
self->p->avrdude_thread = std::move(avrdude_thread); self->p->avrdude_thread = std::move(avrdude_thread);
} }

View file

@ -12,6 +12,7 @@ class AvrDude
{ {
public: public:
typedef std::shared_ptr<AvrDude> Ptr; typedef std::shared_ptr<AvrDude> Ptr;
typedef std::function<void()> RunFn;
typedef std::function<void(const char * /* msg */, unsigned /* size */)> MessageFn; typedef std::function<void(const char * /* msg */, unsigned /* size */)> MessageFn;
typedef std::function<void(const char * /* task */, unsigned /* progress */)> ProgressFn; typedef std::function<void(const char * /* task */, unsigned /* progress */)> ProgressFn;
typedef std::function<void(int /* exit status */)> CompleteFn; typedef std::function<void(int /* exit status */)> CompleteFn;
@ -29,6 +30,11 @@ public:
// Set avrdude cli arguments // Set avrdude cli arguments
AvrDude& args(std::vector<std::string> args); AvrDude& args(std::vector<std::string> args);
// Set a callback to be called just after run() before avrdude is ran
// This can be used to perform any needed setup tasks from the background thread.
// This has no effect when using run_sync().
AvrDude& on_run(RunFn fn);
// Set message output callback // Set message output callback
AvrDude& on_message(MessageFn fn); AvrDude& on_message(MessageFn fn);

View file

@ -376,6 +376,10 @@ static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen
FD_SET(fd->ifd, &rfds); FD_SET(fd->ifd, &rfds);
nfds = select(fd->ifd + 1, &rfds, NULL, NULL, &to2); nfds = select(fd->ifd + 1, &rfds, NULL, NULL, &to2);
// FIXME: The timeout has different behaviour on Linux vs other Unices
// On Linux, the timeout is modified by subtracting the time spent,
// on OS X (for example), it is not modified.
// POSIX recommends re-initializing it before selecting.
if (nfds == 0) { if (nfds == 0) {
avrdude_message(MSG_NOTICE2, "%s: ser_recv(): programmer is not responding\n", avrdude_message(MSG_NOTICE2, "%s: ser_recv(): programmer is not responding\n",
progname); progname);

View file

@ -10,6 +10,7 @@
#include <wx/event.h> #include <wx/event.h>
#include <wx/sizer.h> #include <wx/sizer.h>
#include <wx/settings.h> #include <wx/settings.h>
#include <wx/timer.h>
#include <wx/panel.h> #include <wx/panel.h>
#include <wx/button.h> #include <wx/button.h>
#include <wx/filepicker.h> #include <wx/filepicker.h>
@ -36,7 +37,7 @@ namespace Slic3r {
enum AvrdudeEvent enum AvrdudeEvent
{ {
AE_MESSAGE, AE_MESSAGE,
AE_PRORGESS, AE_PROGRESS,
AE_EXIT, AE_EXIT,
}; };
@ -62,7 +63,6 @@ struct FirmwareDialog::priv
std::vector<Utils::SerialPortInfo> ports; std::vector<Utils::SerialPortInfo> ports;
wxFilePickerCtrl *hex_picker; wxFilePickerCtrl *hex_picker;
wxStaticText *txt_status; wxStaticText *txt_status;
wxStaticText *txt_progress;
wxGauge *progressbar; wxGauge *progressbar;
wxCollapsiblePane *spoiler; wxCollapsiblePane *spoiler;
wxTextCtrl *txt_stdout; wxTextCtrl *txt_stdout;
@ -72,6 +72,8 @@ struct FirmwareDialog::priv
wxString btn_flash_label_ready; wxString btn_flash_label_ready;
wxString btn_flash_label_flashing; wxString btn_flash_label_flashing;
wxTimer timer_pulse;
// This is a shared pointer holding the background AvrDude task // This is a shared pointer holding the background AvrDude task
// also serves as a status indication (it is set _iff_ the background task is running, otherwise it is reset). // also serves as a status indication (it is set _iff_ the background task is running, otherwise it is reset).
AvrDude::Ptr avrdude; AvrDude::Ptr avrdude;
@ -83,6 +85,7 @@ struct FirmwareDialog::priv
q(q), q(q),
btn_flash_label_ready(_(L("Flash!"))), btn_flash_label_ready(_(L("Flash!"))),
btn_flash_label_flashing(_(L("Cancel"))), btn_flash_label_flashing(_(L("Cancel"))),
timer_pulse(q),
avrdude_config((fs::path(::Slic3r::resources_dir()) / "avrdude" / "avrdude.conf").string()), avrdude_config((fs::path(::Slic3r::resources_dir()) / "avrdude" / "avrdude.conf").string()),
progress_tasks_done(0), progress_tasks_done(0),
cancelled(false) cancelled(false)
@ -131,6 +134,7 @@ void FirmwareDialog::priv::flashing_status(bool value, AvrDudeComplete complete)
progressbar->SetValue(0); progressbar->SetValue(0);
progress_tasks_done = 0; progress_tasks_done = 0;
cancelled = false; cancelled = false;
timer_pulse.Start(50);
} else { } else {
auto text_color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT); auto text_color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
port_picker->Enable(); port_picker->Enable();
@ -186,6 +190,7 @@ void FirmwareDialog::priv::perform_upload()
avrdude = AvrDude() avrdude = AvrDude()
.sys_config(avrdude_config) .sys_config(avrdude_config)
.args(args) .args(args)
.on_run([]() { /* TODO: needed? */ })
.on_message(std::move([q](const char *msg, unsigned /* size */) { .on_message(std::move([q](const char *msg, unsigned /* size */) {
auto evt = new wxCommandEvent(EVT_AVRDUDE, q->GetId()); auto evt = new wxCommandEvent(EVT_AVRDUDE, q->GetId());
auto wxmsg = wxString::FromUTF8(msg); auto wxmsg = wxString::FromUTF8(msg);
@ -195,7 +200,7 @@ void FirmwareDialog::priv::perform_upload()
})) }))
.on_progress(std::move([q](const char * /* task */, unsigned progress) { .on_progress(std::move([q](const char * /* task */, unsigned progress) {
auto evt = new wxCommandEvent(EVT_AVRDUDE, q->GetId()); auto evt = new wxCommandEvent(EVT_AVRDUDE, q->GetId());
evt->SetExtraLong(AE_PRORGESS); evt->SetExtraLong(AE_PROGRESS);
evt->SetInt(progress); evt->SetInt(progress);
wxQueueEvent(q, evt); wxQueueEvent(q, evt);
})) }))
@ -226,19 +231,19 @@ void FirmwareDialog::priv::on_avrdude(const wxCommandEvent &evt)
txt_stdout->AppendText(evt.GetString()); txt_stdout->AppendText(evt.GetString());
break; break;
case AE_PRORGESS: case AE_PROGRESS:
// We try to track overall progress here. // We try to track overall progress here.
// When uploading the firmware, avrdude first reads a littlebit of status data, // When uploading the firmware, avrdude first reads a littlebit of status data,
// then performs write, then reading (verification). // then performs write, then reading (verification).
// We Pulse() during the first read and combine progress of the latter two tasks. // We ignore the first task (which just let's the timer_pulse work)
// and then display overall progress during the latter two tasks.
if (progress_tasks_done == 0) { if (progress_tasks_done > 0) {
progressbar->Pulse();
} else {
progressbar->SetValue(progress_tasks_done - 100 + evt.GetInt()); progressbar->SetValue(progress_tasks_done - 100 + evt.GetInt());
} }
if (evt.GetInt() == 100) { if (evt.GetInt() == 100) {
timer_pulse.Stop();
progress_tasks_done += 100; progress_tasks_done += 100;
} }
@ -376,6 +381,8 @@ FirmwareDialog::FirmwareDialog(wxWindow *parent) :
} }
}); });
Bind(wxEVT_TIMER, [this](wxTimerEvent &evt) { this->p->progressbar->Pulse(); });
Bind(EVT_AVRDUDE, [this](wxCommandEvent &evt) { this->p->on_avrdude(evt); }); Bind(EVT_AVRDUDE, [this](wxCommandEvent &evt) { this->p->on_avrdude(evt); });
Bind(wxEVT_CLOSE_WINDOW, [this](wxCloseEvent &evt) { Bind(wxEVT_CLOSE_WINDOW, [this](wxCloseEvent &evt) {