Skip to content

Commit bbf104d

Browse files
committed
Refactor - DisplayHardware iterator functions, remove auto use explicit types
1 parent 7073839 commit bbf104d

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/Wippersnapper_demo.ino.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmp4dvs_pq5"
1+
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmp000bzb8y"
22
#include <Arduino.h>
33
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
44
# 16 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"

src/components/display/controller.cpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ bool DisplayController::Handle_Display_AddOrReplace(
4646
WS_DEBUG_PRINTLN(msgAdd->name);
4747

4848
// Does this display hw instance already exist?
49-
for (auto it = _hw_instances.begin(); it != _hw_instances.end(); ++it) {
50-
if (strcmp((*it)->getName(), msgAdd->name) == 0) {
51-
WS_DEBUG_PRINTLN("[display] Display instance already exists, removing...");
52-
delete *it;
53-
_hw_instances.erase(it);
54-
break;
49+
DisplayHardware *existingDisplay = findDisplay(msgAdd->name);
50+
if (existingDisplay != nullptr) {
51+
WS_DEBUG_PRINTLN("[display] Display exists, removing...");
52+
for (std::vector<DisplayHardware *>::iterator it = _hw_instances.begin();
53+
it != _hw_instances.end(); ++it) {
54+
if (*it == existingDisplay) {
55+
delete *it;
56+
_hw_instances.erase(it);
57+
break;
5558
}
59+
}
5660
}
5761

5862
// Configure display type
@@ -102,16 +106,25 @@ bool DisplayController::Handle_Display_AddOrReplace(
102106
*/
103107
bool DisplayController::Handle_Display_Remove(
104108
wippersnapper_display_v1_DisplayRemove *msgRemove) {
105-
// Find the display instance by name
106-
for (auto it = _hw_instances.begin(); it != _hw_instances.end(); ++it) {
107-
if (strcmp((*it)->getName(), msgRemove->name) == 0) {
109+
if (!msgRemove || !msgRemove->name)
110+
return false;
111+
112+
DisplayHardware *display = findDisplay(msgRemove->name);
113+
114+
if (display == nullptr)
115+
return false; // Display not found
116+
117+
// Remove from vector
118+
for (std::vector<DisplayHardware*>::iterator it = _hw_instances.begin();
119+
it != _hw_instances.end(); ++it) {
120+
if (*it == display) {
108121
delete *it;
109122
_hw_instances.erase(it);
110123
WS_DEBUG_PRINTLN("[display] Display removed successfully!");
111124
return true;
112125
}
113126
}
114-
WS_DEBUG_PRINTLN("[display] Could not remove display, not found!");
127+
115128
return false;
116129
}
117130

@@ -124,13 +137,7 @@ bool DisplayController::Handle_Display_Remove(
124137
bool DisplayController::Handle_Display_Write(
125138
wippersnapper_display_v1_DisplayWrite *msgWrite) {
126139
// Get the driver instance for the display
127-
DisplayHardware *display = nullptr;
128-
for (auto &hw_instance : _hw_instances) {
129-
if (strcmp(hw_instance->getName(), msgWrite->name) == 0) {
130-
display = hw_instance;
131-
break;
132-
}
133-
}
140+
DisplayHardware *display = findDisplay(msgWrite->name);
134141

135142
// Early-out if driver instance not found
136143
if (!display) {
@@ -175,4 +182,22 @@ void DisplayController::update(int32_t rssi, bool is_connected) {
175182

176183
WS.feedWDT();
177184
WS.runNetFSM();
185+
}
186+
187+
/*!
188+
* @brief Finds a DisplayHardware instance by its name.
189+
* @param name The name of the display to find.
190+
* @return Pointer to the DisplayHardware instance if found, nullptr otherwise.
191+
*/
192+
DisplayHardware* DisplayController::findDisplay(const char* name) {
193+
if (name == nullptr)
194+
return nullptr;
195+
196+
for (std::vector<DisplayHardware*>::iterator it = _hw_instances.begin(); it != _hw_instances.end(); ++it) {
197+
if (*it != nullptr && strcmp((*it)->getName(), name) == 0) {
198+
return *it;
199+
}
200+
}
201+
202+
return nullptr;
178203
}

src/components/display/controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class DisplayController {
3636
bool Handle_Display_Remove(wippersnapper_display_v1_DisplayRemove *msgRemove);
3737
bool Handle_Display_Write(wippersnapper_display_v1_DisplayWrite *msgWrite);
3838
void update(int32_t rssi, bool is_connected);
39-
4039
private:
40+
DisplayHardware* findDisplay(const char* name);
4141
std::vector<DisplayHardware *>
4242
_hw_instances; ///< Holds pointers to DisplayHardware instances
4343
unsigned long _last_bar_update; ///< Timestamp of last status bar update

0 commit comments

Comments
 (0)