- refactored

git-svn-id: http://moon:8086/svn/software/trunk/libsrc/cpp@891 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2022-05-30 07:02:43 +00:00
parent dcd4bfefc7
commit 1c8b02b279
3 changed files with 67 additions and 54 deletions
+48 -39
View File
@@ -2,6 +2,8 @@
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <algorithm>
#include <crc/crc.h>
#include <radio/symbol.h>
#include "Formatter.hpp"
@@ -27,46 +29,24 @@ Formatter::~Formatter()
{
}
void Formatter::idle()
{
if (m_dataCount)
{
// Commit partial DATA frame
commit(FRAME_TYPE_DATA, m_dataCount);
m_dataCount = 0;
}
else
{
// IDLE frame not full
if (m_idleCount < FRAME_NUM_BYTES_PER_FRAME)
{
m_frame.data[m_idleCount++] = 0;
}
if (m_idleCount == FRAME_NUM_BYTES_PER_FRAME)
{
// Commit full IDLE frame of size FRAME_NUM_BYTES_PER_FRAME
commit(FRAME_TYPE_IDLE, m_idleCount);
m_idleCount = 0;
}
}
}
void Formatter::process(uint8_t data)
void Formatter::process_data(uint8_t *pData, uint32_t len)
{
if (m_idleCount)
{
// Commit partial IDLE frame
// Idle in progress: Commit partial IDLE frame
commit(FRAME_TYPE_IDLE, m_idleCount);
m_idleCount = 0;
}
else
size_t remain = len;
while(remain)
{
// DATA frame not full
if (m_dataCount < FRAME_NUM_BYTES_PER_FRAME)
{
m_frame.data[m_dataCount++] = data;
}
size_t frame_remain = FRAME_NUM_BYTES_PER_FRAME - m_dataCount;
size_t to_write = std::min(remain, frame_remain);
memcpy(&m_frame.data[m_dataCount], pData, to_write);
m_dataCount += to_write;
remain -= to_write;
pData += to_write;
if (m_dataCount == FRAME_NUM_BYTES_PER_FRAME)
{
@@ -77,15 +57,44 @@ void Formatter::process(uint8_t data)
}
}
void Formatter::process_idle(uint32_t len)
{
if (m_dataCount)
{
// Commit partial DATA frame
commit(FRAME_TYPE_DATA, m_dataCount);
m_dataCount = 0;
}
size_t remain = len;
while(remain)
{
size_t frame_remain = FRAME_NUM_BYTES_PER_FRAME - m_dataCount;
size_t to_write = std::min(remain, frame_remain);
memset(&m_frame.data[m_dataCount], 0, to_write);
m_dataCount += to_write;
remain -= to_write;
if (m_idleCount == FRAME_NUM_BYTES_PER_FRAME)
{
// Commit full IDLE frame of size FRAME_NUM_BYTES_PER_FRAME
commit(FRAME_TYPE_IDLE, m_idleCount);
m_idleCount = 0;
}
}
}
void Formatter::process(void *pData, uint32_t len)
{
uint8_t *pByte;
pByte = (uint8_t*)pData;
while(len--)
if (pData)
{
process(*(pByte++));
// Data available: Process data
process_data((uint8_t*)pData, len);
}
else
{
// Data not available: Process Idle
process_idle(len);
}
}