Skip to content

Commit

Permalink
v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr committed Jan 10, 2021
1 parent e203336 commit b700ae7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 73 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@

## Arduino EEPROM CString List Manager - EPList (For keep some Strings on External EEPROM and minimize use memory on Arduino Board).

Template class header file.

Template class.

Written by Piotr Kupczyk ([email protected])
2020
v. 0.3
v. 0.5

Github: https://github.com/piotrku91/

Depedencies:
### Depedencies:

SparkFun_External_EEPROM.h // Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM

Tip:
### Tip:

For proper work execute Wire.begin() function in setup() function (main sketch).

Example of create objects:
### Example of create objects:

EPList<64> EC(0x50); // Static allocation (i2c chip Address)

EPList<64> *EC = new EPList<64>(0x50); // Dynamic allocation (Address, Size of EEPROM chip, Page Size)


EPList<64> EC(0x50,32000,128); // Static allocation with more settings (i2c chip Address, Size of EEPROM chip, Page Size)

EPList<64> *EC = new EPList<64>(0x50,32000,128); // Dynamic allocation (Address, Size of EEPROM chip, Page Size)

50 changes: 30 additions & 20 deletions example/EEPROMCStringList/EEPROMCStringList.ino
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
#include <EPList.h>
#include <EPList.cpp>
#include <EPList.cpp> // Include cpp file as well for link template functions.

void setup() {
void setup()
{
Serial.begin(9600);
Wire.begin();

//EPList<64> EC(0x50,32000,128); // Static allocation
//EC.FillList(20,"PUSTY"); // Example of clear list and init first 20 items as PUSTY
delay(1000);

// delay(1000);
}


void loop() {
EPList<64> *EC = new EPList<64>(0x50,32000,128); // Dynamic allocation
void loop()
{
EPList<64> *EC = new EPList<64>(0x50, 32000, 128); // Dynamic allocation

Serial.print("Items count: ");
Serial.println(EC->size());

// EC->pushItem("I'm inside the chip");
// EC->pushItem("Thank you.");

// Example of read full list
for (int i=0; i<EC->size(); i++)
{
Serial.print(i);
Serial.print(" : ");
Serial.println(EC->getItem(i)); // Access to item
delay(10);
Serial.print("Available space: ");
Serial.println(EC->CountSpace());
//delay(5000);

//Example of add some items.
EC->pushItem("I'm inside the chip");
EC->pushItem("Thank you.");

//
//EC->FillList(EC->CountSpace(),"PUSTY"); // Example of clear list and init full available space by PUSTY.

// Example of read full list
for (int i = 0; i < EC->size(); i++)
{
Serial.print(i);
Serial.print(" : ");
Serial.println((*EC)[i]); // Access to item by overloaded operator [] (same as EC->getItem(i);). For assign use only EC->setItem(i,"example");
delay(10);
};

delay(1000);

delete EC; // Free memory
while(1) {};
}
while (1)
{
}; // Freeze program.
}
45 changes: 21 additions & 24 deletions src/EPList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

/*
Arduino EEPROM CString List Manager - EPList (For keep some Strings on External EEPROM and minimize use memory on Arduino Board).
Template class header file.
Written by Piotr Kupczyk ([email protected])
2020
v. 0.3
v. 0.5
Github: https://github.com/piotrku91/
Expand All @@ -19,32 +18,30 @@ SparkFun_External_EEPROM.h // Click here to get the library: http://librarymanag


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <int InitStringSize>
const int EPList<InitStringSize>::size()
template <unsigned int InitStringSize>
const unsigned int EPList<InitStringSize>::size()
{
return Memory.get(0, ItemsCounter);
delay(10);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int InitStringSize>
template <unsigned int InitStringSize>
void EPList<InitStringSize>::SaveCounter()
{
delay(10);
Memory.put(0, ItemsCounter);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int InitStringSize>
const char *EPList<InitStringSize>::getItem(const uint8_t &Index)
template <unsigned int InitStringSize>
const char *EPList<InitStringSize>::getItem(const unsigned int &Index)
{
if (!MemReady())
return "NR!"; // NOT READY
if (Index < ItemsCounter)
{
m_ActualIndex = Index;

Memory.get(sizeof(ItemsCounter) + (m_ActualIndex * m_StringSize), m_Value);
Memory.get(sizeof(ItemsCounter) + (Index * m_StringSize), m_Value);
return m_Value;
};

Expand All @@ -53,17 +50,16 @@ const char *EPList<InitStringSize>::getItem(const uint8_t &Index)
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int InitStringSize>
bool EPList<InitStringSize>::setItem(const uint8_t &Index, const char *NewCaption)
template <unsigned int InitStringSize>
bool EPList<InitStringSize>::setItem(const unsigned int &Index, const char *NewCaption)
{
if (MemReady())
{

if (Index < ItemsCounter)
{
strcpy(m_Value, NewCaption);
m_ActualIndex = Index;
Memory.put(sizeof(ItemsCounter) + (m_ActualIndex * m_StringSize), m_Value);
Memory.put(sizeof(ItemsCounter) + (Index * m_StringSize), m_Value);

return true; // Changed
};
Expand All @@ -74,27 +70,28 @@ bool EPList<InitStringSize>::setItem(const uint8_t &Index, const char *NewCaptio
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int InitStringSize>
template <unsigned int InitStringSize>
bool EPList<InitStringSize>::pushItem(const char *NewCaption)
{
if (MemReady())

if ((MemReady()) && (isFreeSpace()))
{

strcpy(m_Value, NewCaption);

ItemsCounter++;
SaveCounter();
m_ActualIndex = ItemsCounter - 1;


Memory.put(sizeof(ItemsCounter) + (m_ActualIndex * m_StringSize), m_Value);
Memory.put(sizeof(ItemsCounter) + ((ItemsCounter - 1) * m_StringSize), m_Value);

return true; // Changed
};

return false; // Not Changed (memory not ready)
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <int InitStringSize>
template <unsigned int InitStringSize>
bool EPList<InitStringSize>::ClearList(bool areyousure)
{
if ((MemReady()) && (areyousure))
Expand All @@ -109,21 +106,21 @@ bool EPList<InitStringSize>::ClearList(bool areyousure)
return false; // Not Changed (memory not ready)
};

template <int InitStringSize>
template <unsigned int InitStringSize>
const ExternalEEPROM *EPList<InitStringSize>::RawAccess()
{
return &Memory;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int InitStringSize>
const char * EPList<InitStringSize>::operator[](const uint8_t &Index)
template <unsigned int InitStringSize>
const char * EPList<InitStringSize>::operator[](const unsigned int &Index)
{
return getItem(Index);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int InitStringSize>
template <unsigned int InitStringSize>
void EPList<InitStringSize>::FillList(int ItemsToFill, const char *NewString)
{
ClearList(true); // Erase list
Expand All @@ -136,6 +133,6 @@ void EPList<InitStringSize>::FillList(int ItemsToFill, const char *NewString)
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int InitStringSize>
template <unsigned int InitStringSize>
bool EPList<InitStringSize>::MemReady() { return Memory.isConnected(); };
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51 changes: 27 additions & 24 deletions src/EPList.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Template class header file.
Written by Piotr Kupczyk ([email protected])
2020
v. 0.3
v. 0.5
Github: https://github.com/piotrku91/
Expand Down Expand Up @@ -44,53 +44,56 @@ Address 4 + (2*64) -> 00 00 00 00 00 00 00 00 00 00 00 00 ... (Item 2) - char ar
*/

template <int InitStringSize>
template <unsigned int InitStringSize>
class EPList
{
private:
unsigned int ItemsCounter; // Keeps size of list on the begin of EEPROM (Address 0x0);
const int m_StringSize; // Size of actual size of Item defined by template.
const unsigned int m_StringSize; // Size of actual size of Item defined by template.
unsigned int m_SpaceSize; // Max amount of items available.
unsigned int ItemsCounter; // Keeps size of list on the begin of EEPROM (Address 0x0);

uint8_t m_ActualIndex; // Last touched item on list
char m_Value[InitStringSize]; // Last touched item on list

ExternalEEPROM Memory; // SparkFun_External_EEPROM object

public: // Constructors
EPList(uint8_t EEPROMAddress,int MemorySize,uint8_t PageSize) : m_StringSize{InitStringSize}, ItemsCounter(0)
EPList(uint8_t EEPROMAddress, int MemorySize, uint8_t PageSize) : m_StringSize{InitStringSize}, ItemsCounter{0}
{
Memory.begin(EEPROMAddress);
Memory.setMemorySize(MemorySize);
Memory.setPageSize(PageSize);
Memory.enablePollForWriteComplete();
Memory.setPageWriteTime(3);
Memory.setPageSize(PageSize);
Memory.enablePollForWriteComplete();
Memory.setPageWriteTime(3);

ItemsCounter = size(); // Load Items counter
CountSpace();
};
EPList(uint8_t EEPROMAddress = 0x50) : m_StringSize{InitStringSize}, ItemsCounter(0)
{
ItemsCounter = size();
CountSpace();
};

EPList(uint8_t EEPROMAddress=0x50) : m_StringSize{InitStringSize}, ItemsCounter(0) {ItemsCounter = size();};

private: // Private functions
bool MemReady();
void SaveCounter(); // Save size of list

public: // Public Functions
const char *getItem(const uint8_t &Index);
bool setItem(const uint8_t &Index, const char *NewString);
bool pushItem(const char *NewString);
const int size(); // Return actual size of list

bool ClearList(bool areyousure = false); // !!!!! Erasing full list !!!!!!
void FillList(int ItemsToFill,const char *NewString); // !!!!! Erasing full list and fill by NewString !!!!!!
public: // Public Functions
const char *getItem(const unsigned int &Index); // Return item in index
bool setItem(const unsigned int &Index, const char *NewString); // Set item in index by NewString
bool pushItem(const char *NewString); // Add new item on the end
const unsigned int size(); // Return actual size of list
const unsigned int CountSpace() { return m_SpaceSize = floor((Memory.getMemorySize() - sizeof(ItemsCounter)) / m_StringSize); } // Count available space for that size of items
bool isFreeSpace() {return (ItemsCounter<=m_SpaceSize);};

const ExternalEEPROM* RawAccess(); // Access to SparkFun_External_EEPROM object from outside of class.

// Operators overload
const char* operator[](const uint8_t &Index);

bool ClearList(bool areyousure = false); // !!!!! Erasing full list !!!!!!
void FillList(int ItemsToFill, const char *NewString); // !!!!! Erasing full list and fill by NewString !!!!!!

const ExternalEEPROM *RawAccess(); // Access to SparkFun_External_EEPROM object from outside of class.

// Operators overload
const char *operator[](const unsigned &Index);
};

#endif

0 comments on commit b700ae7

Please sign in to comment.