EEPROMextent
Arduino EEPROM library
EEPROM_ItemList.cpp
1 /*************************************************************
2 project: <EEPROMextent>
3 author: <Thierry PARIS>
4 description: <Class for a full hierarchical list of items of the same size stored in EEPROM>
5 *************************************************************/
6 
7 #include "EEPROM_ItemList.hpp"
8 
9 /*
10 The item list area of the EEPROM is a long list of items of the same size.
11 Each item can be identified by a type.
12 Each item has its own structure, but is stored in a 'slot' of EEPROM_ItemList::ItemSize.
13 Each item slot begins with two bytes : the type, and the owner (255 if null)
14 This owner gives the ability to build a hierarchical item tree !
15 The owner id is in reality the address of the slot of the owner !
16 */
17 
18 void EEPROM_ItemListClass::FreeItem(byte inSlotNumber)
19 {
20  // Replace the type by '0' : free slot !
21  EEPROMextent.updateByte(GetItemPosRaw(inSlotNumber), 0);
22  FreeOwnedItems(inSlotNumber);
23 }
24 
25 void EEPROM_ItemListClass::FreeOwnedItems(byte inOwnerSlotNumber)
26 {
27  int slot = 0;
28  for (int pos = this->startListPos; pos < this->startListPos + this->listSize; pos += itemSize)
29  {
30  if (EEPROMextent.readByte(pos) == 0)
31  continue;
32  if (EEPROMextent.readByte(pos + 1) == inOwnerSlotNumber)
33  {
34  // Replace the type by '0' : free slot !
35  EEPROMextent.updateByte(pos, 0);
36  // also remove the owned items, and their owned items in turn...
37  FreeItem(slot);
38  }
39  slot++;
40  }
41 }
42 
43 byte EEPROM_ItemListClass::FindItem(byte inType, byte inStartSlotNumber, byte inOwnerId)
44 {
45  byte slot = inStartSlotNumber;
46 
47  for (int pos = startListPos + (inStartSlotNumber * itemSize); pos < this->startListPos + this->listSize; pos += itemSize)
48  {
49  if (EEPROMextent.readByte(pos) == inType)
50  if (inOwnerId == 255 || EEPROMextent.readByte(pos + 1) == inOwnerId)
51  return slot;
52  slot++;
53  }
54 
55  return 255;
56 }
57 
59 {
60  byte nb = 0;
61 
62  for (int pos = startListPos; pos < this->startListPos + this->listSize; pos += itemSize)
63  if (inType == 0 || EEPROMextent.readByte(pos) == inType)
64  nb++;
65 
66  return nb;
67 }
68 
70 {
71  byte nb = 0;
72 
73  for (int pos = startListPos; pos < this->startListPos + this->listSize; pos += itemSize)
74  if (EEPROMextent.readByte(pos) != 0 && EEPROMextent.readByte(pos + 1) == inOwnerId)
75  nb++;
76 
77  return nb;
78 }
79 
81 {
82  byte slot = 0;
83 
84  for (int pos = startListPos; pos < this->startListPos + this->listSize; pos += itemSize)
85  {
86  byte val = EEPROMextent.readByte(pos);
87  if (val == 0)
88  return slot;
89  slot++;
90  }
91 
92  return 255;
93 }
94 
95 int EEPROM_ItemListClass::SaveItemPrefix(byte inSlotNumber, byte inType, byte inOwner)
96 {
97  int pos = GetItemPosRaw(inSlotNumber);
98  EEPROMextent.updateByte(pos++, inType);
99  EEPROMextent.updateByte(pos++, inOwner);
100  return pos;
101 }
102 
104 {
105  EEPROMextent.clear(this->startListPos, this->listSize);
106 }
107 
108 
109 
void clear(int address, int inSize, byte inFillCharacter = 0)
byte CountItems(byte inType)
int GetItemPosRaw(byte inSlotNumber)
void FreeItem(byte inSlotNumber)
byte CountOwnedItems(byte inOwnerId)
void updateByte(int address, uint8_t value)
Definition: EEPROMextent.h:85
int SaveItemPrefix(byte inSlotNumber, byte inType, byte inOwner)
void FreeOwnedItems(byte inOwnerSlotNumber)
uint8_t readByte(int address)
Definition: EEPROMextent.h:68
byte FindItem(byte inType, byte inStartSlotNumber = 0, byte inOwnerId = 255)