PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PokittoEEPROM.h Source File

PokittoEEPROM.h

00001 /*
00002   EEPROM.h - EEPROM library
00003   Original Copyright (c) 2006 David A. Mellis.  All right reserved.
00004   New version by Christopher Andrews 2015.
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either
00009   version 2.1 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Lesser General Public License for more details.
00015 
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library; if not, write to the Free Software
00018   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 */
00020 
00021 #ifndef EEPROM_h
00022 #define EEPROM_h
00023 
00024 #include <stdint.h>
00025 #include "iap.h"
00026 
00027 /***
00028     EERef class.
00029 
00030     This object references an EEPROM cell.
00031     Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM.
00032     This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell.
00033 ***/
00034 
00035 struct EERef{
00036 
00037     EERef( const int index )
00038         : index( index )                 {}
00039 
00040     //Access/read members.
00041     uint8_t operator*() const            { return eeprom_read_byte( (uint8_t*) index ); }
00042     operator uint8_t() const       { return **this; }
00043 
00044     //Assignment/write members.
00045     EERef &operator=( const EERef &ref ) { return *this = *ref; }
00046     EERef &operator=( uint8_t in )       { return eeprom_write_byte( (uint8_t*) index, in ), *this;  }
00047     EERef &operator +=( uint8_t in )     { return *this = **this + in; }
00048     EERef &operator -=( uint8_t in )     { return *this = **this - in; }
00049     EERef &operator *=( uint8_t in )     { return *this = **this * in; }
00050     EERef &operator /=( uint8_t in )     { return *this = **this / in; }
00051     EERef &operator ^=( uint8_t in )     { return *this = **this ^ in; }
00052     EERef &operator %=( uint8_t in )     { return *this = **this % in; }
00053     EERef &operator &=( uint8_t in )     { return *this = **this & in; }
00054     EERef &operator |=( uint8_t in )     { return *this = **this | in; }
00055     EERef &operator <<=( uint8_t in )    { return *this = **this << in; }
00056     EERef &operator >>=( uint8_t in )    { return *this = **this >> in; }
00057 
00058     EERef &update( uint8_t in )          { return  in != *this ? *this = in : *this; }
00059 
00060     /** Prefix increment/decrement **/
00061     EERef& operator++()                  { return *this += 1; }
00062     EERef& operator--()                  { return *this -= 1; }
00063 
00064     /** Postfix increment/decrement **/
00065     uint8_t operator++ (int){
00066         uint8_t ret = **this;
00067         return ++(*this), ret;
00068     }
00069 
00070     uint8_t operator-- (int){
00071         uint8_t ret = **this;
00072         return --(*this), ret;
00073     }
00074 
00075     int index; //Index of current EEPROM cell.
00076 };
00077 
00078 /***
00079     EEPtr class.
00080 
00081     This object is a bidirectional pointer to EEPROM cells represented by EERef objects.
00082     Just like a normal pointer type, this can be dereferenced and repositioned using
00083     increment/decrement operators.
00084 ***/
00085 
00086 struct EEPtr{
00087 
00088     EEPtr( const int index )
00089         : index( index )                {}
00090 
00091     operator int() const          { return index; }
00092     EEPtr &operator=( int in )          { return index = in, *this; }
00093 
00094     //Iterator functionality.
00095     bool operator!=( const EEPtr &ptr ) { return index != ptr.index; }
00096     EERef operator*()                   { return index; }
00097 
00098     /** Prefix & Postfix increment/decrement **/
00099     EEPtr& operator++()                 { return ++index, *this; }
00100     EEPtr& operator--()                 { return --index, *this; }
00101     EEPtr operator++ (int)              { return index++; }
00102     EEPtr operator-- (int)              { return index--; }
00103 
00104     int index; //Index of current EEPROM cell.
00105 };
00106 
00107 /***
00108     EEPROMClass class.
00109 
00110     This object represents the entire EEPROM space.
00111     It wraps the functionality of EEPtr and EERef into a basic interface.
00112     This class is also 100% backwards compatible with earlier Arduino core releases.
00113 ***/
00114 
00115 struct EEPROMClass{
00116 
00117     //Basic user access methods.
00118     EERef operator[]( const int idx )    { return idx; }
00119     uint8_t read( int idx )              { return EERef( idx ); }
00120     void write( int idx, uint8_t val )   { (EERef( idx )) = val; }
00121     void update( int idx, uint8_t val )  { EERef( idx ).update( val ); }
00122 
00123     //STL and C++11 iteration capability.
00124     EEPtr begin()                        { return 0x00; }
00125     EEPtr end()                          { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
00126     uint16_t length()                    { return 0x1000; } // length for pokitto is 4kB
00127 
00128     //Functionality to 'get' and 'put' objects to and from EEPROM.
00129     template< typename T > T &get( int idx, T &t ){
00130         EEPtr e = idx;
00131         uint8_t *ptr = (uint8_t*) &t;
00132         for( int count = sizeof(T) ; count ; --count, ++e )  *ptr++ = *e;
00133         return t;
00134     }
00135 
00136     template< typename T > const T &put( int idx, const T &t ){
00137         EEPtr e = idx;
00138         const uint8_t *ptr = (const uint8_t*) &t;
00139         for( int count = sizeof(T) ; count ; --count, ++e )  (*e).update( *ptr++ );
00140         return t;
00141     }
00142 };
00143 
00144 static EEPROMClass EEPROM;
00145 #endif
00146