1
0

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 <florian.fleissner@inpartik.de>
This commit is contained in:
Florian Fleissner 2019-11-20 17:59:01 +01:00 committed by Jesse Vincent
parent 638306d7b3
commit fb9b1bdff9
No known key found for this signature in database
GPG Key ID: CC228463465E40BC
2 changed files with 67 additions and 140 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "EEPROM.h"
uint8_t EERef::eeprom_[kaleidoscope::hardware::mcu::eeprom_size];
EEPROMClass EEPROM;

View File

@ -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;
}
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 &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;
}
EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; }
/** Prefix increment/decrement **/
EERef& operator++() {
return *this += 1;
}
EERef& operator--() {
return *this -= 1;
}
EERef& operator++() { return *this += 1; }
EERef& operator--() { return *this -= 1; }
/** Postfix increment/decrement **/
uint8_t operator++ (int) {
uint8_t operator++ (int){
uint8_t ret = **this;
return ++(*this), ret;
}
uint8_t operator-- (int) {
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];
static uint8_t eeprom_[];
};
/***
@ -168,45 +131,31 @@ 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);
}
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;
}
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) {
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;
for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e;
return t;
}
template< typename T > const T &put(int idx, const T &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++);
for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ );
return t;
}
};