Having a given byte array which represents a capital "A", which is laying on the right site. (source)
The expected result is to rotate the byte array counter clockwise for a standing "A".
My attempt to convert the given array into a rotated version is working but not nice. Something in my code is not correct in "loop()" at the bit shifting and calculation part. For this reason I had to handle x==5 and x==6 separately.
How can I rotate a byte array counter clockwise in a more generic way in c?
Finally the array is displayed at a 8x8 LED Matrix on Arduino. See code and output below.
Code:
#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);
}
}
Output LEDs:
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 _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _