Welcome, Guest
Username: Password: Secret Key Remember me

TOPIC: BIT Masking and Bitwise Operations to Solve an LED Addressing Issue

BIT Masking and Bitwise Operations to Solve an LED Addressing Issue 9 months 1 week ago #17892

  • hop
  • hop's Avatar
  • Offline
  • Platinum Boarder
  • Posts: 418
  • Thanks received: 45
Greets Everyone!

This topic belongs in a C++ programming forum but that does not exist yet, so this seemed to be the closest related area.

I was faced with an issue addressing RGB WS2812B devices on a strip when configured in an odd way. Naturally, when the strip is intact in it's straight layout, it's pretty simple to address LED 1,2,3,4, etc. in a normal way, getting normal results.

All that goes out the window when you start to wrap or dislocate segments of the strip. When you start messing with the geography of the the layout of the LED device, you need to be creative addressing these relocated LED devices in code.

Such is the case with my driveway lantern effects project. Yes, the "bulb" fixture uses 32 LED's, but its layout is an array of LED's using rows and columns. To make this more complicated, to reduce the wiring needed between segments, I had to alter the common mapping on every-other strip of LEDS.

The layout is pretty simple... 8 strips of 4 LED's with each strip running the length of the pvc pipe at the center. To cut down on wiring, I decided to run the strips end to end, and still laid out to unfold into a straight line of 32 LED's. So when laid out on the fixture, each 4 LED strip with be in reverse order as far as mapping. This meant that LED 3 (zero's compliment, meaning the first LED is 0 or zero) leads to LED 4. The first strip is addressed 0,1,2,3 but the second strip is backwards because still connected end-to-end. So where LED 0 is actually at the top of strip one, LED 4 is at the bottom of strip 2. Obviously, LED mapping should be mapped row and column accurately, so I needed a formula that would reverse the LED order, but only for every other strip.

Research will have to be done to see if it is more "COST EFFECTIVE" do just create an array of proper LED address mapping, meaning that the actual LED address is placed at the desired LED location in the array. For example... say that you want LED 1,2,3,4 to actually be LED 1, 10,20, and 30 in the strip. You would create an array of {1,10,20,30} and use the index of the array as the actual LED to address.

I myself would rather use math, even at the binary level, to calculate the proper LED to address so that is the purpose of this post. Perhaps this comes from all the assembly code I wrote once upon a time, when I did not have a complex language and related compiler to hold my hand.


After listing the numbers from 0 to 31 in Excel, I started to look for a pattern using binary. I knew this going into it, but showing the binary confirmed it for me. I noticed right away that the LED address can be the same as the index on every even strip, but had to be reversed on every other strip. Masking bit 2 will give me a test to determine if the address needs to be altered from the index before returning it from the function.

There are a few ways to yield the reversed index, but it seemed simple enough to multiply bits 4-2 (after right shifting by 2) by 4 then add the remainder of 3 minus the remainder of the index in bits 1-0 (to reverse the order).

So, if the strip is odd (bit 2 set and not zero) then calculate the modified address, or if not, then just return the address.
int maddr(int x, int y){ // prototype matrix address
  int actualAddr=x*4+y; // calculate actual strip address
	// x= column, y=row  There are 8 colums of 4 led rows each.
  if((actualAddr&B100)!=0){ // is the group of four an odd strip?
    return (4*((actualAddr&B11100)>>2))+(3-(actualAddr&B11)); // yes - reverse the row index
  }else{
    return actualAddr; // no - leave it alone
  }
}
Last Edit: 9 months 1 week ago by hop.

BIT Masking and Bitwise Operations to Solve an LED Addressing Issue 9 months 1 week ago #17896

  • Jon Chandler
  • Jon Chandler's Avatar
  • Offline
  • Moderator
  • Posts: 329
  • Thanks received: 340
I was trying to reverse the bits of a byte one. Seems like it should be easy but as you found, it's someone involved.

On the monitor I'm working on, the normal state of the signals may be on or off. Setting up a mask of normal values made finding alarms easy. XORing the data and the normal mask shows which channels are in alarm.

BIT Masking and Bitwise Operations to Solve an LED Addressing Issue 9 months 6 days ago #17897

  • hop
  • hop's Avatar
  • Offline
  • Platinum Boarder
  • Posts: 418
  • Thanks received: 45
Yes, I have always loved logic gates and how they solve logic level math in a project. This is probably why I went towards digital instead of analog, and why FPGA is so intriguing.
Time to create page: 0.331 seconds