From 8a0a915df4616ea9beccbb3f26b44f3e9d1e9d00 Mon Sep 17 00:00:00 2001 From: Cellie Date: Sun, 25 May 2025 10:23:51 +0200 Subject: [PATCH] Refactor `fetchTile` for clarity and less branching --- src/OpenStreetMap-esp32.cpp | 55 ++++++++++++++++++------------------- src/OpenStreetMap-esp32.hpp | 2 +- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/OpenStreetMap-esp32.cpp b/src/OpenStreetMap-esp32.cpp index 85325a0..9846260 100644 --- a/src/OpenStreetMap-esp32.cpp +++ b/src/OpenStreetMap-esp32.cpp @@ -454,45 +454,42 @@ bool OpenStreetMap::fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t return false; } + PNG *png = getPNGForCore(); + if (!png) + { + result = "PNG decoder unavailable"; + return false; + } + char url[64]; snprintf(url, sizeof(url), "https://tile.openstreetmap.org/%u/%u/%u.png", static_cast(zoom), static_cast(x), static_cast(y)); - int decodeResult; - { - auto buffer = urlToBuffer(url, result); - if (!buffer) - return false; - - PNG *png = getPNGForCore(); - if (!png) - { - result = "PNG decoder unavailable"; - return false; - } - - const int16_t rc = png->openRAM(buffer.value()->get(), buffer.value()->size(), PNGDraw); - if (rc != PNG_SUCCESS) - { - result = "PNG Decoder Error: " + String(rc); - return false; - } + const auto buffer = urlToBuffer(url, result); + if (!buffer) + return false; - if (png->getWidth() != OSM_TILESIZE || png->getHeight() != OSM_TILESIZE) - { - result = "Unexpected tile size: w=" + String(png->getWidth()) + " h=" + String(png->getHeight()); - return false; - } + const int16_t rc = png->openRAM(buffer.value()->get(), buffer.value()->size(), PNGDraw); + if (rc != PNG_SUCCESS) + { + result = "PNG Decoder Error: " + String(rc); + return false; + } - currentInstance = this; - currentTileBuffer = tile.buffer; - decodeResult = png->decode(0, PNG_FAST_PALETTE); - currentTileBuffer = nullptr; - currentInstance = nullptr; + if (png->getWidth() != OSM_TILESIZE || png->getHeight() != OSM_TILESIZE) + { + result = "Unexpected tile size: w=" + String(png->getWidth()) + " h=" + String(png->getHeight()); + return false; } + currentInstance = this; + currentTileBuffer = tile.buffer; + const int decodeResult = png->decode(0, PNG_FAST_PALETTE); + currentTileBuffer = nullptr; + currentInstance = nullptr; + if (decodeResult != PNG_SUCCESS) { result = "Decoding " + String(url) + " failed with code: " + String(decodeResult); diff --git a/src/OpenStreetMap-esp32.hpp b/src/OpenStreetMap-esp32.hpp index 7a4a9b5..00cc340 100644 --- a/src/OpenStreetMap-esp32.hpp +++ b/src/OpenStreetMap-esp32.hpp @@ -43,7 +43,7 @@ constexpr uint16_t OSM_TILE_TIMEOUT_MS = 2500; constexpr uint16_t OSM_DEFAULT_CACHE_ITEMS = 10; constexpr uint16_t OSM_MAX_ZOOM = 18; constexpr UBaseType_t OSM_TASK_PRIORITY = 10; -constexpr uint32_t OSM_TASK_STACKSIZE = 4096; +constexpr uint32_t OSM_TASK_STACKSIZE = 5120; constexpr uint32_t OSM_JOB_QUEUE_SIZE = 50; using tileList = std::vector>;