CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
EEPROMClass Class Reference

26 bytes of emulated EEPROM in the option-byte area, worked on in a RAM copy and written by commit() (see EEPROM). More...

#include <EEPROM.h>

Public Member Functions

 EEPROMClass (void)
 Makes the object; nothing is read until begin().
 
 ~EEPROMClass (void)
 Calls end(), which commits any change.
 
void begin (void)
 Copies the 26 bytes from the option-byte area into RAM (26 bytes on the heap, allocated the first time).
 
uint8_t * getDataPtr ()
 The 26-byte RAM copy, to change in place; marks it changed, so the next commit() writes it.
 
uint8_t const * getConstDataPtr () const
 The 26-byte RAM copy, read-only.
 
uint32_t ReadOptionBytes (void)
 Reads the option bytes Data0 and Data1 straight from the chip (no begin() needed).
 
uint8_t read (int const idx)
 Reads a byte of the RAM copy.
 
void write (int const idx, uint8_t const val)
 Changes a byte of the RAM copy; commit() writes it.
 
void erase (void)
 Sets the 26 bytes of the RAM copy to 0xFF; commit() writes them.
 
bool commit (void)
 Writes the RAM copy to the option-byte area, if anything changed since begin() or the last commit().
 
bool end (void)
 Commits, then forgets the RAM copy (its 26 bytes are not freed); begin() starts again.
 
template<typename T >
T & get (int const address, T &t)
 Copies sizeof(T) bytes of the RAM copy into t.
 
template<typename T >
const T & put (int const address, const T &t)
 Copies t into the RAM copy, if it differs; commit() writes it.
 
size_t length ()
 The emulated EEPROM's size.
 
uint8_t & operator[] (int const address)
 A byte of the RAM copy, to read or change; marks the copy changed.
 
uint8_t const & operator[] (int const address) const
 A byte of the RAM copy, read-only.
 

Protected Attributes

uint8_t * _data = nullptr
 The RAM copy (26 bytes after begin()).
 
bool _dirty = false
 Whether the copy changed since begin() or the last commit().
 
size_t _size = 0
 The copy's size: 26 after begin(), else 0.
 

Detailed Description

26 bytes of emulated EEPROM in the option-byte area, worked on in a RAM copy and written by commit() (see EEPROM).

Constructor & Destructor Documentation

◆ EEPROMClass()

EEPROMClass::EEPROMClass ( void  )

Makes the object; nothing is read until begin().

◆ ~EEPROMClass()

EEPROMClass::~EEPROMClass ( void  )

Calls end(), which commits any change.

Member Function Documentation

◆ begin()

void EEPROMClass::begin ( void  )

Copies the 26 bytes from the option-byte area into RAM (26 bytes on the heap, allocated the first time).

Call it before anything else.

On the CH32X035 only bytes 0 and 1 (Data0, Data1) are read; bytes 2-25 start as 0xFF.

◆ getDataPtr()

uint8_t * EEPROMClass::getDataPtr ( )

The 26-byte RAM copy, to change in place; marks it changed, so the next commit() writes it.

Returns
the copy, or nullptr before begin().

◆ getConstDataPtr()

uint8_t const * EEPROMClass::getConstDataPtr ( ) const

The 26-byte RAM copy, read-only.

Returns
the copy, or nullptr before begin().

◆ ReadOptionBytes()

uint32_t EEPROMClass::ReadOptionBytes ( void  )

Reads the option bytes Data0 and Data1 straight from the chip (no begin() needed).

Returns
Data0's half-word in the high 16 bits and Data1's in the low 16; in each, the low byte is the value and the high byte its inverse.

◆ read()

uint8_t EEPROMClass::read ( int const  idx)

Reads a byte of the RAM copy.

Parameters
idx0 to 25.
Returns
the byte; 0 if idx is out of range or before begin().

◆ write()

void EEPROMClass::write ( int const  idx,
uint8_t const  val 
)

Changes a byte of the RAM copy; commit() writes it.

Parameters
idx0 to 25; anything else is ignored, as is a call before begin().
valthe new value.

◆ erase()

void EEPROMClass::erase ( void  )

Sets the 26 bytes of the RAM copy to 0xFF; commit() writes them.

◆ commit()

bool EEPROMClass::commit ( void  )

Writes the RAM copy to the option-byte area, if anything changed since begin() or the last commit().

Erases the whole area and rewrites it (see EEPROM). On the CH32X035 it writes nothing.

Returns
true if nothing had changed, or (CH32V003) once written: what was written is not checked. false on the CH32X035 whenever there was something to write.

◆ end()

bool EEPROMClass::end ( void  )

Commits, then forgets the RAM copy (its 26 bytes are not freed); begin() starts again.

Returns
what commit() returned.

◆ get()

template<typename T >
T & EEPROMClass::get ( int const  address,
T &  t 
)
inline

Copies sizeof(T) bytes of the RAM copy into t.

Template Parameters
Tany plain type or struct.
Parameters
addressthe first byte, 0 to 26 - sizeof(T).
treceives the bytes; left as it was if they would not fit.
Returns
t.

◆ put()

template<typename T >
const T & EEPROMClass::put ( int const  address,
const T &  t 
)
inline

Copies t into the RAM copy, if it differs; commit() writes it.

Template Parameters
Tany plain type or struct.
Parameters
addressthe first byte, 0 to 26 - sizeof(T); nothing is stored if t would not fit.
tthe value.
Returns
t.

◆ length()

size_t EEPROMClass::length ( )
inline

The emulated EEPROM's size.

Returns
26 after begin(), 0 before.

◆ operator[]() [1/2]

uint8_t & EEPROMClass::operator[] ( int const  address)
inline

A byte of the RAM copy, to read or change; marks the copy changed.

No range check: call begin() first and keep to 0-25.

Parameters
address0 to 25.
Returns
the byte.

◆ operator[]() [2/2]

uint8_t const & EEPROMClass::operator[] ( int const  address) const
inline

A byte of the RAM copy, read-only.

No range check.

Parameters
address0 to 25.
Returns
the byte.

Member Data Documentation

◆ _data

uint8_t* EEPROMClass::_data = nullptr
protected

The RAM copy (26 bytes after begin()).

◆ _dirty

bool EEPROMClass::_dirty = false
protected

Whether the copy changed since begin() or the last commit().

◆ _size

size_t EEPROMClass::_size = 0
protected

The copy's size: 26 after begin(), else 0.


The documentation for this class was generated from the following file: