|
14 | 14 | */
|
15 | 15 | #include "controller.h"
|
16 | 16 |
|
| 17 | +/*! |
| 18 | + @brief Lambda function to create a dispDrvBase instance |
| 19 | +*/ |
| 20 | +using FnCreateDispDrv = |
| 21 | + std::function<dispDrvBase *(int16_t, int16_t, int16_t, int16_t, int16_t)>; |
| 22 | + |
| 23 | +// Factory for creating a new display drivers |
| 24 | +// NOTE: When you add a new display driver, make sure to add it to the factory! |
| 25 | +static const std::map<std::string, FnCreateDispDrv> FactoryDrvDisp = { |
| 26 | + {"grayscale4_eaamfgn", |
| 27 | + [](int16_t dc, int16_t rst, int16_t cs, int16_t sram_cs, |
| 28 | + int16_t busy) -> dispDrvBase * { |
| 29 | + return new drvDispThinkInkGrayscale4Eaamfgn(dc, rst, cs, sram_cs, busy); |
| 30 | + }}}; |
| 31 | + |
| 32 | +/*! |
| 33 | + @brief Creates a new display driver instance based on the driver name. |
| 34 | + @param driver_name |
| 35 | + The name of the display driver to create. |
| 36 | + @param dc |
| 37 | + Data/Command pin number. |
| 38 | + @param rst |
| 39 | + Reset pin number. |
| 40 | + @param cs |
| 41 | + Chip Select pin number. |
| 42 | + @param sram_cs |
| 43 | + Optional SRAM Chip Select pin number (default: -1). |
| 44 | + @param busy |
| 45 | + Optional Busy pin number (default: -1). |
| 46 | + @return Pointer to the created display driver instance, or nullptr if the |
| 47 | + driver name is not recognized. |
| 48 | +*/ |
| 49 | +dispDrvBase *CreateDrvDisp(const char *driver_name, int16_t dc, int16_t rst, |
| 50 | + int16_t cs, int16_t sram_cs = -1, |
| 51 | + int16_t busy = -1) { |
| 52 | + auto it = FactoryDrvDisp.find(driver_name); |
| 53 | + if (it == FactoryDrvDisp.end()) { |
| 54 | + return nullptr; |
| 55 | + } |
| 56 | + return it->second(dc, rst, cs, sram_cs, busy); |
| 57 | +} |
| 58 | + |
17 | 59 | /*!
|
18 | 60 | @brief Constructs a new DisplayHardware object
|
19 | 61 | */
|
@@ -76,15 +118,8 @@ bool DisplayHardware::beginEPD(
|
76 | 118 | return false; // Unsupported mode
|
77 | 119 | }
|
78 | 120 |
|
79 |
| - // If we already have a display driver assigned to this hardware instance, |
80 |
| - // clean it up! |
81 |
| - if (_thinkink_driver == |
82 |
| - wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_MFGN) { |
83 |
| - delete _disp_thinkink_grayscale4_eaamfgn; |
84 |
| - _disp_thinkink_grayscale4_eaamfgn = nullptr; |
85 |
| - _thinkink_driver = |
86 |
| - wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_UNSPECIFIED; |
87 |
| - } |
| 121 | + // TODO: If we already have a display driver assigned to this hardware |
| 122 | + // instance, clean it up! |
88 | 123 |
|
89 | 124 | // Parse all SPI bus pins
|
90 | 125 | // Check length
|
@@ -117,40 +152,25 @@ bool DisplayHardware::beginEPD(
|
117 | 152 |
|
118 | 153 | // TODO: Configure SPI bus selection (UNUSED AS OF RIGHT NOW)
|
119 | 154 |
|
| 155 | + // Create display driver object using the factory function |
| 156 | + _disp_drv_base = CreateDrvDisp(_name, dc, rst, cs, srcs, busy); |
| 157 | + if (!_disp_drv_base) { |
| 158 | + WS_DEBUG_PRINTLN("[display] Failed to create display driver!"); |
| 159 | + return false; // Failed to create display driver |
| 160 | + } |
| 161 | + |
120 | 162 | // Configure EPD mode
|
121 |
| - thinkinkmode_t epd_mode; |
| 163 | + thinkinkmode_t epd_mode = THINKINK_MONO; |
122 | 164 | if (config->mode == wippersnapper_display_v1_EPDMode_EPD_MODE_GRAYSCALE4) {
|
123 | 165 | epd_mode = THINKINK_GRAYSCALE4;
|
124 | 166 | WS_DEBUG_PRINTLN("[display] EPD mode: GRAYSCALE4");
|
125 |
| - } else if (config->mode == wippersnapper_display_v1_EPDMode_EPD_MODE_MONO) { |
126 |
| - epd_mode = THINKINK_MONO; |
127 |
| - WS_DEBUG_PRINTLN("[display] EPD mode: MONO"); |
128 | 167 | }
|
129 | 168 |
|
130 |
| - // Configure the EPD driver based on panel type |
131 |
| - if (config->panel == |
132 |
| - wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_MFGN) { |
133 |
| - WS_DEBUG_PRINTLN("[display] EPD panel: ThinkInk 290 Grayscale4 MFGN"); |
134 |
| - _thinkink_driver = |
135 |
| - wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_MFGN; |
136 |
| - _disp_thinkink_grayscale4_eaamfgn = |
137 |
| - new drvDispThinkInkGrayscale4Eaamfgn(dc, rst, cs, srcs, busy); |
138 |
| - if (!_disp_thinkink_grayscale4_eaamfgn) { |
139 |
| - WS_DEBUG_PRINTLN("[display] Failed to allocate ThinkInk driver!"); |
140 |
| - return false; // Allocation failed |
141 |
| - } |
142 |
| - if (!_disp_thinkink_grayscale4_eaamfgn->begin(epd_mode)) { |
143 |
| - WS_DEBUG_PRINTLN("[display] Failed to initialize ThinkInk driver!"); |
144 |
| - delete _disp_thinkink_grayscale4_eaamfgn; // Clean up if initialization |
145 |
| - // failed |
146 |
| - _disp_thinkink_grayscale4_eaamfgn = nullptr; |
147 |
| - return false; // Initialization failed |
148 |
| - } |
149 |
| - WS_DEBUG_PRINTLN("[display] ThinkInk 290 Grayscale4 MFGN driver " |
150 |
| - "initialized successfully!"); |
151 |
| - } else { |
152 |
| - WS_DEBUG_PRINTLN("[display] Unsupported EPD panel type!"); |
153 |
| - return false; // Unsupported panel type |
| 169 | + if (!_disp_drv_base->begin(epd_mode)) { |
| 170 | + WS_DEBUG_PRINTLN("[display] Failed to begin display driver!"); |
| 171 | + delete _disp_drv_base; // Clean up if initialization failed |
| 172 | + _disp_drv_base = nullptr; |
| 173 | + return false; // Failed to begin display driver |
154 | 174 | }
|
155 | 175 |
|
156 | 176 | return true; // Configuration successful
|
|
0 commit comments