Skip to content

Commit

Permalink
Addition of getRows(n, k) and getNumberOfRows(n) to the datalogger. (#…
Browse files Browse the repository at this point in the history
…431)

* MicroBitLog.cpp & .h: Added getRows(n, k) and getNumberOfRows(n).

* MicroBitLog: getNumberOfRows(n): Now returns 0 if n > number of rows.

* MicroBitLog: getNumberOfRows(n): Fixed bug associated with last commit behaviour change.

* MicroBitLog: getNumberOfRows(n): Documentation of code change for new behaviour

* MicroBitLog: getNumberOfRows(n): misname oops
  • Loading branch information
KierPalin authored Jul 16, 2024
1 parent 9579fab commit 96faa85
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
15 changes: 15 additions & 0 deletions inc/MicroBitLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ namespace codal
*/
int readData(void *data, uint32_t index, uint32_t len, DataFormat format, uint32_t length);

/**
* Get the number of rows (including the header) in the datalogger.
* @param fromRowIndex 0-based index of starting row: bumped up to 0 if negative.
* @return number of rows + header.
*/
uint32_t getNumberOfRows(uint32_t fromRowIndex = 0);

/**
* Get n rows worth of logged data as a ManagedString.
* @param fromRowIndex 0-based index of starting row.
* @param nRows number of rows to get from fromRowIndex.
* @return ManagedString between the parameter range, each row separated by a newline, each column by a comma.
*/
ManagedString getRows(uint32_t fromRowIndex, int nRows);

private:

/**
Expand Down
89 changes: 89 additions & 0 deletions source/MicroBitLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,95 @@ int MicroBitLog::_readSource( uint8_t *&data, uint32_t &index, uint32_t &len, ui
return r;
}

/**
* Get the number of rows (including the header) in the datalogger.
* @param fromRowIndex 0-based index of starting row: bumped up to 0 if negative.
* @return number of rows + header.
*/
uint32_t MicroBitLog::getNumberOfRows(uint32_t fromRowIndex)
{
constexpr uint8_t rowSeparator = 10; // newline char
uint32_t rowCount = 0;

uint32_t end = dataStart;
bool startRowFound = (fromRowIndex == 0) ? true : false;

// Read read until we see a 0xFF character (unused memory)
uint8_t c = 0;
while(c != 0xff)
{
cache.read(end, &c, 1);
if (c == rowSeparator) {
rowCount++;
if (!startRowFound && fromRowIndex == rowCount) {
startRowFound = true;
rowCount = 0;
}
}
end++;
}

// Will be the case if fromRowIndex is beyond the number of rows:
if (!startRowFound)
return 0;
return rowCount;
}

/**
* Get n rows worth of logged data as a ManagedString.
* @param fromRowIndex 0-based index of starting row.
* @param nRows number of rows to get from fromRowIndex.
* @return ManagedString between the parameter range, each row separated by a newline, each column by a comma.
*/
ManagedString MicroBitLog::getRows(uint32_t fromRowIndex, int nRows)
{
if (fromRowIndex >= dataEnd || nRows <= 0)
return ManagedString("", 0);

constexpr uint8_t rowSeparator = 10; // newline char in asci
const uint32_t rowSeparatorTargetCount = fromRowIndex + nRows;

uint32_t startOfRowN = dataStart;
uint32_t endOfDataChunk = dataEnd;

uint32_t end = dataStart;
uint32_t rowSeparatorCount = 0;
bool startRowFound = (fromRowIndex == 0) ? true : false;

// Read until we see a 0xFF character (unused memory)
uint8_t c = 0;
while(c != 0xff)
{
cache.read(end, &c, 1);

if (c == rowSeparator) {
rowSeparatorCount++;
if (!startRowFound && rowSeparatorCount == fromRowIndex)
{
startRowFound = true;
startOfRowN = end + 1;
}

else if (rowSeparatorCount == rowSeparatorTargetCount)
{
endOfDataChunk = end;
break;
}
}

end++;
}

// fromRowIndex was beyond the datalogger:
if (!startRowFound)
return ManagedString("", 0);

const int dataLength = endOfDataChunk - startOfRowN;
char rows[dataLength];
cache.read(startOfRowN, rows, dataLength);
return ManagedString(rows, dataLength);
}

/**
* Destructor.
*/
Expand Down

0 comments on commit 96faa85

Please sign in to comment.