@@ -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*/
103107bool 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(
124137bool 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}
0 commit comments