// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/*
handle disk IO for terrain code
*/
#include
#include
#include
#include
#include
#include "AP_Terrain.h"
#if AP_TERRAIN_AVAILABLE
#include
#include
#include
#include
#include
#include
#include
extern const AP_HAL::HAL& hal;
/*
calculate bit number in grid_block bitmap. This corresponds to a
bit representing a 4x4 mavlink transmitted block
*/
uint8_t AP_Terrain::grid_bitnum(uint8_t idx_x, uint8_t idx_y)
{
ASSERT_RANGE(idx_x,0,27);
ASSERT_RANGE(idx_y,0,31);
uint8_t subgrid_x = idx_x / TERRAIN_GRID_MAVLINK_SIZE;
uint8_t subgrid_y = idx_y / TERRAIN_GRID_MAVLINK_SIZE;
ASSERT_RANGE(subgrid_x,0,TERRAIN_GRID_BLOCK_MUL_X-1);
ASSERT_RANGE(subgrid_y,0,TERRAIN_GRID_BLOCK_MUL_Y-1);
return subgrid_y + TERRAIN_GRID_BLOCK_MUL_Y*subgrid_x;
}
/*
given a grid_info check that a given idx_x/idx_y is available (set
in the bitmap)
*/
bool AP_Terrain::check_bitmap(const struct grid_block &grid, uint8_t idx_x, uint8_t idx_y)
{
uint8_t bitnum = grid_bitnum(idx_x, idx_y);
return (grid.bitmap & (((uint64_t)1U)<millis();
return cache[i];
}
if (cache[i].last_access_ms < cache[oldest_i].last_access_ms) {
oldest_i = i;
}
}
// Not found. Use the oldest grid and make it this grid,
// initially unpopulated
struct grid_cache &grid = cache[oldest_i];
memset(&grid, 0, sizeof(grid));
grid.grid.lat = info.grid_lat;
grid.grid.lon = info.grid_lon;
grid.grid.spacing = grid_spacing;
grid.grid.grid_idx_x = info.grid_idx_x;
grid.grid.grid_idx_y = info.grid_idx_y;
grid.grid.lat_degrees = info.lat_degrees;
grid.grid.lon_degrees = info.lon_degrees;
grid.grid.version = TERRAIN_GRID_FORMAT_VERSION;
grid.last_access_ms = hal.scheduler->millis();
// mark as waiting for disk read
grid.state = GRID_CACHE_DISKWAIT;
return grid;
}
/*
find cache index of disk_block
*/
int16_t AP_Terrain::find_io_idx(enum GridCacheState state)
{
// try first with given state
for (uint16_t i=0; i