@@ -46,13 +46,17 @@ bool DisplayController::Handle_Display_AddOrReplace(
46
46
WS_DEBUG_PRINTLN (msgAdd->name );
47
47
48
48
// 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 ;
55
58
}
59
+ }
56
60
}
57
61
58
62
// Configure display type
@@ -102,16 +106,25 @@ bool DisplayController::Handle_Display_AddOrReplace(
102
106
*/
103
107
bool DisplayController::Handle_Display_Remove (
104
108
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) {
108
121
delete *it;
109
122
_hw_instances.erase (it);
110
123
WS_DEBUG_PRINTLN (" [display] Display removed successfully!" );
111
124
return true ;
112
125
}
113
126
}
114
- WS_DEBUG_PRINTLN ( " [display] Could not remove display, not found! " );
127
+
115
128
return false ;
116
129
}
117
130
@@ -124,13 +137,7 @@ bool DisplayController::Handle_Display_Remove(
124
137
bool DisplayController::Handle_Display_Write (
125
138
wippersnapper_display_v1_DisplayWrite *msgWrite) {
126
139
// 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 );
134
141
135
142
// Early-out if driver instance not found
136
143
if (!display) {
@@ -175,4 +182,22 @@ void DisplayController::update(int32_t rssi, bool is_connected) {
175
182
176
183
WS.feedWDT ();
177
184
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 ;
178
203
}
0 commit comments