Skip to content

Commit 83af887

Browse files
fix: restore board-agnostic I2C device selection for OLED display
The recent OLED display inversion fixes inadvertently hard-coded DT_NODELABEL(i2c1) which only exists on XIAO board, breaking Adafruit Feather builds which use i2c0. Changes: - Restored get_i2c_device() helper function that tries both I2C interfaces - Uses DEVICE_DT_GET_OR_NULL() for runtime I2C device detection - Tries i2c1 first (XIAO board), then falls back to i2c0 (Feather board) - Used DEVICE_DT_GET_OR_NULL() for display device to handle missing aliases - Replaced all hard-coded DT_NODELABEL(i2c1) calls with helper function Both boards now build successfully: - XIAO: Uses i2c1 and oled_display alias when available - Feather: Uses i2c0 and gracefully handles missing display This maintains the OLED display inversion fixes for XIAO while ensuring cross-board compatibility for the codebase. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 69b1cab commit 83af887

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

app/src/oled_display.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@
2020
#define LOG_MODULE_NAME oled_display
2121
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
2222

23+
/* Helper to get I2C device safely at runtime */
24+
static const struct device *get_i2c_device(void)
25+
{
26+
/* Try to get i2c1 first (XIAO), then i2c0 (Feather) */
27+
const struct device *dev;
28+
29+
/* Try i2c1 first */
30+
dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c1));
31+
if (dev && device_is_ready(dev)) {
32+
return dev;
33+
}
34+
35+
/* Fallback to i2c0 */
36+
dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(i2c0));
37+
if (dev && device_is_ready(dev)) {
38+
return dev;
39+
}
40+
41+
return NULL;
42+
}
2343

2444
/* Display device and configuration */
2545
static const struct device *display_dev;
@@ -52,8 +72,8 @@ int oled_display_init(void)
5272
LOG_INF("Checking for OLED display...");
5373

5474
/* Get display device */
55-
display_dev = DEVICE_DT_GET(DT_ALIAS(oled_display));
56-
if (!device_is_ready(display_dev)) {
75+
display_dev = DEVICE_DT_GET_OR_NULL(DT_ALIAS(oled_display));
76+
if (!display_dev || !device_is_ready(display_dev)) {
5777
LOG_INF("OLED display not detected - continuing without display");
5878
display_available = false;
5979
display_ready = false;
@@ -335,8 +355,8 @@ static int oled_display_normal(void)
335355
}
336356

337357
/* Get I2C device */
338-
i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c1));
339-
if (!device_is_ready(i2c_dev)) {
358+
i2c_dev = get_i2c_device();
359+
if (!i2c_dev || !device_is_ready(i2c_dev)) {
340360
LOG_WRN("I2C device not available for display normal mode");
341361
return 0; /* Silently continue */
342362
}
@@ -367,8 +387,8 @@ static int oled_display_invert(void)
367387
int ret;
368388

369389
/* Get I2C device */
370-
i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c1));
371-
if (!device_is_ready(i2c_dev)) {
390+
i2c_dev = get_i2c_device();
391+
if (!i2c_dev || !device_is_ready(i2c_dev)) {
372392
LOG_ERR("I2C device not ready for display inversion");
373393
return -ENODEV;
374394
}
@@ -400,8 +420,8 @@ static int oled_display_set_contrast(uint8_t contrast)
400420
int ret;
401421

402422
/* Get I2C device */
403-
i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c1));
404-
if (!device_is_ready(i2c_dev)) {
423+
i2c_dev = get_i2c_device();
424+
if (!i2c_dev || !device_is_ready(i2c_dev)) {
405425
LOG_WRN("I2C device not available for contrast adjustment");
406426
return 0; /* Silently continue */
407427
}
@@ -510,8 +530,8 @@ int oled_display_splash_screen(uint32_t duration_ms)
510530
}
511531

512532
/* Turn display OFF completely to avoid any flashing */
513-
const struct device *i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c1));
514-
if (device_is_ready(i2c_dev)) {
533+
const struct device *i2c_dev = get_i2c_device();
534+
if (i2c_dev && device_is_ready(i2c_dev)) {
515535
uint8_t display_off_cmd[2] = {0x00, 0xAE}; /* Display OFF command */
516536
i2c_write(i2c_dev, display_off_cmd, 2, 0x3c);
517537
}
@@ -550,7 +570,7 @@ int oled_display_splash_screen(uint32_t duration_ms)
550570
k_sleep(K_MSEC(10));
551571

552572
/* Now turn display back ON - content is already rendered and inverted */
553-
if (device_is_ready(i2c_dev)) {
573+
if (i2c_dev && device_is_ready(i2c_dev)) {
554574
uint8_t display_on_cmd[2] = {0x00, 0xAF}; /* Display ON command */
555575
i2c_write(i2c_dev, display_on_cmd, 2, 0x3c);
556576
}

0 commit comments

Comments
 (0)