Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions include/graphics/map/MapTileSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
class MapTileSettings
{
public:
static constexpr size_t PREFIX_SIZE = 10;
static constexpr size_t TILE_STYLE_SIZE = 20;
static constexpr size_t TILE_FORMAT_SIZE = 10;

MapTileSettings() = default;
static uint8_t getDefaultZoom(void) { return zoomDefault; }
static void setDefaultZoom(uint8_t zoom) { zoomDefault = zoom; }
Expand All @@ -28,19 +32,21 @@ class MapTileSettings
static void setDefaultLon(float lon) { defaultLon = lon; }

static const char *getPrefix(void) { return prefix; }
static void setPrefix(const char *p) { strcpy(prefix, p); }
static void setPrefix(const char *p) { copyBounded(prefix, PREFIX_SIZE, p); }

static const char *getTileStyle(void) { return tileStyle; }
static void setTileStyle(const char *p)
{
strcpy(tileStyle, p);
copyBounded(tileStyle, TILE_STYLE_SIZE, p);
size_t len = strlen(tileStyle);
if (len > 0 && tileStyle[len - 1] != '/')
strcat(tileStyle, "/");
if (len > 0 && tileStyle[len - 1] != '/' && len + 1 < TILE_STYLE_SIZE) {
tileStyle[len] = '/';
tileStyle[len + 1] = '\0';
}
}

static const char *getTileFormat(void) { return tileFormat; }
static void setTileFormat(const char *p) { strcpy(tileFormat, p); }
static void setTileFormat(const char *p) { copyBounded(tileFormat, TILE_FORMAT_SIZE, p); }

static uint16_t getTileProvider(void) { return tileProviderId; }
static void setTileProvider(uint16_t id) { tileProviderId = id; }
Expand All @@ -55,6 +61,20 @@ class MapTileSettings
static void setSaveOK(bool ok) { save = ok; }

private:
static void copyBounded(char *dst, size_t dstSize, const char *src)
{
if (!dst || dstSize == 0) {
return;
}
if (!src) {
dst[0] = '\0';
return;
}

strncpy(dst, src, dstSize - 1);
dst[dstSize - 1] = '\0';
}

static uint8_t zoomLevel;
static uint8_t zoomDefault;
static uint16_t tileSize;
Expand Down
10 changes: 5 additions & 5 deletions source/graphics/map/MapTileSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ uint16_t MapTileSettings::tileProviderId = 0; // default url index to load
uint32_t MapTileSettings::cacheSize = 50 * 1024; // LV_FS_CACHE_FROM_BUFFER
float MapTileSettings::defaultLat = 51.5003646652f; // @theBigBentern
float MapTileSettings::defaultLon = -0.1214328476f;
char MapTileSettings::prefix[10] = "/maps"; // default map tile directory
char MapTileSettings::tileStyle[20] = ""; // { osm/, atlas/, atlas-mobile/, ...}
char MapTileSettings::tileFormat[10] = "png"; // use jpg or png
bool MapTileSettings::debug = false; // draw tile frame and info
bool MapTileSettings::save = false; // ok to save tile back to SD card
char MapTileSettings::prefix[MapTileSettings::PREFIX_SIZE] = "/maps"; // default map tile directory
char MapTileSettings::tileStyle[MapTileSettings::TILE_STYLE_SIZE] = ""; // { osm/, atlas/, atlas-mobile/, ...}
char MapTileSettings::tileFormat[MapTileSettings::TILE_FORMAT_SIZE] = "png"; // use jpg or png
bool MapTileSettings::debug = false; // draw tile frame and info
bool MapTileSettings::save = false; // ok to save tile back to SD card
#ifdef MAP_TILES_GREY
bool MapTileSettings::colorTiles = false;
#else
Expand Down
15 changes: 14 additions & 1 deletion source/graphics/map/TileProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ std::string TileProvider::url(const char *filename)
std::string TileProvider::url(int z, int x, int y)
{
std::string provider, url;
std::tie(provider, url) = urlTemplates[MapTileSettings::getTileProvider()];
if (urlTemplates.empty()) {
ILOG_ERROR("no URL templates available");
return "";
}

size_t selected = MapTileSettings::getTileProvider();
if (selected >= urlTemplates.size()) {
ILOG_WARN("tile provider index out of range: %u (max %u)", (unsigned int)selected,
(unsigned int)(urlTemplates.size() - 1));
selected = 0;
MapTileSettings::setTileProvider(0);
}

std::tie(provider, url) = urlTemplates[selected];
size_t pos;
while ((pos = url.find("{z}")) != std::string::npos)
url.replace(pos, 3, std::to_string(z));
Expand Down
12 changes: 12 additions & 0 deletions source/graphics/map/URLService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#ifdef ARDUINO_ARCH_ESP32

#include "WiFi.h"

// from ConvertPNG.c
extern "C" {
bool decodeImgGrey(const void *data, size_t size, lv_img_dsc_t **img);
Expand All @@ -23,13 +25,23 @@ bool URLService::load(const char *name, void *img)
~HttpEndGuard() { client.end(); }
} httpGuard{http};

if (WiFi.status() != WL_CONNECTED) {
ILOG_DEBUG("URLService::load skipped (WiFi not connected)");
return false;
}

struct LvFreeGuard {
uint8_t *&ptr;
~LvFreeGuard() { lv_free(ptr); }
};

// transform filename to provider url
std::string url = TileProvider::url(name);
if (url.empty()) {
ILOG_ERROR("empty URL for tile %s", name ? name : "(null)");
return false;
}

http.begin(url.c_str());
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
Expand Down
Loading