2010-12-23 19:02:51 -04:00
|
|
|
#ifndef AP_EEProm_H
|
|
|
|
#define AP_EEProm_H
|
|
|
|
|
2010-12-24 01:25:13 -04:00
|
|
|
/*
|
|
|
|
* AP_EEProm.h
|
|
|
|
* Copyright (C) James Goppert 2010 <james.goppert@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-12-25 03:09:37 -04:00
|
|
|
#include <AP_Common.h>
|
2010-12-23 19:02:51 -04:00
|
|
|
#include <AP_Vector.h>
|
|
|
|
#include <avr/eeprom.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// The interface for data entries in the eeprom registry
|
2010-12-28 14:50:22 -04:00
|
|
|
class AP_EEPromEntryI
|
2010-12-23 19:02:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Pure virtual function for setting the data value
|
|
|
|
/// as a float. The function must handle the cast to
|
|
|
|
/// the stored variable types.
|
2010-12-23 19:02:51 -04:00
|
|
|
virtual void setEntry(float val) = 0;
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Pure virtual function for getting data as a float.
|
|
|
|
/// The function must handle the cast from the
|
|
|
|
/// stored variable types.
|
2010-12-23 19:02:51 -04:00
|
|
|
virtual float getEntry() = 0;
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Pure virtual function for getting entry name.
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual const char * getEntryName() = 0;
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Get the id of the variable.
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual uint16_t getEntryId() = 0;
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Get the address of the variable.
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual uint16_t getEntryAddress() = 0;
|
2010-12-23 19:02:51 -04:00
|
|
|
};
|
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
///The main EEProm Registry class.
|
2010-12-28 14:50:22 -04:00
|
|
|
class AP_EEPromRegistry : public Vector<AP_EEPromEntryI *>
|
2010-12-23 19:02:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Default constructor
|
2010-12-23 19:02:51 -04:00
|
|
|
AP_EEPromRegistry(uint16_t maxSize) :
|
2010-12-24 01:25:13 -04:00
|
|
|
_newAddress(0), _newId(0), _maxSize(maxSize)
|
2010-12-23 19:02:51 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Add an entry to the registry
|
2010-12-28 14:50:22 -04:00
|
|
|
void add(AP_EEPromEntryI * entry, uint16_t & id, uint16_t & address, size_t size);
|
2010-12-23 19:02:51 -04:00
|
|
|
|
|
|
|
private:
|
2010-12-27 00:11:00 -04:00
|
|
|
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
|
2010-12-23 19:02:51 -04:00
|
|
|
};
|
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Global eepromRegistry declaration.
|
2010-12-24 01:25:13 -04:00
|
|
|
extern AP_EEPromRegistry eepromRegistry;
|
2010-12-23 19:02:51 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// The EEProm Variable template class.
|
|
|
|
/// This class implements get/set/save/load etc for the
|
|
|
|
/// abstract template type.
|
2010-12-23 19:02:51 -04:00
|
|
|
template <class type>
|
2010-12-28 14:50:22 -04:00
|
|
|
class AP_EEPromVar : public AP_EEPromEntryI, public AP_Var<type>
|
2010-12-23 19:02:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-27 00:11:00 -04:00
|
|
|
/// The default constrcutor
|
2010-12-25 12:51:35 -04:00
|
|
|
AP_EEPromVar(type data = 0, const char * name = "", bool sync=false) :
|
2010-12-25 03:09:37 -04:00
|
|
|
AP_Var<type>(data,name,sync)
|
2010-12-23 19:02:51 -04:00
|
|
|
{
|
2010-12-25 03:09:37 -04:00
|
|
|
eepromRegistry.add(this,_id,_address,sizeof(type));
|
2010-12-23 19:02:51 -04:00
|
|
|
}
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-28 14:50:22 -04:00
|
|
|
virtual void setEntry(float val) { this->setF(val); }
|
|
|
|
virtual float getEntry() { return this->getF(); }
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual const char * getEntryName() { return this->getName(); }
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Get the id of the variable.
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual uint16_t getEntryId() { return _id; }
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-27 00:11:00 -04:00
|
|
|
/// Get the address of the variable.
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual uint16_t getEntryAddress() { return _address; }
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-23 19:02:51 -04:00
|
|
|
private:
|
2010-12-27 00:11:00 -04:00
|
|
|
uint16_t _id; /// Variable identifier
|
|
|
|
uint16_t _address; /// EEProm address of variable
|
2010-12-23 19:02:51 -04:00
|
|
|
};
|
|
|
|
|
2010-12-29 04:26:21 -04:00
|
|
|
typedef AP_EEPromVar<float> AP_EEPROM_Float;
|
|
|
|
typedef AP_EEPromVar<int8_t> AP_EEPROM_Int8;
|
|
|
|
typedef AP_EEPromVar<uint8_t> AP_EEPROM_Uint8;
|
|
|
|
typedef AP_EEPromVar<int16_t> AP_EEPROM_Int16;
|
|
|
|
typedef AP_EEPromVar<uint16_t> AP_EEPROM_Uint16;
|
|
|
|
typedef AP_EEPromVar<int32_t> AP_EEPROM_Int32;
|
|
|
|
typedef AP_EEPromVar<uint32_t> AP_EEPROM_Unt32;
|
|
|
|
typedef AP_EEPromVar<bool> AP_EEPROM_Bool;
|
|
|
|
|
|
|
|
|
2010-12-23 19:02:51 -04:00
|
|
|
#endif
|