[英]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.