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

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
7:72f87b7c7400
direct lcd stuff used by sensitive

Who changed what in which revision?

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