2012-02-11 07:51:35 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
// total up and check overflow
|
|
|
|
// check size of group var_info
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
/// @file AP_Param.cpp
|
|
|
|
/// @brief The AP variable store.
|
|
|
|
|
|
|
|
|
2012-09-18 15:08:38 -03:00
|
|
|
#include <AP_HAL.h>
|
2012-02-11 07:51:35 -04:00
|
|
|
#include <AP_Common.h>
|
2012-02-17 19:00:26 -04:00
|
|
|
#include <AP_Math.h>
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-09-18 15:08:38 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
|
|
|
|
2014-11-09 17:09:08 -04:00
|
|
|
#define ENABLE_DEBUG 0
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2014-11-09 17:09:08 -04:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
# define Debug(fmt, args ...) do {hal.console->printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
|
2012-02-11 07:51:35 -04:00
|
|
|
#else
|
2014-11-09 17:09:08 -04:00
|
|
|
# define Debug(fmt, args ...)
|
2012-02-11 07:51:35 -04:00
|
|
|
#endif
|
|
|
|
|
2012-02-12 05:18:10 -04:00
|
|
|
// some useful progmem macros
|
|
|
|
#define PGM_UINT8(addr) pgm_read_byte((const prog_char *)addr)
|
|
|
|
#define PGM_UINT16(addr) pgm_read_word((const uint16_t *)addr)
|
2012-08-06 22:00:43 -03:00
|
|
|
#define PGM_FLOAT(addr) pgm_read_float((const float *)addr)
|
2012-02-12 05:18:10 -04:00
|
|
|
#define PGM_POINTER(addr) pgm_read_pointer((const void *)addr)
|
|
|
|
|
2012-11-05 07:08:43 -04:00
|
|
|
// the 'GROUP_ID' of a element of a group is the 18 bit identifier
|
|
|
|
// used to distinguish between this element of the group and other
|
|
|
|
// elements of the same group. It is calculated using a bit shift per
|
|
|
|
// level of nesting, so the first level of nesting gets 6 bits the 2nd
|
|
|
|
// level gets the next 6 bits, and the 3rd level gets the last 6
|
|
|
|
// bits. This limits groups to having at most 64 elements.
|
2012-02-12 18:54:46 -04:00
|
|
|
#define GROUP_ID(grpinfo, base, i, shift) ((base)+(((uint16_t)PGM_UINT8(&grpinfo[i].idx))<<(shift)))
|
|
|
|
|
2012-02-24 20:37:15 -04:00
|
|
|
// Note about AP_Vector3f handling.
|
|
|
|
// The code has special cases for AP_Vector3f to allow it to be viewed
|
|
|
|
// as both a single 3 element vector and as a set of 3 AP_Float
|
|
|
|
// variables. This is done to make it possible for MAVLink to see
|
|
|
|
// vectors as parameters, which allows users to save their compass
|
|
|
|
// offsets in MAVLink parameter files. The code involves quite a few
|
|
|
|
// special cases which could be generalised to any vector/matrix type
|
|
|
|
// if we end up needing this behaviour for other than AP_Vector3f
|
2012-02-12 18:54:46 -04:00
|
|
|
|
2012-02-24 20:37:15 -04:00
|
|
|
|
|
|
|
// static member variables for AP_Param.
|
2012-02-11 07:51:35 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
// number of rows in the _var_info[] table
|
2012-02-12 19:12:02 -04:00
|
|
|
uint8_t AP_Param::_num_vars;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
// storage and naming information about all types that can be saved
|
|
|
|
const AP_Param::Info *AP_Param::_var_info;
|
|
|
|
|
2014-08-13 01:43:19 -03:00
|
|
|
// storage object
|
|
|
|
StorageAccess AP_Param::_storage(StorageManager::StorageParam);
|
|
|
|
|
|
|
|
|
2013-06-04 01:02:49 -03:00
|
|
|
// write to EEPROM
|
2012-02-11 07:51:35 -04:00
|
|
|
void AP_Param::eeprom_write_check(const void *ptr, uint16_t ofs, uint8_t size)
|
|
|
|
{
|
2014-08-13 01:43:19 -03:00
|
|
|
_storage.write_block(ofs, ptr, size);
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// write a sentinal value at the given offset
|
|
|
|
void AP_Param::write_sentinal(uint16_t ofs)
|
|
|
|
{
|
|
|
|
struct Param_header phdr;
|
2012-02-12 18:54:46 -04:00
|
|
|
phdr.type = _sentinal_type;
|
|
|
|
phdr.key = _sentinal_key;
|
|
|
|
phdr.group_element = _sentinal_group;
|
2012-02-11 07:51:35 -04:00
|
|
|
eeprom_write_check(&phdr, ofs, sizeof(phdr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// erase all EEPROM variables by re-writing the header and adding
|
|
|
|
// a sentinal
|
|
|
|
void AP_Param::erase_all(void)
|
|
|
|
{
|
|
|
|
struct EEPROM_header hdr;
|
|
|
|
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("erase_all");
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
// write the header
|
2012-02-12 19:12:02 -04:00
|
|
|
hdr.magic[0] = k_EEPROM_magic0;
|
|
|
|
hdr.magic[1] = k_EEPROM_magic1;
|
2012-02-11 07:51:35 -04:00
|
|
|
hdr.revision = k_EEPROM_revision;
|
2012-02-12 19:12:02 -04:00
|
|
|
hdr.spare = 0;
|
2012-02-11 07:51:35 -04:00
|
|
|
eeprom_write_check(&hdr, 0, sizeof(hdr));
|
|
|
|
|
|
|
|
// add a sentinal directly after the header
|
|
|
|
write_sentinal(sizeof(struct EEPROM_header));
|
|
|
|
}
|
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
// validate a group info table
|
2012-08-17 03:18:11 -03:00
|
|
|
bool AP_Param::check_group_info(const struct AP_Param::GroupInfo * group_info,
|
|
|
|
uint16_t * total_size,
|
2014-11-09 17:09:08 -04:00
|
|
|
uint8_t group_shift,
|
|
|
|
uint8_t prefix_length)
|
2012-02-12 03:22:57 -04:00
|
|
|
{
|
|
|
|
uint8_t type;
|
2012-02-12 18:54:46 -04:00
|
|
|
int8_t max_idx = -1;
|
2012-02-12 03:22:57 -04:00
|
|
|
for (uint8_t i=0;
|
2012-02-12 05:18:10 -04:00
|
|
|
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
2012-02-12 03:22:57 -04:00
|
|
|
i++) {
|
2012-02-28 19:54:40 -04:00
|
|
|
#ifdef AP_NESTED_GROUPS_ENABLED
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
|
|
|
// a nested group
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *ginfo = (const struct GroupInfo *)PGM_POINTER(&group_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
if (group_shift + _group_level_shift >= _group_bits) {
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("double group nesting in %S", group_info[i].name);
|
2012-02-12 03:22:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ginfo == NULL ||
|
2014-11-09 17:09:08 -04:00
|
|
|
!check_group_info(ginfo, total_size, group_shift + _group_level_shift, prefix_length + strlen_P(group_info[i].name))) {
|
2012-02-12 03:22:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-28 19:54:40 -04:00
|
|
|
#endif // AP_NESTED_GROUPS_ENABLED
|
2012-02-12 18:54:46 -04:00
|
|
|
uint8_t idx = PGM_UINT8(&group_info[i].idx);
|
|
|
|
if (idx >= (1<<_group_level_shift)) {
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("idx too large (%u) in %S", idx, group_info[i].name);
|
2012-02-12 03:22:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-12 18:54:46 -04:00
|
|
|
if ((int8_t)idx <= max_idx) {
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("indexes must be in increasing order in %S", group_info[i].name);
|
2012-02-12 18:54:46 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
max_idx = (int8_t)idx;
|
2012-02-12 03:22:57 -04:00
|
|
|
uint8_t size = type_size((enum ap_var_type)type);
|
|
|
|
if (size == 0) {
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("invalid type in %S", group_info[i].name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (prefix_length + strlen_P(group_info[i].name) > 16) {
|
|
|
|
Debug("suffix is too long in %S", group_info[i].name);
|
2012-02-12 03:22:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
(*total_size) += size + sizeof(struct Param_header);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-02 00:45:18 -04:00
|
|
|
// check for duplicate key values
|
|
|
|
bool AP_Param::duplicate_key(uint8_t vindex, uint8_t key)
|
|
|
|
{
|
|
|
|
for (uint8_t i=vindex+1; i<_num_vars; i++) {
|
|
|
|
uint8_t key2 = PGM_UINT8(&_var_info[i].key);
|
|
|
|
if (key2 == key) {
|
|
|
|
// no duplicate keys allowed
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
// validate the _var_info[] table
|
|
|
|
bool AP_Param::check_var_info(void)
|
|
|
|
{
|
|
|
|
uint16_t total_size = sizeof(struct EEPROM_header);
|
|
|
|
|
2012-02-12 19:12:02 -04:00
|
|
|
for (uint8_t i=0; i<_num_vars; i++) {
|
2012-02-12 05:18:10 -04:00
|
|
|
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
2012-03-02 00:45:18 -04:00
|
|
|
uint8_t key = PGM_UINT8(&_var_info[i].key);
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
|
|
|
if (i == 0) {
|
|
|
|
// first element can't be a group, for first() call
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *group_info = (const struct GroupInfo *)PGM_POINTER(&_var_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
if (group_info == NULL ||
|
2014-11-09 17:09:08 -04:00
|
|
|
!check_group_info(group_info, &total_size, 0, strlen_P(_var_info[i].name))) {
|
2012-02-12 03:22:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint8_t size = type_size((enum ap_var_type)type);
|
|
|
|
if (size == 0) {
|
2012-02-12 16:43:09 -04:00
|
|
|
// not a valid type - the top level list can't contain
|
2012-02-12 18:54:46 -04:00
|
|
|
// AP_PARAM_NONE
|
2012-02-12 03:22:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
total_size += size + sizeof(struct Param_header);
|
|
|
|
}
|
2012-03-02 00:45:18 -04:00
|
|
|
if (duplicate_key(i, key)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
2012-08-06 22:00:43 -03:00
|
|
|
|
|
|
|
// we no longer check if total_size is larger than _eeprom_size,
|
|
|
|
// as we allow for more variables than could fit, relying on not
|
|
|
|
// saving default values
|
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// setup the _var_info[] table
|
2012-12-13 18:58:23 -04:00
|
|
|
bool AP_Param::setup(void)
|
2012-02-11 07:51:35 -04:00
|
|
|
{
|
|
|
|
struct EEPROM_header hdr;
|
2012-02-12 03:22:57 -04:00
|
|
|
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("setup %u vars", (unsigned)_num_vars);
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
// check the header
|
2014-08-13 01:43:19 -03:00
|
|
|
_storage.read_block(&hdr, 0, sizeof(hdr));
|
2012-02-12 19:12:02 -04:00
|
|
|
if (hdr.magic[0] != k_EEPROM_magic0 ||
|
|
|
|
hdr.magic[1] != k_EEPROM_magic1 ||
|
2012-02-11 07:51:35 -04:00
|
|
|
hdr.revision != k_EEPROM_revision) {
|
|
|
|
// header doesn't match. We can't recover any variables. Wipe
|
|
|
|
// the header and setup the sentinal directly after the header
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("bad header in setup - erasing");
|
2012-02-11 07:51:35 -04:00
|
|
|
erase_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-17 00:12:48 -04:00
|
|
|
// check if AP_Param has been initialised
|
|
|
|
bool AP_Param::initialised(void)
|
|
|
|
{
|
|
|
|
return _var_info != NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
// find the info structure given a header and a group_info table
|
|
|
|
// return the Info structure and a pointer to the variables storage
|
|
|
|
const struct AP_Param::Info *AP_Param::find_by_header_group(struct Param_header phdr, void **ptr,
|
|
|
|
uint8_t vindex,
|
|
|
|
const struct GroupInfo *group_info,
|
|
|
|
uint8_t group_base,
|
|
|
|
uint8_t group_shift)
|
|
|
|
{
|
|
|
|
uint8_t type;
|
|
|
|
for (uint8_t i=0;
|
2012-02-12 05:18:10 -04:00
|
|
|
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
2012-02-12 03:22:57 -04:00
|
|
|
i++) {
|
2012-02-28 19:54:40 -04:00
|
|
|
#ifdef AP_NESTED_GROUPS_ENABLED
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
|
|
|
// a nested group
|
|
|
|
if (group_shift + _group_level_shift >= _group_bits) {
|
|
|
|
// too deeply nested - this should have been caught by
|
|
|
|
// setup() !
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *ginfo = (const struct GroupInfo *)PGM_POINTER(&group_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
const struct AP_Param::Info *ret = find_by_header_group(phdr, ptr, vindex, ginfo,
|
2012-02-12 18:54:46 -04:00
|
|
|
GROUP_ID(group_info, group_base, i, group_shift),
|
2012-02-12 03:22:57 -04:00
|
|
|
group_shift + _group_level_shift);
|
|
|
|
if (ret != NULL) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-28 19:54:40 -04:00
|
|
|
#endif // AP_NESTED_GROUPS_ENABLED
|
2012-02-12 18:54:46 -04:00
|
|
|
if (GROUP_ID(group_info, group_base, i, group_shift) == phdr.group_element) {
|
2012-02-12 03:22:57 -04:00
|
|
|
// found a group element
|
2012-02-12 05:18:10 -04:00
|
|
|
*ptr = (void*)(PGM_POINTER(&_var_info[vindex].ptr) + PGM_UINT16(&group_info[i].offset));
|
2012-02-12 03:22:57 -04:00
|
|
|
return &_var_info[vindex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// find the info structure given a header
|
|
|
|
// return the Info structure and a pointer to the variables storage
|
|
|
|
const struct AP_Param::Info *AP_Param::find_by_header(struct Param_header phdr, void **ptr)
|
|
|
|
{
|
|
|
|
// loop over all named variables
|
2012-02-12 19:12:02 -04:00
|
|
|
for (uint8_t i=0; i<_num_vars; i++) {
|
2012-02-12 05:18:10 -04:00
|
|
|
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
2012-02-12 18:54:46 -04:00
|
|
|
uint8_t key = PGM_UINT8(&_var_info[i].key);
|
2012-02-11 07:51:35 -04:00
|
|
|
if (key != phdr.key) {
|
|
|
|
// not the right key
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type != AP_PARAM_GROUP) {
|
|
|
|
// if its not a group then we are done
|
2012-02-12 05:18:10 -04:00
|
|
|
*ptr = (void*)PGM_POINTER(&_var_info[i].ptr);
|
2012-02-11 07:51:35 -04:00
|
|
|
return &_var_info[i];
|
|
|
|
}
|
|
|
|
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *group_info = (const struct GroupInfo *)PGM_POINTER(&_var_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
return find_by_header_group(phdr, ptr, i, group_info, 0, 0);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the info structure for a variable in a group
|
2012-08-17 03:18:11 -03:00
|
|
|
const struct AP_Param::Info *AP_Param::find_var_info_group(const struct GroupInfo * group_info,
|
|
|
|
uint8_t vindex,
|
|
|
|
uint8_t group_base,
|
|
|
|
uint8_t group_shift,
|
2012-11-05 07:08:43 -04:00
|
|
|
uint32_t * group_element,
|
2012-02-23 23:19:00 -04:00
|
|
|
const struct GroupInfo **group_ret,
|
2013-04-19 04:46:40 -03:00
|
|
|
uint8_t * idx) const
|
2012-02-12 03:22:57 -04:00
|
|
|
{
|
2012-02-12 05:18:10 -04:00
|
|
|
uintptr_t base = PGM_POINTER(&_var_info[vindex].ptr);
|
2012-02-12 03:22:57 -04:00
|
|
|
uint8_t type;
|
|
|
|
for (uint8_t i=0;
|
2012-02-12 05:18:10 -04:00
|
|
|
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
2012-02-12 03:22:57 -04:00
|
|
|
i++) {
|
2012-02-23 23:19:00 -04:00
|
|
|
uintptr_t ofs = PGM_POINTER(&group_info[i].offset);
|
2012-02-28 19:54:40 -04:00
|
|
|
#ifdef AP_NESTED_GROUPS_ENABLED
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *ginfo = (const struct GroupInfo *)PGM_POINTER(&group_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
// a nested group
|
|
|
|
if (group_shift + _group_level_shift >= _group_bits) {
|
|
|
|
// too deeply nested - this should have been caught by
|
|
|
|
// setup() !
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const struct AP_Param::Info *info;
|
|
|
|
info = find_var_info_group(ginfo, vindex,
|
2012-02-12 18:54:46 -04:00
|
|
|
GROUP_ID(group_info, group_base, i, group_shift),
|
2012-02-12 03:22:57 -04:00
|
|
|
group_shift + _group_level_shift,
|
|
|
|
group_element,
|
2012-02-23 23:19:00 -04:00
|
|
|
group_ret,
|
|
|
|
idx);
|
2012-02-12 03:22:57 -04:00
|
|
|
if (info != NULL) {
|
|
|
|
return info;
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
2012-02-28 19:54:40 -04:00
|
|
|
} else // Forgive the poor formatting - if continues below.
|
|
|
|
#endif // AP_NESTED_GROUPS_ENABLED
|
2012-08-17 03:18:11 -03:00
|
|
|
if ((uintptr_t) this == base + ofs) {
|
2012-02-23 23:19:00 -04:00
|
|
|
*group_element = GROUP_ID(group_info, group_base, i, group_shift);
|
|
|
|
*group_ret = &group_info[i];
|
|
|
|
*idx = 0;
|
|
|
|
return &_var_info[vindex];
|
|
|
|
} else if (type == AP_PARAM_VECTOR3F &&
|
2012-08-17 03:18:11 -03:00
|
|
|
(base+ofs+sizeof(float) == (uintptr_t) this ||
|
|
|
|
base+ofs+2*sizeof(float) == (uintptr_t) this)) {
|
2012-02-24 20:37:15 -04:00
|
|
|
// we are inside a Vector3f. We need to work out which
|
|
|
|
// element of the vector the current object refers to.
|
2012-08-17 03:18:11 -03:00
|
|
|
*idx = (((uintptr_t) this) - (base+ofs))/sizeof(float);
|
2012-02-12 18:54:46 -04:00
|
|
|
*group_element = GROUP_ID(group_info, group_base, i, group_shift);
|
2012-02-12 03:22:57 -04:00
|
|
|
*group_ret = &group_info[i];
|
|
|
|
return &_var_info[vindex];
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the info structure for a variable
|
2012-11-05 07:08:43 -04:00
|
|
|
const struct AP_Param::Info *AP_Param::find_var_info(uint32_t * group_element,
|
2012-08-17 03:18:11 -03:00
|
|
|
const struct GroupInfo ** group_ret,
|
|
|
|
uint8_t * idx)
|
2012-02-11 07:51:35 -04:00
|
|
|
{
|
2012-02-12 19:12:02 -04:00
|
|
|
for (uint8_t i=0; i<_num_vars; i++) {
|
2012-02-12 05:18:10 -04:00
|
|
|
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
|
|
|
uintptr_t base = PGM_POINTER(&_var_info[i].ptr);
|
2012-02-11 07:51:35 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *group_info = (const struct GroupInfo *)PGM_POINTER(&_var_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
const struct AP_Param::Info *info;
|
2012-02-23 23:19:00 -04:00
|
|
|
info = find_var_info_group(group_info, i, 0, 0, group_element, group_ret, idx);
|
2012-02-12 03:22:57 -04:00
|
|
|
if (info != NULL) {
|
|
|
|
return info;
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
2012-08-17 03:18:11 -03:00
|
|
|
} else if (base == (uintptr_t) this) {
|
2012-02-23 23:19:00 -04:00
|
|
|
*group_element = 0;
|
|
|
|
*group_ret = NULL;
|
|
|
|
*idx = 0;
|
|
|
|
return &_var_info[i];
|
|
|
|
} else if (type == AP_PARAM_VECTOR3F &&
|
2012-08-17 03:18:11 -03:00
|
|
|
(base+sizeof(float) == (uintptr_t) this ||
|
|
|
|
base+2*sizeof(float) == (uintptr_t) this)) {
|
2012-02-24 20:37:15 -04:00
|
|
|
// we are inside a Vector3f. Work out which element we are
|
|
|
|
// referring to.
|
2012-08-17 03:18:11 -03:00
|
|
|
*idx = (((uintptr_t) this) - base)/sizeof(float);
|
2012-02-11 07:51:35 -04:00
|
|
|
*group_element = 0;
|
2012-02-12 03:22:57 -04:00
|
|
|
*group_ret = NULL;
|
2012-02-11 07:51:35 -04:00
|
|
|
return &_var_info[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-08 00:28:16 -04:00
|
|
|
|
|
|
|
// find the info structure for a variable
|
2013-04-19 04:46:40 -03:00
|
|
|
const struct AP_Param::Info *AP_Param::find_var_info_token(const ParamToken &token,
|
2013-01-08 00:28:16 -04:00
|
|
|
uint32_t * group_element,
|
|
|
|
const struct GroupInfo ** group_ret,
|
2013-04-19 04:46:40 -03:00
|
|
|
uint8_t * idx) const
|
2013-01-08 00:28:16 -04:00
|
|
|
{
|
2013-04-19 04:46:40 -03:00
|
|
|
uint8_t i = token.key;
|
2013-01-08 00:28:16 -04:00
|
|
|
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
|
|
|
uintptr_t base = PGM_POINTER(&_var_info[i].ptr);
|
|
|
|
if (type == AP_PARAM_GROUP) {
|
|
|
|
const struct GroupInfo *group_info = (const struct GroupInfo *)PGM_POINTER(&_var_info[i].group_info);
|
|
|
|
const struct AP_Param::Info *info;
|
|
|
|
info = find_var_info_group(group_info, i, 0, 0, group_element, group_ret, idx);
|
|
|
|
if (info != NULL) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
} else if (base == (uintptr_t) this) {
|
|
|
|
*group_element = 0;
|
|
|
|
*group_ret = NULL;
|
|
|
|
*idx = 0;
|
|
|
|
return &_var_info[i];
|
|
|
|
} else if (type == AP_PARAM_VECTOR3F &&
|
|
|
|
(base+sizeof(float) == (uintptr_t) this ||
|
|
|
|
base+2*sizeof(float) == (uintptr_t) this)) {
|
|
|
|
// we are inside a Vector3f. Work out which element we are
|
|
|
|
// referring to.
|
|
|
|
*idx = (((uintptr_t) this) - base)/sizeof(float);
|
|
|
|
*group_element = 0;
|
|
|
|
*group_ret = NULL;
|
|
|
|
return &_var_info[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// return the storage size for a AP_PARAM_* type
|
2013-01-01 21:51:25 -04:00
|
|
|
uint8_t AP_Param::type_size(enum ap_var_type type)
|
2012-02-11 07:51:35 -04:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AP_PARAM_NONE:
|
|
|
|
case AP_PARAM_GROUP:
|
|
|
|
return 0;
|
|
|
|
case AP_PARAM_INT8:
|
|
|
|
return 1;
|
|
|
|
case AP_PARAM_INT16:
|
|
|
|
return 2;
|
|
|
|
case AP_PARAM_INT32:
|
|
|
|
return 4;
|
|
|
|
case AP_PARAM_FLOAT:
|
|
|
|
return 4;
|
|
|
|
case AP_PARAM_VECTOR3F:
|
|
|
|
return 3*4;
|
2012-02-11 08:00:08 -04:00
|
|
|
case AP_PARAM_VECTOR6F:
|
|
|
|
return 6*4;
|
2012-02-11 07:51:35 -04:00
|
|
|
case AP_PARAM_MATRIX3F:
|
|
|
|
return 3*3*4;
|
|
|
|
}
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("unknown type %u\n", type);
|
2012-02-11 07:51:35 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan the EEPROM looking for a given variable by header content
|
|
|
|
// return true if found, along with the offset in the EEPROM where
|
|
|
|
// the variable is stored
|
2013-05-24 11:30:22 -03:00
|
|
|
// if not found return the offset of the sentinal
|
2013-07-09 04:53:04 -03:00
|
|
|
// if the sentinal isn't found either, the offset is set to 0xFFFF
|
2012-02-11 07:51:35 -04:00
|
|
|
bool AP_Param::scan(const AP_Param::Param_header *target, uint16_t *pofs)
|
|
|
|
{
|
|
|
|
struct Param_header phdr;
|
|
|
|
uint16_t ofs = sizeof(AP_Param::EEPROM_header);
|
2014-08-13 01:43:19 -03:00
|
|
|
while (ofs < _storage.size()) {
|
|
|
|
_storage.read_block(&phdr, ofs, sizeof(phdr));
|
2012-02-11 07:51:35 -04:00
|
|
|
if (phdr.type == target->type &&
|
|
|
|
phdr.key == target->key &&
|
|
|
|
phdr.group_element == target->group_element) {
|
|
|
|
// found it
|
|
|
|
*pofs = ofs;
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-12 18:54:46 -04:00
|
|
|
// note that this is an ||, not an &&, as this makes us more
|
|
|
|
// robust to power off while adding a variable to EEPROM
|
|
|
|
if (phdr.type == _sentinal_type ||
|
|
|
|
phdr.key == _sentinal_key ||
|
|
|
|
phdr.group_element == _sentinal_group) {
|
2012-02-11 07:51:35 -04:00
|
|
|
// we've reached the sentinal
|
|
|
|
*pofs = ofs;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ofs += type_size((enum ap_var_type)phdr.type) + sizeof(phdr);
|
|
|
|
}
|
2013-07-09 04:53:04 -03:00
|
|
|
*pofs = 0xffff;
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("scan past end of eeprom");
|
2012-02-11 07:51:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-24 11:30:22 -03:00
|
|
|
/**
|
|
|
|
* add a _X, _Y, _Z suffix to the name of a Vector3f element
|
|
|
|
* @param buffer
|
|
|
|
* @param buffer_size
|
|
|
|
* @param idx Suffix: 0 --> _X; 1 --> _Y; 2 --> _Z; (other --> undefined)
|
|
|
|
*/
|
2013-04-19 04:46:40 -03:00
|
|
|
void AP_Param::add_vector3f_suffix(char *buffer, size_t buffer_size, uint8_t idx) const
|
2012-02-23 23:19:00 -04:00
|
|
|
{
|
2013-05-24 11:30:22 -03:00
|
|
|
const size_t len = strnlen(buffer, buffer_size);
|
|
|
|
if (len + 2 <= buffer_size) {
|
|
|
|
buffer[len] = '_';
|
|
|
|
buffer[len + 1] = static_cast<char>('X' + idx);
|
|
|
|
if (len + 3 <= buffer_size) {
|
|
|
|
buffer[len + 2] = 0;
|
|
|
|
}
|
2012-11-20 07:27:11 -04:00
|
|
|
}
|
2012-02-23 23:19:00 -04:00
|
|
|
}
|
|
|
|
|
2013-01-08 00:28:16 -04:00
|
|
|
// Copy the variable's whole name to the supplied buffer.
|
|
|
|
//
|
|
|
|
// If the variable is a group member, prepend the group name.
|
|
|
|
//
|
2013-04-19 04:46:40 -03:00
|
|
|
void AP_Param::copy_name_token(const ParamToken &token, char *buffer, size_t buffer_size, bool force_scalar) const
|
2013-01-08 00:28:16 -04:00
|
|
|
{
|
|
|
|
uint32_t group_element;
|
|
|
|
const struct GroupInfo *ginfo;
|
|
|
|
uint8_t idx;
|
|
|
|
const struct AP_Param::Info *info = find_var_info_token(token, &group_element, &ginfo, &idx);
|
|
|
|
if (info == NULL) {
|
|
|
|
*buffer = 0;
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("no info found");
|
2013-01-08 00:28:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
strncpy_P(buffer, info->name, buffer_size);
|
|
|
|
if (ginfo != NULL) {
|
|
|
|
uint8_t len = strnlen(buffer, buffer_size);
|
|
|
|
if (len < buffer_size) {
|
|
|
|
strncpy_P(&buffer[len], ginfo->name, buffer_size-len);
|
|
|
|
}
|
|
|
|
if ((force_scalar || idx != 0) && AP_PARAM_VECTOR3F == PGM_UINT8(&ginfo->type)) {
|
|
|
|
// the caller wants a specific element in a Vector3f
|
|
|
|
add_vector3f_suffix(buffer, buffer_size, idx);
|
2012-02-23 23:19:00 -04:00
|
|
|
}
|
|
|
|
} else if ((force_scalar || idx != 0) && AP_PARAM_VECTOR3F == PGM_UINT8(&info->type)) {
|
2012-08-17 03:18:11 -03:00
|
|
|
add_vector3f_suffix(buffer, buffer_size, idx);
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a variable by name in a group
|
|
|
|
AP_Param *
|
|
|
|
AP_Param::find_group(const char *name, uint8_t vindex, const struct GroupInfo *group_info, enum ap_var_type *ptype)
|
|
|
|
{
|
|
|
|
uint8_t type;
|
|
|
|
for (uint8_t i=0;
|
2012-02-12 05:18:10 -04:00
|
|
|
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
2012-02-12 03:22:57 -04:00
|
|
|
i++) {
|
2012-02-29 22:48:54 -04:00
|
|
|
#ifdef AP_NESTED_GROUPS_ENABLED
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *ginfo = (const struct GroupInfo *)PGM_POINTER(&group_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
AP_Param *ap = find_group(name, vindex, ginfo, ptype);
|
|
|
|
if (ap != NULL) {
|
|
|
|
return ap;
|
|
|
|
}
|
2012-02-29 22:48:54 -04:00
|
|
|
} else
|
|
|
|
#endif // AP_NESTED_GROUPS_ENABLED
|
|
|
|
if (strcasecmp_P(name, group_info[i].name) == 0) {
|
2012-02-12 05:18:10 -04:00
|
|
|
uintptr_t p = PGM_POINTER(&_var_info[vindex].ptr);
|
2012-02-12 03:22:57 -04:00
|
|
|
*ptype = (enum ap_var_type)type;
|
2012-02-12 05:18:10 -04:00
|
|
|
return (AP_Param *)(p + PGM_POINTER(&group_info[i].offset));
|
2012-02-23 23:19:00 -04:00
|
|
|
} else if (type == AP_PARAM_VECTOR3F) {
|
|
|
|
// special case for finding Vector3f elements
|
2012-11-20 07:27:11 -04:00
|
|
|
uint8_t suffix_len = strnlen_P(group_info[i].name, AP_MAX_NAME_SIZE);
|
2012-02-23 23:19:00 -04:00
|
|
|
if (strncmp_P(name, group_info[i].name, suffix_len) == 0 &&
|
|
|
|
name[suffix_len] == '_' &&
|
2012-11-20 07:27:11 -04:00
|
|
|
(name[suffix_len+1] == 'X' ||
|
|
|
|
name[suffix_len+1] == 'Y' ||
|
|
|
|
name[suffix_len+1] == 'Z')) {
|
2012-02-23 23:19:00 -04:00
|
|
|
uintptr_t p = PGM_POINTER(&_var_info[vindex].ptr);
|
|
|
|
AP_Float *v = (AP_Float *)(p + PGM_POINTER(&group_info[i].offset));
|
|
|
|
*ptype = AP_PARAM_FLOAT;
|
|
|
|
switch (name[suffix_len+1]) {
|
|
|
|
case 'X':
|
|
|
|
return (AP_Float *)&v[0];
|
|
|
|
case 'Y':
|
|
|
|
return (AP_Float *)&v[1];
|
|
|
|
case 'Z':
|
|
|
|
return (AP_Float *)&v[2];
|
|
|
|
}
|
|
|
|
}
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
}
|
2012-02-12 03:22:57 -04:00
|
|
|
return NULL;
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// Find a variable by name.
|
|
|
|
//
|
|
|
|
AP_Param *
|
2012-02-12 03:22:57 -04:00
|
|
|
AP_Param::find(const char *name, enum ap_var_type *ptype)
|
2012-02-11 07:51:35 -04:00
|
|
|
{
|
2012-02-12 19:12:02 -04:00
|
|
|
for (uint8_t i=0; i<_num_vars; i++) {
|
2012-02-12 05:18:10 -04:00
|
|
|
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
2012-02-11 07:51:35 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
|
|
|
uint8_t len = strnlen_P(_var_info[i].name, AP_MAX_NAME_SIZE);
|
|
|
|
if (strncmp_P(name, _var_info[i].name, len) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *group_info = (const struct GroupInfo *)PGM_POINTER(&_var_info[i].group_info);
|
2012-02-26 19:51:57 -04:00
|
|
|
AP_Param *ap = find_group(name + len, i, group_info, ptype);
|
|
|
|
if (ap != NULL) {
|
|
|
|
return ap;
|
|
|
|
}
|
|
|
|
// we continue looking as we want to allow top level
|
|
|
|
// parameter to have the same prefix name as group
|
|
|
|
// parameters, for example CAM_P_G
|
2012-02-11 07:51:35 -04:00
|
|
|
} else if (strcasecmp_P(name, _var_info[i].name) == 0) {
|
2012-02-12 03:22:57 -04:00
|
|
|
*ptype = (enum ap_var_type)type;
|
2012-02-12 05:18:10 -04:00
|
|
|
return (AP_Param *)PGM_POINTER(&_var_info[i].ptr);
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-30 08:45:25 -03:00
|
|
|
// Find a variable by name.
|
|
|
|
//
|
|
|
|
AP_Param *
|
|
|
|
AP_Param::find_P(const prog_char_t *name, enum ap_var_type *ptype)
|
|
|
|
{
|
|
|
|
char param_name[AP_MAX_NAME_SIZE+1];
|
|
|
|
strncpy_P(param_name, name, AP_MAX_NAME_SIZE);
|
|
|
|
param_name[AP_MAX_NAME_SIZE] = 0;
|
|
|
|
return find(param_name, ptype);
|
|
|
|
}
|
|
|
|
|
2012-09-19 18:42:35 -03:00
|
|
|
// Find a variable by index. Note that this is quite slow.
|
|
|
|
//
|
|
|
|
AP_Param *
|
2013-01-08 18:45:02 -04:00
|
|
|
AP_Param::find_by_index(uint16_t idx, enum ap_var_type *ptype, ParamToken *token)
|
2012-09-19 18:42:35 -03:00
|
|
|
{
|
|
|
|
AP_Param *ap;
|
|
|
|
uint16_t count=0;
|
2013-01-08 18:45:02 -04:00
|
|
|
for (ap=AP_Param::first(token, ptype);
|
2012-09-19 18:42:35 -03:00
|
|
|
ap && count < idx;
|
2013-01-08 18:45:02 -04:00
|
|
|
ap=AP_Param::next_scalar(token, ptype)) {
|
2012-09-19 18:42:35 -03:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return ap;
|
|
|
|
}
|
|
|
|
|
2012-12-13 18:58:23 -04:00
|
|
|
// Find a object by name.
|
|
|
|
//
|
|
|
|
AP_Param *
|
|
|
|
AP_Param::find_object(const char *name)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<_num_vars; i++) {
|
|
|
|
if (strcasecmp_P(name, _var_info[i].name) == 0) {
|
|
|
|
return (AP_Param *)PGM_POINTER(&_var_info[i].ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// Save the variable to EEPROM, if supported
|
|
|
|
//
|
2013-06-02 01:48:36 -03:00
|
|
|
bool AP_Param::save(bool force_save)
|
2012-02-11 07:51:35 -04:00
|
|
|
{
|
2012-11-05 07:08:43 -04:00
|
|
|
uint32_t group_element = 0;
|
2012-02-12 03:22:57 -04:00
|
|
|
const struct GroupInfo *ginfo;
|
2012-02-23 23:19:00 -04:00
|
|
|
uint8_t idx;
|
|
|
|
const struct AP_Param::Info *info = find_var_info(&group_element, &ginfo, &idx);
|
|
|
|
const AP_Param *ap;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
if (info == NULL) {
|
|
|
|
// we don't have any info on how to store it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Param_header phdr;
|
|
|
|
|
|
|
|
// create the header we will use to store the variable
|
2012-02-12 03:22:57 -04:00
|
|
|
if (ginfo != NULL) {
|
2012-02-12 05:18:10 -04:00
|
|
|
phdr.type = PGM_UINT8(&ginfo->type);
|
2012-02-12 03:22:57 -04:00
|
|
|
} else {
|
2012-02-12 05:18:10 -04:00
|
|
|
phdr.type = PGM_UINT8(&info->type);
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
2012-02-12 18:54:46 -04:00
|
|
|
phdr.key = PGM_UINT8(&info->key);
|
|
|
|
phdr.group_element = group_element;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
2012-02-23 23:19:00 -04:00
|
|
|
ap = this;
|
|
|
|
if (phdr.type != AP_PARAM_VECTOR3F && idx != 0) {
|
|
|
|
// only vector3f can have non-zero idx for now
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (idx != 0) {
|
|
|
|
ap = (const AP_Param *)((uintptr_t)ap) - (idx*sizeof(float));
|
|
|
|
}
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// scan EEPROM to find the right location
|
|
|
|
uint16_t ofs;
|
|
|
|
if (scan(&phdr, &ofs)) {
|
|
|
|
// found an existing copy of the variable
|
2012-02-23 23:19:00 -04:00
|
|
|
eeprom_write_check(ap, ofs+sizeof(phdr), type_size((enum ap_var_type)phdr.type));
|
2012-02-11 07:51:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
2012-08-17 03:18:11 -03:00
|
|
|
if (ofs == (uint16_t) ~0) {
|
2012-02-11 07:51:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-06 22:00:43 -03:00
|
|
|
// if the value is the default value then don't save
|
2012-08-08 00:13:30 -03:00
|
|
|
if (phdr.type <= AP_PARAM_FLOAT) {
|
|
|
|
float v1 = cast_to_float((enum ap_var_type)phdr.type);
|
|
|
|
float v2;
|
|
|
|
if (ginfo != NULL) {
|
|
|
|
v2 = PGM_FLOAT(&ginfo->def_value);
|
|
|
|
} else {
|
|
|
|
v2 = PGM_FLOAT(&info->def_value);
|
|
|
|
}
|
2013-06-02 01:48:36 -03:00
|
|
|
if (v1 == v2 && !force_save) {
|
2012-08-08 00:13:30 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (phdr.type != AP_PARAM_INT32 &&
|
2013-01-10 14:42:24 -04:00
|
|
|
(fabsf(v1-v2) < 0.0001f*fabsf(v1))) {
|
2012-08-08 00:13:30 -03:00
|
|
|
// for other than 32 bit integers, we accept values within
|
|
|
|
// 0.01 percent of the current value as being the same
|
2012-08-17 03:18:11 -03:00
|
|
|
return true;
|
2012-08-08 00:13:30 -03:00
|
|
|
}
|
2012-08-06 22:00:43 -03:00
|
|
|
}
|
|
|
|
|
2014-08-13 01:43:19 -03:00
|
|
|
if (ofs+type_size((enum ap_var_type)phdr.type)+2*sizeof(phdr) >= _storage.size()) {
|
2012-08-06 22:00:43 -03:00
|
|
|
// we are out of room for saving variables
|
2013-05-30 08:45:25 -03:00
|
|
|
hal.console->println_P(PSTR("EEPROM full"));
|
2012-08-06 22:00:43 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// write a new sentinal, then the data, then the header
|
|
|
|
write_sentinal(ofs + sizeof(phdr) + type_size((enum ap_var_type)phdr.type));
|
2012-02-23 23:19:00 -04:00
|
|
|
eeprom_write_check(ap, ofs+sizeof(phdr), type_size((enum ap_var_type)phdr.type));
|
2012-02-11 07:51:35 -04:00
|
|
|
eeprom_write_check(&phdr, ofs, sizeof(phdr));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the variable from EEPROM, if supported
|
|
|
|
//
|
|
|
|
bool AP_Param::load(void)
|
|
|
|
{
|
2012-11-05 07:08:43 -04:00
|
|
|
uint32_t group_element = 0;
|
2012-02-12 03:22:57 -04:00
|
|
|
const struct GroupInfo *ginfo;
|
2012-02-23 23:19:00 -04:00
|
|
|
uint8_t idx;
|
|
|
|
const struct AP_Param::Info *info = find_var_info(&group_element, &ginfo, &idx);
|
2012-02-11 07:51:35 -04:00
|
|
|
if (info == NULL) {
|
|
|
|
// we don't have any info on how to load it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Param_header phdr;
|
|
|
|
|
|
|
|
// create the header we will use to match the variable
|
2012-02-12 03:22:57 -04:00
|
|
|
if (ginfo != NULL) {
|
2012-02-12 05:18:10 -04:00
|
|
|
phdr.type = PGM_UINT8(&ginfo->type);
|
2012-02-12 03:22:57 -04:00
|
|
|
} else {
|
2012-02-12 05:18:10 -04:00
|
|
|
phdr.type = PGM_UINT8(&info->type);
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
2012-02-12 18:54:46 -04:00
|
|
|
phdr.key = PGM_UINT8(&info->key);
|
|
|
|
phdr.group_element = group_element;
|
2012-02-11 07:51:35 -04:00
|
|
|
|
|
|
|
// scan EEPROM to find the right location
|
|
|
|
uint16_t ofs;
|
|
|
|
if (!scan(&phdr, &ofs)) {
|
2012-08-06 22:00:43 -03:00
|
|
|
// if the value isn't stored in EEPROM then set the default value
|
|
|
|
if (ginfo != NULL) {
|
|
|
|
uintptr_t base = PGM_POINTER(&info->ptr);
|
2012-08-17 03:18:11 -03:00
|
|
|
set_value((enum ap_var_type)phdr.type, (void*)(base + PGM_UINT16(&ginfo->offset)),
|
2012-08-06 22:00:43 -03:00
|
|
|
PGM_FLOAT(&ginfo->def_value));
|
|
|
|
} else {
|
|
|
|
set_value((enum ap_var_type)phdr.type, (void*)PGM_POINTER(&info->ptr), PGM_FLOAT(&info->def_value));
|
|
|
|
}
|
2012-02-11 07:51:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-27 18:42:34 -04:00
|
|
|
if (phdr.type != AP_PARAM_VECTOR3F && idx != 0) {
|
|
|
|
// only vector3f can have non-zero idx for now
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_Param *ap;
|
|
|
|
ap = this;
|
|
|
|
if (idx != 0) {
|
|
|
|
ap = (AP_Param *)((uintptr_t)ap) - (idx*sizeof(float));
|
|
|
|
}
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// found it
|
2014-08-13 01:43:19 -03:00
|
|
|
_storage.read_block(ap, ofs+sizeof(phdr), type_size((enum ap_var_type)phdr.type));
|
2012-02-11 07:51:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-17 03:18:11 -03:00
|
|
|
// set a AP_Param variable to a specified value
|
2013-05-30 08:45:25 -03:00
|
|
|
void AP_Param::set_value(enum ap_var_type type, void *ptr, float value)
|
2012-08-06 22:00:43 -03:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AP_PARAM_INT8:
|
2013-05-30 08:45:25 -03:00
|
|
|
((AP_Int8 *)ptr)->set(value);
|
2012-08-06 22:00:43 -03:00
|
|
|
break;
|
|
|
|
case AP_PARAM_INT16:
|
2013-05-30 08:45:25 -03:00
|
|
|
((AP_Int16 *)ptr)->set(value);
|
2012-08-06 22:00:43 -03:00
|
|
|
break;
|
|
|
|
case AP_PARAM_INT32:
|
2013-05-30 08:45:25 -03:00
|
|
|
((AP_Int32 *)ptr)->set(value);
|
2012-08-06 22:00:43 -03:00
|
|
|
break;
|
|
|
|
case AP_PARAM_FLOAT:
|
2013-05-30 08:45:25 -03:00
|
|
|
((AP_Float *)ptr)->set(value);
|
2012-08-06 22:00:43 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 17:40:42 -04:00
|
|
|
// load default values for scalars in a group. This does not recurse
|
|
|
|
// into other objects. This is a static function that should be called
|
|
|
|
// in the objects constructor
|
|
|
|
void AP_Param::setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info)
|
2012-08-06 22:00:43 -03:00
|
|
|
{
|
2012-12-12 17:40:42 -04:00
|
|
|
uintptr_t base = (uintptr_t)object_pointer;
|
2012-08-06 22:00:43 -03:00
|
|
|
uint8_t type;
|
|
|
|
for (uint8_t i=0;
|
|
|
|
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
|
|
|
i++) {
|
2012-12-12 17:40:42 -04:00
|
|
|
if (type <= AP_PARAM_FLOAT) {
|
2012-08-06 22:00:43 -03:00
|
|
|
void *ptr = (void *)(base + PGM_UINT16(&group_info[i].offset));
|
2012-08-17 03:18:11 -03:00
|
|
|
set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&group_info[i].def_value));
|
2012-08-06 22:00:43 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 20:37:41 -03:00
|
|
|
// set a value directly in an object. This should only be used by
|
|
|
|
// example code, not by mainline vehicle code
|
|
|
|
void AP_Param::set_object_value(const void *object_pointer,
|
|
|
|
const struct GroupInfo *group_info,
|
|
|
|
const char *name, float value)
|
|
|
|
{
|
|
|
|
uintptr_t base = (uintptr_t)object_pointer;
|
|
|
|
uint8_t type;
|
|
|
|
for (uint8_t i=0;
|
|
|
|
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
|
|
|
i++) {
|
|
|
|
if (strcmp(name, group_info[i].name) == 0 && type <= AP_PARAM_FLOAT) {
|
|
|
|
void *ptr = (void *)(base + PGM_UINT16(&group_info[i].offset));
|
|
|
|
set_value((enum ap_var_type)type, ptr, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2012-12-12 17:40:42 -04:00
|
|
|
// load default values for all scalars in a sketch. This does not
|
|
|
|
// recurse into sub-objects
|
2012-12-13 18:58:23 -04:00
|
|
|
void AP_Param::setup_sketch_defaults(void)
|
2012-08-06 22:00:43 -03:00
|
|
|
{
|
2012-12-13 18:58:23 -04:00
|
|
|
setup();
|
2012-08-06 22:00:43 -03:00
|
|
|
for (uint8_t i=0; i<_num_vars; i++) {
|
|
|
|
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
2012-12-12 17:40:42 -04:00
|
|
|
if (type <= AP_PARAM_FLOAT) {
|
2012-08-06 22:00:43 -03:00
|
|
|
void *ptr = (void*)PGM_POINTER(&_var_info[i].ptr);
|
|
|
|
set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&_var_info[i].def_value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-11 07:51:35 -04:00
|
|
|
// Load all variables from EEPROM
|
|
|
|
//
|
|
|
|
bool AP_Param::load_all(void)
|
|
|
|
{
|
|
|
|
struct Param_header phdr;
|
|
|
|
uint16_t ofs = sizeof(AP_Param::EEPROM_header);
|
2012-08-06 22:00:43 -03:00
|
|
|
|
2014-08-13 01:43:19 -03:00
|
|
|
while (ofs < _storage.size()) {
|
|
|
|
_storage.read_block(&phdr, ofs, sizeof(phdr));
|
2012-02-12 18:54:46 -04:00
|
|
|
// note that this is an || not an && for robustness
|
|
|
|
// against power off while adding a variable
|
|
|
|
if (phdr.type == _sentinal_type ||
|
|
|
|
phdr.key == _sentinal_key ||
|
|
|
|
phdr.group_element == _sentinal_group) {
|
2012-02-11 07:51:35 -04:00
|
|
|
// we've reached the sentinal
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct AP_Param::Info *info;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
info = find_by_header(phdr, &ptr);
|
|
|
|
if (info != NULL) {
|
2014-08-13 01:43:19 -03:00
|
|
|
_storage.read_block(ptr, ofs+sizeof(phdr), type_size((enum ap_var_type)phdr.type));
|
2012-02-11 07:51:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ofs += type_size((enum ap_var_type)phdr.type) + sizeof(phdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we didn't find the sentinal
|
2014-11-09 17:09:08 -04:00
|
|
|
Debug("no sentinal in load_all");
|
2012-02-11 07:51:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-12 03:22:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
// return the first variable in _var_info
|
2012-02-19 01:57:49 -04:00
|
|
|
AP_Param *AP_Param::first(ParamToken *token, enum ap_var_type *ptype)
|
2012-02-12 03:22:57 -04:00
|
|
|
{
|
2012-02-19 01:57:49 -04:00
|
|
|
token->key = 0;
|
|
|
|
token->group_element = 0;
|
2012-02-23 23:19:00 -04:00
|
|
|
token->idx = 0;
|
2012-02-12 03:22:57 -04:00
|
|
|
if (_num_vars == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-12 04:18:24 -04:00
|
|
|
if (ptype != NULL) {
|
2012-02-12 05:18:10 -04:00
|
|
|
*ptype = (enum ap_var_type)PGM_UINT8(&_var_info[0].type);
|
2012-02-12 04:18:24 -04:00
|
|
|
}
|
2012-02-12 05:18:10 -04:00
|
|
|
return (AP_Param *)(PGM_POINTER(&_var_info[0].ptr));
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the next variable in a group, recursing into groups
|
|
|
|
/// as needed
|
|
|
|
AP_Param *AP_Param::next_group(uint8_t vindex, const struct GroupInfo *group_info,
|
|
|
|
bool *found_current,
|
|
|
|
uint8_t group_base,
|
|
|
|
uint8_t group_shift,
|
2012-02-19 01:57:49 -04:00
|
|
|
ParamToken *token,
|
2012-02-12 03:22:57 -04:00
|
|
|
enum ap_var_type *ptype)
|
|
|
|
{
|
2012-02-23 23:19:00 -04:00
|
|
|
enum ap_var_type type;
|
2012-02-12 03:22:57 -04:00
|
|
|
for (uint8_t i=0;
|
2012-02-23 23:19:00 -04:00
|
|
|
(type=(enum ap_var_type)PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
2012-02-12 03:22:57 -04:00
|
|
|
i++) {
|
2012-02-29 23:48:43 -04:00
|
|
|
#ifdef AP_NESTED_GROUPS_ENABLED
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
|
|
|
// a nested group
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *ginfo = (const struct GroupInfo *)PGM_POINTER(&group_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
AP_Param *ap;
|
2012-02-12 18:54:46 -04:00
|
|
|
ap = next_group(vindex, ginfo, found_current, GROUP_ID(group_info, group_base, i, group_shift),
|
2012-02-12 03:22:57 -04:00
|
|
|
group_shift + _group_level_shift, token, ptype);
|
|
|
|
if (ap != NULL) {
|
|
|
|
return ap;
|
|
|
|
}
|
2012-02-29 23:48:43 -04:00
|
|
|
} else
|
|
|
|
#endif // AP_NESTED_GROUPS_ENABLED
|
|
|
|
{
|
2012-02-12 03:22:57 -04:00
|
|
|
if (*found_current) {
|
|
|
|
// got a new one
|
2012-02-19 01:57:49 -04:00
|
|
|
token->key = vindex;
|
|
|
|
token->group_element = GROUP_ID(group_info, group_base, i, group_shift);
|
2012-02-23 23:19:00 -04:00
|
|
|
token->idx = 0;
|
2012-02-12 04:18:24 -04:00
|
|
|
if (ptype != NULL) {
|
2012-02-23 23:19:00 -04:00
|
|
|
*ptype = type;
|
2012-02-12 04:18:24 -04:00
|
|
|
}
|
2012-02-12 05:18:10 -04:00
|
|
|
return (AP_Param*)(PGM_POINTER(&_var_info[vindex].ptr) + PGM_UINT16(&group_info[i].offset));
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
2012-02-19 01:57:49 -04:00
|
|
|
if (GROUP_ID(group_info, group_base, i, group_shift) == token->group_element) {
|
2012-02-12 03:22:57 -04:00
|
|
|
*found_current = true;
|
2012-02-23 23:19:00 -04:00
|
|
|
if (type == AP_PARAM_VECTOR3F && token->idx < 3) {
|
|
|
|
// return the next element of the vector as a
|
|
|
|
// float
|
|
|
|
token->idx++;
|
|
|
|
if (ptype != NULL) {
|
|
|
|
*ptype = AP_PARAM_FLOAT;
|
|
|
|
}
|
|
|
|
uintptr_t ofs = (uintptr_t)PGM_POINTER(&_var_info[vindex].ptr) + PGM_UINT16(&group_info[i].offset);
|
2013-05-24 11:39:32 -03:00
|
|
|
ofs += sizeof(float)*(token->idx - 1u);
|
2012-02-23 23:19:00 -04:00
|
|
|
return (AP_Param *)ofs;
|
|
|
|
}
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the next variable in _var_info, recursing into groups
|
|
|
|
/// as needed
|
2012-02-19 01:57:49 -04:00
|
|
|
AP_Param *AP_Param::next(ParamToken *token, enum ap_var_type *ptype)
|
2012-02-12 03:22:57 -04:00
|
|
|
{
|
2012-02-19 01:57:49 -04:00
|
|
|
uint8_t i = token->key;
|
2012-02-12 03:22:57 -04:00
|
|
|
bool found_current = false;
|
|
|
|
if (i >= _num_vars) {
|
|
|
|
// illegal token
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-23 23:19:00 -04:00
|
|
|
enum ap_var_type type = (enum ap_var_type)PGM_UINT8(&_var_info[i].type);
|
|
|
|
|
|
|
|
// allow Vector3f to be seen as 3 variables. First as a vector,
|
|
|
|
// then as 3 separate floats
|
|
|
|
if (type == AP_PARAM_VECTOR3F && token->idx < 3) {
|
|
|
|
token->idx++;
|
|
|
|
if (ptype != NULL) {
|
|
|
|
*ptype = AP_PARAM_FLOAT;
|
|
|
|
}
|
2013-05-24 11:39:32 -03:00
|
|
|
return (AP_Param *)(((token->idx - 1u)*sizeof(float))+(uintptr_t)PGM_POINTER(&_var_info[i].ptr));
|
2012-02-23 23:19:00 -04:00
|
|
|
}
|
|
|
|
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type != AP_PARAM_GROUP) {
|
|
|
|
i++;
|
|
|
|
found_current = true;
|
|
|
|
}
|
|
|
|
for (; i<_num_vars; i++) {
|
2012-02-23 23:19:00 -04:00
|
|
|
type = (enum ap_var_type)PGM_UINT8(&_var_info[i].type);
|
2012-02-12 03:22:57 -04:00
|
|
|
if (type == AP_PARAM_GROUP) {
|
2012-02-12 05:18:10 -04:00
|
|
|
const struct GroupInfo *group_info = (const struct GroupInfo *)PGM_POINTER(&_var_info[i].group_info);
|
2012-02-12 03:22:57 -04:00
|
|
|
AP_Param *ap = next_group(i, group_info, &found_current, 0, 0, token, ptype);
|
|
|
|
if (ap != NULL) {
|
|
|
|
return ap;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// found the next one
|
2012-02-19 01:57:49 -04:00
|
|
|
token->key = i;
|
|
|
|
token->group_element = 0;
|
2012-02-23 23:19:00 -04:00
|
|
|
token->idx = 0;
|
2012-02-12 04:18:24 -04:00
|
|
|
if (ptype != NULL) {
|
2012-02-23 23:19:00 -04:00
|
|
|
*ptype = type;
|
2012-02-12 04:18:24 -04:00
|
|
|
}
|
2012-02-12 05:18:10 -04:00
|
|
|
return (AP_Param *)(PGM_POINTER(&_var_info[i].ptr));
|
2012-02-12 03:22:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the next scalar in _var_info, recursing into groups
|
|
|
|
/// as needed
|
2012-02-19 01:57:49 -04:00
|
|
|
AP_Param *AP_Param::next_scalar(ParamToken *token, enum ap_var_type *ptype)
|
2012-02-12 03:22:57 -04:00
|
|
|
{
|
|
|
|
AP_Param *ap;
|
2012-02-12 04:18:24 -04:00
|
|
|
enum ap_var_type type;
|
|
|
|
while ((ap = next(token, &type)) != NULL && type > AP_PARAM_FLOAT) ;
|
|
|
|
if (ap != NULL && ptype != NULL) {
|
|
|
|
*ptype = type;
|
|
|
|
}
|
2012-02-12 03:22:57 -04:00
|
|
|
return ap;
|
|
|
|
}
|
2012-02-12 04:18:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
/// cast a variable to a float given its type
|
2013-04-19 04:46:40 -03:00
|
|
|
float AP_Param::cast_to_float(enum ap_var_type type) const
|
2012-02-12 04:18:58 -04:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AP_PARAM_INT8:
|
|
|
|
return ((AP_Int8 *)this)->cast_to_float();
|
|
|
|
case AP_PARAM_INT16:
|
|
|
|
return ((AP_Int16 *)this)->cast_to_float();
|
|
|
|
case AP_PARAM_INT32:
|
|
|
|
return ((AP_Int32 *)this)->cast_to_float();
|
|
|
|
case AP_PARAM_FLOAT:
|
|
|
|
return ((AP_Float *)this)->cast_to_float();
|
|
|
|
default:
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
}
|
2012-02-12 19:16:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
// print the value of all variables
|
2013-04-19 04:46:40 -03:00
|
|
|
void AP_Param::show(const AP_Param *ap, const char *s,
|
|
|
|
enum ap_var_type type, AP_HAL::BetterStream *port)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case AP_PARAM_INT8:
|
|
|
|
port->printf_P(PSTR("%s: %d\n"), s, (int)((AP_Int8 *)ap)->get());
|
|
|
|
break;
|
|
|
|
case AP_PARAM_INT16:
|
|
|
|
port->printf_P(PSTR("%s: %d\n"), s, (int)((AP_Int16 *)ap)->get());
|
|
|
|
break;
|
|
|
|
case AP_PARAM_INT32:
|
|
|
|
port->printf_P(PSTR("%s: %ld\n"), s, (long)((AP_Int32 *)ap)->get());
|
|
|
|
break;
|
|
|
|
case AP_PARAM_FLOAT:
|
2013-09-21 02:45:19 -03:00
|
|
|
port->printf_P(PSTR("%s: %f\n"), s, ((AP_Float *)ap)->get());
|
2013-04-19 04:46:40 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the value of all variables
|
|
|
|
void AP_Param::show(const AP_Param *ap, const ParamToken &token,
|
|
|
|
enum ap_var_type type, AP_HAL::BetterStream *port)
|
|
|
|
{
|
|
|
|
char s[AP_MAX_NAME_SIZE+1];
|
|
|
|
ap->copy_name_token(token, s, sizeof(s), true);
|
|
|
|
s[AP_MAX_NAME_SIZE] = 0;
|
|
|
|
show(ap, s, type, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the value of all variables
|
|
|
|
void AP_Param::show_all(AP_HAL::BetterStream *port)
|
2012-02-12 19:16:55 -04:00
|
|
|
{
|
2012-02-19 01:57:49 -04:00
|
|
|
ParamToken token;
|
2012-02-12 19:16:55 -04:00
|
|
|
AP_Param *ap;
|
|
|
|
enum ap_var_type type;
|
|
|
|
|
|
|
|
for (ap=AP_Param::first(&token, &type);
|
|
|
|
ap;
|
2012-02-23 23:19:00 -04:00
|
|
|
ap=AP_Param::next_scalar(&token, &type)) {
|
2013-04-19 04:46:40 -03:00
|
|
|
show(ap, token, type, port);
|
2012-02-12 19:16:55 -04:00
|
|
|
}
|
|
|
|
}
|
2012-12-18 22:36:35 -04:00
|
|
|
|
2013-05-30 08:45:25 -03:00
|
|
|
// convert one old vehicle parameter to new object parameter
|
|
|
|
void AP_Param::convert_old_parameter(const struct ConversionInfo *info)
|
|
|
|
{
|
|
|
|
|
|
|
|
// find the old value in EEPROM.
|
|
|
|
uint16_t pofs;
|
|
|
|
AP_Param::Param_header header;
|
|
|
|
header.type = PGM_UINT8(&info->type);
|
|
|
|
header.key = PGM_UINT8(&info->old_key);
|
|
|
|
header.group_element = PGM_UINT8(&info->old_group_element);
|
|
|
|
if (!scan(&header, &pofs)) {
|
|
|
|
// the old parameter isn't saved in the EEPROM. It was
|
|
|
|
// probably still set to the default value, which isn't stored
|
|
|
|
// no need to convert
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the old value from EEPROM
|
|
|
|
uint8_t old_value[type_size((enum ap_var_type)header.type)];
|
2014-08-13 01:43:19 -03:00
|
|
|
_storage.read_block(old_value, pofs+sizeof(header), sizeof(old_value));
|
2013-05-30 08:45:25 -03:00
|
|
|
const AP_Param *ap = (const AP_Param *)&old_value[0];
|
|
|
|
|
|
|
|
// find the new variable in the variable structures
|
|
|
|
enum ap_var_type ptype;
|
|
|
|
AP_Param *ap2;
|
|
|
|
ap2 = find_P((const prog_char_t *)&info->new_name[0], &ptype);
|
|
|
|
if (ap2 == NULL) {
|
|
|
|
hal.console->printf_P(PSTR("Unknown conversion '%S'\n"), info->new_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if we can load it from EEPROM
|
|
|
|
if (ap2->load()) {
|
|
|
|
// the new parameter already has a value set by the user, or
|
|
|
|
// has already been converted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if they are the same type
|
2013-09-23 00:35:23 -03:00
|
|
|
if (ptype == (ap_var_type)header.type) {
|
2013-05-30 08:45:25 -03:00
|
|
|
// copy the value over only if the new parameter does not already
|
|
|
|
// have the old value (via a default).
|
|
|
|
if (memcmp(ap2, ap, sizeof(old_value)) != 0) {
|
|
|
|
memcpy(ap2, ap, sizeof(old_value));
|
|
|
|
// and save
|
|
|
|
ap2->save();
|
|
|
|
}
|
|
|
|
} else if (ptype <= AP_PARAM_FLOAT && header.type <= AP_PARAM_FLOAT) {
|
|
|
|
// perform scalar->scalar conversion
|
|
|
|
float v = ap->cast_to_float((enum ap_var_type)header.type);
|
|
|
|
if (v != ap2->cast_to_float(ptype)) {
|
|
|
|
// the value needs to change
|
|
|
|
set_value(ptype, ap2, v);
|
|
|
|
ap2->save();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// can't do vector<->scalar conversion, or different vector types
|
|
|
|
hal.console->printf_P(PSTR("Bad conversion type '%S'\n"), info->new_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// convert old vehicle parameters to new object parametersv
|
|
|
|
void AP_Param::convert_old_parameters(const struct ConversionInfo *conversion_table, uint8_t table_size)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<table_size; i++) {
|
|
|
|
convert_old_parameter(&conversion_table[i]);
|
|
|
|
}
|
|
|
|
}
|