Skip to content

Commit

Permalink
Misc
Browse files Browse the repository at this point in the history
Android: Add disconnect, re-pair and factory reset
Qt: Show better log output
Qt: Show message in statusbar(PC)/as toast(Android)
Qt: Show the same MAC address only once
Qt: Fix command for setting LDAC mode
Qt: Add command for controlling playing state
Qt: Fix logic for setting ambient sound volume
  • Loading branch information
wh201906 committed Jun 15, 2023
1 parent 7366980 commit fd66ea8
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 19 deletions.
20 changes: 16 additions & 4 deletions Android/app/src/main/res/raw/cmd.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
"name": "cmd_game_off",
"cmd": "0900"
},
{
"name": "cmd_poweroff",
"cmd": "CE"
},
{
"name": "cmd_ldac_off",
"cmd": "4900"
Expand All @@ -50,5 +46,21 @@
{
"name": "cmd_ldac_96k",
"cmd": "4902"
},
{
"name": "cmd_poweroff",
"cmd": "CE"
},
{
"name": "cmd_disconnect",
"cmd": "CD"
},
{
"name": "cmd_re_pair",
"cmd": "CF"
},
{
"name": "cmd_factory_reset",
"cmd": "07"
}
]
3 changes: 3 additions & 0 deletions Android/app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<string name="cmd_ldac_off">LDAC: OFF</string>
<string name="cmd_ldac_48k">LDAC: 48kHz/44.1kHz</string>
<string name="cmd_ldac_96k">LDAC: 96kHz</string>
<string name="cmd_disconnect">断开连接</string>
<string name="cmd_factory_reset">恢复出厂设置</string>
<string name="cmd_re_pair">重新配对</string>
</resources>
3 changes: 3 additions & 0 deletions Android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<string name="cmd_ldac_48k">LDAC: 48kHz/44.1kHz</string>
<string name="cmd_ldac_96k">LDAC: 96kHz</string>
<string name="cmd_poweroff">Poweroff</string>
<string name="cmd_disconnect">Disconnect</string>
<string name="cmd_re_pair">Re-pair</string>
<string name="cmd_factory_reset">Factory Reset</string>

<string name="toast_no_bluetooth">Bluetooth adapter not found</string>
<string name="toast_bluetooth_not_open">Bluetooth not open</string>
Expand Down
16 changes: 9 additions & 7 deletions Qt/comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Comm::Comm(QObject *parent)

bool Comm::sendCommand(const QByteArray& cmd, bool isRaw)
{
qDebug() << "send:" << cmd.toHex();
if(isRaw)
return write(cmd);
else
return write(addChecksum(addPacketHead(cmd)));
QByteArray data = cmd;
if(!isRaw)
data = addChecksum(addPacketHead(cmd));
qDebug() << "send:" << data.toHex();
return write(data);
}

bool Comm::sendCommand(const char *hexCmd, bool isRaw)
Expand Down Expand Up @@ -73,15 +73,17 @@ QByteArray Comm::checkValidity(QByteArray data)
if(data[0] != '\xBB' && data[0] != '\xCC')
{
qDebug() << "error:"
<< "unexpected head:" << (int)data[0];
<< "unexpected head:" << (int)data[0]
<< "data:" << data.toHex();
return QByteArray();
}
int expectedLength = (int)data[1] + 4;
if(expectedLength != data.length())
{
qDebug() << "packet length error:"
<< "expected:" << expectedLength
<< "received:" << data.length();
<< "received:" << data.length()
<< "data:" << data.toHex();
return QByteArray();
}
return removeCheckSum(data);
Expand Down
1 change: 1 addition & 0 deletions Qt/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected slots:
signals:
void newData(const QByteArray& data);
void stateChanged(bool connected);
void showMessage(const QString& msg);
};

#endif // COMM_H
6 changes: 6 additions & 0 deletions Qt/commrfcomm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ void CommRFCOMM::onStateChanged()
{
QBluetoothSocket::SocketState state = m_socket->state();
if(state == QBluetoothSocket::ConnectedState)
{
emit stateChanged(true);
emit showMessage(tr("Device Connected"));
}
else if(state == QBluetoothSocket::UnconnectedState)
{
emit stateChanged(false);
emit showMessage(tr("Device Disconnected"));
}
}
10 changes: 10 additions & 0 deletions Qt/deviceform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ DeviceForm::DeviceForm(QWidget *parent) :
ui->disconnectButton->setVisible(false);
ui->searchStopButton->setVisible(false);

ui->deviceTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

connect(ui->searchRFCOMMButton, &QPushButton::clicked, this, &DeviceForm::onSearchButtonClicked);
connect(ui->searchBLEButton, &QPushButton::clicked, this, &DeviceForm::onSearchButtonClicked);
connect(ui->deviceTableWidget, &QTableWidget::cellClicked, this, &DeviceForm::onDeviceTableCellClicked);
Expand All @@ -47,6 +49,7 @@ void DeviceForm::onSearchButtonClicked()
return;
}
ui->deviceTableWidget->setRowCount(0);
m_shownDevices.clear();
#ifdef Q_OS_ANDROID
getBondedTarget(m_isCurrDiscoveryMethodBLE);
#endif
Expand All @@ -63,6 +66,11 @@ void DeviceForm::onDeviceDiscovered(const QBluetoothDeviceInfo &info)
{
QString address = info.address().toString();
QString name = info.name();
if(m_shownDevices.contains(address, Qt::CaseInsensitive))
{
qDebug() << "dumplicate:" << address << name;
return;
}
QTableWidget* deviceTable = ui->deviceTableWidget;
int i;

Expand All @@ -78,6 +86,7 @@ void DeviceForm::onDeviceDiscovered(const QBluetoothDeviceInfo &info)
typeItem->setText(tr("RFCOMM"));
typeItem->setData(Qt::UserRole, m_isCurrDiscoveryMethodBLE);
deviceTable->setItem(i, 2, typeItem);
m_shownDevices.append(address);


qDebug() << name
Expand Down Expand Up @@ -172,6 +181,7 @@ void DeviceForm::getBondedTarget(bool isBLE)
typeItem->setText(tr("RFCOMM"));
typeItem->setData(Qt::UserRole, isBLE);
deviceTable->setItem(i, 2, typeItem);
m_shownDevices.append(address);
}
}
#endif
Expand Down
1 change: 1 addition & 0 deletions Qt/deviceform.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private slots:

QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr;
bool m_isCurrDiscoveryMethodBLE = false;
QStringList m_shownDevices;
#ifdef Q_OS_ANDROID
void getBondedTarget(bool isBLE);
#endif
Expand Down
28 changes: 27 additions & 1 deletion Qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <QDebug>
#include <QScroller>
#include <QScrollBar>
#include <QMessageBox>
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#endif

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
Expand All @@ -15,14 +19,17 @@ MainWindow::MainWindow(QWidget *parent)

m_deviceForm = new DeviceForm;
m_w820p = new W820NBPlusForm;
ui->tabWidget->setTabText(0, m_w820p->windowTitle());
ui->tabWidget->insertTab(0, m_deviceForm, tr("Device"));
ui->scrollAreaWidgetContents->layout()->addWidget(m_w820p);

connect(m_deviceForm, &DeviceForm::connectTo, this, &MainWindow::connectToDevice);
connect(m_deviceForm, &DeviceForm::disconnectDevice, this, &MainWindow::disconnectDevice);
connect(this, &MainWindow::commStateChanged, m_deviceForm, &DeviceForm::onCommStateChanged);


#ifdef Q_OS_ANDROID
ui->statusBar->hide();
#endif
QScroller::grabGesture(ui->scrollArea);
// ui->scrollArea->horizontalScrollBar()->setEnabled(false);

Expand Down Expand Up @@ -53,6 +60,8 @@ void MainWindow::connectToDevice(const QString& address, bool isBLE)
connect(m_w820p, QOverload<const char*, bool>::of(&W820NBPlusForm::sendCommand), m_comm, QOverload<const char*, bool>::of(&Comm::sendCommand));
connect(m_comm, &Comm::newData, m_w820p, &W820NBPlusForm::processData);
connect(this, &MainWindow::readSettings, m_w820p, &W820NBPlusForm::readSettings);
connect(m_comm, &Comm::showMessage, this, &MainWindow::showMessage);
connect(m_w820p, &W820NBPlusForm::showMessage, this, &MainWindow::showMessage);
m_comm->open(address);
}

Expand Down Expand Up @@ -82,3 +91,20 @@ void MainWindow::on_readSettingsButton_clicked()
emit readSettings();
}

void MainWindow::showMessage(const QString& msg)
{
#ifdef Q_OS_ANDROID
QtAndroid::runOnAndroidThread([ = ]
{
QAndroidJniObject javaString = QAndroidJniObject::fromString(msg);
QAndroidJniObject toast = QAndroidJniObject::callStaticObjectMethod("android/widget/Toast", "makeText",
"(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;",
QtAndroid::androidActivity().object(),
javaString.object(),
jint(0)); // short toast
toast.callMethod<void>("show");
});
#else
ui->statusBar->showMessage(msg, 2000); // 2000ms is the duration of short toast
#endif
}
2 changes: 2 additions & 0 deletions Qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class MainWindow : public QMainWindow
MainWindow(QWidget *parent = nullptr);
~MainWindow();

public slots:
void showMessage(const QString &msg);
private:
Ui::MainWindow *ui;

Expand Down
3 changes: 2 additions & 1 deletion Qt/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
<string>mEDIFIER</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -75,6 +75,7 @@
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<resources/>
<connections/>
Expand Down
52 changes: 48 additions & 4 deletions Qt/w820nbplusform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ void W820NBPlusForm::onCheckBoxInControlSettingsGroupClicked()
void W820NBPlusForm::onBtnInLDACGroupClicked()
{
if(ui->LDACOFFButton->isChecked())
emit sendCommand("C400");
emit sendCommand("4900");
else if(ui->LDAC48kButton->isChecked())
emit sendCommand("C401");
emit sendCommand("4901");
else if(ui->LDAC96kButton->isChecked())
emit sendCommand("C402");
emit sendCommand("4902");
}

void W820NBPlusForm::on_gameModeBox_clicked()
Expand Down Expand Up @@ -112,6 +112,8 @@ void W820NBPlusForm::on_ASSetButton_clicked()
QByteArray cmd = "\xC1\x03";
cmd += (char)(6 + ui->ASBox->value());
emit sendCommand(cmd);
// setting ambient sound volume triggers ambient sound mode
ui->noiseAmbientSoundButton->setChecked(true);
}


Expand Down Expand Up @@ -293,7 +295,7 @@ void W820NBPlusForm::processData(const QByteArray& data)

void W820NBPlusForm::readSettings()
{
const int interval = 200;
const int interval = 150;
int i = 0;

QTimer::singleShot(i, [ = ] {on_batteryGetButton_clicked();});
Expand Down Expand Up @@ -336,3 +338,45 @@ void W820NBPlusForm::on_firmwareGetButton_clicked()
emit sendCommand("C6");
}


void W820NBPlusForm::on_cmdSentButton_clicked()
{
emit sendCommand(QByteArray::fromHex(ui->cmdEdit->text().toLatin1()), ui->cmdRawBox->isChecked());
}


void W820NBPlusForm::on_PCPlayButton_clicked()
{
emit sendCommand("C200");
}


void W820NBPlusForm::on_PCPauseButton_clicked()
{
emit sendCommand("C201");
}


void W820NBPlusForm::on_PCVolUpButton_clicked()
{
emit sendCommand("C202");
}


void W820NBPlusForm::on_PCVolDownButton_clicked()
{
emit sendCommand("C203");
}


void W820NBPlusForm::on_PCPrevButton_clicked()
{
emit sendCommand("C205");
}


void W820NBPlusForm::on_PCNextButton_clicked()
{
emit sendCommand("C204");
}

8 changes: 8 additions & 0 deletions Qt/w820nbplusform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public slots:
signals:
void sendCommand(const QByteArray& cmd, bool isRaw = false);
void sendCommand(const char* hexCmd, bool isRaw = false);
void showMessage(const QString& msg);
private slots:
void onBtnInNoiseGroupClicked();
void onBtnInSoundEffectGroupClicked();
Expand All @@ -49,6 +50,13 @@ private slots:
void on_batteryGetButton_clicked();
void on_MACGetButton_clicked();
void on_firmwareGetButton_clicked();
void on_cmdSentButton_clicked();
void on_PCPlayButton_clicked();
void on_PCPauseButton_clicked();
void on_PCVolUpButton_clicked();
void on_PCVolDownButton_clicked();
void on_PCPrevButton_clicked();
void on_PCNextButton_clicked();
};

#endif // W820NBPLUSFORM_H
Loading

0 comments on commit fd66ea8

Please sign in to comment.