-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmap_tiles.cpp
More file actions
executable file
·509 lines (420 loc) · 15.4 KB
/
map_tiles.cpp
File metadata and controls
executable file
·509 lines (420 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "map_tiles.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "esp_log.h"
#include "esp_heap_caps.h"
static const char* TAG = "map_tiles";
// Internal structure for map tiles instance
struct map_tiles_t {
// Configuration
char* base_path;
char* tile_folders[MAP_TILES_MAX_TYPES];
int tile_type_count;
int current_tile_type;
int grid_cols;
int grid_rows;
int tile_count;
int zoom;
bool use_spiram;
bool initialized;
// Tile management
int tile_x;
int tile_y;
int marker_offset_x;
int marker_offset_y;
bool tile_loading_error;
// Tile data - arrays will be allocated dynamically based on actual grid size
uint8_t** tile_bufs;
lv_image_dsc_t* tile_imgs;
};
map_tiles_handle_t map_tiles_init(const map_tiles_config_t* config)
{
if (!config || !config->base_path || config->tile_type_count <= 0 ||
config->tile_type_count > MAP_TILES_MAX_TYPES ||
config->default_tile_type < 0 || config->default_tile_type >= config->tile_type_count) {
ESP_LOGE(TAG, "Invalid configuration");
return NULL;
}
// Validate grid size - use defaults if not specified or invalid
int grid_cols = config->grid_cols;
int grid_rows = config->grid_rows;
if (grid_cols <= 0 || grid_cols > MAP_TILES_MAX_GRID_COLS) {
ESP_LOGW(TAG, "Invalid grid_cols %d, using default %d", grid_cols, MAP_TILES_DEFAULT_GRID_COLS);
grid_cols = MAP_TILES_DEFAULT_GRID_COLS;
}
if (grid_rows <= 0 || grid_rows > MAP_TILES_MAX_GRID_ROWS) {
ESP_LOGW(TAG, "Invalid grid_rows %d, using default %d", grid_rows, MAP_TILES_DEFAULT_GRID_ROWS);
grid_rows = MAP_TILES_DEFAULT_GRID_ROWS;
}
int tile_count = grid_cols * grid_rows;
// Validate that all tile folders are provided
for (int i = 0; i < config->tile_type_count; i++) {
if (!config->tile_folders[i]) {
ESP_LOGE(TAG, "Tile folder %d is NULL", i);
return NULL;
}
}
map_tiles_handle_t handle = (map_tiles_handle_t)calloc(1, sizeof(struct map_tiles_t));
if (!handle) {
ESP_LOGE(TAG, "Failed to allocate handle");
return NULL;
}
// Copy base path
handle->base_path = strdup(config->base_path);
if (!handle->base_path) {
ESP_LOGE(TAG, "Failed to allocate base path");
free(handle);
return NULL;
}
// Copy tile folder names
handle->tile_type_count = config->tile_type_count;
for (int i = 0; i < config->tile_type_count; i++) {
handle->tile_folders[i] = strdup(config->tile_folders[i]);
if (!handle->tile_folders[i]) {
ESP_LOGE(TAG, "Failed to allocate tile folder %d", i);
// Clean up previously allocated folders
for (int j = 0; j < i; j++) {
free(handle->tile_folders[j]);
}
free(handle->base_path);
free(handle);
return NULL;
}
}
handle->zoom = config->default_zoom;
handle->use_spiram = config->use_spiram;
handle->current_tile_type = config->default_tile_type;
handle->grid_cols = grid_cols;
handle->grid_rows = grid_rows;
handle->tile_count = tile_count;
handle->initialized = true;
handle->tile_loading_error = false;
// Initialize tile data - allocate arrays based on actual tile count
handle->tile_bufs = (uint8_t**)calloc(tile_count, sizeof(uint8_t*));
handle->tile_imgs = (lv_image_dsc_t*)calloc(tile_count, sizeof(lv_image_dsc_t));
if (!handle->tile_bufs || !handle->tile_imgs) {
ESP_LOGE(TAG, "Failed to allocate tile arrays");
// Clean up
if (handle->tile_bufs) free(handle->tile_bufs);
if (handle->tile_imgs) free(handle->tile_imgs);
for (int i = 0; i < handle->tile_type_count; i++) {
free(handle->tile_folders[i]);
}
free(handle->base_path);
free(handle);
return NULL;
}
ESP_LOGI(TAG, "Map tiles initialized with base path: %s, %d tile types, current type: %s, zoom: %d, grid: %dx%d",
handle->base_path, handle->tile_type_count,
handle->tile_folders[handle->current_tile_type], handle->zoom,
handle->grid_cols, handle->grid_rows);
return handle;
}
void map_tiles_set_zoom(map_tiles_handle_t handle, int zoom_level)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
handle->zoom = zoom_level;
ESP_LOGI(TAG, "Zoom level set to %d", zoom_level);
}
int map_tiles_get_zoom(map_tiles_handle_t handle)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return 0;
}
return handle->zoom;
}
bool map_tiles_set_tile_type(map_tiles_handle_t handle, int tile_type)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return false;
}
if (tile_type < 0 || tile_type >= handle->tile_type_count) {
ESP_LOGE(TAG, "Invalid tile type: %d (valid range: 0-%d)", tile_type, handle->tile_type_count - 1);
return false;
}
handle->current_tile_type = tile_type;
ESP_LOGI(TAG, "Tile type set to %d (%s)", tile_type, handle->tile_folders[tile_type]);
return true;
}
int map_tiles_get_tile_type(map_tiles_handle_t handle)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return -1;
}
return handle->current_tile_type;
}
int map_tiles_get_tile_type_count(map_tiles_handle_t handle)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return 0;
}
return handle->tile_type_count;
}
const char* map_tiles_get_tile_type_folder(map_tiles_handle_t handle, int tile_type)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return NULL;
}
if (tile_type < 0 || tile_type >= handle->tile_type_count) {
ESP_LOGE(TAG, "Invalid tile type: %d", tile_type);
return NULL;
}
return handle->tile_folders[tile_type];
}
bool map_tiles_load_tile(map_tiles_handle_t handle, int index, int tile_x, int tile_y)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return false;
}
if (index < 0 || index >= handle->tile_count) {
ESP_LOGE(TAG, "Invalid tile index: %d", index);
return false;
}
char path[256];
const char* folder = handle->tile_folders[handle->current_tile_type];
snprintf(path, sizeof(path), "%s/%s/%d/%d/%d.bin",
handle->base_path, folder, handle->zoom, tile_x, tile_y);
FILE *f = fopen(path, "rb");
if (!f) {
ESP_LOGW(TAG, "Tile not found: %s", path);
return false;
}
// Skip 12-byte header
fseek(f, 12, SEEK_SET);
// Allocate buffer if needed
if (!handle->tile_bufs[index]) {
uint32_t caps = handle->use_spiram ? (MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) : MALLOC_CAP_DMA;
handle->tile_bufs[index] = (uint8_t*)heap_caps_malloc(
MAP_TILES_TILE_SIZE * MAP_TILES_TILE_SIZE * MAP_TILES_BYTES_PER_PIXEL, caps);
if (!handle->tile_bufs[index]) {
ESP_LOGE(TAG, "Tile %d: allocation failed", index);
fclose(f);
return false;
}
}
// Clear buffer
memset(handle->tile_bufs[index], 0,
MAP_TILES_TILE_SIZE * MAP_TILES_TILE_SIZE * MAP_TILES_BYTES_PER_PIXEL);
// Read tile data
size_t bytes_read = fread(handle->tile_bufs[index], 1,
MAP_TILES_TILE_SIZE * MAP_TILES_TILE_SIZE * MAP_TILES_BYTES_PER_PIXEL, f);
fclose(f);
if (bytes_read != MAP_TILES_TILE_SIZE * MAP_TILES_TILE_SIZE * MAP_TILES_BYTES_PER_PIXEL) {
ESP_LOGW(TAG, "Incomplete tile read: %zu bytes", bytes_read);
}
// Setup image descriptor
handle->tile_imgs[index].header.w = MAP_TILES_TILE_SIZE;
handle->tile_imgs[index].header.h = MAP_TILES_TILE_SIZE;
handle->tile_imgs[index].header.cf = MAP_TILES_COLOR_FORMAT;
handle->tile_imgs[index].header.stride = MAP_TILES_TILE_SIZE * MAP_TILES_BYTES_PER_PIXEL;
handle->tile_imgs[index].data = (const uint8_t*)handle->tile_bufs[index];
handle->tile_imgs[index].data_size = MAP_TILES_TILE_SIZE * MAP_TILES_TILE_SIZE * MAP_TILES_BYTES_PER_PIXEL;
handle->tile_imgs[index].reserved = NULL;
handle->tile_imgs[index].reserved_2 = NULL;
ESP_LOGD(TAG, "Loaded tile %d from %s", index, path);
return true;
}
void map_tiles_gps_to_tile_xy(map_tiles_handle_t handle, double lat, double lon, double* x, double* y)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
if (!x || !y) {
ESP_LOGE(TAG, "Invalid output parameters");
return;
}
double lat_rad = lat * M_PI / 180.0;
int n = 1 << handle->zoom;
*x = (lon + 180.0) / 360.0 * n;
*y = (1.0 - log(tan(lat_rad) + 1.0 / cos(lat_rad)) / M_PI) / 2.0 * n;
}
void map_tiles_tile_xy_to_gps(map_tiles_handle_t handle, double x, double y, double* lat, double* lon)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
if (!lat || !lon) {
ESP_LOGE(TAG, "Invalid output parameters");
return;
}
int n = 1 << handle->zoom;
*lon = x / n * 360.0 - 180.0;
double lat_rad = atan(sinh(M_PI * (1 - 2 * y / n)));
*lat = lat_rad * 180.0 / M_PI;
}
void map_tiles_get_center_gps(map_tiles_handle_t handle, double* lat, double* lon)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
if (!lat || !lon) {
ESP_LOGE(TAG, "Invalid output parameters");
return;
}
// Calculate center tile coordinates (center of the grid)
double center_x = handle->tile_x + handle->grid_cols / 2.0;
double center_y = handle->tile_y + handle->grid_rows / 2.0;
// Convert to GPS coordinates
map_tiles_tile_xy_to_gps(handle, center_x, center_y, lat, lon);
}
void map_tiles_set_center_from_gps(map_tiles_handle_t handle, double lat, double lon)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
double x, y;
map_tiles_gps_to_tile_xy(handle, lat, lon, &x, &y);
handle->tile_x = (int)x - handle->grid_cols / 2;
handle->tile_y = (int)y - handle->grid_rows / 2;
// Calculate pixel offset within the tile
handle->marker_offset_x = (int)((x - (int)x) * MAP_TILES_TILE_SIZE);
handle->marker_offset_y = (int)((y - (int)y) * MAP_TILES_TILE_SIZE);
ESP_LOGI(TAG, "GPS to tile: tile_x=%d, tile_y=%d, offset_x=%d, offset_y=%d",
handle->tile_x, handle->tile_y, handle->marker_offset_x, handle->marker_offset_y);
}
bool map_tiles_is_gps_within_tiles(map_tiles_handle_t handle, double lat, double lon)
{
if (!handle || !handle->initialized) {
return false;
}
double x, y;
map_tiles_gps_to_tile_xy(handle, lat, lon, &x, &y);
int gps_tile_x = (int)x;
int gps_tile_y = (int)y;
bool within_x = (gps_tile_x >= handle->tile_x && gps_tile_x < handle->tile_x + handle->grid_cols);
bool within_y = (gps_tile_y >= handle->tile_y && gps_tile_y < handle->tile_y + handle->grid_rows);
return within_x && within_y;
}
void map_tiles_get_position(map_tiles_handle_t handle, int* tile_x, int* tile_y)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
if (tile_x) *tile_x = handle->tile_x;
if (tile_y) *tile_y = handle->tile_y;
}
void map_tiles_set_position(map_tiles_handle_t handle, int tile_x, int tile_y)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
handle->tile_x = tile_x;
handle->tile_y = tile_y;
}
void map_tiles_get_marker_offset(map_tiles_handle_t handle, int* offset_x, int* offset_y)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
if (offset_x) *offset_x = handle->marker_offset_x;
if (offset_y) *offset_y = handle->marker_offset_y;
}
void map_tiles_set_marker_offset(map_tiles_handle_t handle, int offset_x, int offset_y)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
handle->marker_offset_x = offset_x;
handle->marker_offset_y = offset_y;
}
lv_image_dsc_t* map_tiles_get_image(map_tiles_handle_t handle, int index)
{
if (!handle || !handle->initialized || index < 0 || index >= handle->tile_count) {
return NULL;
}
return &handle->tile_imgs[index];
}
uint8_t* map_tiles_get_buffer(map_tiles_handle_t handle, int index)
{
if (!handle || !handle->initialized || index < 0 || index >= handle->tile_count) {
return NULL;
}
return handle->tile_bufs[index];
}
void map_tiles_set_loading_error(map_tiles_handle_t handle, bool error)
{
if (!handle || !handle->initialized) {
ESP_LOGE(TAG, "Handle not initialized");
return;
}
handle->tile_loading_error = error;
}
bool map_tiles_has_loading_error(map_tiles_handle_t handle)
{
if (!handle || !handle->initialized) {
return true;
}
return handle->tile_loading_error;
}
void map_tiles_cleanup(map_tiles_handle_t handle)
{
if (!handle) {
return;
}
if (handle->initialized) {
// Free tile buffers
if (handle->tile_bufs) {
for (int i = 0; i < handle->tile_count; i++) {
if (handle->tile_bufs[i]) {
heap_caps_free(handle->tile_bufs[i]);
handle->tile_bufs[i] = NULL;
}
}
free(handle->tile_bufs);
handle->tile_bufs = NULL;
}
// Free tile image descriptors array
if (handle->tile_imgs) {
free(handle->tile_imgs);
handle->tile_imgs = NULL;
}
handle->initialized = false;
ESP_LOGI(TAG, "Map tiles cleaned up");
}
// Free base path and folder names, then handle
if (handle->base_path) {
free(handle->base_path);
}
for (int i = 0; i < handle->tile_type_count; i++) {
if (handle->tile_folders[i]) {
free(handle->tile_folders[i]);
}
}
free(handle);
}
void map_tiles_get_grid_size(map_tiles_handle_t handle, int* cols, int* rows)
{
if (!handle || !handle->initialized || !cols || !rows) {
if (cols) *cols = 0;
if (rows) *rows = 0;
return;
}
*cols = handle->grid_cols;
*rows = handle->grid_rows;
}
int map_tiles_get_tile_count(map_tiles_handle_t handle)
{
if (!handle || !handle->initialized) {
return 0;
}
return handle->tile_count;
}