From fb9b1bdff9037ce87a794f8907ddcf155da3fc8c Mon Sep 17 00:00:00 2001 From: Florian Fleissner Date: Wed, 20 Nov 2019 17:59:01 +0100 Subject: [PATCH] Virtual EEPROM implementation moved over to virtual device Only the virtual device knows about the size of its EEPROM storage. Including device headers in the virtual core is not intended (and technical not possible). Signed-off-by: Florian Fleissner --- virtual/cores/arduino/EEPROM.cpp | 22 ---- virtual/cores/arduino/EEPROM.h | 185 +++++++++++-------------------- 2 files changed, 67 insertions(+), 140 deletions(-) delete mode 100644 virtual/cores/arduino/EEPROM.cpp diff --git a/virtual/cores/arduino/EEPROM.cpp b/virtual/cores/arduino/EEPROM.cpp deleted file mode 100644 index a32e702..0000000 --- a/virtual/cores/arduino/EEPROM.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* -*- mode: c++ -*- - * Kaleidoscope - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "EEPROM.h" - -uint8_t EERef::eeprom_[kaleidoscope::hardware::mcu::eeprom_size]; - -EEPROMClass EEPROM; diff --git a/virtual/cores/arduino/EEPROM.h b/virtual/cores/arduino/EEPROM.h index d9a3d6d..262292b 100644 --- a/virtual/cores/arduino/EEPROM.h +++ b/virtual/cores/arduino/EEPROM.h @@ -25,8 +25,6 @@ #include "Arduino.h" -#include KALEIDOSCOPE_MCU_H - /*** EERef class. @@ -35,84 +33,49 @@ This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. ***/ -struct EERef { +struct EERef{ - EERef(const int index) - : index(index) {} + EERef( const int index ) + : index( index ) {} + + //Access/read members. + uint8_t operator*() const { return eeprom_[index]; } + operator uint8_t() const { return **this; } + + //Assignment/write members. + EERef &operator=( const EERef &ref ) { return *this = *ref; } + EERef &operator=( uint8_t in ) { eeprom_[index] = in; return *this; } + EERef &operator +=( uint8_t in ) { return *this = **this + in; } + EERef &operator -=( uint8_t in ) { return *this = **this - in; } + EERef &operator *=( uint8_t in ) { return *this = **this * in; } + EERef &operator /=( uint8_t in ) { return *this = **this / in; } + EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; } + EERef &operator %=( uint8_t in ) { return *this = **this % in; } + EERef &operator &=( uint8_t in ) { return *this = **this & in; } + EERef &operator |=( uint8_t in ) { return *this = **this | in; } + EERef &operator <<=( uint8_t in ) { return *this = **this << in; } + EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } + + EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } + + /** Prefix increment/decrement **/ + EERef& operator++() { return *this += 1; } + EERef& operator--() { return *this -= 1; } + + /** Postfix increment/decrement **/ + uint8_t operator++ (int){ + uint8_t ret = **this; + return ++(*this), ret; + } - //Access/read members. - uint8_t operator*() const { - return eeprom_[index]; - } - operator uint8_t() const { - return **this; - } - - //Assignment/write members. - EERef &operator=(const EERef &ref) { - return *this = *ref; - } - EERef &operator=(uint8_t in) { - eeprom_[index] = in; - return *this; - } - EERef &operator +=(uint8_t in) { - return *this = **this + in; - } - EERef &operator -=(uint8_t in) { - return *this = **this - in; - } - EERef &operator *=(uint8_t in) { - return *this = **this * in; - } - EERef &operator /=(uint8_t in) { - return *this = **this / in; - } - EERef &operator ^=(uint8_t in) { - return *this = **this ^ in; - } - EERef &operator %=(uint8_t in) { - return *this = **this % in; - } - EERef &operator &=(uint8_t in) { - return *this = **this & in; - } - EERef &operator |=(uint8_t in) { - return *this = **this | in; - } - EERef &operator <<=(uint8_t in) { - return *this = **this << in; - } - EERef &operator >>=(uint8_t in) { - return *this = **this >> in; - } - - EERef &update(uint8_t in) { - return in != *this ? *this = in : *this; - } - - /** Prefix increment/decrement **/ - EERef& operator++() { - return *this += 1; - } - EERef& operator--() { - return *this -= 1; - } - - /** Postfix increment/decrement **/ - uint8_t operator++ (int) { - uint8_t ret = **this; - return ++(*this), ret; - } - - uint8_t operator-- (int) { - uint8_t ret = **this; - return --(*this), ret; - } - - int index; //Index of current EEPROM cell. - - static uint8_t eeprom_[kaleidoscope::hardware::mcu::eeprom_size]; + uint8_t operator-- (int){ + uint8_t ret = **this; + return --(*this), ret; + } + + int index; //Index of current EEPROM cell. + + static uint8_t eeprom_[]; }; /*** @@ -168,47 +131,33 @@ struct EEPtr { This class is also 100% backwards compatible with earlier Arduino core releases. ***/ -struct EEPROMClass { +struct EEPROMClass{ - //Basic user access methods. - EERef operator[](const int idx) { - return idx; - } - uint8_t read(int idx) { - return EERef(idx); - } - void write(int idx, uint8_t val) { - (EERef(idx)) = val; - } - void update(int idx, uint8_t val) { - EERef(idx).update(val); - } - - //STL and C++11 iteration capability. - EEPtr begin() { - return 0x00; - } - EEPtr end() { - return length(); //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. - } - uint16_t length() { - return kaleidoscope::hardware::mcu::eeprom_size; - } - - //Functionality to 'get' and 'put' objects to and from EEPROM. - template< typename T > T &get(int idx, T &t) { - EEPtr e = idx; - uint8_t *ptr = (uint8_t*) &t; - for (int count = sizeof(T) ; count ; --count, ++e) *ptr++ = *e; - return t; - } - - template< typename T > const T &put(int idx, const T &t) { - EEPtr e = idx; - const uint8_t *ptr = (const uint8_t*) &t; - for (int count = sizeof(T) ; count ; --count, ++e)(*e).update(*ptr++); - return t; - } + //Basic user access methods. + EERef operator[]( const int idx ) { return idx; } + uint8_t read( int idx ) { return EERef( idx ); } + void write( int idx, uint8_t val ) { (EERef( idx )) = val; } + void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } + + //STL and C++11 iteration capability. + EEPtr begin() { return 0x00; } + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + uint16_t length(); + + //Functionality to 'get' and 'put' objects to and from EEPROM. + template< typename T > T &get( int idx, T &t ){ + EEPtr e = idx; + uint8_t *ptr = (uint8_t*) &t; + for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; + return t; + } + + template< typename T > const T &put( int idx, const T &t ){ + EEPtr e = idx; + const uint8_t *ptr = (const uint8_t*) &t; + for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); + return t; + } }; extern EEPROMClass EEPROM;