Added friendly names to serial ports, added automatic selection of Prusa's printers in the firmware updater dialog

This commit is contained in:
bubnikv 2018-05-21 16:14:20 +02:00 committed by Vojtech Kral
parent 7f8265e2be
commit 33f21422e6
8 changed files with 187 additions and 94 deletions

View file

@ -23,6 +23,7 @@
#include "libslic3r/Utils.hpp"
#include "avrdude/avrdude-slic3r.hpp"
#include "GUI.hpp"
#include "../Utils/Serial.hpp"
namespace fs = boost::filesystem;
@ -58,6 +59,7 @@ struct FirmwareDialog::priv
FirmwareDialog *q; // PIMPL back pointer ("Q-Pointer")
wxComboBox *port_picker;
std::vector<Utils::SerialPortInfo> ports;
wxFilePickerCtrl *hex_picker;
wxStaticText *txt_status;
wxStaticText *txt_progress;
@ -95,13 +97,22 @@ struct FirmwareDialog::priv
void FirmwareDialog::priv::find_serial_ports()
{
auto ports = GUI::scan_serial_ports();
port_picker->Clear();
for (const auto &port : ports) { port_picker->Append(port); }
if (ports.size() > 0 && port_picker->GetValue().IsEmpty()) {
port_picker->SetSelection(0);
auto new_ports = Utils::scan_serial_ports_extended();
if (new_ports != this->ports) {
this->ports = new_ports;
port_picker->Clear();
for (const auto &port : this->ports)
port_picker->Append(port.friendly_name);
if (ports.size() > 0) {
int idx = port_picker->GetValue().IsEmpty() ? 0 : -1;
for (int i = 0; i < (int)this->ports.size(); ++ i)
if (this->ports[i].is_printer) {
idx = i;
break;
}
if (idx != -1)
port_picker->SetSelection(idx);
}
}
}
@ -140,9 +151,15 @@ void FirmwareDialog::priv::flashing_status(bool value, AvrDudeComplete complete)
void FirmwareDialog::priv::perform_upload()
{
auto filename = hex_picker->GetPath();
auto port = port_picker->GetValue();
if (filename.IsEmpty() || port.IsEmpty()) { return; }
auto filename = hex_picker->GetPath();
std::string port = port_picker->GetValue().ToStdString();
int selection = port_picker->GetSelection();
if (selection != -1) {
// Verify whether the combo box list selection equals to the combo box edit value.
if (this->ports[selection].friendly_name == port)
port = this->ports[selection].port;
}
if (filename.IsEmpty() || port.empty()) { return; }
flashing_status(true);
@ -150,7 +167,7 @@ void FirmwareDialog::priv::perform_upload()
"-v",
"-p", "atmega2560",
"-c", "wiring",
"-P", port.ToStdString(),
"-P", port,
"-b", "115200", // XXX: is this ok to hardcode?
"-D",
"-U", (boost::format("flash:w:%1%:i") % filename.ToStdString()).str()

View file

@ -4,7 +4,6 @@
#include <assert.h>
#include <cmath>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/split.hpp>
@ -25,7 +24,6 @@
#undef max
#endif
#include "boost/nowide/convert.hpp"
#pragma comment(lib, "user32.lib")
#endif
#include <wx/app.h>
@ -88,83 +86,6 @@ void enable_screensaver()
#endif
}
std::vector<std::string> scan_serial_ports()
{
std::vector<std::string> out;
#ifdef _WIN32
// 1) Open the registry key SERIALCOM.
HKEY hKey;
LONG lRes = ::RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &hKey);
assert(lRes == ERROR_SUCCESS);
if (lRes == ERROR_SUCCESS) {
// 2) Get number of values of SERIALCOM key.
DWORD cValues; // number of values for key
{
TCHAR achKey[255]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys=0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
// Get the class name and the value count.
lRes = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
assert(lRes == ERROR_SUCCESS);
}
// 3) Read the SERIALCOM values.
{
DWORD dwIndex = 0;
for (int i = 0; i < cValues; ++ i, ++ dwIndex) {
wchar_t valueName[2048];
DWORD valNameLen = 2048;
DWORD dataType;
wchar_t data[2048];
DWORD dataSize = 4096;
lRes = ::RegEnumValueW(hKey, dwIndex, valueName, &valNameLen, nullptr, &dataType, (BYTE*)&data, &dataSize);
if (lRes == ERROR_SUCCESS && dataType == REG_SZ && valueName[0] != 0)
out.emplace_back(boost::nowide::narrow(data));
}
}
::RegCloseKey(hKey);
}
#else
// UNIX and OS X
std::initializer_list<const char*> prefixes { "ttyUSB" , "ttyACM", "tty.", "cu.", "rfcomm" };
for (auto &dir_entry : boost::filesystem::directory_iterator(boost::filesystem::path("/dev"))) {
std::string name = dir_entry.path().filename().string();
for (const char *prefix : prefixes) {
if (boost::starts_with(name, prefix)) {
out.emplace_back(dir_entry.path().string());
break;
}
}
}
#endif
out.erase(std::remove_if(out.begin(), out.end(),
[](const std::string &key){
return boost::starts_with(key, "Bluetooth") || boost::starts_with(key, "FireFly");
}),
out.end());
return out;
}
bool debugged()
{
#ifdef _WIN32

View file

@ -68,7 +68,6 @@ inline t_file_wild_card& get_file_wild_card() {
void disable_screensaver();
void enable_screensaver();
std::vector<std::string> scan_serial_ports();
bool debugged();
void break_to_debugger();

View file

@ -6,6 +6,7 @@
#include "slic3r/Utils/Http.hpp"
#include "slic3r/Utils/OctoPrint.hpp"
#include "slic3r/Utils/Serial.hpp"
#include "BonjourDialog.hpp"
#include "WipeTowerDialog.hpp"
#include "ButtonsDescription.hpp"
@ -1693,7 +1694,7 @@ void TabPrinter::build()
void TabPrinter::update_serial_ports(){
Field *field = get_field("serial_port");
Choice *choice = static_cast<Choice *>(field);
choice->set_values(scan_serial_ports());
choice->set_values(Utils::scan_serial_ports());
}
void TabPrinter::extruders_count_changed(size_t extruders_count){