简体   繁体   English

如何在 C++ 中异步地从/向 uint32_t 存储和弹出 uint8_t?

[英]How to store and pop uint8_t from/to uint32_t asynchronously in C++?

I'm working on a MCU, enhancing the library , making it non-blocking.我正在研究 MCU,增强,使其非阻塞。

It already blocks code using while loop for certain times.它已经在某些时候阻止了使用while循环的代码。

I've tweaked the library to remove all these blockings, sniff out all what should be sent to a std::vector<uint8_t> internalBuffer .我已经调整了库以删除所有这些阻塞,嗅出所有应该发送到std::vector<uint8_t> internalBuffer and used micros() to asynchronously send bytes through stream output in loop().并使用micros()通过 loop() 中的流输出异步发送字节。

I'm Enhancing that by converting std::vector<uint8_t> internalBuffer to std::vector<uint32_t> to take way less space (using ESP8266 with aligned 4 bytes).我通过将std::vector<uint8_t> internalBufferstd::vector<uint32_t>来增强它以占用更少的空间(使用 ESP8266 对齐 4 个字节)。

I know how to store and extract uint8_t to uint32_t , But when it comes to Asynchronous store and extract, Here comes un-known probabilities to me.我知道如何将uint8_t存储和提取到uint32_t ,但是当涉及到异步存储和提取时,对我来说这是未知的概率。

Because storing and extracting uint8_t are asynchronous, storing is anytime, extracting is constrained by time interval.因为存储和提取uint8_t是异步的,存储是随时的,提取受时间间隔的限制。

For what's worth mentioning : I've already done storing, but I don't have a complete extraction code.值得一提的是:我已经完成了存储,但我没有完整的提取代码。

void Pos_Printer::storeInBuffer(uint8_t c)
{
    uint32_t longInt = 0UL;
    if (excessBytesInBuffer == 0)
    {
      longInt = c;
    }
    else
    {
      longInt = internalBuffer.back();
      internalBuffer.pop_back();
      longInt = longInt << 8 * excessBytesInBuffer + c;
    }
    excessBytesInBuffer++;
    excessBytesInBuffer %= 4;

    internalBuffer.push_back(longInt);
}

Uncompleted extraction (at CLASS::loop()) :未完成的提取(在 CLASS::loop() 处):

  if (micros() - resumeTime > 0 && internalBuffer.size())
  {
    uint32_t longInt = 0UL;
    uint8_t byteToSend = 0;

    if(internalBuffer.size() == 1)      
    {                   //Work on excessBytesInBuffer
      longInt = internalBuffer.at(0);
      if(excessBytesInBuffer)
      {                         //What if remained 1 and stored +3 Bytes after this ?
        byteToSend = (longInt >> 8 * excessBytesInBuffer) & 0xFF;

      }
    }
    else
    {    

    }

    stream->write(byteToSend);
    internalBuffer.erase(internalBuffer.begin()); //Uncompleted 
    lastWritingTime = micros();
  }

Another solution : I've thought of another solution, To store 1 of 4 bytes of (uint32_t) to keep track how many bytes stored (from 1 to 3).另一种解决方案:我想到了另一种解决方案,存储 (uint32_t) 的 4 个字节中的 1 个以跟踪存储的字节数(从 1 到 3)。 But that's a big loss if wanted to optimize storing an image to the library (as example).但是如果想要优化将图像存储到库(例如),这是一个很大的损失。

Just like you keep excessBytesInBuffer in storing, you need something like missingBytesInBuffer in extraction.就像您在存储中保留excessBytesInBuffer一样,您需要在提取中使用missingBytesInBuffer东西。 Then you would only do internalBuffer.erase(internalBuffer.begin());那么你只会做internalBuffer.erase(internalBuffer.begin()); when missingBytesInBuffer is 4.missingBytesInBuffer为 4 时。

You could speed things up if you store four bytes at a time, assuming you have them all on the calling side.如果您一次存储四个字节,则可以加快速度,假设您在调用端拥有它们。

Similarly, you can extract four bytes at a time and keep them in a local buffer (cache).同样,您可以一次提取四个字节并将它们保存在本地缓冲区(缓存)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM