From 4499db10bf4bd4deec275ad0bfdd71046d49b311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Mestre?= <36825825+SebastianMestre@users.noreply.github.com> Date: Sun, 30 Dec 2018 20:02:04 -0300 Subject: [PATCH] Isobufferbuffer integration (#62) * Update all code to use the new isoBufferBuffer interface, and remove the old one; also rename isoBufferBuffer::add(uint8_t) to insert_hex * remove unnecessary include directive from isobuffer.h * Remove 'Legacy Interface' comment from isoBufferBuffer header and implementation, move insert_hex closer to other insert functions in the header and implementation * Remove TODO comment of a finished task --- Desktop_Interface/i2cdecoder.cpp | 24 +++++++-------- Desktop_Interface/i2cdecoder.h | 2 +- Desktop_Interface/isobuffer.h | 1 - Desktop_Interface/isobufferbuffer.cpp | 41 +++++--------------------- Desktop_Interface/isobufferbuffer.h | 8 +---- Desktop_Interface/uartstyledecoder.cpp | 11 ++++--- 6 files changed, 25 insertions(+), 62 deletions(-) diff --git a/Desktop_Interface/i2cdecoder.cpp b/Desktop_Interface/i2cdecoder.cpp index b0aa1f1b..44b74851 100644 --- a/Desktop_Interface/i2cdecoder.cpp +++ b/Desktop_Interface/i2cdecoder.cpp @@ -32,8 +32,7 @@ void i2cDecoder::reset() { std::lock_guard lock(mutex); - delete serialBuffer; - serialBuffer = new isoBufferBuffer(I2C_BUFFER_LENGTH); + serialBuffer->clear(); } } @@ -150,15 +149,15 @@ void i2cDecoder::decodeAddress(edge sdaEdge, edge sclEdge) { qDebug() << "Finished Address Decode"; if (currentBitStream & 0b0000000000000010) - serialBuffer->add("READ: "); + serialBuffer->insert("READ: "); else - serialBuffer->add("WRITE: "); + serialBuffer->insert("WRITE: "); - serialBuffer->add((uint8_t)((currentBitStream & 0b0000000111111100) >> 2)); - serialBuffer->add(' '); + serialBuffer->insert_hex((uint8_t)((currentBitStream & 0b0000000111111100) >> 2)); + serialBuffer->insert(' '); if (currentBitStream & 0b0000000000000001) - serialBuffer->add("(NACK)"); + serialBuffer->insert("(NACK)"); consoleStateInvalid = true; @@ -183,11 +182,11 @@ void i2cDecoder::decodeData(edge sdaEdge, edge sclEdge) { qDebug() << "Finished Data byte Decode"; - serialBuffer->add((uint8_t)((currentBitStream & 0b0000000111111110) >> 1)); - serialBuffer->add(' '); + serialBuffer->insert_hex((uint8_t)((currentBitStream & 0b0000000111111110) >> 1)); + serialBuffer->insert(' '); if (currentBitStream & 0b0000000000000001) - serialBuffer->add("(NACK)"); + serialBuffer->insert("(NACK)"); consoleStateInvalid = true; @@ -208,7 +207,7 @@ void i2cDecoder::startCondition() void i2cDecoder::stopCondition() { state = transmissionState::idle; - serialBuffer->add('\n'); + serialBuffer->insert('\n'); qDebug() << "I2C STOP"; } @@ -217,8 +216,7 @@ void i2cDecoder::updateConsole(){ if (!consoleStateInvalid) return; - uint32_t numCharsInBuffer = serialBuffer->getNumCharsInBuffer(); - console->setPlainText(QString::fromLocal8Bit(serialBuffer->get(numCharsInBuffer), numCharsInBuffer)); + console->setPlainText(QString::fromLocal8Bit(serialBuffer->begin(), serialBuffer->size())); if(sda->serialAutoScroll){ QTextCursor c = console->textCursor(); c.movePosition(QTextCursor::End); diff --git a/Desktop_Interface/i2cdecoder.h b/Desktop_Interface/i2cdecoder.h index 253dc62e..326ad820 100644 --- a/Desktop_Interface/i2cdecoder.h +++ b/Desktop_Interface/i2cdecoder.h @@ -29,7 +29,7 @@ enum class edge: uint8_t constexpr uint8_t addressBitStreamLength = 9; constexpr uint8_t dataBitStreamLength = 9; -constexpr uint32_t I2C_BUFFER_LENGTH = 8192; +constexpr uint32_t I2C_BUFFER_LENGTH = 4096; class i2cDecoder : public QObject { diff --git a/Desktop_Interface/isobuffer.h b/Desktop_Interface/isobuffer.h index 6fd4e1af..3aad3dde 100644 --- a/Desktop_Interface/isobuffer.h +++ b/Desktop_Interface/isobuffer.h @@ -12,7 +12,6 @@ #include "xmega.h" #include "desktop_settings.h" -#include "isobufferbuffer.h" #include "genericusbdriver.h" class isoDriver; diff --git a/Desktop_Interface/isobufferbuffer.cpp b/Desktop_Interface/isobufferbuffer.cpp index e2d6e9fd..30b27eb1 100644 --- a/Desktop_Interface/isobufferbuffer.cpp +++ b/Desktop_Interface/isobufferbuffer.cpp @@ -22,12 +22,6 @@ * half of the allocated buffer, which is a notable improvement. */ - -// TODO: go through the usages of this class and: -// 1. adapt code to use the new interface and remove the old one -// 2. adjust the size of the requested buffer to accomodate -// the improved memory efficiency of the new algorithm - isoBufferBuffer::isoBufferBuffer(uint32_t length) : m_data(std::make_unique(length*2)) , m_capacity(length) @@ -59,6 +53,13 @@ void isoBufferBuffer::insert(std::string const & s) insert(c); } +void isoBufferBuffer::insert_hex(uint8_t x) +{ + char str[5]; + sprintf(str, "0x%02hhx", x); + insert((char const *)str); +} + char const* isoBufferBuffer::query(uint32_t count) const { if (count > m_capacity) @@ -97,31 +98,3 @@ uint32_t isoBufferBuffer::capacity() const } -// Legacy Interface Implementation -void isoBufferBuffer::add(std::string const & newString) -{ - insert(newString); -} - - -void isoBufferBuffer::add(char newChar) -{ - insert(newChar); -} - -void isoBufferBuffer::add(uint8_t newByte) -{ - char newString[5]; - sprintf(newString, "0x%02hhx", newByte); - insert((char const *)newString); -} - -uint32_t isoBufferBuffer::getNumCharsInBuffer() -{ - return size(); -} - -char const * isoBufferBuffer::get(uint32_t length) -{ - return query(length); -} diff --git a/Desktop_Interface/isobufferbuffer.h b/Desktop_Interface/isobufferbuffer.h index 65b299c0..2d3bf4b3 100644 --- a/Desktop_Interface/isobufferbuffer.h +++ b/Desktop_Interface/isobufferbuffer.h @@ -30,6 +30,7 @@ public: void insert(char c); void insert(char const * s); void insert(std::string const & s); + void insert_hex(uint8_t x); char const * query(uint32_t length) const; // TODO?: add ability to get a copy of the content @@ -42,13 +43,6 @@ public: uint32_t size() const; uint32_t capacity() const; - - // Legacy Interface - void add(uint8_t newByte); - void add(char newChar); - void add(std::string const & newString); - char const *get(uint32_t length); - uint32_t getNumCharsInBuffer(); private: std::unique_ptr m_data; uint32_t m_capacity; diff --git a/Desktop_Interface/uartstyledecoder.cpp b/Desktop_Interface/uartstyledecoder.cpp index 5858e357..fe0be07f 100644 --- a/Desktop_Interface/uartstyledecoder.cpp +++ b/Desktop_Interface/uartstyledecoder.cpp @@ -14,7 +14,7 @@ uartStyleDecoder::uartStyleDecoder(QObject *parent_in) : QObject(parent_in) updateTimer->start(CONSOLE_UPDATE_TIMER_PERIOD); connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateConsole())); - serialBuffer = new isoBufferBuffer(SERIAL_BUFFER_LENGTH * 2); + serialBuffer = new isoBufferBuffer(SERIAL_BUFFER_LENGTH); if(parent->channel == 1) console = parent->console1; else if(parent->channel == 2) console = parent->console2; @@ -31,10 +31,9 @@ uartStyleDecoder::~uartStyleDecoder() void uartStyleDecoder::updateConsole(){ std::lock_guard lock(mutex); if(!newUartSymbol) return; - //qDebug() << numCharsInBuffer; + //qDebug() << serialBuffer->size(); - uint32_t numCharsInBuffer = serialBuffer->getNumCharsInBuffer(); - console->setPlainText(QString::fromLocal8Bit(serialBuffer->get(numCharsInBuffer), numCharsInBuffer)); + console->setPlainText(QString::fromLocal8Bit(serialBuffer->begin(), serialBuffer->size())); if(parent->serialAutoScroll){ //http://stackoverflow.com/questions/21059678/how-can-i-set-auto-scroll-for-a-qtgui-qtextedit-in-pyqt4 DANKON QTextCursor c = console->textCursor(); @@ -195,10 +194,10 @@ void uartStyleDecoder::decodeDatabit(int mode){ } if (parityCheckFailed) { - serialBuffer->add("\n\n"); + serialBuffer->insert("\n\n"); parityCheckFailed = false; } - serialBuffer->add(tempchar); + serialBuffer->insert(tempchar); } char uartStyleDecoder::decode_baudot(short symbol){