Skip to content

Commit ec77c65

Browse files
k1-801CalcProgrammer1
authored andcommitted
Rudimentary rescanning implemented but button for it not added due to SDK conflicts. Stop detection button.
Commit amended by Adam Honse <calcprogrammer1@gmail.com>
1 parent be84a60 commit ec77c65

5 files changed

Lines changed: 112 additions & 28 deletions

File tree

ResourceManager.cpp

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,13 @@ ResourceManager::ResourceManager()
2323
{
2424
detection_percent = 100;
2525
detection_string = "";
26+
detection_is_required = false;
27+
DetectDevicesThread = nullptr;
2628
}
2729

2830
ResourceManager::~ResourceManager()
2931
{
30-
ResourceManager::get()->WaitForDeviceDetection();
31-
32-
for(RGBController* rgb_controller : rgb_controllers)
33-
{
34-
delete rgb_controller;
35-
}
36-
37-
for(i2c_smbus_interface* bus : busses)
38-
{
39-
delete bus;
40-
}
41-
42-
DetectDevicesThread->join();
43-
delete DetectDevicesThread;
32+
Cleanup();
4433
}
4534

4635
void ResourceManager::RegisterI2CBus(i2c_smbus_interface *bus)
@@ -104,19 +93,59 @@ void ResourceManager::DeviceListChanged()
10493

10594
unsigned int ResourceManager::GetDetectionPercent()
10695
{
107-
return(detection_percent);
96+
return (detection_percent.load());
10897
}
10998

110-
std::string ResourceManager::GetDetectionString()
99+
const char *ResourceManager::GetDetectionString()
111100
{
112-
return(detection_string);
101+
return (detection_string);
102+
}
103+
104+
void ResourceManager::Cleanup()
105+
{
106+
ResourceManager::get()->WaitForDeviceDetection();
107+
108+
for(RGBController* rgb_controller : rgb_controllers)
109+
{
110+
delete rgb_controller;
111+
}
112+
rgb_controllers.clear();
113+
114+
for(i2c_smbus_interface* bus : busses)
115+
{
116+
delete bus;
117+
}
118+
busses.clear();
119+
120+
if(DetectDevicesThread)
121+
{
122+
DetectDevicesThread->join();
123+
delete DetectDevicesThread;
124+
DetectDevicesThread = nullptr;
125+
}
113126
}
114127

115128
void ResourceManager::DetectDevices()
116129
{
130+
/*-------------------------------------------------*\
131+
| Do nothing is it is already detecting devices |
132+
\*-------------------------------------------------*/
133+
if(detection_is_required.load())
134+
{
135+
return;
136+
}
137+
138+
/*-------------------------------------------------*\
139+
| If there's anything left from the last time, |
140+
| we shall remove it first |
141+
\*-------------------------------------------------*/
142+
detection_percent = 0;
143+
Cleanup();
144+
117145
/*-------------------------------------------------*\
118146
| Start the device detection thread |
119147
\*-------------------------------------------------*/
148+
detection_is_required = true;
120149
DetectDevicesThread = new std::thread(&ResourceManager::DetectDevicesThreadFunction, this);
121150

122151
/*-------------------------------------------------*\
@@ -161,17 +190,17 @@ void ResourceManager::DetectDevicesThreadFunction()
161190
/*-------------------------------------------------*\
162191
| Detect i2c busses |
163192
\*-------------------------------------------------*/
164-
for(unsigned int i2c_bus_detector_idx = 0; i2c_bus_detector_idx < i2c_bus_detectors.size(); i2c_bus_detector_idx++)
193+
for(unsigned int i2c_bus_detector_idx = 0; i2c_bus_detector_idx < i2c_bus_detectors.size() && detection_is_required.load(); i2c_bus_detector_idx++)
165194
{
166195
i2c_bus_detectors[i2c_bus_detector_idx](busses);
167196
}
168197

169198
/*-------------------------------------------------*\
170199
| Detect i2c devices |
171200
\*-------------------------------------------------*/
172-
for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < i2c_device_detectors.size(); i2c_detector_idx++)
201+
for(unsigned int i2c_detector_idx = 0; i2c_detector_idx < i2c_device_detectors.size() && detection_is_required.load(); i2c_detector_idx++)
173202
{
174-
detection_string = i2c_device_detector_strings[i2c_detector_idx];
203+
detection_string = i2c_device_detector_strings[i2c_detector_idx].c_str();
175204
DeviceListChanged();
176205

177206
bool this_device_disabled = false;
@@ -207,9 +236,9 @@ void ResourceManager::DetectDevicesThreadFunction()
207236
/*-------------------------------------------------*\
208237
| Detect other devices |
209238
\*-------------------------------------------------*/
210-
for(unsigned int detector_idx = 0; detector_idx < device_detectors.size(); detector_idx++)
239+
for(unsigned int detector_idx = 0; detector_idx < device_detectors.size() && detection_is_required.load(); detector_idx++)
211240
{
212-
detection_string = device_detector_strings[detector_idx];
241+
detection_string = device_detector_strings[detector_idx].c_str();
213242
DeviceListChanged();
214243

215244
bool this_device_disabled = false;
@@ -243,10 +272,26 @@ void ResourceManager::DetectDevicesThreadFunction()
243272
}
244273

245274
profile_manager.LoadSizeFromProfile("sizes.ors");
246-
275+
276+
/*-------------------------------------------------*\
277+
| Make sure that when the detection is done, |
278+
| progress bar is set to 100% |
279+
\*-------------------------------------------------*/
280+
281+
detection_is_required = false;
282+
detection_percent = 100;
283+
detection_string = "";
284+
247285
DetectDeviceMutex.unlock();
248286
}
249287

288+
void ResourceManager::StopDeviceDetection()
289+
{
290+
detection_is_required = false;
291+
detection_percent = 100;
292+
detection_string = "Stopping";
293+
}
294+
250295
void ResourceManager::WaitForDeviceDetection()
251296
{
252297
DetectDeviceMutex.lock();

ResourceManager.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,26 @@ class ResourceManager
3636
void RegisterDeviceListChangeCallback(ResourceManagerCallback new_callback, void * new_callback_arg);
3737

3838
unsigned int GetDetectionPercent();
39-
std::string GetDetectionString();
39+
const char* GetDetectionString();
4040

4141
void DeviceListChanged();
4242

43+
void Cleanup();
44+
4345
void DetectDevices();
4446

4547
void DetectDevicesThreadFunction();
4648

49+
void StopDeviceDetection();
50+
4751
void WaitForDeviceDetection();
4852

4953
private:
5054
static std::unique_ptr<ResourceManager> instance;
5155

52-
unsigned int detection_percent;
53-
std::string detection_string;
56+
std::atomic<bool> detection_is_required;
57+
std::atomic<unsigned int> detection_percent;
58+
const char* detection_string;
5459

5560
std::vector<i2c_smbus_interface*> busses;
5661
std::vector<RGBController*> rgb_controllers;

qt/OpenRGBDialog2.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ OpenRGBDialog2::OpenRGBDialog2(std::vector<i2c_smbus_interface *>& bus, std::vec
167167
UpdateProfileList();
168168

169169
/*-----------------------------------------------------*\
170-
| Update the device list |
170+
| Update the device list and make sure the |
171+
| ProgressBar gets a proper value |
171172
\*-----------------------------------------------------*/
172173
UpdateDevicesList();
173174
}
@@ -292,6 +293,11 @@ void OpenRGBDialog2::ClearDevicesList()
292293

293294
void OpenRGBDialog2::UpdateDevicesList()
294295
{
296+
/*-----------------------------------------------------*\
297+
| Clear on each update |
298+
\*-----------------------------------------------------*/
299+
ClearDevicesList();
300+
295301
/*-----------------------------------------------------*\
296302
| Set up list of devices |
297303
\*-----------------------------------------------------*/
@@ -452,7 +458,6 @@ void OpenRGBDialog2::on_QuickWhite()
452458

453459
void OpenRGBDialog2::on_ClientListUpdated()
454460
{
455-
ClearDevicesList();
456461
UpdateDevicesList();
457462

458463
ui->DetectionProgressBar->setValue(ResourceManager::get()->GetDetectionPercent());
@@ -462,6 +467,7 @@ void OpenRGBDialog2::on_ClientListUpdated()
462467
{
463468
ui->DetectionProgressBar->setVisible(false);
464469
ui->DetectionProgressLabel->setVisible(false);
470+
ui->ButtonStopDetection->setVisible(false);
465471

466472
ui->ButtonToggleDeviceView->setVisible(true);
467473
ui->ButtonLoadProfile->setVisible(true);
@@ -618,3 +624,23 @@ void Ui::OpenRGBDialog2::on_ButtonToggleDeviceView_clicked()
618624
device_view_showing = true;
619625
}
620626
}
627+
628+
void Ui::OpenRGBDialog2::on_ButtonStopDetection_clicked()
629+
{
630+
/*---------------------------------------------------------*\
631+
| Notify the detection thread that it has to die |
632+
\*---------------------------------------------------------*/
633+
ResourceManager::get()->StopDeviceDetection();
634+
635+
/*---------------------------------------------------------*\
636+
| Pretend we're done already by hiding the progress bar |
637+
\*---------------------------------------------------------*/
638+
ui->DetectionProgressBar->setVisible(false);
639+
ui->DetectionProgressLabel->setVisible(false);
640+
ui->ButtonStopDetection->setVisible(false);
641+
642+
ui->ButtonLoadProfile->setVisible(true);
643+
ui->ButtonSaveProfile->setVisible(true);
644+
ui->ButtonDeleteProfile->setVisible(true);
645+
ui->ProfileBox->setVisible(true);
646+
}

qt/OpenRGBDialog2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ private slots:
9494
void on_ButtonLoadProfile_clicked();
9595
void on_ButtonDeleteProfile_clicked();
9696
void on_ButtonToggleDeviceView_clicked();
97+
void on_ButtonStopDetection_clicked();
9798
};
9899

99100
#endif // OPENRGBDIALOG2_H

qt/OpenRGBDialog2.ui

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@
116116
</property>
117117
</widget>
118118
</item>
119+
<item>
120+
<widget class="QPushButton" name="ButtonStopDetection">
121+
<property name="text">
122+
<string>Cancel</string>
123+
</property>
124+
</widget>
125+
</item>
119126
</layout>
120127
</item>
121128
</layout>

0 commit comments

Comments
 (0)