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-24 01:25:13 -04:00
|
|
|
/**
|
|
|
|
* The interface for data entries in the eeprom registry
|
|
|
|
*/
|
2010-12-23 19:02:51 -04:00
|
|
|
class AP_EEPromEntry
|
|
|
|
{
|
|
|
|
public:
|
2010-12-24 01:25:13 -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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-25 03:09:37 -04:00
|
|
|
* Pure virtual function for getting entry name.
|
2010-12-24 01:25:13 -04:00
|
|
|
*/
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual const char * getEntryName() = 0;
|
2010-12-24 01:25:13 -04:00
|
|
|
|
|
|
|
/**
|
2010-12-25 03:09:37 -04:00
|
|
|
* Get the id of the variable.
|
2010-12-24 01:25:13 -04:00
|
|
|
*/
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual uint16_t getEntryId() = 0;
|
2010-12-24 01:25:13 -04:00
|
|
|
|
|
|
|
/**
|
2010-12-25 03:09:37 -04:00
|
|
|
* Get the address of the variable.
|
2010-12-24 01:25:13 -04:00
|
|
|
*/
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual uint16_t getEntryAddress() = 0;
|
2010-12-23 19:02:51 -04:00
|
|
|
};
|
|
|
|
|
2010-12-24 01:25:13 -04:00
|
|
|
/**
|
|
|
|
* The main EEProm Registry class.
|
|
|
|
*/
|
2010-12-24 19:59:52 -04:00
|
|
|
class AP_EEPromRegistry : public Vector<AP_EEPromEntry *>
|
2010-12-23 19:02:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-24 01:25:13 -04:00
|
|
|
|
2010-12-23 19:02:51 -04:00
|
|
|
/**
|
|
|
|
* Default constructor
|
|
|
|
*/
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an entry to the registry
|
|
|
|
*/
|
2010-12-24 01:25:13 -04:00
|
|
|
void add(AP_EEPromEntry * entry, uint16_t & id, uint16_t & address, size_t size);
|
2010-12-23 19:02:51 -04:00
|
|
|
|
|
|
|
private:
|
2010-12-24 01:25:13 -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-24 01:25:13 -04:00
|
|
|
/**
|
|
|
|
* Global eepromRegistry declaration.
|
|
|
|
*/
|
|
|
|
extern AP_EEPromRegistry eepromRegistry;
|
2010-12-23 19:02:51 -04:00
|
|
|
|
2010-12-24 01:25:13 -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-25 03:09:37 -04:00
|
|
|
class AP_EEPromVar : public AP_EEPromEntry, public AP_Var<type>
|
2010-12-23 19:02:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2010-12-24 01:25:13 -04:00
|
|
|
/**
|
|
|
|
* The default constrcutor
|
|
|
|
*/
|
2010-12-25 03:09:37 -04:00
|
|
|
AP_EEPromVar(type data, const char * name = "", bool sync=false) :
|
|
|
|
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-25 03:09:37 -04:00
|
|
|
virtual void setEntry(float val) { this->setAsFloat(val); }
|
|
|
|
virtual float getEntry() { return this->getAsFloat(); }
|
|
|
|
virtual const char * getEntryName() { return this->getName(); }
|
2010-12-24 01:25:13 -04:00
|
|
|
|
|
|
|
/**
|
2010-12-25 03:09:37 -04:00
|
|
|
* Get the id of the variable.
|
2010-12-24 01:25:13 -04:00
|
|
|
*/
|
2010-12-25 03:09:37 -04:00
|
|
|
virtual uint16_t getEntryId() { return _id; }
|
2010-12-24 01:25:13 -04:00
|
|
|
|
|
|
|
/**
|
2010-12-25 03:09:37 -04:00
|
|
|
* Get the address of the variable.
|
2010-12-24 01:25:13 -04:00
|
|
|
*/
|
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-25 03:09:37 -04:00
|
|
|
uint16_t _id; /** Variable identifier */
|
|
|
|
uint16_t _address; /** EEProm address of variable */
|
2010-12-23 19:02:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|