diff --git a/Desktop_Interface/i2cdecoder.cpp b/Desktop_Interface/i2cdecoder.cpp index 033597ed..b75170fe 100644 --- a/Desktop_Interface/i2cdecoder.cpp +++ b/Desktop_Interface/i2cdecoder.cpp @@ -7,6 +7,7 @@ i2cDecoder::i2cDecoder(isoBuffer* sda_in, isoBuffer* scl_in) : sda(sda_in), scl(scl_in) { + serialBuffer = new isoBufferBuffer(I2C_BUFFER_LENGTH); } void i2cDecoder::reset() @@ -22,6 +23,12 @@ void i2cDecoder::reset() serialPtr_bit = sda->back * 8; + { + std::lock_guard lock(mutex); + + delete serialBuffer; + serialBuffer = new isoBufferBuffer(I2C_BUFFER_LENGTH); + } } @@ -196,3 +203,18 @@ void i2cDecoder::dataByteCompleted(uint8_t byte, bool ACKed) { } + +void i2cDecoder::updateConsole(){ + std::lock_guard lock(mutex); + if (!consoleStateInvalid) + return; + + uint32_t numCharsInBuffer = serialBuffer->getNumCharsInBuffer(); + console->setPlainText(QString::fromLocal8Bit(serialBuffer->get(numCharsInBuffer), numCharsInBuffer)); + if(sda->serialAutoScroll){ + QTextCursor c = console->textCursor(); + c.movePosition(QTextCursor::End); + console->setTextCursor(c); + } + consoleStateInvalid = false; +} diff --git a/Desktop_Interface/i2cdecoder.h b/Desktop_Interface/i2cdecoder.h index 6e4c7e83..068881e8 100644 --- a/Desktop_Interface/i2cdecoder.h +++ b/Desktop_Interface/i2cdecoder.h @@ -3,6 +3,8 @@ #include #include "isobuffer.h" +#include "isobufferbuffer.h" +#include namespace i2c { @@ -24,6 +26,7 @@ enum class edge: uint8_t }; constexpr uint8_t addressBitStreamLength = 9; +constexpr uint32_t I2C_BUFFER_LENGTH = 8192; class i2cDecoder : public QObject { @@ -33,6 +36,9 @@ public: // misc isoBuffer* sda; isoBuffer* scl; + QPlainTextEdit* console; + isoBufferBuffer* serialBuffer = nullptr; + std::mutex mutex; // State vars uint8_t currentSdaValue = 0; @@ -41,6 +47,7 @@ public: uint8_t previousSclValue = 0; uint64_t serialPtr_bit = 0; transmissionState state = transmissionState::unknown; + bool consoleStateInvalid; // Data Transmission uint8_t currentBitIndex = 0; @@ -59,10 +66,9 @@ public: void stopCondition(); void dataByteCompleted(uint8_t byte, bool ACKed); void reset(); - signals: public slots: - + void updateConsole(); }; } // Namespace i2c diff --git a/Desktop_Interface/isobufferbuffer.cpp b/Desktop_Interface/isobufferbuffer.cpp index 2d6e44e2..3ebb8a64 100644 --- a/Desktop_Interface/isobufferbuffer.cpp +++ b/Desktop_Interface/isobufferbuffer.cpp @@ -1,25 +1,42 @@ #include "isobufferbuffer.h" -isoBufferBuffer::isoBufferBuffer(int length) +isoBufferBuffer::isoBufferBuffer(uint32_t length) + : bufferLength(length) { - bufferLength = length; - mid = length/2; - buffer = (char *) malloc((length * 3) / 2); + mid = bufferLength/2; + buffer = (char *) malloc((bufferLength * 3) / 2); } +void isoBufferBuffer::add(std::string newString) +{ + for (char& newChar : newString) + add(newChar); +} + + void isoBufferBuffer::add(char newChar){ buffer[ptr] = newChar; - if(ptr= bufferLength){ + + if(ptr < mid) + buffer[ptr + bufferLength] = newChar; + + if (ptr >= bufferLength) ptr = 0; - } else ptr++; + + numCharsInBuffer = std::min(numCharsInBuffer + 1, mid); } -char *isoBufferBuffer::get(int length){ - if (length>mid) qFatal("isoBuffer::get; length requested is too high."); - if(ptr mid) + qFatal("isoBuffer::get; length requested is too high."); + if(ptr < mid) + return &buffer[ptr + bufferLength - length]; + else + return &buffer[ptr - length]; } diff --git a/Desktop_Interface/isobufferbuffer.h b/Desktop_Interface/isobufferbuffer.h index f000bbd7..135a2e84 100644 --- a/Desktop_Interface/isobufferbuffer.h +++ b/Desktop_Interface/isobufferbuffer.h @@ -5,18 +5,22 @@ #include #include +#include class isoBufferBuffer { public: - isoBufferBuffer(int length); + isoBufferBuffer(uint32_t length); void add(char newChar); - char *get(int length); + void add(std::string newString); + char *get(uint32_t length); + uint32_t getNumCharsInBuffer(); private: - int bufferLength; - int mid; - int ptr; + uint32_t bufferLength; + uint32_t mid; + uint32_t ptr; char *buffer; + uint32_t numCharsInBuffer = 0; }; #endif // ISOBUFFERBUFFER_H diff --git a/Desktop_Interface/uartstyledecoder.cpp b/Desktop_Interface/uartstyledecoder.cpp index 9fdb855a..abf5b932 100644 --- a/Desktop_Interface/uartstyledecoder.cpp +++ b/Desktop_Interface/uartstyledecoder.cpp @@ -12,7 +12,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 * 2); if(parent->channel == 1) console = parent->console1; else if(parent->channel == 2) console = parent->console2; @@ -31,6 +31,7 @@ void uartStyleDecoder::updateConsole(){ if(!newUartSymbol) return; //qDebug() << numCharsInBuffer; + uint32_t numCharsInBuffer = serialBuffer->getNumCharsInBuffer(); console->setPlainText(QString::fromLocal8Bit(serialBuffer->get(numCharsInBuffer), numCharsInBuffer)); if(parent->serialAutoScroll){ //http://stackoverflow.com/questions/21059678/how-can-i-set-auto-scroll-for-a-qtgui-qtextedit-in-pyqt4 DANKON @@ -117,7 +118,6 @@ unsigned char uartStyleDecoder::getNextUartBit(){ void uartStyleDecoder::decodeNextUartBit(unsigned char bitValue) { if(dataBit_current == dataBit_max){ - if(numCharsInBuffer