具有代表大写“A”的给定字节数组,该数组位于正确的站点上。 (source)
预期的结果是逆时针旋转字节数组以显示“A”。
我尝试将给定数组转换为旋转版本是有效但不好。我的代码中的某些东西在位移和计算部分的“loop()”中是不正确的。出于这个原因,我必须分别处理x == 5和x == 6.
如何在c?
中以更通用的方式逆时针旋转字节数组最后,阵列显示在Arduino上的8x8 LED矩阵中。请参阅下面的代码和输出。
代码:
#include "LedControl.h"
LedControl lc=LedControl(12,11,10,4);
void setup(){
for (int addr=0; addr<lc.getDeviceCount(); addr++){
lc.shutdown(addr,false);
lc.setIntensity(addr,0);
lc.clearDisplay(addr);
}
}
void loop(){
// given
byte a[5]={B01111110,B00010001,B00010001,B01111110,B00000000};
// expected
byte a2[8]={B01100000,B10010000,B10010000,B10010000,B11110000,B10010000,B10010000,B00000000};
// rotated
byte a3[8];
byte row;
for (int x = 0; x < 8; x++){
row = B00000000;
for (int y = 0; y < 5; y++){
if (x==0 || x==1 || x==2 || x==3 || x==4) {
row |= (a[y] & B00000001 << x) << 7-x-y;
}
if (x==5) {
row |= (a[0] & B00100000) << 2;
row |= (a[1] & B00100000) << 1;
row |= (a[2] & B00100000);
row |= (a[3] & B00100000) >> 1;
}
if (x==6) {
row |= (a[0] & B01000000) << 1;
row |= (a[1] & B01000000);
row |= (a[2] & B01000000) >> 1;
row |= (a[3] & B01000000) >> 2;
}
}
a3[x] = row;
}
// output
for(int i=0; i<8; i++){
lc.setRow(0,i,a[i]); // given
lc.setRow(1,i,a2[i]); // expected
lc.setRow(2,i,a3[i]); // rotated
delay(100);
}
}
输出LED:
given a expected a2
rotated a3
_ o o o o o o _ _ o o _ _ _ _ _
_ _ _ o _ _ _ o o _ _ o _ _ _ _
_ _ _ o _ _ _ o o _ _ o _ _ _ _
_ o o o o o o _ o _ _ o _ _ _ _
_ _ _ _ _ _ _ _ o o o o _ _ _ _
_ _ _ _ _ _ _ _ o _ _ o _ _ _ _
_ _ _ _ _ _ _ _ o _ _ o _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
答案 0 :(得分:1)
您的代码似乎过于夸张。您可以使用嵌套循环来迭代任何可能的源数据位并相应地设置目标数据(这基本上交换了索引),如:
#include <stdio.h>
typedef unsigned char byte;
void printCharacter(const byte* data, size_t length)
{
for (size_t i = 0; i < length; ++i)
{
for (size_t j = 0; j < 8; ++j)
{
const unsigned char mask = 1 << j;
printf("%c ", data[i] & mask ? 'o' : '-');
}
printf("\n");
}
}
void rotate(const byte* source, byte* dest, size_t length)
{
/* for each bit position starting from first */
for (size_t j = 0; j < 8; ++j)
{
/* this is the mask of the i-th bit in source data */
const unsigned char mask = 1 << j;
/* for each row in source data (which will become column) */
for (size_t i = 0; i < length; ++i)
{
/* if j-th bit of i-th row set */
if (source[i] & mask)
/* then set i-th bit of j-th row */
dest[j] |= 1 << i;
}
}
}
int main() {
byte a[5]= { 0b01111110,0b00010001,0b00010001,0b01111110,0b00000000 };
byte b[8]= { 0 };
printCharacter(a, 5);
rotate(a, b, 5);
printCharacter(b, 8);
return 0;
}
现在输出
- o o o o o o -
o - - - o - - -
o - - - o - - -
- o o o o o o -
- - - - - - - -
- o o - - - - -
o - - o - - - -
o - - o - - - -
o - - o - - - -
o o o o - - - -
o - - o - - - -
o - - o - - - -
- - - - - - - -
这不是你想要的,但你只需要根据你想要的旋转调整掩码/索引从第一位/最后一位开始。