Skip to content

Commit eeb41c7

Browse files
Refactor fetchTile for clarity and less branching (#76)
1 parent 31a2f69 commit eeb41c7

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

src/OpenStreetMap-esp32.cpp

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -454,45 +454,42 @@ bool OpenStreetMap::fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t
454454
return false;
455455
}
456456

457+
PNG *png = getPNGForCore();
458+
if (!png)
459+
{
460+
result = "PNG decoder unavailable";
461+
return false;
462+
}
463+
457464
char url[64];
458465
snprintf(url, sizeof(url), "https://tile.openstreetmap.org/%u/%u/%u.png",
459466
static_cast<unsigned int>(zoom),
460467
static_cast<unsigned int>(x),
461468
static_cast<unsigned int>(y));
462469

463-
int decodeResult;
464-
{
465-
auto buffer = urlToBuffer(url, result);
466-
if (!buffer)
467-
return false;
468-
469-
PNG *png = getPNGForCore();
470-
if (!png)
471-
{
472-
result = "PNG decoder unavailable";
473-
return false;
474-
}
475-
476-
const int16_t rc = png->openRAM(buffer.value()->get(), buffer.value()->size(), PNGDraw);
477-
if (rc != PNG_SUCCESS)
478-
{
479-
result = "PNG Decoder Error: " + String(rc);
480-
return false;
481-
}
470+
const auto buffer = urlToBuffer(url, result);
471+
if (!buffer)
472+
return false;
482473

483-
if (png->getWidth() != OSM_TILESIZE || png->getHeight() != OSM_TILESIZE)
484-
{
485-
result = "Unexpected tile size: w=" + String(png->getWidth()) + " h=" + String(png->getHeight());
486-
return false;
487-
}
474+
const int16_t rc = png->openRAM(buffer.value()->get(), buffer.value()->size(), PNGDraw);
475+
if (rc != PNG_SUCCESS)
476+
{
477+
result = "PNG Decoder Error: " + String(rc);
478+
return false;
479+
}
488480

489-
currentInstance = this;
490-
currentTileBuffer = tile.buffer;
491-
decodeResult = png->decode(0, PNG_FAST_PALETTE);
492-
currentTileBuffer = nullptr;
493-
currentInstance = nullptr;
481+
if (png->getWidth() != OSM_TILESIZE || png->getHeight() != OSM_TILESIZE)
482+
{
483+
result = "Unexpected tile size: w=" + String(png->getWidth()) + " h=" + String(png->getHeight());
484+
return false;
494485
}
495486

487+
currentInstance = this;
488+
currentTileBuffer = tile.buffer;
489+
const int decodeResult = png->decode(0, PNG_FAST_PALETTE);
490+
currentTileBuffer = nullptr;
491+
currentInstance = nullptr;
492+
496493
if (decodeResult != PNG_SUCCESS)
497494
{
498495
result = "Decoding " + String(url) + " failed with code: " + String(decodeResult);

src/OpenStreetMap-esp32.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ constexpr uint16_t OSM_TILE_TIMEOUT_MS = 2500;
4343
constexpr uint16_t OSM_DEFAULT_CACHE_ITEMS = 10;
4444
constexpr uint16_t OSM_MAX_ZOOM = 18;
4545
constexpr UBaseType_t OSM_TASK_PRIORITY = 10;
46-
constexpr uint32_t OSM_TASK_STACKSIZE = 4096;
46+
constexpr uint32_t OSM_TASK_STACKSIZE = 5120;
4747
constexpr uint32_t OSM_JOB_QUEUE_SIZE = 50;
4848

4949
using tileList = std::vector<std::pair<uint32_t, int32_t>>;

0 commit comments

Comments
 (0)