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

Committer:
Pokitto
Date:
Tue Jan 30 10:41:47 2018 +0000
Revision:
31:f4b9b85c7b62
Sound output improvements added:  louder, clearer, faster!

Who changed what in which revision?

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