Skip to content

Commit fc41a7d

Browse files
committed
Add beginEPD full implementation with safety concerns and a better init flow
1 parent c308694 commit fc41a7d

23 files changed

Lines changed: 151 additions & 67 deletions

File tree

.vscode/settings.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
{
22
"files.associations": {
33
"limits": "c",
4-
"type_traits": "c"
4+
"type_traits": "c",
5+
"array": "cpp",
6+
"deque": "cpp",
7+
"list": "cpp",
8+
"string": "cpp",
9+
"unordered_map": "cpp",
10+
"unordered_set": "cpp",
11+
"vector": "cpp",
12+
"string_view": "cpp",
13+
"format": "cpp",
14+
"initializer_list": "cpp",
15+
"span": "cpp"
516
},
617
"C_Cpp.dimInactiveRegions": true,
718
"dotnet.defaultSolution": "disable",

src/components/display/hardware.cpp

Lines changed: 104 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ DisplayHardware::DisplayHardware() {
3434
The display type to set.
3535
*/
3636
void DisplayHardware::setType(wippersnapper_display_v1_DisplayType type) {
37-
_type = type;
37+
_type = type;
3838
}
3939

4040
/*!
4141
@brief Gets the hardware's display type.
4242
@return The current display type.
4343
*/
4444
wippersnapper_display_v1_DisplayType DisplayHardware::getType() {
45-
return _type;
45+
return _type;
4646
}
4747

4848
/*!
@@ -53,37 +53,106 @@ wippersnapper_display_v1_DisplayType DisplayHardware::getType() {
5353
Pointer to the SPI configuration structure for EPD.
5454
@return True if configuration was successful, False otherwise.
5555
*/
56-
bool DisplayHardware::beginEPD(wippersnapper_display_v1_EPDConfig *config, wippersnapper_display_v1_EpdSpiConfig *spi_config) {
57-
// Convert pins in config to int16_t instances
58-
int16_t dc = -1, rst = -1, cs = -1, srcs = -1, busy = -1;
59-
dc = (int16_t)atoi(spi_config->pin_dc+ 1);
60-
rst = (int16_t)atoi(spi_config->pin_rst+ 1);
61-
cs = (int16_t)atoi(spi_config->pin_cs+ 1);
62-
srcs = (int16_t)atoi(spi_config->pin_sram_cs+ 1);
63-
busy = (int16_t)atoi(spi_config->pin_busy+ 1);
64-
65-
// Configure EPD mode
66-
thinkinkmode_t epd_mode;
67-
if (config->mode == wippersnapper_display_v1_EPDMode_EPD_MODE_GRAYSCALE4) {
68-
epd_mode = THINKINK_GRAYSCALE4;
69-
} else if (config->mode == wippersnapper_display_v1_EPDMode_EPD_MODE_MONO) {
70-
epd_mode = THINKINK_MONO;
71-
} else {
72-
epd_mode = THINKINK_MONO; // Default to mono
73-
}
74-
75-
// Assign driver instance based on panel type
76-
if (config->panel == wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_213_GRAYSCALE4_MFGN) {
77-
_disp_thinkink_grayscale4_eaamfgn = new ThinkInk_290_Grayscale4_EAAMFGN(dc, rst, cs, srcs, busy);
78-
_disp_thinkink_grayscale4_eaamfgn->begin(epd_mode);
79-
} else if (config->panel == wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_213_GRAYSCALE4_T5) {
80-
_disp_thinkink_grayscale4_t5 = new ThinkInk_290_Grayscale4_T5(dc, rst, cs, srcs, busy);
81-
_disp_thinkink_grayscale4_t5->begin(epd_mode);
82-
} else {
83-
return false; // Unsupported panel type
84-
}
85-
86-
return true; // Configuration successful
56+
bool DisplayHardware::beginEPD(
57+
wippersnapper_display_v1_EPDConfig *config,
58+
wippersnapper_display_v1_EpdSpiConfig *spi_config) {
59+
// Validate pointers
60+
if (config == nullptr || spi_config == nullptr) {
61+
return false;
62+
}
63+
64+
// Validate panel type
65+
if (config->panel ==
66+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_UNSPECIFIED) {
67+
return false; // Unsupported panel type
68+
}
69+
70+
// Validate mode is a correct EPD mode
71+
if (config->mode == wippersnapper_display_v1_EPDMode_EPD_MODE_UNSPECIFIED) {
72+
return false; // Unsupported mode
73+
}
74+
75+
// If we already have a display driver assigned to this hardware instance,
76+
// clean it up!
77+
if (_thinkink_driver ==
78+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_MFGN) {
79+
delete _disp_thinkink_grayscale4_eaamfgn;
80+
_disp_thinkink_grayscale4_eaamfgn = nullptr;
81+
_thinkink_driver =
82+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_UNSPECIFIED;
83+
} else if (
84+
_thinkink_driver ==
85+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_T5) {
86+
delete _disp_thinkink_grayscale4_t5;
87+
_disp_thinkink_grayscale4_t5 = nullptr;
88+
_thinkink_driver =
89+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_UNSPECIFIED;
90+
}
91+
92+
// Parse all SPI bus pins
93+
// Check length
94+
if (strlen(spi_config->pin_dc) < 2 || strlen(spi_config->pin_rst) < 2 ||
95+
strlen(spi_config->pin_cs) < 2) {
96+
return false;
97+
}
98+
// SPI pins must start with 'D'
99+
if (spi_config->pin_dc[0] != 'D' || spi_config->pin_rst[0] != 'D' ||
100+
spi_config->pin_cs[0] != 'D') {
101+
return false;
102+
}
103+
104+
// Parse and assign pins
105+
int16_t srcs = -1, busy = -1;
106+
int16_t dc = (int16_t)atoi(spi_config->pin_dc + 1);
107+
int16_t rst = (int16_t)atoi(spi_config->pin_rst + 1);
108+
int16_t cs = (int16_t)atoi(spi_config->pin_cs + 1);
109+
110+
// Optionally parse SRAM CS and BUSY pins
111+
if (strlen(spi_config->pin_sram_cs) >= 2 &&
112+
spi_config->pin_sram_cs[0] == 'D') {
113+
srcs = (int16_t)atoi(spi_config->pin_sram_cs + 1);
114+
}
115+
if (strlen(spi_config->pin_busy) >= 2 && spi_config->pin_busy[0] == 'D') {
116+
busy = (int16_t)atoi(spi_config->pin_busy + 1);
117+
}
118+
119+
// TODO: Configure SPI bus selection (UNUSED AS OF RIGHT NOW)
120+
121+
// Configure EPD mode
122+
thinkinkmode_t epd_mode;
123+
if (config->mode == wippersnapper_display_v1_EPDMode_EPD_MODE_GRAYSCALE4) {
124+
epd_mode = THINKINK_GRAYSCALE4;
125+
} else if (config->mode == wippersnapper_display_v1_EPDMode_EPD_MODE_MONO) {
126+
epd_mode = THINKINK_MONO;
127+
}
128+
129+
// Assign driver instance based on panel type
130+
if (config->panel ==
131+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_MFGN) {
132+
_disp_thinkink_grayscale4_eaamfgn =
133+
new ThinkInk_290_Grayscale4_EAAMFGN(dc, rst, cs, srcs, busy);
134+
if (!_disp_thinkink_grayscale4_eaamfgn)
135+
return false; // Allocation failed
136+
// Initialize the display
137+
_disp_thinkink_grayscale4_eaamfgn->begin(epd_mode);
138+
_thinkink_driver =
139+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_MFGN;
140+
} else if (
141+
config->panel ==
142+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_T5) {
143+
_disp_thinkink_grayscale4_t5 =
144+
new ThinkInk_290_Grayscale4_T5(dc, rst, cs, srcs, busy);
145+
if (!_disp_thinkink_grayscale4_t5)
146+
return false; // Allocation failed
147+
// Initialize the display
148+
_disp_thinkink_grayscale4_t5->begin(epd_mode);
149+
_thinkink_driver =
150+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_T5;
151+
} else {
152+
return false; // Unsupported panel type
153+
}
154+
155+
return true; // Configuration successful
87156
}
88157

89158
/*!
@@ -93,7 +162,7 @@ bool DisplayHardware::beginEPD(wippersnapper_display_v1_EPDConfig *config, wippe
93162
@return True if initialization was successful, false otherwise.
94163
*/
95164
bool DisplayHardware::begin(bool reset) {
96-
return true; // Placeholder for actual initialization logic
165+
return true; // Placeholder for actual initialization logic
97166
}
98167

99168
/*!
@@ -102,6 +171,5 @@ bool DisplayHardware::begin(bool reset) {
102171
The size of the text to set.
103172
*/
104173
void setTextSize(uint8_t sz) {
105-
// Placeholder for setting text size on the display TODO
174+
// Placeholder for setting text size on the display TODO
106175
}
107-

src/components/display/hardware.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ class DisplayHardware {
3333
// High-level API functions
3434
void setType(wippersnapper_display_v1_DisplayType type);
3535
wippersnapper_display_v1_DisplayType getType();
36-
void beginEPD(wippersnapper_display_v1_EPDConfig *config, wippersnapper_display_v1_EpdSpiConfig *spi_config);
36+
void beginEPD(wippersnapper_display_v1_EPDConfig *config,
37+
wippersnapper_display_v1_EpdSpiConfig *spi_config);
3738
bool begin(bool reset = true);
3839
// API functions to abstract Adafruit_GFX
3940
void setTextSize(uint8_t sz);
4041

41-
private:
42-
wippersnapper_display_v1_DisplayType _type; ///< Display type
43-
// TODO: Make these drivers instead?
44-
ThinkInk_290_Grayscale4_EAAMFGN *_disp_thinkink_grayscale4_eaamfgn = nullptr; //< 2025 MagTag with SSD1680Z chipset
45-
ThinkInk_290_Grayscale4_T5 *_disp_thinkink_grayscale4_t5 = nullptr; ///< Pre-2025 MagTag with IL0373 chipset
42+
private:
43+
wippersnapper_display_v1_DisplayType _type; ///< Display type
44+
wippersnapper_display_v1_EPDThinkInkPanel
45+
_thinkink_driver; ///< ThinkInk driver type
46+
// TODO: Make these drivers instead?
47+
ThinkInk_290_Grayscale4_EAAMFGN *_disp_thinkink_grayscale4_eaamfgn =
48+
nullptr; //< 2025 MagTag with SSD1680Z chipset
49+
ThinkInk_290_Grayscale4_T5 *_disp_thinkink_grayscale4_t5 =
50+
nullptr; ///< Pre-2025 MagTag with IL0373 chipset
4651
};
4752
#endif // WS_DISPLAY_HARDWARE_H

src/wippersnapper/description/v1/description.pb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Automatically generated nanopb constant definitions */
2-
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 16:18:27 2025. */
2+
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 17:34:50 2025. */
33

44
#include "wippersnapper/description/v1/description.pb.h"
55
#if PB_PROTO_HEADER_VERSION != 40

src/wippersnapper/description/v1/description.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Automatically generated nanopb header */
2-
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 16:18:27 2025. */
2+
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 17:34:50 2025. */
33

44
#ifndef PB_WIPPERSNAPPER_DESCRIPTION_V1_WIPPERSNAPPER_DESCRIPTION_V1_DESCRIPTION_PB_H_INCLUDED
55
#define PB_WIPPERSNAPPER_DESCRIPTION_V1_WIPPERSNAPPER_DESCRIPTION_V1_DESCRIPTION_PB_H_INCLUDED

src/wippersnapper/display/v1/display.pb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Automatically generated nanopb constant definitions */
2-
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 16:18:27 2025. */
2+
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 17:34:50 2025. */
33

44
#include "wippersnapper/display/v1/display.pb.h"
55
#if PB_PROTO_HEADER_VERSION != 40

src/wippersnapper/display/v1/display.pb.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Automatically generated nanopb header */
2-
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 16:18:27 2025. */
2+
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 17:34:50 2025. */
33

44
#ifndef PB_WIPPERSNAPPER_DISPLAY_V1_WIPPERSNAPPER_DISPLAY_V1_DISPLAY_PB_H_INCLUDED
55
#define PB_WIPPERSNAPPER_DISPLAY_V1_WIPPERSNAPPER_DISPLAY_V1_DISPLAY_PB_H_INCLUDED
@@ -37,8 +37,8 @@ typedef enum _wippersnapper_display_v1_EPDColors {
3737

3838
typedef enum _wippersnapper_display_v1_EPDThinkInkPanel {
3939
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_UNSPECIFIED = 0,
40-
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_213_GRAYSCALE4_MFGN = 1,
41-
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_213_GRAYSCALE4_T5 = 2
40+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_MFGN = 1,
41+
wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_T5 = 2
4242
} wippersnapper_display_v1_EPDThinkInkPanel;
4343

4444
/* Struct definitions */
@@ -112,8 +112,8 @@ typedef struct _wippersnapper_display_v1_DisplayWrite {
112112
#define _wippersnapper_display_v1_EPDColors_ARRAYSIZE ((wippersnapper_display_v1_EPDColors)(wippersnapper_display_v1_EPDColors_EPD_COLORS_YELLOW+1))
113113

114114
#define _wippersnapper_display_v1_EPDThinkInkPanel_MIN wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_UNSPECIFIED
115-
#define _wippersnapper_display_v1_EPDThinkInkPanel_MAX wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_213_GRAYSCALE4_T5
116-
#define _wippersnapper_display_v1_EPDThinkInkPanel_ARRAYSIZE ((wippersnapper_display_v1_EPDThinkInkPanel)(wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_213_GRAYSCALE4_T5+1))
115+
#define _wippersnapper_display_v1_EPDThinkInkPanel_MAX wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_T5
116+
#define _wippersnapper_display_v1_EPDThinkInkPanel_ARRAYSIZE ((wippersnapper_display_v1_EPDThinkInkPanel)(wippersnapper_display_v1_EPDThinkInkPanel_EPD_THINK_INK_PANEL_290_GRAYSCALE4_T5+1))
117117

118118

119119
#ifdef __cplusplus

src/wippersnapper/ds18x20/v1/ds18x20.pb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Automatically generated nanopb constant definitions */
2-
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 16:18:27 2025. */
2+
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 17:34:50 2025. */
33

44
#include "wippersnapper/ds18x20/v1/ds18x20.pb.h"
55
#if PB_PROTO_HEADER_VERSION != 40

src/wippersnapper/ds18x20/v1/ds18x20.pb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Automatically generated nanopb header */
2-
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 16:18:27 2025. */
2+
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 17:34:50 2025. */
33

44
#ifndef PB_WIPPERSNAPPER_DS18X20_V1_WIPPERSNAPPER_DS18X20_V1_DS18X20_PB_H_INCLUDED
55
#define PB_WIPPERSNAPPER_DS18X20_V1_WIPPERSNAPPER_DS18X20_V1_DS18X20_PB_H_INCLUDED

src/wippersnapper/i2c/v1/i2c.pb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Automatically generated nanopb constant definitions */
2-
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 16:18:27 2025. */
2+
/* Generated by nanopb-0.4.5-dev at Fri Aug 15 17:34:50 2025. */
33

44
#include "wippersnapper/i2c/v1/i2c.pb.h"
55
#if PB_PROTO_HEADER_VERSION != 40

0 commit comments

Comments
 (0)