Go Down

Topic: 8x8 Matrix and Loops (Read 952 times) previous topic - next topic

lloyddean

We're using the C++ programming language.

I encoded the x, y values into BCD pairs with the left hand 4 bits as 'x' and the right most 4 bits as 'y'.  This works since 4-bits has the range 0 - 15 for 16 unique values which is less than needed for your 8 x 8 LED matrix.

Binary representation of the hex value 0x43 is a concatenation of '0100' (decimal 4) as the hi 4 bits of a single byte and '0011' (or decimal 3) as the lo 4-bits of the byte.  This gives you '01000011' as the encoded byte.

To extract the 'X' value you'll need to shift the byte 4 bits to the right. This throws the lo 4 bits into the but buck leaving '0100'.  Next to extract the 'Y' we need the lo 4 bits which can be extracted with a simple bit-wise 'AND' (which in C/C++ is performed with the '&' operator) leaving use with '0011'.

We now have decode the BCD encoded x and y value form the encoded value 0x43.

All sequences members are encoded values stored in a lookup table.  The lookup table here is simply a C/C++ array of precomputed values.

Does this make sense?

dr0wned

That actually helps a lot. I had to do some searching but was able to wrap my mind around how it all works. 

My only other question is in the line: int y = pattern  & 7;

In this case, was 7 chosen because we know that the first four bits would be zero and that would make sure that once we use 'AND' the first 4 bits would predictably be 0, in essence making it as if only 4 bits mattered?

Thanks again for all your help.

lloyddean

#7
Jan 18, 2013, 05:58 pm Last Edit: Jan 19, 2013, 02:40 am by lloyddean Reason: 1
You have an 8 x 8 matrix meaning x, or y, needs a minimum of 8 (0 - 7) unique values to identify any individual LED.

The encoding is 4 bits X and 4 bits Y

X can be had with -

Code: [Select]

0x43 = 01000011
>> 1   --------    1 falls away
0x21   00100001
>> 1   --------    1 falls away
0x10   00010000
>> 1   --------    0 falls away
0x08   00001000
>> 1   --------    0 falls away
0x04   00000100    X


and X can be had with

Code: [Select]

0x43 = 01000011
0x07 = 00000111
  &   --------
0x03   00000011    Y


By rights we should perform an AND with 0x0F (0 - 15) but we don't need the additional range so I simply used 0x07 (for 0 - 7).

EDIT: And as to why I encoded the 2 values into a single byte?  The answer is probably obvious - to occupy half a much memory space!


And I keep forgetting to answer your question as to HOW the table was generated.

I used a generalized algorithms for an M x N version of the curve but could've used your program as the basis of a very quick and dirty C++ console program thusly -

Code: [Select]

/* Set 'current' directory:
*
*      cd ~/Desktop/Spiral/
*
* Compile with:
*
*      clang++ -stdlib=libc++ -std=c++11 main.cpp
*
* Run with:
*
*      ./a.out
*/

#include <iostream>

int main()
{
   using std::cout;
   using std::endl;
   using std::uppercase;
   
   for ( int x = 0; x <= 7; x++ ) { cout << "0x" << uppercase << 0 << x << ", "; }
   for ( int x = 1; x <= 7; x++ ) { cout << "0x" << uppercase << x << 7 << ", "; }
   for ( int x = 6; x >= 0; x-- ) { cout << "0x" << uppercase << 7 << x << ", "; }
   for ( int x = 6; x >= 1; x-- ) { cout << "0x" << uppercase << x << 0 << ", "; }
   for ( int x = 1; x <= 6; x++ ) { cout << "0x" << uppercase << 1 << x << ", "; }
   for ( int x = 2; x <= 5; x++ ) { cout << "0x" << uppercase << x << 6 << ", "; }
   for ( int x = 6; x >= 1; x-- ) { cout << "0x" << uppercase << 6 << x << ", "; }
   for ( int x = 5; x >= 2; x-- ) { cout << "0x" << uppercase << x << 1 << ", "; }    
   for ( int x = 2; x <= 5; x++ ) { cout << "0x" << uppercase << 2 << x << ", "; }
   for ( int x = 3; x <= 4; x++ ) { cout << "0x" << uppercase << x << 5 << ", "; }
   for ( int x = 5; x >= 2; x-- ) { cout << "0x" << uppercase << 5 << x << ", "; }
   for ( int x = 4; x >= 3; x-- ) { cout << "0x" << uppercase << x << 2 << ", "; }

   cout << "0x" << uppercase << 3 << 3 << ", ";
   cout << "0x" << uppercase << 3 << 4 << ", ";
   cout << "0x" << uppercase << 4 << 4 << ", ";
   cout << "0x" << uppercase << 4 << 3;

   cout << endl;
       
   return 0;
}


lloyddean

Made you look!

Actually I added to the above message ...

Go Up