example 'meshtastic': fix USB for macOS

This commit is contained in:
pls.153 2024-04-13 18:42:23 +02:00
parent 480e3eabfd
commit a8a6b0058b
3 changed files with 18 additions and 12 deletions

View file

@ -5,19 +5,24 @@
USB_ME::USB_ME() { USB_ME::USB_ME() {
const auto infos = QSerialPortInfo::availablePorts(); const auto infos = QSerialPortInfo::availablePorts();
const QStringList boards = { "RAK" }; // TODO: currently RAK only const QStringList boards = { "RAK", "UART" }; // TODO
for (const QSerialPortInfo& info : infos) { for (const QSerialPortInfo& info : infos) {
QString name(info.description() + " " + info.manufacturer()); QString name(info.description() + " " + info.manufacturer());
qDebug() << "USB:" << info.portName() << name; QString port(info.portName());
for (auto board : boards) { qDebug() << "USB:" << port << name;
if (name.contains(board, Qt::CaseInsensitive)) { if (port.startsWith("tty") &&
setPortName(info.portName()); (port.contains("ACM", Qt::CaseInsensitive) || // Linux
goto done; port.contains("USB", Qt::CaseInsensitive))) { // macOS
for (auto board : boards) {
if (name.contains(board, Qt::CaseInsensitive)) {
setPortName(info.portName());
goto done;
}
} }
} }
} }
done: done:
connect(this, &QSerialPort::readyRead, this, &USB_ME::read); connect(this, &QSerialPort::readyRead, this, &USB_ME::read2);
connect(this, &QSerialPort::errorOccurred, connect(this, &QSerialPort::errorOccurred,
[](QSerialPort::SerialPortError error) { [](QSerialPort::SerialPortError error) {
if (error != QSerialPort::NoError) { if (error != QSerialPort::NoError) {
@ -54,10 +59,11 @@ void USB_ME::write2(const QByteArray& data) {
} }
} }
void USB_ME::read() { void USB_ME::read2() {
const char header[] = { '\x94', '\xc3' };
static QTimer* timer = nullptr; static QTimer* timer = nullptr;
QByteArray data(readAll()); const char header[] = { '\x94', '\xc3' };
const int MAX = 0xffff;
QByteArray data(read(MAX));
if (data.startsWith(header)) { // skip evtl. debug log if (data.startsWith(header)) { // skip evtl. debug log
if (state == Open) { if (state == Open) {
Q_EMIT setReady(portName()); Q_EMIT setReady(portName());

View file

@ -19,7 +19,7 @@ public:
public Q_SLOTS: public Q_SLOTS:
void wantConfigId(); void wantConfigId();
void write2(const QByteArray&); void write2(const QByteArray&);
void read(); void read2();
Q_SIGNALS: Q_SIGNALS:
void setReady(const QString&); void setReady(const QString&);

View file

@ -80,7 +80,7 @@
(defun set-ready-usb (port) ; see Qt (defun set-ready-usb (port) ; see Qt
(setf *ready* t (setf *ready* t
*mode* :usb) *mode* :usb)
(app:toast (x:cc (tr "radio") ": " port) 2) (app:toast (x:cc "USB: " port) 2)
(get-node-config nil) (get-node-config nil)
(values)) (values))