Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpSyntax › hex + arrays (with MAX7129/LED matrix)

Page Index Toggle Pages: 1
hex + arrays (with MAX7129/LED matrix) (Read 10468 times)
09/20/06 at 17:15:33

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
Where I'm stuck now: trying to create an array of row/col addresses for each LED to serve as a lookup table - then I can just send in an int  - 0-63 (from Director) to turn an LED on. The code below compiles but using Serial.print( pixArray[1][0]) to check values seems to indicate that I am not populating the array correctly.

am I missing something really basic here?

byte  pixArray[64][2];

void definePixArray(){

  byte rows[8]= {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x02};
  byte cols[8]= {0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40};
  int r;
  int c;

  for (r = 0, r < 64; r++;){
     for (c = 0, c < 8; c++;){
     pixArray[r][0] = rows[r/8];
     pixArray[r][1] = cols[c];
     }
  }
}
 
IP Logged
 
Reply #1 - 09/20/06 at 20:08:47

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
Wny not use the MAX7219 library Matrix library?
Then you just do mymatrix.write(row,column,HIGH); to turn an LED on.
(Maybe this is what you are doing, and the lookup table is to convert the int.)

From director, you can still send an int with the LED number, and call:
mymatrix.write(lednumber&7, lednumber >> 3, HIGH);  // Turn on LED #lednumber
(You might have to swap the first two parameters if I got the row and column backwards)

lednumber&7 is the lower 3 bits of the LED number which is the row.
lednumber >> 3 is the next 3 bits of the LED number which is the column.
(lednumber >> 3 is the same as lednumber/8, but it might be faster.)
 
IP Logged
 
Reply #2 - 09/21/06 at 01:22:37

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
Alan_Kilian wrote on 09/20/06 at 20:08:47:
Wny not use the MAX7219 library Matrix library?
Then you just do mymatrix.write(row,column,HIGH); to turn an LED on.
(Maybe this is what you are doing, and the lookup table is to convert the int.)

From director, you can still send an int with the LED number, and call:
mymatrix.write(lednumber&7, lednumber >> 3, HIGH);  // Turn on LED #lednumber
(You might have to swap the first two parameters if I got the row and column backwards)

lednumber&7 is the lower 3 bits of the LED number which is the row.
lednumber >> 3 is the next 3 bits of the LED number which is the column.
(lednumber >> 3 is the same as lednumber/8, but it might be faster.)


I just discovered the Matrix library last night. Works great (I was using the code from Zambetti's site which I guess the library code is referencing).

I figured a lookup table would be faster than doing operations within Wiring. I must admit that the strict typing in Wiring and the the thin documentation have been a bit frustrating. For example- I don''t know what you are doing with "lednumber >> 3"
and stuff like not understanding some of the errors the compiler throws (esp with array stuff).

I've been programming in Director and Flash for years but those apps do more hand holding etc. Is there a language reference I can look at that Wiring is based on?
 
IP Logged
 
Reply #3 - 09/21/06 at 05:58:40

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
I'm sorry for just tossing those strange sets of characters at you without an explanation.
I've been programming in C for about 22 years so it's as natural as English for me.
(Actually, I was in Japan, and I "talked" to an engineer in C for about an hour. He didn;t know Englich and I didn't know Japanese, but we both knew C and we could write some pretty complicated
questions and answers. It was incredible.)

So, "lednumber >> 3" does a "shift-right" three bits. It's a hardware thing.

Look for a reference to the C language and yo'll get a lot of Wiring.
Look for a refeence to the C++ language and you'll get more from Wiring, but I wouldn't start there.
 
IP Logged
 
Reply #4 - 09/21/06 at 15:37:53

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
that's a great story (@ the Japanese engineer)

I figured >> was bit shifting but I didn't see any references in the docs, etc. (same for &) but I used some print statements to see the results and it made sense.

So with your code I can send in an integer and easily target specific LEDs.

One more thing - using bit shifting or splitting - is there a fast way I can send in a high/low flag so that both the LED address and it's state can be encapsulated in one incoming message?

thanks for all your help!
 
IP Logged
 
Reply #5 - 09/21/06 at 18:20:30

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
You need 0-63 to select the LED, so use the next bit up to determine the state.
That bit is worth 128, so add 128 if you want the LED lit.
To turn off LED 0, send a 0 To turn off led 63 send a 63
To turn on LED 0 send a 128 To turn on led 63, send 191  (128 + 63)

row = value & 7;
col = (value >> 3) & 7;
state = (value >> 4) & 1;
mymatrix.write(row,col,state);

In Director, figure out the LED number (0-63) and then add 128 to that value if you want the LED lit.
 
IP Logged
 
Reply #6 - 09/22/06 at 01:06:16

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
awesome! Bitwise operations always have seemed a dark art - abstract enough to not easily visualize (for me at least).

thanks!
 
IP Logged
 
Reply #7 - 09/22/06 at 18:13:42

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
Alan_Kilian wrote on 09/21/06 at 18:20:30:
You need 0-63 to 
row = value & 7;
col = (value >> 3) & 7;
state = (value >> 4) & 1;
mymatrix.write(row,col,state);


So I've been using the above code but it's not working as expected. Is

         state = (value >> 4) & 1;

correct? Using a print statement, I see that the result is always 1
 
IP Logged
 
Reply #8 - 09/22/06 at 19:35:53

Alan_Kilian   Offline
Full Member
Wiring? It looks like
Java to me.

Posts: 118
***
 
roy_pardi wrote on 09/22/06 at 18:13:42:
So I've been using the above code but it's not working as expected. Is
state = (value >> 4) & 1;
correct? Using a print statement, I see that the result is always 1
END QUOTE

Ugh, no it's not right.
Try   state = (value >> 6) & 1;
Sorry.
 
IP Logged
 
Reply #9 - 09/22/06 at 23:58:50

roy_pardi   Offline
YaBB Newbies
Boston USA

Posts: 49
*
 
     Try   state = (value >> 6) & 1;

Interesting - with that if I send:

0 +320 - turns LED 1 on
63 + 320 - turns LED 63 on

0 - turns LED 1 off
320 - turns LED 63 off

stumbled on 320 just by accident. I can work with that for now.
320 - 63 = 257 so...  Undecided
 
IP Logged
 
Page Index Toggle Pages: 1