Skip to content

Commit ab55789

Browse files
committed
Implement HandleDisplayRemove
1 parent 37904f3 commit ab55789

File tree

6 files changed

+68
-59
lines changed

6 files changed

+68
-59
lines changed

src/Wippersnapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,8 +1682,8 @@ bool cbDecodeDisplayMsg(pb_istream_t *stream, const pb_field_t *field, void **ar
16821682

16831683
// Attempt to add or replace a display component
16841684
bool did_add = WS._displayController->Handle_Display_AddOrReplace(&msgAddReq);
1685-
// TODO: Add response handling and publishing here
1686-
1685+
// TODO: Add response handling and publishing here, for now it always returns true and doesnt publish back to the broker
1686+
}
16871687
return true;
16881688
}
16891689

src/Wippersnapper_demo.ino.cpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/components/display/controller.cpp

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,64 @@ DisplayController::DisplayController() {
2424
/*!
2525
@brief Destructor
2626
*/
27-
DisplayController::DisplayController() {
27+
DisplayController::~DisplayController() {
2828
// TODO
2929
}
3030

3131
/*!
3232
@brief Handles a Display_AddOrReplace message.
3333
@param msgAdd
3434
Pointer to a DisplayAddOrReplace message structure.
35-
@return True if the display was added or replaced successfully, false otherwise.
35+
@return True if the display was added or replaced successfully, false
36+
otherwise.
3637
*/
37-
bool DisplayController::Handle_Display_AddOrReplace(wippersnapper_display_v1_DisplayAddOrReplace *msgAdd) {
38-
DisplayHardware *display = new DisplayHardware(msgAdd->name);
39-
40-
// Configure display type
41-
display->setType(msgAdd->type);
42-
43-
// Attempt to initialize display hardware instance
44-
bool did_begin = false;
45-
if (msgAdd->which_config == wippersnapper_display_v1_DisplayAddOrReplace_epd_config_tag) {
46-
did_begin = display->beginEPD(&msgAdd->config.epd_config, &msgAdd->interface_type.spi_epd);
47-
} else {
48-
WS_DEBUG_PRINTLN("[display] Unsupported display configuration type!");
49-
return false;
50-
}
38+
bool DisplayController::Handle_Display_AddOrReplace(
39+
wippersnapper_display_v1_DisplayAddOrReplace *msgAdd) {
40+
DisplayHardware *display = new DisplayHardware(msgAdd->name);
5141

52-
// Check if the display began successfully
53-
if (!did_begin) {
54-
WS_DEBUG_PRINTLN("[display] Failed to initialize display!");
55-
delete display; // Clean up if initialization failed
56-
return false;
57-
}
42+
// Configure display type
43+
display->setType(msgAdd->type);
44+
45+
// Attempt to initialize display hardware instance
46+
bool did_begin = false;
47+
if (msgAdd->which_config ==
48+
wippersnapper_display_v1_DisplayAddOrReplace_epd_config_tag) {
49+
did_begin = display->beginEPD(&msgAdd->config.epd_config,
50+
&msgAdd->interface_type.spi_epd);
51+
} else {
52+
WS_DEBUG_PRINTLN("[display] Unsupported display configuration type!");
53+
return false;
54+
}
55+
56+
// Check if the display began successfully
57+
if (!did_begin) {
58+
WS_DEBUG_PRINTLN("[display] Failed to initialize display!");
59+
delete display; // Clean up if initialization failed
60+
return false;
61+
}
5862

59-
_hw_instances.push_back(display); // Store the display instance
60-
WS_DEBUG_PRINTLN("[display] Display added or replaced successfully!");
61-
return true; // Placeholder
63+
_hw_instances.push_back(display); // Store the display instance
64+
WS_DEBUG_PRINTLN("[display] Display added or replaced successfully!");
65+
return true; // Placeholder
6266
}
67+
68+
/*!
69+
@brief Handles a Display_Remove message.
70+
@param msgRemove
71+
Pointer to a DisplayRemove message structure.
72+
@return True if the display was removed successfully, false otherwise.
73+
*/
74+
bool DisplayController::Handle_Display_Remove(
75+
wippersnapper_display_v1_DisplayRemove *msgRemove) {
76+
// Find the display instance by name
77+
for (auto it = _hw_instances.begin(); it != _hw_instances.end(); ++it) {
78+
if (strcmp((*it)->getName(), msgRemove->name) == 0) {
79+
delete *it;
80+
_hw_instances.erase(it);
81+
WS_DEBUG_PRINTLN("[display] Display removed successfully!");
82+
return true;
83+
}
84+
}
85+
WS_DEBUG_PRINTLN("[display] Could not remove display, not found!");
86+
return false;
87+
}

src/components/display/controller.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ class DisplayController {
3131
public:
3232
DisplayController();
3333
~DisplayController();
34-
bool Handle_Display_AddOrReplace(wippersnapper_display_v1_DisplayAddOrReplace *msgAdd);
35-
//bool Handle_Display_Remove(...);
36-
//bool Handle_Display_Write(...);
34+
bool Handle_Display_AddOrReplace(
35+
wippersnapper_display_v1_DisplayAddOrReplace *msgAdd);
36+
bool Handle_Display_Remove(wippersnapper_display_v1_DisplayRemove *msgRemove);
37+
// bool Handle_Display_Write(...);
3738
private:
38-
std::vector<DisplayHardware*> _hw_instances; ///< Holds pointers to DisplayHardware instances
39+
std::vector<DisplayHardware *>
40+
_hw_instances; ///< Holds pointers to DisplayHardware instances
3941
};
4042
extern Wippersnapper Ws; ///< Global WS instance
4143
#endif

src/components/display/hardware.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ bool DisplayHardware::begin(bool reset) {
171171
@param sz
172172
The size of the text to set.
173173
*/
174-
void setTextSize(uint8_t sz) {
174+
void DisplayHardware::setTextSize(uint8_t sz) {
175175
// Placeholder for setting text size on the display TODO
176176
}
177+
178+
/*!
179+
@brief Gets the name of the display hardware instance.
180+
@return The name of the display hardware instance.
181+
*/
182+
const char *DisplayHardware::getName() { return _name; }

src/components/display/hardware.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ class DisplayHardware {
3131
DisplayHardware(const char *name);
3232
~DisplayHardware();
3333
// High-level API functions
34+
const char *getName();
3435
void setType(wippersnapper_display_v1_DisplayType type);
3536
wippersnapper_display_v1_DisplayType getType();
36-
bool beginEPD(wippersnapper_display_v1_EPDConfig *config, wippersnapper_display_v1_EpdSpiConfig *spi_config);
37+
bool beginEPD(wippersnapper_display_v1_EPDConfig *config,
38+
wippersnapper_display_v1_EpdSpiConfig *spi_config);
3739
bool begin(bool reset = true);
3840
// API functions to abstract Adafruit_GFX
3941
void setTextSize(uint8_t sz);

0 commit comments

Comments
 (0)