[英]How to compare a memory bits in C++?
我需要内存位比较功能的帮助。
我在这里购买了带有4个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;
}
}
如果有帮助:我正在使用的电路板的数据表 。 第7页提供了我正在使用的内存映射。
另外,我有一个视频显示工作。
这不是一个真正的答案,但我认为这可能是朝此方向迈出的一步。 由于存在太多的代码重复和令人困惑的条件代码,因此您应该从重构开始。 然后,将更容易理解算法。 我对此表示怀疑,尽管没有保证它不会有错误。
摆脱getShadowRam的影响,然后将绘图修改为如下所示:
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);
}
嗯。可能我在这里错过了一些东西,但是为什么不将大开关更改为:
if(color != prev_color)
{
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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.