Skip to content

Commit

Permalink
Add debugging to the ring buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Feb 15, 2016
1 parent 5185a0c commit 1bfa368
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 38 deletions.
2 changes: 1 addition & 1 deletion DMRIPSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ m_pingTimer(1000U, 5U),
m_buffer(NULL),
m_salt(NULL),
m_streamId(NULL),
m_rxData(1000U),
m_rxData(1000U, "DMR IPSC"),
m_callsign(),
m_rxFrequency(0U),
m_txFrequency(0U),
Expand Down
2 changes: 1 addition & 1 deletion DMRSlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool CDMRSlot::m_voice2 = true;

CDMRSlot::CDMRSlot(unsigned int slotNo, unsigned int timeout) :
m_slotNo(slotNo),
m_queue(1000U),
m_queue(1000U, "DMR Slot"),
m_state(RS_LISTENING),
m_embeddedLC(),
m_lc(NULL),
Expand Down
2 changes: 1 addition & 1 deletion DStarControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ m_gateway(NULL),
m_network(network),
m_display(display),
m_duplex(duplex),
m_queue(1000U),
m_queue(1000U, "D-Star Control"),
m_header(),
m_state(RS_LISTENING),
m_net(false),
Expand Down
2 changes: 1 addition & 1 deletion DStarNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ m_enabled(false),
m_outId(0U),
m_outSeq(0U),
m_inId(0U),
m_buffer(1000U),
m_buffer(1000U, "D-Star Network"),
m_pollTimer(1000U, 60U),
m_linkStatus(LS_NONE),
m_linkReflector(NULL)
Expand Down
16 changes: 8 additions & 8 deletions Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ m_dmrEnabled(false),
m_ysfEnabled(false),
m_serial(port, SERIAL_115200, true),
m_buffer(NULL),
m_rxDStarData(1000U),
m_txDStarData(1000U),
m_rxDMRData1(1000U),
m_rxDMRData2(1000U),
m_txDMRData1(1000U),
m_txDMRData2(1000U),
m_rxYSFData(1000U),
m_txYSFData(1000U),
m_rxDStarData(1000U, "Modem RX D-Star"),
m_txDStarData(1000U, "Modem TX D-Star"),
m_rxDMRData1(1000U, "Modem RX DMR1"),
m_rxDMRData2(1000U, "Modem RX DMR2"),
m_txDMRData1(1000U, "Modem TX DMR1"),
m_txDMRData2(1000U, "Modem TX DMR2"),
m_rxYSFData(1000U, "Modem RX YSF"),
m_txYSFData(1000U, "Modem TX YSF"),
m_statusTimer(1000U, 0U, 100U),
m_dstarSpace(0U),
m_dmrSpace1(0U),
Expand Down
66 changes: 41 additions & 25 deletions RingBuffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2006-2009,2012,2013,2015 by Jonathan Naylor G4KLX
* Copyright (C) 2006-2009,2012,2013,2015,2016 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,18 +19,24 @@
#ifndef RingBuffer_H
#define RingBuffer_H

#include "Log.h"

#include <cstdio>
#include <cassert>
#include <cstring>

template<class T> class CRingBuffer {
public:
CRingBuffer(unsigned int length) :
CRingBuffer(unsigned int length, const char* name) :
m_length(length),
m_name(name),
m_buffer(NULL),
m_iPtr(0U),
m_oPtr(0U)
m_oPtr(0U),
m_full(false)
{
assert(length > 0U);
assert(name != NULL);

m_buffer = new T[length];

Expand All @@ -42,19 +48,24 @@ template<class T> class CRingBuffer {
delete[] m_buffer;
}

unsigned int addData(const T* buffer, unsigned int nSamples)
bool addData(const T* buffer, unsigned int nSamples)
{
if (nSamples > freeSpace())
return 0U;

for (unsigned int i = 0U; i < nSamples; i++) {
if (m_full) {
LogError("**** Overflow in %s ring buffer", m_name);
return false;
}

m_buffer[m_iPtr++] = buffer[i];

if (m_iPtr == m_length)
m_iPtr = 0U;

if (m_iPtr == m_oPtr)
m_full = true;
}

return nSamples;
return true;
}

unsigned int getData(T* buffer, unsigned int nSamples)
Expand All @@ -65,6 +76,8 @@ template<class T> class CRingBuffer {
nSamples = data;

for (unsigned int i = 0U; i < nSamples; i++) {
m_full = false;

buffer[i] = m_buffer[m_oPtr++];

if (m_oPtr == m_length)
Expand Down Expand Up @@ -94,21 +107,30 @@ template<class T> class CRingBuffer {

void clear()
{
m_iPtr = 0U;
m_oPtr = 0U;
m_iPtr = 0U;
m_oPtr = 0U;
m_full = false;

::memset(m_buffer, 0x00, m_length * sizeof(T));
}

unsigned int freeSpace() const
{
if (m_oPtr == m_iPtr)
return m_length - 1U;
if (m_oPtr == m_iPtr && !m_full)
return m_length;

if (m_oPtr == m_iPtr && m_full)
return 0U;

if (m_iPtr > m_oPtr)
return m_iPtr - m_oPtr;

if (m_oPtr > m_iPtr)
return m_oPtr - m_iPtr - 1U;
return (m_length + m_iPtr) - m_oPtr;
}

return m_length - (m_iPtr - m_oPtr) - 1U;
unsigned int dataSize() const
{
return m_length - freeSpace();
}

bool hasSpace(unsigned int length) const
Expand All @@ -118,27 +140,21 @@ template<class T> class CRingBuffer {

bool hasData() const
{
return m_oPtr != m_iPtr;
return m_oPtr != m_iPtr || (m_oPtr == m_iPtr && m_full);
}

bool isEmpty() const
{
return m_oPtr == m_iPtr;
return m_oPtr == m_iPtr && !m_full;
}

private:
unsigned int m_length;
const char* m_name;
T* m_buffer;
volatile unsigned int m_iPtr;
volatile unsigned int m_oPtr;

unsigned int dataSize() const
{
if (m_iPtr >= m_oPtr)
return m_iPtr - m_oPtr;

return m_length - (m_oPtr - m_iPtr);
}
volatile bool m_full;
};

#endif
2 changes: 1 addition & 1 deletion YSFEcho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "YSFDefines.h"

CYSFEcho::CYSFEcho(unsigned int delay, unsigned int space) :
m_buffer(space),
m_buffer(space, "YSF Echo"),
m_timer(1000U, delay)
{
}
Expand Down

0 comments on commit 1bfa368

Please sign in to comment.