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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
Pokitto
Date:
Tue Sep 19 08:47:36 2017 +0000
Revision:
2:968589ca3484
Parent:
0:e8b8f36b4505
Got rid of warnings at compile time

Who changed what in which revision?

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