From fdcf5a180673d23c6d709b398b712bc6bea4805d Mon Sep 17 00:00:00 2001 From: ainyan03 <205502311+ainyan03@users.noreply.github.com> Date: Thu, 15 Oct 2026 03:35:04 +0000 Subject: [PATCH] Fix the build on ESP targets without board detection and on SDL M5GFX.cpp failed to compile on ESP targets that have no board detection package (ESP32-S2, for example): the detection headers and helpers were guarded by the list of supported targets, but init_impl() and autodetect() used them unconditionally. A single internal macro now selects the detection path. On the other targets no detection runs and the board stays board_unknown, as in 0.2.33; a fixed_board request is refused with a warning. Unsupported targets now also skip NVS reads and writes, whereas 0.2.33 performed unsuccessful detection and could write the unknown result back to NVS. On SDL, setDetectConfig() was defined only inside the ESP section, so calling it (M5Unified::begin() always does) failed to link. It is now defined for every platform. SDL init uses fixed_board when it is set, then M5GFX_BOARD, then the previous default. --- extras/board_spec/test/detect_attempt.test.js | 1 + .../board_spec/test/detect_families.test.js | 1 + extras/board_spec/test/fixed_board.test.js | 5 +- src/M5GFX.cpp | 84 ++++++++++++------- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/extras/board_spec/test/detect_attempt.test.js b/extras/board_spec/test/detect_attempt.test.js index fb8c723d..a84f20fe 100644 --- a/extras/board_spec/test/detect_attempt.test.js +++ b/extras/board_spec/test/detect_attempt.test.js @@ -22,6 +22,7 @@ test("production attempt yields before construction and preserves adoption and r #include #include #define CONFIG_IDF_TARGET_ESP32C6 1 +#define M5GFX_HAS_BOARD_DETECT #define ESP_LOGW(...) ((void)0) #define ESP_LOGI(...) ((void)0) using namespace m5gfx; diff --git a/extras/board_spec/test/detect_families.test.js b/extras/board_spec/test/detect_families.test.js index 853f3757..cf57db40 100644 --- a/extras/board_spec/test/detect_families.test.js +++ b/extras/board_spec/test/detect_families.test.js @@ -84,6 +84,7 @@ test("production SPI and Paper hint restrictions recover a different member on a const main=await fs.readFile(path.join(src,"../M5GFX.cpp"),"utf8"); const compat=body(main,"board_t M5GFX::autodetect(").replaceAll("board_t::board_unknown","board_id_unknown"); await compileRun(common+` +#define M5GFX_HAS_BOARD_DETECT struct member_t {const board_desc_t* desc;}; namespace wiring {namespace papers3 {constexpr int internal_i2c_sda=0,internal_i2c_scl=1;}} namespace pmic_ops {constexpr int pm1_i2c_addr=0x6e,pm1_i2c_freq=100000,pm1_device_id=0x2050;} diff --git a/extras/board_spec/test/fixed_board.test.js b/extras/board_spec/test/fixed_board.test.js index 4aaa7d5e..58c5e01e 100644 --- a/extras/board_spec/test/fixed_board.test.js +++ b/extras/board_spec/test/fixed_board.test.js @@ -15,7 +15,9 @@ test("fixed acceptance bypasses NVS and detection and retries clean startup afte const finish = body(main, "static board_detect::detect_outcome_t finish_detection_setup("); const find = body(registry, "const board_entry_t* find_board("); const start = main.indexOf(" _detect_started = true;", main.indexOf("bool M5GFX::init_impl(")); - const prefix = main.slice(start, main.indexOf(" static constexpr char NVS_KEY", start)); + const end = main.indexOf("\n#if defined (M5GFX_HAS_BOARD_DETECT)\n static constexpr char NVS_KEY", start); + assert.ok(end > start, "fixed startup prefix ends before the guarded NVS path"); + const prefix = main.slice(start, end); const getterSource = await fs.readFile(path.join(src, "M5GFX.h"), "utf8"); const fixedGetter = body(getterSource, "board_t getFixedBoard("); await compileRun(` @@ -27,6 +29,7 @@ test("fixed acceptance bypasses NVS and detection and retries clean startup afte #include #define CONFIG_IDF_TARGET 1 #define CONFIG_IDF_TARGET_ESP32S3 1 +#define M5GFX_HAS_BOARD_DETECT #define ESP_LOGW(...) (++warnings) #define ESP_LOGI(...) ((void)0) using namespace m5gfx; diff --git a/src/M5GFX.cpp b/src/M5GFX.cpp index 1008c672..55d2c2bd 100644 --- a/src/M5GFX.cpp +++ b/src/M5GFX.cpp @@ -37,7 +37,9 @@ #include "board_detect/m5/setup_sentinels.hpp" +#undef M5GFX_HAS_BOARD_DETECT #if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32) || defined (CONFIG_IDF_TARGET_ESP32S3) || defined (CONFIG_IDF_TARGET_ESP32C3) || defined (CONFIG_IDF_TARGET_ESP32C5) || defined (CONFIG_IDF_TARGET_ESP32C6) || defined (CONFIG_IDF_TARGET_ESP32C61) || defined (CONFIG_IDF_TARGET_ESP32P4) || defined (CONFIG_IDF_TARGET_ESP32H2) +#define M5GFX_HAS_BOARD_DETECT #include "board_detect/board_detect.inl" #include "board_detect/detect_session.hpp" #if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32) @@ -112,6 +114,19 @@ namespace m5gfx if (_instance == nullptr) _instance = this; } + void M5GFX::setDetectConfig(const detect_config_t& config) + { + const bool changed = _detect_config.fallback_board != config.fallback_board + || _detect_config.fixed_board != config.fixed_board; + _detect_config = config; +#if defined (ESP_PLATFORM) + if (_detect_started && changed) + { ESP_LOGW(LIBRARY_NAME, "[Autodetect] configuration stored after init; applies to the next detection"); } +#else + (void)changed; +#endif + } + #if defined ( ESP_PLATFORM ) // 判別 transaction の候補機種 I2C プローブはソフトウェアポートを使う。 @@ -780,7 +795,7 @@ namespace m5gfx #include "board_detect/m5/esp32p4_setup.inl" #endif -#if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32) || defined (CONFIG_IDF_TARGET_ESP32S3) || defined (CONFIG_IDF_TARGET_ESP32C3) || defined (CONFIG_IDF_TARGET_ESP32C5) || defined (CONFIG_IDF_TARGET_ESP32C6) || defined (CONFIG_IDF_TARGET_ESP32C61) || defined (CONFIG_IDF_TARGET_ESP32P4) || defined (CONFIG_IDF_TARGET_ESP32H2) +#if defined (M5GFX_HAS_BOARD_DETECT) #if defined (CONFIG_IDF_TARGET_ESP32S3) static bool conditional_detection_pins_unavailable() { @@ -1191,19 +1206,6 @@ namespace m5gfx } #endif - void M5GFX::setDetectConfig(const detect_config_t& config) - { - const bool changed = _detect_config.fallback_board != config.fallback_board - || _detect_config.fixed_board != config.fixed_board; - _detect_config = config; -#if defined (ESP_PLATFORM) - if (_detect_started && changed) - { ESP_LOGW(LIBRARY_NAME, "[Autodetect] configuration stored after init; applies to the next detection"); } -#else - (void)changed; -#endif - } - void M5GFX::_set_backlight(lgfx::ILight* bl) { // if (_light_last) { delete _light_last; } @@ -1236,26 +1238,29 @@ namespace m5gfx _fixed_board = board_t::board_unknown; if (_detect_config.fixed_board != board_t::board_unknown) { +#if defined (M5GFX_HAS_BOARD_DETECT) const auto* entry = select_fixed_board(_detect_config.fixed_board); - if (entry == nullptr) + if (entry != nullptr) { - ESP_LOGW(LIBRARY_NAME, "Fixed board:%u is not supported by this chip", - static_cast(_detect_config.fixed_board)); - return false; + // Acceptance is independent of display construction; a failed init may retry. + _fixed_board = _detect_config.fixed_board; + const auto outcome = run_fixed_detection_session(*entry, use_reset, + [this](board_detect::m5::display_parts_t& parts, board_t setup_board) + { + return !reject_detected_setup(setup_board) + && _adopt_detected_parts(parts.bus, parts.panel, parts.light, parts.touch); + }); + if (!outcome.setup_succeeded) { return false; } + _board = _fixed_board; + return _finish_detected_init(use_clear); } - // Acceptance is independent of display construction; a failed init may retry. - _fixed_board = _detect_config.fixed_board; - const auto outcome = run_fixed_detection_session(*entry, use_reset, - [this](board_detect::m5::display_parts_t& parts, board_t setup_board) - { - return !reject_detected_setup(setup_board) - && _adopt_detected_parts(parts.bus, parts.panel, parts.light, parts.touch); - }); - if (!outcome.setup_succeeded) { return false; } - _board = _fixed_board; - return _finish_detected_init(use_clear); +#endif + ESP_LOGW(LIBRARY_NAME, "Fixed board:%u is not supported by this chip", + static_cast(_detect_config.fixed_board)); + return false; } +#if defined (M5GFX_HAS_BOARD_DETECT) static constexpr char NVS_KEY[] = "AUTODETECT"; std::uint32_t nvs_board = 0; std::uint32_t nvs_handle = 0; @@ -1359,6 +1364,10 @@ namespace m5gfx } } +#else + // Unsupported chips retain the legacy unknown-board result; build hints are ignored. + (void)use_reset; +#endif return _finish_detected_init(use_clear); } @@ -1397,6 +1406,7 @@ namespace m5gfx bool* no_signature, board_t* candidate_board) { panel(nullptr); +#if defined (M5GFX_HAS_BOARD_DETECT) const auto package = select_detection_package(true); board_detect::detect_request_t request; request.hint = static_cast(board) <= UINT16_MAX @@ -1424,6 +1434,16 @@ namespace m5gfx panel(_panel_last.get()); return outcome.setup_succeeded ? static_cast(outcome.result.def->id) : board_t::board_unknown; +#else + (void)use_reset; + (void)board; + (void)final_attempt; + (void)transient_fallback; + (void)no_signature; + (void)candidate_board; + panel(_panel_last.get()); + return board_t::board_unknown; +#endif } #else @@ -1431,9 +1451,11 @@ namespace m5gfx bool M5GFX::init_impl(bool use_reset, bool use_clear) { _detect_started = true; - board_t b = board_t::board_unknown; + // SDL has no detection pass; a fixed board overrides its build-time model. + _fixed_board = _detect_config.fixed_board; + board_t b = _fixed_board; #if defined (M5GFX_BOARD) - b = M5GFX_BOARD; + if (b == board_t::board_unknown) { b = M5GFX_BOARD; } #endif _board = autodetect(use_reset, b); return LGFX_Device::init_impl(use_reset, use_clear);