Don't use EEPROM for now
This commit is contained in:
parent
16f71c888b
commit
6bcc22c045
@ -26,6 +26,13 @@ constexpr long maximumTemperature = temperatureShiftForStorage + 255 * 500;
|
|||||||
|
|
||||||
constexpr uint8_t temperatureMaximumValue = 255;
|
constexpr uint8_t temperatureMaximumValue = 255;
|
||||||
|
|
||||||
|
enum class StorageFlags: uint8_t {
|
||||||
|
HasInitializedEEPROM = 1,
|
||||||
|
FailedToInitEEPROM = 2,
|
||||||
|
FailedToWriteEEPROM = 4,
|
||||||
|
RTCStorageOverflow = 5,
|
||||||
|
};
|
||||||
|
|
||||||
void storageConfigure(bool isFirstRun);
|
void storageConfigure(bool isFirstRun);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,4 +57,6 @@ uint16_t getTimeSinceValidTemperature(uint8_t sensorIndex);
|
|||||||
|
|
||||||
uint16_t getRecordedBytesAtOffset(uint8_t* buffer, uint16_t offset, uint16_t count);
|
uint16_t getRecordedBytesAtOffset(uint8_t* buffer, uint16_t offset, uint16_t count);
|
||||||
|
|
||||||
void discardAllRecordedBytes();
|
void discardAllRecordedBytes();
|
||||||
|
|
||||||
|
uint8_t getStorageErrorFlags();
|
@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
constexpr uint8_t absoluteTemperatureIndicator = 0xFF;
|
constexpr uint8_t absoluteTemperatureIndicator = 0xFF;
|
||||||
|
|
||||||
// The number of bytes to accumulate in RTC memory before writing it to EEPROM
|
RTC_DATA_ATTR uint8_t storageFlags = 0;
|
||||||
constexpr uint32_t eepromChunkSize = 100;
|
|
||||||
|
|
||||||
// Storage for temperature measurements in RTC memory that survives deep sleep
|
// Storage for temperature measurements in RTC memory that survives deep sleep
|
||||||
RTC_DATA_ATTR uint8_t data[rtcStorageSize];
|
RTC_DATA_ATTR uint8_t data[rtcStorageSize];
|
||||||
@ -18,31 +17,47 @@ RTC_DATA_ATTR uint32_t numberOfDiscardedMeasurements = 0;
|
|||||||
RTC_DATA_ATTR uint32_t eepromIndex = 0;
|
RTC_DATA_ATTR uint32_t eepromIndex = 0;
|
||||||
RTC_DATA_ATTR bool isFirstRunAfterPowerOn = true;
|
RTC_DATA_ATTR bool isFirstRunAfterPowerOn = true;
|
||||||
|
|
||||||
|
constexpr uint16_t eepromOffset = sizeof(uint16_t);
|
||||||
|
constexpr uint16_t eepromDataSize = eepromSize - eepromOffset;
|
||||||
|
|
||||||
// Keeps the last valid temperatures for each sensor
|
// Keeps the last valid temperatures for each sensor
|
||||||
RTC_DATA_ATTR Temperature lastTemperatures[temperatureSensorCount];
|
RTC_DATA_ATTR Temperature lastTemperatures[temperatureSensorCount];
|
||||||
RTC_DATA_ATTR uint32_t lastValidTemperatureTime[temperatureSensorCount];
|
RTC_DATA_ATTR uint32_t lastValidTemperatureTime[temperatureSensorCount];
|
||||||
RTC_DATA_ATTR uint8_t lastMeasurements[temperatureSensorCount] = {0, 0};
|
RTC_DATA_ATTR uint8_t lastMeasurements[temperatureSensorCount] = {0, 0};
|
||||||
|
|
||||||
bool didSetupEEPROM = false;
|
void setStorageFlag(StorageFlags flag) {
|
||||||
|
storageFlags |= static_cast<uint8_t>(flag);
|
||||||
|
}
|
||||||
|
|
||||||
// On first boot, this is set to true; Afterwards it's remembered to be false
|
void clearStorageFlag(StorageFlags flag) {
|
||||||
RTC_DATA_ATTR bool eepromIsConsideredEmpty = true;
|
storageFlags &= ~static_cast<uint8_t>(flag);
|
||||||
|
}
|
||||||
|
|
||||||
constexpr uint16_t eepromOffset = sizeof(uint16_t);
|
bool hasStorageFlag(StorageFlags flag) {
|
||||||
constexpr uint16_t eepromDataSize = eepromSize - eepromOffset;
|
return storageFlags & static_cast<uint8_t>(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool hasInitializedEEPROM() {
|
||||||
|
return hasStorageFlag(StorageFlags::HasInitializedEEPROM);
|
||||||
|
}
|
||||||
|
|
||||||
|
void didInitializeEEPROM() {
|
||||||
|
setStorageFlag(StorageFlags::HasInitializedEEPROM);
|
||||||
|
}
|
||||||
|
|
||||||
void setupEEPROMIfNeeded() {
|
void setupEEPROMIfNeeded() {
|
||||||
if (didSetupEEPROM) {
|
if (hasInitializedEEPROM()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bool success = EEPROM.begin(eepromSize);
|
bool success = EEPROM.begin(eepromSize);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
Serial.println("Failed to set up EEPROM");
|
Serial.println("Failed to set up EEPROM");
|
||||||
didSetupEEPROM = false;
|
setStorageFlag(StorageFlags::FailedToInitEEPROM);
|
||||||
}
|
}
|
||||||
Serial.print("EEPROM size: ");
|
Serial.print("EEPROM size: ");
|
||||||
Serial.println(eepromSize);
|
Serial.println(eepromSize);
|
||||||
didSetupEEPROM = true;
|
clearStorageFlag(StorageFlags::FailedToInitEEPROM);
|
||||||
|
setStorageFlag(StorageFlags::HasInitializedEEPROM);
|
||||||
}
|
}
|
||||||
|
|
||||||
void resetLastMeasurements() {
|
void resetLastMeasurements() {
|
||||||
@ -59,29 +74,18 @@ void storageConfigure(bool isFirstRun) {
|
|||||||
// Ensure that first values are stored
|
// Ensure that first values are stored
|
||||||
resetLastMeasurements();
|
resetLastMeasurements();
|
||||||
}
|
}
|
||||||
}
|
// Initialize EEPROM on every wake
|
||||||
|
// setupEEPROMIfNeeded();
|
||||||
uint16_t getNumberOfBytesStoredInEEPROM() {
|
//setStorageFlag(StorageFlags::HasInitializedEEPROM);
|
||||||
if (eepromIsConsideredEmpty) {
|
|
||||||
return 0; // Discard any previous bytes
|
|
||||||
}
|
|
||||||
setupEEPROMIfNeeded();
|
|
||||||
return EEPROM.readShort(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveDataToEEPROMIfNeeded() {
|
void moveDataToEEPROMIfNeeded() {
|
||||||
if (dataIndex < eepromChunkSize) {
|
if (dataIndex < rtcStorageSize) {
|
||||||
return; // Wait until more bytes are available
|
return;
|
||||||
}
|
}
|
||||||
// Determine EEPROM start address
|
|
||||||
uint16_t eepromIndex = getNumberOfBytesStoredInEEPROM() + eepromOffset;
|
|
||||||
if (eepromIndex >= eepromDataSize) {
|
|
||||||
return; // No more space in EEPROM, keep filling RTC memory
|
|
||||||
}
|
|
||||||
// Preparing to write new data, so set to false
|
|
||||||
eepromIsConsideredEmpty = false;
|
|
||||||
setupEEPROMIfNeeded();
|
setupEEPROMIfNeeded();
|
||||||
if (!didSetupEEPROM) {
|
if (!hasInitializedEEPROM()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +93,6 @@ void moveDataToEEPROMIfNeeded() {
|
|||||||
uint16_t bytesRemaining = eepromDataSize - eepromIndex;
|
uint16_t bytesRemaining = eepromDataSize - eepromIndex;
|
||||||
uint16_t bytesToWrite = min(dataIndex, bytesRemaining);
|
uint16_t bytesToWrite = min(dataIndex, bytesRemaining);
|
||||||
EEPROM.writeBytes(eepromIndex, data, bytesToWrite); // TODO: Check that result == bytesToWrite
|
EEPROM.writeBytes(eepromIndex, data, bytesToWrite); // TODO: Check that result == bytesToWrite
|
||||||
EEPROM.writeShort(0, eepromIndex + bytesToWrite);
|
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
|
|
||||||
// Move remaining data to front of array (if any)
|
// Move remaining data to front of array (if any)
|
||||||
@ -99,8 +102,13 @@ void moveDataToEEPROMIfNeeded() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void saveByteAtCurrentIndex(uint8_t byte) {
|
void saveByteAtCurrentIndex(uint8_t byte) {
|
||||||
|
if (dataIndex >= rtcStorageSize) {
|
||||||
|
setStorageFlag(StorageFlags::RTCStorageOverflow);
|
||||||
|
return;
|
||||||
|
}
|
||||||
data[dataIndex] = byte;
|
data[dataIndex] = byte;
|
||||||
dataIndex += 1;
|
dataIndex += 1;
|
||||||
|
//moveDataToEEPROMIfNeeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t byteForAbsoluteTemperature(Temperature* temp) {
|
uint8_t byteForAbsoluteTemperature(Temperature* temp) {
|
||||||
@ -199,13 +207,11 @@ void saveTemperatures(Temperature* temperatures) {
|
|||||||
saveByteAtCurrentIndex(valueToStore);
|
saveByteAtCurrentIndex(valueToStore);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
moveDataToEEPROMIfNeeded();
|
|
||||||
numberOfMeasurements += 1;
|
numberOfMeasurements += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t getTotalNumberOfStoredBytes() {
|
uint16_t getTotalNumberOfStoredBytes() {
|
||||||
return getNumberOfBytesStoredInEEPROM() + dataIndex;
|
return dataIndex; // + eepromIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t getNumberOfMeasurements() {
|
uint16_t getNumberOfMeasurements() {
|
||||||
@ -225,8 +231,14 @@ uint16_t getTimeSinceValidTemperature(uint8_t sensorIndex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint16_t getRecordedBytesAtOffset(uint8_t* buffer, uint16_t offset, uint16_t count) {
|
uint16_t getRecordedBytesAtOffset(uint8_t* buffer, uint16_t offset, uint16_t count) {
|
||||||
|
// Copy remaining bytes from RTC memory
|
||||||
|
uint16_t remainingBytes = dataIndex - offset;
|
||||||
|
uint16_t bytesToCopy = min(count, remainingBytes);
|
||||||
|
memcpy(buffer, data + offset, count);
|
||||||
|
return bytesToCopy;
|
||||||
|
/*
|
||||||
// TODO: Check limits
|
// TODO: Check limits
|
||||||
uint16_t eepromByteCount = getNumberOfBytesStoredInEEPROM();
|
uint16_t eepromByteCount = eepromIndex;
|
||||||
uint16_t endIndex = offset + count;
|
uint16_t endIndex = offset + count;
|
||||||
uint16_t eepromStart = min(offset, eepromByteCount);
|
uint16_t eepromStart = min(offset, eepromByteCount);
|
||||||
uint16_t eepromEnd = min(endIndex, eepromByteCount);
|
uint16_t eepromEnd = min(endIndex, eepromByteCount);
|
||||||
@ -242,11 +254,16 @@ uint16_t getRecordedBytesAtOffset(uint8_t* buffer, uint16_t offset, uint16_t cou
|
|||||||
uint16_t bytesToCopyFromRTC = min(count, dataIndex);
|
uint16_t bytesToCopyFromRTC = min(count, dataIndex);
|
||||||
memcpy(buffer + bytesToCopyFromEEPROM, data + offset, bytesToCopyFromRTC);
|
memcpy(buffer + bytesToCopyFromEEPROM, data + offset, bytesToCopyFromRTC);
|
||||||
return bytesToCopyFromEEPROM + bytesToCopyFromRTC;
|
return bytesToCopyFromEEPROM + bytesToCopyFromRTC;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void discardAllRecordedBytes() {
|
void discardAllRecordedBytes() {
|
||||||
eepromIsConsideredEmpty = true;
|
eepromIndex = 0;
|
||||||
dataIndex = 0;
|
dataIndex = 0;
|
||||||
numberOfDiscardedMeasurements += numberOfMeasurements;
|
numberOfDiscardedMeasurements += numberOfMeasurements;
|
||||||
numberOfMeasurements = 0;
|
numberOfMeasurements = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t getStorageErrorFlags() {
|
||||||
|
return storageFlags;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user