initial files checked in
This commit is contained in:
113
libraries/Misc/circular_buffer.h
Normal file
113
libraries/Misc/circular_buffer.h
Normal file
@ -0,0 +1,113 @@
|
||||
#ifndef circ_buffer_h
|
||||
#define circ_buffer_h
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/// <summary>
|
||||
/// This class allows a fixed size circular buffer.
|
||||
/// When push_back is called, oldest data is overwritten.
|
||||
/// Does not use any dynamic allocators.
|
||||
/// </summary>
|
||||
template <class ItemType, int elementCnt> class circular_buffer
|
||||
{
|
||||
private:
|
||||
ItemType m_arr[elementCnt];
|
||||
int m_writePos;
|
||||
int m_size;
|
||||
|
||||
void advanceWritePos()
|
||||
{
|
||||
if(m_size < elementCnt)
|
||||
{
|
||||
m_size++;
|
||||
}
|
||||
|
||||
m_writePos++;
|
||||
if(m_writePos >= elementCnt)
|
||||
{
|
||||
m_writePos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
circular_buffer(const circular_buffer<ItemType, elementCnt>& rhs);
|
||||
public:
|
||||
circular_buffer()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
void operator=(const circular_buffer<ItemType, elementCnt>& rhs)
|
||||
{
|
||||
memcpy(m_arr, rhs.m_arr, sizeof(m_arr));
|
||||
m_size = rhs.m_size;
|
||||
m_writePos = rhs.m_writePos;
|
||||
}
|
||||
|
||||
void push_back(const ItemType& item)
|
||||
{
|
||||
m_arr[m_writePos] = item;
|
||||
advanceWritePos();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
memset(m_arr,0,sizeof(m_arr));
|
||||
m_size = m_writePos = 0;
|
||||
}
|
||||
|
||||
void sort()
|
||||
{
|
||||
if (size() < 2)
|
||||
return;
|
||||
|
||||
bool swapped;
|
||||
do
|
||||
{
|
||||
swapped = false;
|
||||
for(int ix=0; ix<size()-1; ix++)
|
||||
{
|
||||
if(at(ix) > at(ix+1))
|
||||
{
|
||||
ItemType tmp = at(ix);
|
||||
at(ix) = at(ix+1);
|
||||
at(ix+1) = tmp;
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
}while(swapped);
|
||||
}
|
||||
|
||||
int size() const { return m_size; }
|
||||
bool isFull() const { return size() == elementCnt; }
|
||||
ItemType& operator[](int pos) {return at(pos);}
|
||||
|
||||
ItemType& at(int pos)
|
||||
{
|
||||
if(m_size < elementCnt)
|
||||
{
|
||||
return m_arr[pos];
|
||||
}
|
||||
|
||||
int readPos = m_writePos + pos;
|
||||
if(readPos >= elementCnt)
|
||||
{
|
||||
readPos -= elementCnt;
|
||||
}
|
||||
|
||||
return m_arr[readPos];
|
||||
}
|
||||
|
||||
#if _DEBUG_ENABLED
|
||||
void print()
|
||||
{
|
||||
printf("---\n");
|
||||
for(int i=0; i<size(); i++)
|
||||
{
|
||||
printf("%d = %d\n", i, at(i));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif //circ_buffer_h
|
||||
96
libraries/Misc/circular_log.h
Normal file
96
libraries/Misc/circular_log.h
Normal file
@ -0,0 +1,96 @@
|
||||
#ifndef circ_log_h
|
||||
#define circ_log_h
|
||||
|
||||
#include <TimeLib.h> //https://github.com/PaulStoffregen/Time
|
||||
#include <stdlib.h>
|
||||
|
||||
template <int elementCnt> class circular_log
|
||||
{
|
||||
private:
|
||||
char m_log[elementCnt];
|
||||
|
||||
bool removeLastFromLog()
|
||||
{
|
||||
char* nextLine = strstr(m_log+1, "<BR>");
|
||||
if(nextLine == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int newLineLen = strlen(nextLine);
|
||||
memmove(m_log, nextLine, newLineLen);
|
||||
m_log[newLineLen] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
circular_log()
|
||||
{
|
||||
memset(m_log, 0, sizeof(m_log));
|
||||
}
|
||||
|
||||
const char* c_str() const { return m_log; }
|
||||
int freeSpace() const { return elementCnt - strlen(m_log) - 1; }
|
||||
|
||||
void LogXml(const char* msg)
|
||||
{
|
||||
char szNew[256] = "";
|
||||
snprintf(szNew, sizeof(szNew)-1, "<BR>%02d - %02d:%02d | ", day(), hour(), minute());
|
||||
|
||||
int ix = strlen(szNew);
|
||||
while(*msg != '\0' && ix < 250)
|
||||
{
|
||||
if(*msg == '<')
|
||||
{
|
||||
szNew[ix++] = '&';
|
||||
szNew[ix++] = 'l';
|
||||
szNew[ix++] = 't';
|
||||
szNew[ix++] = ';';
|
||||
}
|
||||
else if(*msg == '>')
|
||||
{
|
||||
szNew[ix++] = '&';
|
||||
szNew[ix++] = 'g';
|
||||
szNew[ix++] = 't';
|
||||
szNew[ix++] = ';';
|
||||
}
|
||||
else
|
||||
{
|
||||
szNew[ix++] = *msg;
|
||||
}
|
||||
|
||||
msg++;
|
||||
}
|
||||
|
||||
const int newLen = strlen(szNew);
|
||||
while(freeSpace() < newLen)
|
||||
{
|
||||
if(removeLastFromLog() == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
strcat(m_log, szNew);
|
||||
}
|
||||
|
||||
void Log(const char* msg)
|
||||
{
|
||||
char szNew[256] = "";
|
||||
snprintf(szNew, sizeof(szNew)-1, "<BR>%02d - %02d:%02d | %s", day(), hour(), minute(), msg);
|
||||
|
||||
const int newLen = strlen(szNew);
|
||||
while(freeSpace() < newLen)
|
||||
{
|
||||
if(removeLastFromLog() == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
strcat(m_log, szNew);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //circular_log_h
|
||||
Reference in New Issue
Block a user