Skip to content

Commit 0a37b19

Browse files
Allocate before SD.begin to simplify the code
1 parent c8377e7 commit 0a37b19

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/OpenStreetMap-esp32.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,21 @@ bool OpenStreetMap::downloadAndDecodeTile(CachedTile &tile, uint32_t x, uint32_t
414414

415415
bool OpenStreetMap::saveMap(const char *filename, LGFX_Sprite &map, String &result, uint8_t sdPin, uint32_t frequency)
416416
{
417-
log_i("Saving map, this may take a while...");
417+
log_i("Saving map as %s", filename);
418418

419419
if (!map.getBuffer())
420420
{
421421
result = "No data in map";
422422
return false;
423423
}
424424

425+
MemoryBuffer rowBuffer(map.width() * 3);
426+
if (!rowBuffer.isAllocated())
427+
{
428+
result = "Row buffer allocation failed";
429+
return false;
430+
}
431+
425432
if (!SD.begin(sdPin, SPI, frequency))
426433
{
427434
result = "SD Card mount failed";
@@ -478,15 +485,6 @@ bool OpenStreetMap::saveMap(const char *filename, LGFX_Sprite &map, String &resu
478485
writeLE(biClrUsed, 4);
479486
writeLE(biClrImportant, 4);
480487

481-
MemoryBuffer rowBuffer(map.width() * 3);
482-
if (!rowBuffer.isAllocated())
483-
{
484-
result = "Row buffer allocation failed";
485-
file.close();
486-
SD.end();
487-
return false;
488-
}
489-
490488
uint8_t *buf = rowBuffer.get();
491489
for (uint16_t y = 0; y < map.height(); y++)
492490
{
@@ -506,6 +504,6 @@ bool OpenStreetMap::saveMap(const char *filename, LGFX_Sprite &map, String &resu
506504

507505
file.close();
508506
SD.end();
509-
result = "Screenshot saved";
507+
result = "Map saved as " + String(filename);
510508
return true;
511509
}

0 commit comments

Comments
 (0)