Skip to content

Commit cff2ce1

Browse files
chore: do something make ble happy
1 parent 01d82bc commit cff2ce1

File tree

4 files changed

+95
-131
lines changed

4 files changed

+95
-131
lines changed

src/LinksUi/BLECenterUi.cpp

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <wx/gbsizer.h>
1212
#include <wx/string.h>
1313

14-
#include "Utilities/BLEScanner.h"
14+
#include "Utilities/BLECenterManager.h"
1515
#include "Utilities/BaudRateComboBox.h"
1616
#include "Utilities/DataBitsComboBox.h"
1717
#include "Utilities/FlowBitsComboBox.h"
@@ -21,18 +21,29 @@
2121

2222
#include "Links/BLECenter.h"
2323

24+
class BLECenterUiPrivate
25+
{
26+
public:
27+
BLECenterUiPrivate(BLECenterUi *q_ptr)
28+
: q(q_ptr)
29+
{}
30+
31+
~BLECenterUiPrivate() = default;
32+
33+
public:
34+
BLECenterManager *m_bleMgr{nullptr};
35+
36+
private:
37+
BLECenterUi *q{nullptr};
38+
};
39+
2440
BLECenterUi::BLECenterUi(wxWindow *parent)
2541
: LinkUi(parent)
26-
, m_portNameComboBox(nullptr)
27-
, m_baudRateComboBox(nullptr)
28-
, m_dataBitsComboBox(nullptr)
29-
, m_stopBitsComboBox(nullptr)
30-
, m_parityComboBox(nullptr)
31-
, m_flowBitsComboBox(nullptr)
3242
{
33-
m_bleScanner = new BLEScanner(parent);
34-
Add(m_bleScanner, wxGBPosition(0, 0), wxGBSpan(1, 2), wxEXPAND | wxALL, 0);
35-
43+
d = new BLECenterUiPrivate(this);
44+
d->m_bleMgr = new BLECenterManager(parent);
45+
Add(d->m_bleMgr, wxGBPosition(0, 0), wxGBSpan(1, 2), wxEXPAND | wxALL, 0);
46+
#if 0
3647
m_portNameComboBox = new PortNameComboBox(parent);
3748
m_baudRateComboBox = new BaudRateComboBox(parent);
3849
m_dataBitsComboBox = new DataBitsComboBox(parent);
@@ -46,6 +57,7 @@ BLECenterUi::BLECenterUi(wxWindow *parent)
4657
SetupComboBox(m_stopBitsComboBox, _("Stop bits"), 4, parent);
4758
SetupComboBox(m_parityComboBox, _("Parity"), 5, parent);
4859
SetupComboBox(m_flowBitsComboBox, _("Flow bits"), 6, parent);
60+
#endif
4961

5062
AddGrowableCol(1);
5163
}
@@ -55,76 +67,30 @@ BLECenterUi::~BLECenterUi()
5567
delete dynamic_cast<BLECenter *>(GetLink());
5668
}
5769

58-
void BLECenterUi::Disable()
59-
{
60-
m_portNameComboBox->Disable();
61-
m_baudRateComboBox->Disable();
62-
m_dataBitsComboBox->Disable();
63-
m_stopBitsComboBox->Disable();
64-
m_flowBitsComboBox->Disable();
65-
m_parityComboBox->Disable();
66-
}
70+
void BLECenterUi::Disable() {}
6771

68-
void BLECenterUi::Enable()
69-
{
70-
m_portNameComboBox->Enable();
71-
m_baudRateComboBox->Enable();
72-
m_dataBitsComboBox->Enable();
73-
m_stopBitsComboBox->Enable();
74-
m_flowBitsComboBox->Enable();
75-
m_parityComboBox->Enable();
76-
}
72+
void BLECenterUi::Enable() {}
7773

7874
wxtJson BLECenterUi::DoSave() const
7975
{
8076
nlohmann::json json = nlohmann::json(nlohmann::json::object());
8177
BLECenterParameterKeys keys;
82-
json[keys.portName] = m_portNameComboBox->GetPortName().ToStdString();
83-
json[keys.baudRate] = static_cast<int>(m_baudRateComboBox->GetBaudRate());
84-
json[keys.dataBits] = static_cast<int>(m_dataBitsComboBox->GetDataBits());
85-
json[keys.stopBits] = static_cast<int>(m_stopBitsComboBox->GetStopBits());
86-
json[keys.flowControl] = static_cast<int>(m_flowBitsComboBox->GetFlowBits());
87-
json[keys.parity] = static_cast<int>(m_parityComboBox->GetParity());
8878
return json;
8979
}
9080

9181
void BLECenterUi::DoLoad(const wxtJson &json)
9282
{
9383
BLECenterParameterKeys keys;
94-
// clang-format off
95-
wxString portName = wxtGetJsonObjValue<std::string>(json, keys.portName, std::string(""));
96-
int baudRate = wxtGetJsonObjValue<int>(json, keys.baudRate, 9600);
97-
int dataBits = wxtGetJsonObjValue<int>(json, keys.dataBits, static_cast<int>(itas109::DataBits8));
98-
int stopBits = wxtGetJsonObjValue<int>(json, keys.stopBits, static_cast<int>(itas109::StopOne));
99-
int flowBits = wxtGetJsonObjValue<int>(json, keys.flowControl, static_cast<int>(itas109::FlowNone));
100-
int parity = wxtGetJsonObjValue<int>(json, keys.parity, static_cast<int>(itas109::ParityNone));
101-
// clang-format on
102-
103-
m_portNameComboBox->SetPortName(portName);
104-
m_baudRateComboBox->SetBaudRate(baudRate);
105-
m_dataBitsComboBox->SetDataBits(static_cast<itas109::DataBits>(dataBits));
106-
m_stopBitsComboBox->SetStopBits(static_cast<itas109::StopBits>(stopBits));
107-
m_flowBitsComboBox->SetFlowBits(static_cast<itas109::FlowControl>(flowBits));
108-
m_parityComboBox->SetParity(static_cast<itas109::Parity>(parity));
10984
}
11085

11186
void BLECenterUi::DoRefreshDevice()
11287
{
11388
if (GetLink()) {
11489
return;
11590
}
116-
117-
m_portNameComboBox->DoRefresh();
11891
}
11992

12093
Link *BLECenterUi::NewLink()
12194
{
12295
return new BLECenter();
12396
}
124-
125-
void BLECenterUi::SetupComboBox(wxComboBox *cb, const wxString &label, int row, wxWindow *parent)
126-
{
127-
auto text = new wxStaticText(parent, wxID_ANY, label);
128-
Add(text, wxGBPosition(row, 0), wxGBSpan(1, 1), wxALIGN_CENTER_VERTICAL | wxALL, 0);
129-
Add(cb, wxGBPosition(row, 1), wxGBSpan(1, 1), wxEXPAND | wxALL, 0);
130-
}

src/LinksUi/BLECenterUi.h

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010

1111
#include "LinkUi.h"
1212

13-
class BLEScanner;
14-
class BaudRateComboBox;
15-
class DataBitsComboBox;
16-
class FlowBitsComboBox;
17-
class ParityComboBox;
18-
class PortNameComboBox;
19-
class StopBitsComboBox;
13+
class BLECenterUiPrivate;
2014
class BLECenterUi : public LinkUi
2115
{
16+
BLECenterUiPrivate *d{nullptr};
17+
2218
public:
2319
BLECenterUi(wxWindow *parent = nullptr);
2420
~BLECenterUi();
@@ -31,16 +27,4 @@ class BLECenterUi : public LinkUi
3127

3228
protected:
3329
Link *NewLink() override;
34-
35-
private:
36-
void SetupComboBox(wxComboBox *cb, const wxString &label, int row, wxWindow *parent);
37-
38-
private:
39-
BLEScanner *m_bleScanner;
40-
PortNameComboBox *m_portNameComboBox;
41-
BaudRateComboBox *m_baudRateComboBox;
42-
DataBitsComboBox *m_dataBitsComboBox;
43-
StopBitsComboBox *m_stopBitsComboBox;
44-
FlowBitsComboBox *m_flowBitsComboBox;
45-
ParityComboBox *m_parityComboBox;
4630
};
Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* wxTools is licensed according to the terms in the file LICENCE(GPL V3) in the root of the source
77
* code directory.
88
**************************************************************************************************/
9-
#include "BLEScanner.h"
9+
#include "BLECenterManager.h"
1010

1111
#include <wx/artprov.h>
1212
#include <wx/bmpbuttn.h>
@@ -19,18 +19,18 @@
1919

2020
#include "Common/wxTools.h"
2121

22-
IMPLEMENT_ABSTRACT_CLASS(BLEScanner, wxPanel)
23-
BEGIN_EVENT_TABLE(BLEScanner, wxPanel)
24-
EVT_THREAD(wxtID_SCAN_START, BLEScanner::onScanStart)
25-
EVT_THREAD(wxtID_SCAN_FOUND, BLEScanner::onScanFound)
26-
EVT_THREAD(wxtID_SCAN_UPDATED, BLEScanner::onScanUpdated)
27-
EVT_THREAD(wxtID_SCAN_STOP, BLEScanner::onScanStop)
22+
IMPLEMENT_ABSTRACT_CLASS(BLECenterManager, wxPanel)
23+
BEGIN_EVENT_TABLE(BLECenterManager, wxPanel)
24+
EVT_THREAD(wxtID_SCAN_START, BLECenterManager::onScanStart)
25+
EVT_THREAD(wxtID_SCAN_FOUND, BLECenterManager::onScanFound)
26+
EVT_THREAD(wxtID_SCAN_UPDATED, BLECenterManager::onScanUpdated)
27+
EVT_THREAD(wxtID_SCAN_STOP, BLECenterManager::onScanStop)
2828
END_EVENT_TABLE()
2929

30-
class BLEScannerThread : public wxThread
30+
class BLECenterManagerThread : public wxThread
3131
{
3232
public:
33-
BLEScannerThread(wxPanel *panel)
33+
BLECenterManagerThread(wxPanel *panel)
3434
: wxThread(wxTHREAD_JOINABLE)
3535
, m_panel(panel)
3636
{}
@@ -70,7 +70,7 @@ class BLEScannerThread : public wxThread
7070
protected:
7171
ExitCode Entry() override
7272
{
73-
SimpleBLE::Adapter adapter = BLEScannerThread::getAdapter();
73+
SimpleBLE::Adapter adapter = BLECenterManagerThread::getAdapter();
7474
if (!adapter.initialized()) {
7575
wxtWarning() << "No BLE adapter found, scanning thread will exit.";
7676
return nullptr;
@@ -183,23 +183,25 @@ class BLEScannerThread : public wxThread
183183
wxPanel *m_panel{nullptr};
184184
};
185185

186-
class BLEScannerPrivate
186+
class BLECenterManagerPrivate
187187
{
188188
public:
189-
BLEScannerPrivate(BLEScanner *q_ptr)
189+
BLECenterManagerPrivate(BLECenterManager *q_ptr)
190190
: q(q_ptr)
191191
{}
192-
~BLEScannerPrivate() = default;
192+
~BLECenterManagerPrivate() = default;
193193

194194
public:
195195
wxComboBox *m_blePeripheralComboBox{nullptr};
196-
wxBitmapButton *m_refreshButton{nullptr};
197-
BLEScannerThread *m_thread{nullptr};
196+
wxComboBox *m_bleServiceComboBox{nullptr};
197+
wxComboBox *m_bleCharacteristicComboBox{nullptr};
198+
wxComboBox *m_bleDescriptorComboBox{nullptr};
199+
BLECenterManagerThread *m_thread{nullptr};
198200

199201
public:
200202
void DoRefreshDevice()
201203
{
202-
SimpleBLE::Adapter adapter = BLEScannerThread::getAdapter();
204+
SimpleBLE::Adapter adapter = BLECenterManagerThread::getAdapter();
203205
if (!adapter.initialized()) {
204206
//wxMessageBox(_("No BLE adapter found."), _("Error"), wxOK | wxICON_ERROR);
205207
return;
@@ -211,59 +213,71 @@ class BLEScannerPrivate
211213
return;
212214
}
213215

214-
m_thread = new BLEScannerThread(q);
216+
m_thread = new BLECenterManagerThread(q);
215217
m_thread->Create();
216218
m_thread->Run();
217219
}
218220

221+
void DoAddComboBox(wxGridBagSizer *sizer, const wxString &label, wxComboBox *control)
222+
{
223+
auto text = new wxStaticText(q, wxID_ANY, label);
224+
sizer->Add(text,
225+
wxGBPosition(m_rowCount, 0),
226+
wxGBSpan(1, 1),
227+
wxALIGN_CENTER_VERTICAL | wxALL,
228+
0);
229+
m_rowCount++;
230+
sizer->Add(control, wxGBPosition(m_rowCount, 0), wxGBSpan(1, 1), wxEXPAND | wxALL, 0);
231+
m_rowCount++;
232+
}
233+
219234
private:
220-
BLEScanner *q{nullptr};
235+
BLECenterManager *q{nullptr};
236+
int m_rowCount{0};
221237
};
222238

223-
BLEScanner::BLEScanner(wxWindow *parent)
239+
#define BLEComboBox \
240+
wxComboBox(this, \
241+
wxID_ANY, \
242+
wxEmptyString, \
243+
wxDefaultPosition, \
244+
wxDefaultSize, \
245+
0, \
246+
nullptr, \
247+
wxCB_READONLY)
248+
249+
BLECenterManager::BLECenterManager(wxWindow *parent)
224250
: wxPanel(parent)
225251
{
226252
wxGridBagSizer *sizer = new wxGridBagSizer(0, 0);
227253
SetSizer(sizer);
228254

229-
d = new BLEScannerPrivate(this);
230-
auto text = new wxStaticText(this, wxID_ANY, _("Peripheral"));
231-
sizer->Add(text, wxGBPosition(0, 0), wxGBSpan(1, 1), wxALIGN_CENTER_VERTICAL | wxALL, 0);
232-
233-
wxBitmapBundle refreshIcon = wxArtProvider::GetBitmapBundle(wxART_REFRESH, wxART_BUTTON);
234-
d->m_refreshButton = new wxBitmapButton(this, wxID_ANY, refreshIcon);
235-
d->m_refreshButton->SetToolTip(_("Refresh BLE device list"));
236-
d->m_refreshButton->Bind(wxEVT_BUTTON, [this](wxCommandEvent &event) { d->DoRefreshDevice(); });
237-
d->m_refreshButton->Hide();
238-
auto refreshSizer = new wxBoxSizer(wxHORIZONTAL);
239-
refreshSizer->AddStretchSpacer();
240-
refreshSizer->Add(d->m_refreshButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 0);
241-
sizer->Add(refreshSizer, wxGBPosition(0, 1), wxGBSpan(1, 1), wxEXPAND | wxALL, 0);
242-
243-
d->m_blePeripheralComboBox = new wxComboBox(this,
244-
wxID_ANY,
245-
wxEmptyString,
246-
wxDefaultPosition,
247-
wxDefaultSize,
248-
0,
249-
nullptr,
250-
wxCB_READONLY);
251-
sizer->Add(d->m_blePeripheralComboBox, wxGBPosition(1, 0), wxGBSpan(1, 2), wxEXPAND | wxALL, 0);
252-
sizer->AddGrowableCol(1);
255+
d = new BLECenterManagerPrivate(this);
256+
d->m_blePeripheralComboBox = new BLEComboBox;
257+
d->m_bleServiceComboBox = new BLEComboBox;
258+
d->m_bleCharacteristicComboBox = new BLEComboBox;
259+
d->m_bleDescriptorComboBox = new BLEComboBox;
260+
261+
d->DoAddComboBox(sizer, _("Peripheral"), d->m_blePeripheralComboBox);
262+
d->DoAddComboBox(sizer, _("Service"), d->m_bleServiceComboBox);
263+
d->DoAddComboBox(sizer, _("Characteristic"), d->m_bleCharacteristicComboBox);
264+
d->DoAddComboBox(sizer, _("Descriptor"), d->m_bleDescriptorComboBox);
265+
266+
sizer->AddGrowableCol(0);
253267
d->DoRefreshDevice();
254268
}
255269

256-
BLEScanner::~BLEScanner()
270+
BLECenterManager::~BLECenterManager()
257271
{
258272
// Nothing to do yet...
259273
}
260274

261-
void BLEScanner::onScanStart(wxThreadEvent &e)
275+
void BLECenterManager::onScanStart(wxThreadEvent &e)
262276
{
263277
wxtInfo() << "Scan started.";
264278
}
265279

266-
void BLEScanner::onScanFound(wxThreadEvent &e)
280+
void BLECenterManager::onScanFound(wxThreadEvent &e)
267281
{
268282
wxtBlePeripheralItem item = e.GetPayload<wxtBlePeripheralItem>();
269283
if (item.identifier.IsEmpty()) {
@@ -279,7 +293,7 @@ void BLEScanner::onScanFound(wxThreadEvent &e)
279293
}
280294
}
281295

282-
void BLEScanner::onScanUpdated(wxThreadEvent &e)
296+
void BLECenterManager::onScanUpdated(wxThreadEvent &e)
283297
{
284298
// Update existing item
285299
wxtBlePeripheralItem item = e.GetPayload<wxtBlePeripheralItem>();
@@ -300,7 +314,7 @@ void BLEScanner::onScanUpdated(wxThreadEvent &e)
300314
onScanFound(e);
301315
}
302316

303-
void BLEScanner::onScanStop(wxThreadEvent &e)
317+
void BLECenterManager::onScanStop(wxThreadEvent &e)
304318
{
305319
wxtInfo() << "Scan stopped.";
306320
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ struct wxtBlePeripheralItem
2323
int rssi;
2424
};
2525

26-
class BLEScannerPrivate;
27-
class BLEScanner : public wxPanel
26+
class BLECenterManagerPrivate;
27+
class BLECenterManager : public wxPanel
2828
{
29-
BLEScannerPrivate *d{nullptr};
29+
BLECenterManagerPrivate *d{nullptr};
3030

3131
public:
32-
BLEScanner(wxWindow *parent);
33-
~BLEScanner() override;
32+
BLECenterManager(wxWindow *parent);
33+
~BLECenterManager() override;
3434

3535
void onScanStart(wxThreadEvent &e);
3636
void onScanFound(wxThreadEvent &e);
3737
void onScanUpdated(wxThreadEvent &e);
3838
void onScanStop(wxThreadEvent &e);
3939

4040
private:
41-
DECLARE_DYNAMIC_CLASS(BLEScanner);
41+
DECLARE_DYNAMIC_CLASS(BLECenterManager);
4242
DECLARE_EVENT_TABLE();
4343
};

0 commit comments

Comments
 (0)