Skip to content

Commit

Permalink
Merge pull request #313 from boblemaire/Alternate-RTC
Browse files Browse the repository at this point in the history
Alternate rtc
  • Loading branch information
boblemaire authored Sep 25, 2021
2 parents b724730 + 6a39140 commit 4b021e3
Show file tree
Hide file tree
Showing 8 changed files with 1,252 additions and 126 deletions.
143 changes: 72 additions & 71 deletions Firmware/IotaWatt/IotaLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ int IotaLog::begin (const char* path ){
return 2;
}
IotaFile.close();
}
IotaFile = SD.open(_path, FILE_WRITE);
if(!IotaFile){
return 2;
}
_fileSize = _physicalSize = IotaFile.size();
}
IotaFile = SD.open(_path, FILE_WRITE);
if(!IotaFile){
return 2;
}
_fileSize = _physicalSize = IotaFile.size();

if(_fileSize){
IotaFile.seek(0);
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
_firstKey = recordKey.UNIXtime;
_firstSerial = recordKey.serial;
IotaFile.seek(_fileSize - _recordSize);
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
_lastKey = recordKey.UNIXtime;
_lastSerial = recordKey.serial;
_entries = _fileSize / _recordSize;
}
if(_fileSize){
IotaFile.seek(0);
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
_firstKey = recordKey.UNIXtime;
_firstSerial = recordKey.serial;
IotaFile.seek(_fileSize - _recordSize);
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
_lastKey = recordKey.UNIXtime;
_lastSerial = recordKey.serial;
_entries = _fileSize / _recordSize;
}

// If there are trailing zero recordKeys at the end,
// try to adjust _filesize down to match logical end of file.
// If there are trailing zero recordKeys at the end,
// try to adjust _filesize down to match logical end of file.

while(_fileSize && _lastSerial == 0){
IotaLogRecord* logRec = new IotaLogRecord;
Expand All @@ -61,11 +61,8 @@ int IotaLog::begin (const char* path ){
_entries--;
delete logRec;
}
if(_fileSize != _physicalSize){
Serial.printf("physical %d, logical %d\r\n", _physicalSize, _fileSize);
}

if(_firstKey > _lastKey){
if(_firstKey > _lastKey){
_wrap = findWrap(0,_firstKey, _fileSize - _recordSize, _lastKey);
IotaFile.seek(_wrap);
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
Expand All @@ -75,13 +72,13 @@ int IotaLog::begin (const char* path ){
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
_lastKey = recordKey.UNIXtime;
_lastSerial = recordKey.serial;
}
}

_lastReadKey = _firstKey;
_lastReadSerial = _firstSerial;
_maxFileSize = max(_fileSize, _maxFileSize);
if(((int32_t) _lastSerial - _firstSerial + 1) != _entries){
_lastReadKey = _firstKey;
_lastReadSerial = _firstSerial;
_maxFileSize = max(_fileSize, _maxFileSize);
if(((int32_t) _lastSerial - _firstSerial + 1) != _entries){
log("IotaLog: file damaged %s\r\n", _path);
log("IotaLog: Creating diagnostic file.");
dumpFile();
Expand All @@ -90,39 +87,43 @@ int IotaLog::begin (const char* path ){
SD.remove(_path);
ESP.restart();
}

for(int i=0; i<_cacheSize; i++){
_cacheKey[i] = _firstKey;
_cacheSerial[i] = _firstSerial;
}

return 0;
return 0;
}

uint32_t IotaLog::findWrap(uint32_t highPos, uint32_t highKey, uint32_t lowPos, uint32_t lowKey){
struct {
uint32_t UNIXtime;
uint32_t serial;
} recordKey;
if((lowPos - highPos) == _recordSize) {
return lowPos;
}
uint32_t midPos = (highPos + lowPos) / 2;
midPos += midPos % _recordSize;
IotaFile.seek(midPos);
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
uint32_t midKey = recordKey.UNIXtime;
if(midKey > highKey){
return findWrap(midPos, midKey, lowPos, lowKey);
}
return findWrap(highPos, highKey, midPos, midKey);
struct {
uint32_t UNIXtime;
uint32_t serial;
} recordKey;
if((lowPos - highPos) == _recordSize) {
return lowPos;
}
uint32_t midPos = (highPos + lowPos) / 2;
midPos += midPos % _recordSize;
IotaFile.seek(midPos);
IotaFile.read((uint8_t*)&recordKey, sizeof(recordKey));
uint32_t midKey = recordKey.UNIXtime;
if(midKey > highKey){
return findWrap(midPos, midKey, lowPos, lowKey);
}
return findWrap(highPos, highKey, midPos, midKey);
}

int IotaLog::readKey (IotaLogRecord* callerRecord){
uint32_t key = callerRecord->UNIXtime - (callerRecord->UNIXtime % _interval);
if(!IotaFile) return 2;
if(_entries == 0) return 1;
if(!IotaFile){
return 2;
}
if(_entries == 0){
return 1;
}
if(key < _firstKey){ // Before the beginning of time
readSerial(callerRecord, _firstSerial);
callerRecord->UNIXtime = key;
Expand All @@ -134,7 +135,7 @@ int IotaLog::readKey (IotaLogRecord* callerRecord){
if(key = _lastKey) return 0;
return 1;
}
//Serial.printf("search %d, highKey %d\r\n", key, _firstKey);

uint32_t lowKey = _firstKey;
int32_t lowSerial = _firstSerial;
uint32_t highKey = _lastKey;
Expand Down Expand Up @@ -169,38 +170,38 @@ int IotaLog::readKey (IotaLogRecord* callerRecord){

void IotaLog::searchKey(IotaLogRecord* callerRecord, const uint32_t key, const uint32_t lowKey, const int32_t lowSerial, const uint32_t highKey, const int32_t highSerial){

int32_t floorSerial = max(lowSerial, highSerial - (int32_t)((highKey - key) / _interval));
int32_t floorSerial = max(lowSerial, highSerial - (int32_t)((highKey - key) / _interval));
int32_t ceilingSerial = min(highSerial, lowSerial + (int32_t)((key - lowKey) / _interval));

//Serial.printf("low %d(%d), high %d(%d), floor %d, Ceiling %d\r\n", lowKey, lowSerial, highKey, highSerial,floorSerial, ceilingSerial);

if(ceilingSerial < highSerial || floorSerial == ceilingSerial){
if(ceilingSerial < highSerial || floorSerial == ceilingSerial){
readSerial(callerRecord, ceilingSerial);

if(callerRecord->UNIXtime == key){
return;
}
searchKey(callerRecord, key, lowKey, lowSerial, callerRecord->UNIXtime, callerRecord->serial);
return;
}
if(floorSerial > lowSerial){
}
if(floorSerial > lowSerial){
readSerial(callerRecord, floorSerial);
if(callerRecord->UNIXtime == key){
return;
}
searchKey(callerRecord, key, callerRecord->UNIXtime, callerRecord->serial, highKey, highSerial);
return;
}
if((highSerial - lowSerial) <= 1){
}
if((highSerial - lowSerial) <= 1){
readSerial(callerRecord, lowSerial);
return;
}
readSerial(callerRecord, (lowSerial + highSerial) / 2);
_readKeyIO++;
if(callerRecord->UNIXtime == key){
}
readSerial(callerRecord, (lowSerial + highSerial) / 2);
_readKeyIO++;
if(callerRecord->UNIXtime == key){
return;
}
if(callerRecord->UNIXtime < key){
}
if(callerRecord->UNIXtime < key){
searchKey(callerRecord, key, callerRecord->UNIXtime, callerRecord->serial, highKey, highSerial);
return;
}
Expand All @@ -209,19 +210,19 @@ void IotaLog::searchKey(IotaLogRecord* callerRecord, const uint32_t key, const u
}

int IotaLog::readNext(IotaLogRecord* callerRecord){
if(!IotaFile) return 2;
if(callerRecord->serial == _lastSerial) return 1;
return readSerial(callerRecord, callerRecord->serial + 1);
if(!IotaFile) return 2;
if(callerRecord->serial == _lastSerial) return 1;
return readSerial(callerRecord, callerRecord->serial + 1);
}

int IotaLog::end(){
IotaFile.close();
return 0;
IotaFile.close();
return 0;
}

boolean IotaLog::isOpen(){
if(IotaFile) return true;
return false;
if(IotaFile) return true;
return false;
}

uint32_t IotaLog::firstKey(){return _firstKey;}
Expand Down
13 changes: 11 additions & 2 deletions Firmware/IotaWatt/IotaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ Script::Script(JsonObject& JsonScript)

var = JsonScript["script"];
if(var.success()){
encodeScript(var.as<char*>());
if( ! encodeScript(var.as<char*>())){
log("Script: invalid Script in output %s.", _name);
}
}
}

Expand Down Expand Up @@ -187,9 +189,16 @@ bool Script::encodeScript(const char* script){

// Operator

else {
else if(strchr(opChars, script[j])){
_tokens[i++] = tokenOperator + strchr(opChars, script[j++]) - opChars;
}

// invalid token/script

else {
_tokens[0] = opEq;
return false;
}
}
_tokens[i] = 0;
return true;
Expand Down
4 changes: 2 additions & 2 deletions Firmware/IotaWatt/IotaWatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include <asyncHTTPrequest.h>

#include <SPI.h>
#include <RTClib.h>
#include <RTC.h>
#include <SD.h>
#include <Wire.h>
#include <ArduinoJson.h>
Expand Down Expand Up @@ -90,7 +90,7 @@ extern MDNSResponder MDNS;
extern IotaLog Current_log;
extern IotaLog History_log;
extern IotaLog *Export_log;
extern RTC_PCF8523 rtc;
extern RTC rtc;
extern Ticker Led_timer;
extern messageLog Message_log;

Expand Down
Loading

0 comments on commit 4b021e3

Please sign in to comment.