c++ - 如何比较 C++ 中的内存位?

标签 c++ c memory arduino bit

我需要内存位比较功能方面的帮助。

我在这里买了一个带 4 x HT1632C 芯片的 LED 矩阵,我正在我的 Arduino Mega2560 上使用它.

此芯片组没有可用代码(它与 HT1632 不同),我正在自己编写。我有一个绘图函数,它获取 x、y 坐标和颜色,并且该像素打开。只有这样才能完美运行。

但我的显示器需要更多性能,所以我尝试制作一个 shadowRam 变量,它是我设备内存的“拷贝”。在我在显示器上绘制任何东西之前,它会检查 shadowRam 以查看是否真的有必要更改该像素。当我在绘图功能上启用此 (getShadowRam) 时,我的显示器有一些,只有一些(整个显示器上有 3 或 4 个)幻影像素(不应打开的像素)。

如果我只是在我的绘图函数上评论 prev_color if,它就可以完美地工作。

此外,我正在清理我的 shadowRam 阵列,将所有矩阵设置为零。

变量:

#define BLACK  0
#define GREEN  1
#define RED    2
#define ORANGE 3
#define CHIP_MAX 8
byte shadowRam[63][CHIP_MAX-1] = {0};

getShadowRam 函数:

byte HT1632C::getShadowRam(byte x, byte y) {
    byte addr, bitval, nChip;

    if (x>=32) {
        nChip = 3 + x/16 + (y>7?2:0);
    } else {
        nChip = 1 + x/16 + (y>7?2:0);
    }

    bitval = 8>>(y&3);

    x = x % 16;
    y = y % 8;
    addr = (x<<1) + (y>>2);

    if ((shadowRam[addr][nChip-1] & bitval) && (shadowRam[addr+32][nChip-1] & bitval)) {
      return ORANGE;
    } else if (shadowRam[addr][nChip-1] & bitval) {
        return GREEN;
    } else if (shadowRam[addr+32][nChip-1] & bitval) {
        return RED;
    } else {
        return BLACK;
    }
}

绘图函数:

void HT1632C::plot (int x, int y, int color)
{
    if (x<0 || x>X_MAX || y<0 || y>Y_MAX)
        return;

    if (color != BLACK && color != GREEN && color != RED && color != ORANGE)
        return;

    char addr, bitval;
    byte nChip;

    byte prev_color = HT1632C::getShadowRam(x,y);

    bitval = 8>>(y&3);

    if (x>=32) {
        nChip = 3 + x/16 + (y>7?2:0);
    } else {
        nChip = 1 + x/16 + (y>7?2:0);
    }

    x = x % 16;
    y = y % 8;
    addr = (x<<1) + (y>>2);

    switch(color) {
        case BLACK:
            if (prev_color != BLACK) { // compare with memory to only set if pixel is other color
                // clear the bit in both planes;
                shadowRam[addr][nChip-1] &= ~bitval;
                HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
                shadowRam[addr+32][nChip-1] &= ~bitval;
                HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
            }
            break;

        case GREEN:
            if (prev_color != GREEN) { // compare with memory to only set if pixel is other color
             // set the bit in the green plane and clear the bit in the red plane;
             shadowRam[addr][nChip-1] |= bitval;
             HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
             shadowRam[addr+32][nChip-1] &= ~bitval;
             HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
            }
            break;

        case RED:
            if (prev_color != RED) { // compare with memory to only set if pixel is other color
                // clear the bit in green plane and set the bit in the red plane;
                shadowRam[addr][nChip-1] &= ~bitval;
                HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
                shadowRam[addr+32][nChip-1] |= bitval;
                HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
            }
            break;

        case ORANGE:
            if (prev_color != ORANGE) { // compare with memory to only set if pixel is other color
                // set the bit in both the green and red planes;
                shadowRam[addr][nChip-1] |= bitval;
                HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
                shadowRam[addr+32][nChip-1] |= bitval;
                HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
            }
            break;
    }
}

如果有帮助: datasheet我正在使用的板。第 7 页有我正在使用的内存映射。

另外,我有一个 video显示工作。

最佳答案

这不是真正的答案,但我认为这可能是解决这个问题的一步。由于有太多代码重复和令人困惑的条件代码,您应该从重构开始。这样就会更容易理解算法。我试了一下,但没有保证它没有错误。

去掉 getShadowRam,修改 plot 如下:

void HT1632C::plot (int x, int y, byte color)
{
  if (x < 0 || x > X_MAX || y < 0 || y > Y_MAX)
    return;

  if (color != BLACK && color != GREEN && color != RED && color != ORANGE)
    return;

  // using local struct to allow local function definitions
  struct shadowRamAccessor {
    shadowRamAccessor(byte x, byte y) {
      nChip = (x >= 32 ? 3 : 1)
        + x / 16
        + (y > 7 ? 2 : 0);
      bitval = 8 >> (y & 3);
      addr = ((x % 16) << 1) + ((y % 8) >> 2);
      highAddr = addr + 32;
    }

    byte& getShadowRam(byte addr) {
      return shadowRam[addr][nChip-1];
    }

    byte getPreviousColor() {
      byte greenComponent = getShadowRam(addr) & bitval ? GREEN : BLACK;
      byte redComponent = getShadowRam(highAddr) & bitval ? RED : BLACK;
      return greenComponent | redComponent;
    }

    void setValue(byte newColor) {
      byte prev_color = getPreviousColor();
      if(newColor != prev_color)
        setValue(newColor & GREEN, newColor & RED);
    }

    void setValue(bool greenBit, bool redBit)
    {
      HT1632C::sendData(nChip, addr,
        greenBit
          ? getShadowRam(addr) |= bitval
          : getShadowRam(addr) &= !bitval
        );
      HT1632C::sendData(nChip, highAddr,
        redBit
          ? getShadowRam(highAddr) |= bitval
          : getShadowRam(highAddr) &= ~bitval
        );
    }

    byte nChip, bitval, addr, highAddr;
  };

  shadowRamAccessor(x, y).setValue(color);
}

关于c++ - 如何比较 C++ 中的内存位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4523653/

相关文章:

c - 如何在 execlp 系统调用后打印行

c - C 中的 For 循环;但没有任何花括号

java - Ubuntu 服务器中出现 OutOfMemory 错误

c++ - 64位进程地址的读内存

c++ - C++内存写入字节以使用内核驱动程序进行寻址

c++ - 无符号字符范围是0到255

c++ - 获取 IIS 7 站点属性

c - 如何简化大量的 C if 语句?

c++ - .containsKey() 用于 C++ 映射的方法

c++ - 确定 std::sort(begin, end) 是否修改了范围