Skip to content

Commit 41598d9

Browse files
committed
Cleanup and share parsePin between beginTft and beginEpd
1 parent 6d5f951 commit 41598d9

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

src/components/display/hardware.cpp

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ wippersnapper_display_v1_DisplayType DisplayHardware::getType() {
9999
return _type;
100100
}
101101

102+
/*!
103+
@brief Parses a pin string (e.g., "D5") and returns the corresponding pin
104+
number.
105+
@param pinStr
106+
The pin string to parse.
107+
@return The pin number, or -1 if the string is invalid.
108+
*/
109+
int16_t DisplayHardware::parsePin(const char* pinStr) {
110+
if (!pinStr || strlen(pinStr) < 2 || pinStr[0] != 'D') {
111+
return -1;
112+
}
113+
return atoi(pinStr + 1);
114+
}
115+
116+
102117
/*!
103118
@brief Configures the EPD display with the provided configuration.
104119
@param config
@@ -129,33 +144,18 @@ bool DisplayHardware::beginEPD(
129144
_drvDisp = nullptr;
130145
}
131146

132-
// Parse all SPI bus pins
133-
// Check length
134-
if (strlen(spi_config->pin_dc) < 2 || strlen(spi_config->pin_rst) < 2 ||
135-
strlen(spi_config->pin_cs) < 2) {
136-
WS_DEBUG_PRINTLN("[display] Invalid SPI pin len!");
137-
return false;
138-
}
139-
// SPI pins must start with 'D'
140-
if (spi_config->pin_dc[0] != 'D' || spi_config->pin_rst[0] != 'D' ||
141-
spi_config->pin_cs[0] != 'D') {
142-
WS_DEBUG_PRINTLN("[display] SPI pins must start with 'D'!");
143-
return false;
144-
}
145-
146147
// Parse and assign pins
147148
int16_t srcs = -1, busy = -1;
148-
int16_t dc = (int16_t)atoi(spi_config->pin_dc + 1);
149-
int16_t rst = (int16_t)atoi(spi_config->pin_rst + 1);
150-
int16_t cs = (int16_t)atoi(spi_config->pin_cs + 1);
149+
int16_t dc = parsePin(spi_config->pin_dc);
150+
int16_t rst = parsePin(spi_config->pin_rst);
151+
int16_t cs = parsePin(spi_config->pin_cs);
151152

152153
// Optionally parse SRAM CS and BUSY pins
153-
if (strlen(spi_config->pin_sram_cs) >= 2 &&
154-
spi_config->pin_sram_cs[0] == 'D') {
155-
srcs = (int16_t)atoi(spi_config->pin_sram_cs + 1);
154+
if (strlen(spi_config->pin_sram_cs) >= 2) {
155+
srcs = parsePin(spi_config->pin_sram_cs);
156156
}
157-
if (strlen(spi_config->pin_busy) >= 2 && spi_config->pin_busy[0] == 'D') {
158-
busy = (int16_t)atoi(spi_config->pin_busy + 1);
157+
if (strlen(spi_config->pin_busy) >= 2) {
158+
busy = parsePin(spi_config->pin_busy);
159159
}
160160

161161
// Configure SPI bus
@@ -210,8 +210,35 @@ bool DisplayHardware::beginEPD(
210210
Pointer to the SPI configuration structure for TFT.
211211
@return True if configuration was successful, False otherwise.
212212
*/
213-
bool beginTft(wippersnapper_display_v1_TftConfig *config, wippersnapper_display_v1_TftSpiConfig *spi_config) {
214-
// TODO
213+
bool DisplayHardware::beginTft(wippersnapper_display_v1_TftConfig *config, wippersnapper_display_v1_TftSpiConfig *spi_config) {
214+
// Validate pointers
215+
if (config == nullptr || spi_config == nullptr) {
216+
WS_DEBUG_PRINTLN("[display] EPD config or SPI config is null!");
217+
return false;
218+
}
219+
220+
// If we already have a display driver assigned to this hardware instance,
221+
// clean it up!
222+
if (_drvDisp) {
223+
delete _drvDisp;
224+
_drvDisp = nullptr;
225+
}
226+
227+
// Parse and assign pins
228+
int16_t rst = -1, miso = -1;
229+
int16_t cs = parsePin(spi_config->pin_dc);
230+
int16_t dc = parsePin(spi_config->pin_rst);
231+
int16_t mosi = parsePin(spi_config->pin_cs);
232+
int16_t sck = parsePin(spi_config->pin_sck);
233+
234+
// Optionally parse SRAM CS and BUSY pins
235+
if (strlen(spi_config->pin_rst) >= 2) {
236+
rst = parsePin(spi_config->pin_rst);
237+
}
238+
if (strlen(spi_config->pin_miso) >= 2) {
239+
miso = parsePin(spi_config->pin_miso);
240+
}
241+
215242
return false;
216243
}
217244

src/components/display/hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class DisplayHardware {
5151
void writeMessage(const char *message);
5252

5353
private:
54+
int16_t parsePin(const char* pinStr);
5455
bool detect_ssd1680(uint8_t cs, uint8_t dc, uint8_t rst);
5556
char _name[64]; ///< Identifies the hardware instance
5657
wippersnapper_display_v1_DisplayType _type; ///< Display type

0 commit comments

Comments
 (0)