diff --git a/src/main.cpp b/src/main.cpp index c9b4630..4db8d36 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,9 +4,13 @@ #include #include #include +#include RtcDS3231 Rtc(Wire); -HX711 scale; +EepromAt24c32 RtcEeprom(Wire);HX711 scale; + +// void printDateTime(const RtcDateTime &dt); + const int rtcPowerPin = 4; const int wakeUpPin = 7; @@ -19,6 +23,10 @@ const int scalePowerPin = 19; const int secondsTillNextWakeup = 3; const long interval = 50; +const int memTotal = 4096; +const int pageSize = 16; +const int pageCount = (memTotal / pageSize) - 1; + void wake() { sleep_disable(); @@ -51,6 +59,7 @@ void setup() digitalWrite(scalePowerPin, HIGH); Rtc.Begin(); + RtcEeprom.Begin(); scale.begin(scaleDataPin, scaleSckPin); scale.set_scale(1000.f); // this value is obtained by calibrating the scale with known weights; see the README for details @@ -118,12 +127,89 @@ void loop() RtcTemperature temp = Rtc.GetTemperature(); + int lastA = -1; + int lastB = -1; + + for (int i = 0; i <= pageCount; i++) + { + uint8_t x = RtcEeprom.GetMemory(i * pageSize); + + if ((char)x == 'a') + { + lastA = i; + } + + if ((char)x == 'b') + { + lastB = i; + } + } + + int head = -1; + uint8_t next = 'c'; + + if (lastA == pageCount) + { + head = pageCount; + next = 'b'; + } + + if (lastB == pageCount) + { + head = pageCount; + next = 'a'; + } + + if (lastA > lastB) + { + head = lastB; + next = 'b'; + } + + if (lastB > lastA) + { + head = lastA; + next = 'a'; + } + + head = (head + 1) % pageCount; + + // Serial.print("Last a: "); + // Serial.print(lastA); + // Serial.print(", Last b: "); + // Serial.print(lastB); + // Serial.print(", HEAD: "); + // Serial.print(head); + // Serial.print(", next: "); + // Serial.println((char)next); + String buffer; + buffer += String((char)next); buffer += String(scale.get_units(1), 2); buffer += F(" kg, "); buffer += String(temp.AsFloatDegC(), 2); buffer += F(" °C"); - Serial.println(buffer); + // Serial.println(buffer); + char data[pageSize]; + buffer.toCharArray(data, pageSize); + + RtcEeprom.SetMemory(head * pageSize, (const uint8_t *)data, sizeof(data) - 1); + + for (int i = 0; i <= pageCount; i++) + { + uint8_t buff[pageSize + 1]; + uint8_t gotten = RtcEeprom.GetMemory(i * pageSize, buff, pageSize); + Serial.print(i); + Serial.print(":\t"); + Serial.print("data read ("); + Serial.print(gotten); + Serial.print(") = \""); + for (uint8_t ch = 0; ch < gotten; ch++) + { + Serial.print((char)buff[ch]); + } + Serial.println("\""); + } Serial.flush(); scale.power_down(); @@ -136,3 +222,6 @@ void loop() sleepNow(); } + + +/ \ No newline at end of file