/*
Bit Byte
Available
8192 1024
Required
Count 20 2
WriteCount 17 3
Sum 37 5
*/
constexpr uint32_t maxWriteCount {100000};
constexpr uint32_t maxCount {999999};
struct eepromStruct {
uint16_t WriteCountLow;
uint8_t HighPart;
uint16_t CountLow;
};
eepromStruct eeData;
uint32_t Count = 0;
uint32_t writeCount = 0;
void setup() {
Serial.begin(115200);
Serial.println(Count);
setEEData(maxCount, maxWriteCount);
getEEData();
setEEData(650, 1300);
getEEData();
}
void loop() {
// put your main code here, to run repeatedly:
}
void setEEData(uint32_t aCount, uint32_t wCount) {
aCount = (aCount > maxCount) ? maxCount : aCount;
wCount = (wCount > maxWriteCount) ? maxWriteCount : wCount;
eeData.CountLow = (aCount & 0xFFFF);
eeData.WriteCountLow = (wCount & 0xFFFF);
eeData.HighPart = (aCount >> 16) & 0x8F;
if ((wCount >> 16) > 0) {
bitSet(eeData.HighPart, 7);
}
Count = aCount;
writeCount = wCount;
printData("set");
}
void getEEData() {
Count = uint32_t(eeData.HighPart & 0x7F) << 16;
Count += eeData.CountLow;
uint32_t writeCount = uint32_t(eeData.HighPart & 0x80) << 9 ;
writeCount += eeData.WriteCountLow;
printData("get");
}
void printData(char *txt){
Serial.print(txt);
Serial.print("\tCount=\t");
Serial.print(Count);
Serial.print("\twCount=\t");
Serial.println(writeCount);
}