PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
POKITTO_HW/PokittoEEPROM.h@36:771321e70814, 2018-04-02 (annotated)
- Committer:
- Pokitto
- Date:
- Mon Apr 02 22:37:22 2018 +0000
- Revision:
- 36:771321e70814
- Child:
- 58:5f58a2846a20
Synced with Github repo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 36:771321e70814 | 1 | /* |
Pokitto | 36:771321e70814 | 2 | EEPROM.h - EEPROM library |
Pokitto | 36:771321e70814 | 3 | Original Copyright (c) 2006 David A. Mellis. All right reserved. |
Pokitto | 36:771321e70814 | 4 | New version by Christopher Andrews 2015. |
Pokitto | 36:771321e70814 | 5 | |
Pokitto | 36:771321e70814 | 6 | This library is free software; you can redistribute it and/or |
Pokitto | 36:771321e70814 | 7 | modify it under the terms of the GNU Lesser General Public |
Pokitto | 36:771321e70814 | 8 | License as published by the Free Software Foundation; either |
Pokitto | 36:771321e70814 | 9 | version 2.1 of the License, or (at your option) any later version. |
Pokitto | 36:771321e70814 | 10 | |
Pokitto | 36:771321e70814 | 11 | This library is distributed in the hope that it will be useful, |
Pokitto | 36:771321e70814 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
Pokitto | 36:771321e70814 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
Pokitto | 36:771321e70814 | 14 | Lesser General Public License for more details. |
Pokitto | 36:771321e70814 | 15 | |
Pokitto | 36:771321e70814 | 16 | You should have received a copy of the GNU Lesser General Public |
Pokitto | 36:771321e70814 | 17 | License along with this library; if not, write to the Free Software |
Pokitto | 36:771321e70814 | 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Pokitto | 36:771321e70814 | 19 | */ |
Pokitto | 36:771321e70814 | 20 | |
Pokitto | 36:771321e70814 | 21 | #ifndef EEPROM_h |
Pokitto | 36:771321e70814 | 22 | #define EEPROM_h |
Pokitto | 36:771321e70814 | 23 | |
Pokitto | 36:771321e70814 | 24 | #include <stdint.h> |
Pokitto | 36:771321e70814 | 25 | #include "iap.h" |
Pokitto | 36:771321e70814 | 26 | |
Pokitto | 36:771321e70814 | 27 | /*** |
Pokitto | 36:771321e70814 | 28 | EERef class. |
Pokitto | 36:771321e70814 | 29 | |
Pokitto | 36:771321e70814 | 30 | This object references an EEPROM cell. |
Pokitto | 36:771321e70814 | 31 | Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM. |
Pokitto | 36:771321e70814 | 32 | This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. |
Pokitto | 36:771321e70814 | 33 | ***/ |
Pokitto | 36:771321e70814 | 34 | |
Pokitto | 36:771321e70814 | 35 | struct EERef{ |
Pokitto | 36:771321e70814 | 36 | |
Pokitto | 36:771321e70814 | 37 | EERef( const int index ) |
Pokitto | 36:771321e70814 | 38 | : index( index ) {} |
Pokitto | 36:771321e70814 | 39 | |
Pokitto | 36:771321e70814 | 40 | //Access/read members. |
Pokitto | 36:771321e70814 | 41 | uint8_t operator*() const { return eeprom_read_byte( (uint16_t*) index ); } |
Pokitto | 36:771321e70814 | 42 | operator const uint8_t() const { return **this; } |
Pokitto | 36:771321e70814 | 43 | |
Pokitto | 36:771321e70814 | 44 | //Assignment/write members. |
Pokitto | 36:771321e70814 | 45 | EERef &operator=( const EERef &ref ) { return *this = *ref; } |
Pokitto | 36:771321e70814 | 46 | EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint16_t*) index, in ), *this; } |
Pokitto | 36:771321e70814 | 47 | EERef &operator +=( uint8_t in ) { return *this = **this + in; } |
Pokitto | 36:771321e70814 | 48 | EERef &operator -=( uint8_t in ) { return *this = **this - in; } |
Pokitto | 36:771321e70814 | 49 | EERef &operator *=( uint8_t in ) { return *this = **this * in; } |
Pokitto | 36:771321e70814 | 50 | EERef &operator /=( uint8_t in ) { return *this = **this / in; } |
Pokitto | 36:771321e70814 | 51 | EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; } |
Pokitto | 36:771321e70814 | 52 | EERef &operator %=( uint8_t in ) { return *this = **this % in; } |
Pokitto | 36:771321e70814 | 53 | EERef &operator &=( uint8_t in ) { return *this = **this & in; } |
Pokitto | 36:771321e70814 | 54 | EERef &operator |=( uint8_t in ) { return *this = **this | in; } |
Pokitto | 36:771321e70814 | 55 | EERef &operator <<=( uint8_t in ) { return *this = **this << in; } |
Pokitto | 36:771321e70814 | 56 | EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } |
Pokitto | 36:771321e70814 | 57 | |
Pokitto | 36:771321e70814 | 58 | EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } |
Pokitto | 36:771321e70814 | 59 | |
Pokitto | 36:771321e70814 | 60 | /** Prefix increment/decrement **/ |
Pokitto | 36:771321e70814 | 61 | EERef& operator++() { return *this += 1; } |
Pokitto | 36:771321e70814 | 62 | EERef& operator--() { return *this -= 1; } |
Pokitto | 36:771321e70814 | 63 | |
Pokitto | 36:771321e70814 | 64 | /** Postfix increment/decrement **/ |
Pokitto | 36:771321e70814 | 65 | uint8_t operator++ (int){ |
Pokitto | 36:771321e70814 | 66 | uint8_t ret = **this; |
Pokitto | 36:771321e70814 | 67 | return ++(*this), ret; |
Pokitto | 36:771321e70814 | 68 | } |
Pokitto | 36:771321e70814 | 69 | |
Pokitto | 36:771321e70814 | 70 | uint8_t operator-- (int){ |
Pokitto | 36:771321e70814 | 71 | uint8_t ret = **this; |
Pokitto | 36:771321e70814 | 72 | return --(*this), ret; |
Pokitto | 36:771321e70814 | 73 | } |
Pokitto | 36:771321e70814 | 74 | |
Pokitto | 36:771321e70814 | 75 | int index; //Index of current EEPROM cell. |
Pokitto | 36:771321e70814 | 76 | }; |
Pokitto | 36:771321e70814 | 77 | |
Pokitto | 36:771321e70814 | 78 | /*** |
Pokitto | 36:771321e70814 | 79 | EEPtr class. |
Pokitto | 36:771321e70814 | 80 | |
Pokitto | 36:771321e70814 | 81 | This object is a bidirectional pointer to EEPROM cells represented by EERef objects. |
Pokitto | 36:771321e70814 | 82 | Just like a normal pointer type, this can be dereferenced and repositioned using |
Pokitto | 36:771321e70814 | 83 | increment/decrement operators. |
Pokitto | 36:771321e70814 | 84 | ***/ |
Pokitto | 36:771321e70814 | 85 | |
Pokitto | 36:771321e70814 | 86 | struct EEPtr{ |
Pokitto | 36:771321e70814 | 87 | |
Pokitto | 36:771321e70814 | 88 | EEPtr( const int index ) |
Pokitto | 36:771321e70814 | 89 | : index( index ) {} |
Pokitto | 36:771321e70814 | 90 | |
Pokitto | 36:771321e70814 | 91 | operator const int() const { return index; } |
Pokitto | 36:771321e70814 | 92 | EEPtr &operator=( int in ) { return index = in, *this; } |
Pokitto | 36:771321e70814 | 93 | |
Pokitto | 36:771321e70814 | 94 | //Iterator functionality. |
Pokitto | 36:771321e70814 | 95 | bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } |
Pokitto | 36:771321e70814 | 96 | EERef operator*() { return index; } |
Pokitto | 36:771321e70814 | 97 | |
Pokitto | 36:771321e70814 | 98 | /** Prefix & Postfix increment/decrement **/ |
Pokitto | 36:771321e70814 | 99 | EEPtr& operator++() { return ++index, *this; } |
Pokitto | 36:771321e70814 | 100 | EEPtr& operator--() { return --index, *this; } |
Pokitto | 36:771321e70814 | 101 | EEPtr operator++ (int) { return index++; } |
Pokitto | 36:771321e70814 | 102 | EEPtr operator-- (int) { return index--; } |
Pokitto | 36:771321e70814 | 103 | |
Pokitto | 36:771321e70814 | 104 | int index; //Index of current EEPROM cell. |
Pokitto | 36:771321e70814 | 105 | }; |
Pokitto | 36:771321e70814 | 106 | |
Pokitto | 36:771321e70814 | 107 | /*** |
Pokitto | 36:771321e70814 | 108 | EEPROMClass class. |
Pokitto | 36:771321e70814 | 109 | |
Pokitto | 36:771321e70814 | 110 | This object represents the entire EEPROM space. |
Pokitto | 36:771321e70814 | 111 | It wraps the functionality of EEPtr and EERef into a basic interface. |
Pokitto | 36:771321e70814 | 112 | This class is also 100% backwards compatible with earlier Arduino core releases. |
Pokitto | 36:771321e70814 | 113 | ***/ |
Pokitto | 36:771321e70814 | 114 | |
Pokitto | 36:771321e70814 | 115 | struct EEPROMClass{ |
Pokitto | 36:771321e70814 | 116 | |
Pokitto | 36:771321e70814 | 117 | //Basic user access methods. |
Pokitto | 36:771321e70814 | 118 | EERef operator[]( const int idx ) { return idx; } |
Pokitto | 36:771321e70814 | 119 | uint8_t read( int idx ) { return EERef( idx ); } |
Pokitto | 36:771321e70814 | 120 | void write( int idx, uint8_t val ) { (EERef( idx )) = val; } |
Pokitto | 36:771321e70814 | 121 | void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } |
Pokitto | 36:771321e70814 | 122 | |
Pokitto | 36:771321e70814 | 123 | //STL and C++11 iteration capability. |
Pokitto | 36:771321e70814 | 124 | EEPtr begin() { return 0x00; } |
Pokitto | 36:771321e70814 | 125 | EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. |
Pokitto | 36:771321e70814 | 126 | uint16_t length() { return EESETTINGS_VOL; } // length for pokitto is 4kB MINUS settings area!! |
Pokitto | 36:771321e70814 | 127 | |
Pokitto | 36:771321e70814 | 128 | //Functionality to 'get' and 'put' objects to and from EEPROM. |
Pokitto | 36:771321e70814 | 129 | template< typename T > T &get( int idx, T &t ){ |
Pokitto | 36:771321e70814 | 130 | EEPtr e = idx; |
Pokitto | 36:771321e70814 | 131 | uint8_t *ptr = (uint8_t*) &t; |
Pokitto | 36:771321e70814 | 132 | for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; |
Pokitto | 36:771321e70814 | 133 | return t; |
Pokitto | 36:771321e70814 | 134 | } |
Pokitto | 36:771321e70814 | 135 | |
Pokitto | 36:771321e70814 | 136 | template< typename T > const T &put( int idx, const T &t ){ |
Pokitto | 36:771321e70814 | 137 | EEPtr e = idx; |
Pokitto | 36:771321e70814 | 138 | const uint8_t *ptr = (const uint8_t*) &t; |
Pokitto | 36:771321e70814 | 139 | for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); |
Pokitto | 36:771321e70814 | 140 | return t; |
Pokitto | 36:771321e70814 | 141 | } |
Pokitto | 36:771321e70814 | 142 | }; |
Pokitto | 36:771321e70814 | 143 | |
Pokitto | 36:771321e70814 | 144 | static EEPROMClass EEPROM; |
Pokitto | 36:771321e70814 | 145 | #endif |
Pokitto | 36:771321e70814 | 146 | |
Pokitto | 36:771321e70814 | 147 |