From 9e983c452eb231f593ac8970bb16b4687fb4d18d Mon Sep 17 00:00:00 2001 From: "james.goppert" Date: Fri, 24 Dec 2010 05:25:13 +0000 Subject: [PATCH] Some EEProm cleanup. git-svn-id: https://arducopter.googlecode.com/svn/trunk@1244 f9c3cf11-9bcb-44bc-f272-b75c42450872 --- libraries/AP_Common/AP_Vector.h | 4 + libraries/AP_EEProm/AP_EEProm.h | 132 +++++++++++++++++++++++++++----- 2 files changed, 117 insertions(+), 19 deletions(-) diff --git a/libraries/AP_Common/AP_Vector.h b/libraries/AP_Common/AP_Vector.h index a2b46b2d85..b757b24dc2 100644 --- a/libraries/AP_Common/AP_Vector.h +++ b/libraries/AP_Common/AP_Vector.h @@ -19,6 +19,10 @@ #ifndef AP_Vector_H #define AP_Vector_H +#include +#include +#include + #ifdef ASSERT const static char vectorSource[] ="AP_Vector.hpp"; #endif diff --git a/libraries/AP_EEProm/AP_EEProm.h b/libraries/AP_EEProm/AP_EEProm.h index 02f260b61f..a93669783a 100644 --- a/libraries/AP_EEProm/AP_EEProm.h +++ b/libraries/AP_EEProm/AP_EEProm.h @@ -1,97 +1,191 @@ #ifndef AP_EEProm_H #define AP_EEProm_H +/* + * AP_EEProm.h + * Copyright (C) James Goppert 2010 + * + * This file 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 file 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 . + */ + #include #include #include +/** + * The interface for data entries in the eeprom registry + */ class AP_EEPromEntry { public: + /** + * Default constructor + */ AP_EEPromEntry(const char * name = "") : _id(0), _address(0), _name(name), _sync(false) { } + + /** + * Note: The set/get entry functions are virtual and therefore require more machine code, so use set/get in + * AP_EEPromVar unless you are accessing via the registry. + */ + + /** + * Pure virtual function for setting the data value as a float. The function must handle the cast to + * the stored variable types. + */ virtual void setEntry(float val) = 0; + + /** + * Pure virtual function for getting data as a float. The function must handle the cast from the + * stored variable types. + */ virtual float getEntry() = 0; + + /** + * Get the id of the variable. + */ uint16_t getId() { return _id; } + + /** + * Get the address of the variable. + */ uint16_t getAddress() { return _address; } + + /** + * Get the name. This is useful for ground stations. + */ const char * getName() { return _name; } + + /** + * If sync is true the a load will always occure before a get and a save will always + * occure before a set. + */ bool setSync(bool sync) { _sync = sync; } + protected: - uint16_t _id; - uint16_t _address; - const char * _name; - bool _sync; + + uint16_t _id; /** Variable identifier */ + uint16_t _address; /** EEProm address of variable */ + const char * _name; /** The name of the variable, a string */ + bool _sync; /** true: save/load before get/set, false: don't call save/load automatically*/ }; +/** + * The main EEProm Registry class. + */ class AP_EEPromRegistry : public AP_Vector { public: + /** * Default constructor */ - AP_EEPromRegistry(uint16_t maxSize) : - _lastAddress(0), _lastId(0), _maxSize(maxSize) + _newAddress(0), _newId(0), _maxSize(maxSize) { } /** * Add an entry to the registry */ - void add(AP_EEPromEntry * entry, uint16_t & id, uint16_t & address, size_t size) - { - if (_lastAddress + size > _maxSize) return; - address = _lastAddress; - _lastAddress += size; - id = _lastId++; - push_back(entry); - } + void add(AP_EEPromEntry * entry, uint16_t & id, uint16_t & address, size_t size); private: - uint16_t _lastAddress; - uint16_t _lastId; - uint16_t _maxSize; + uint16_t _newAddress; /** the address for the next new variable*/ + uint16_t _newId; /** the id of the next new variable*/ + uint16_t _maxSize; /** the maximum size of the eeprom memory*/ }; -AP_EEPromRegistry eepromRegistry(1024); +/** + * Global eepromRegistry declaration. + */ +extern AP_EEPromRegistry eepromRegistry; +/** + * The EEProm Variable template class. This class + * implements get/set/save/load etc for the + * abstract template type. + */ template class AP_EEPromVar : public AP_EEPromEntry { public: + /** + * The default constrcutor + */ AP_EEPromVar(const char * name = "") : AP_EEPromEntry(name) { eepromRegistry.add(this,_id,_address,sizeof(_data)); } + + /** + * Non-virtual set used for efficient variable setting without + * using eepromRegistry + */ void set(type val) { _data = val; if (_sync) save(); } + + /** + * Non-virtual get used for efficient getting of a variable + * without using eepromRegistry + */ type get() { if (_sync) load(); return _data; } + + /** + * Save a variable to eeprom + */ void save() { eeprom_write_block((const void*)&_data,(void*)(_address),sizeof(_data)); } + + /** + * Load a variable from eeprom + */ void load() { eeprom_read_block((void*)&_data,(const void*)(_address),sizeof(_data)); } + + /** + * Virtual call that allows an type of variable to be set by a float. + * This requires casting and will require some thought if initializing a + * boolean etc. Initializing a boolean to false, pass (0.0), true, pass (1.0) etc.. + */ virtual void setEntry(float val) { _data = (type)val; if (_sync) save(); } + + /** + * Virtual call that retrieves any variable types as a float. + */ virtual float getEntry() { if (_sync) load(); return (float)_data; } + private: - type _data; + type _data; /** The data that is stored on the heap */ }; #endif