Pointers, Functions, Pointers to Functions, Eeek!

Max is wearing his sad face, because he cannot wrap his brain around a problem pertaining to pointers.

A few weeks ago I saw Jack Ganssle's blog about a book called Understanding and Using C Pointers by Richard M. Reese. I'm a hardware design engineer by trade. The best you can say is that I dabble in the software side of things. The subtleties of pointers have long confused me, so I decided to purchase this book.

Personally, I think this book is well worth the price if you want to move beyond the "training wheel" stage of C programming. I learned a lot. Unfortunately, I didn’t learn as much as I thought I'd learned, because I cannot wrap my brain around a programming problem.

Here's the deal. This all pertains to my current BADASS Display project, which -- as you will doubtless recall -- is going to feature a 16x16 array of tri-colored LEDs as illustrated below.

I'm planning on using 16 NeoPixel strips, each containing 16 pixels, and each implementing one of the vertical columns in the array. The idea is to take an audio stream from my iPad, split out its spectral components into sixteen frequency "buckets," and then present the results in spectrum-analyzer-form on my display. (I'm still pondering how to extract the frequency spectrum data from the audio stream, but that's a problem for another day.)

Purely for the sake of discussion, let's assume that my NeoPixel strips are named "strip[0]," "strip[1],"… "strip[15]." Meanwhile, the lowest LED (i.e., the one nearest to the bottom of the array) on each strip is numbered 0, while the highest LED is numbered 15. Let's further assume that whatever is processing my audio stream, it generates a series of sixteen five-bit values called "bucket[0]," "bucket[1],"… "bucket[15]." Each of these five-bit values represents the current amplitude associated with that bucket, from 000002 (no LEDS lit) to 100002 (all LEDs lit).

Actually, my previous statement is not strictly true, because these five-bit values actually represent the number of the highest (vertically speaking) LED being lit. The way in which we subsequently display this information depends on what we wish to do -- we could display a solid bar of LEDs from LED 0 up to and including the LED in question, or we could simply light up this topmost LED, or… the list goes on.

But we are wandering off into the weeds. What I would ideally like to do is to have a function called "lightStrip()" that I can call from the main body of my program. Perhaps something like the following:

But then we come to the way in which the folks at Adafruit have created their libraries and instantiate their NeoPixel strips. I think this is done in C++, but -- thankfully -- the gory details are hidden from me. A stripped-down version of the instantiation might look something like the following:

The first parameter is the number of pixels in your strip (16 in this case), while the second parameter is the number of the digital I/O pin you wish to use to drive this strip (1 in this case).

Later on, in the body of your code, you can use calls like the following:

In the case of the "strip.setPixelColor()" function call, "i" is the number of the pixel (ranging from 0 to 15 in our case) whose color you wish to specify using three eight-bit values for the red, green, and blue channels. The interesting thing is that the "strip.setPixelColor()" function doesn’t actually drive the strip itself. It just stores the new color values in an array in memory. This means you can specify the color values of any and all elements in the strip without actually writing to the strip each time.

Once we have all of our ducks in a row, as it were, we use the "strip.show()" function to upload all of the color values into the strip. Now, we can instantiate multiple strips called "strip0," "strip1," "strip2," and so forth if we wish. But this is where I grind to a halt. How can I pass these different strips into my "lightStrip()" function? I know that in regular C it's possible to pass a pointer to one function as an argument into another function, but how does that relate to what I'm talking about here? All I can say is that any thoughts you care to share will be very gratefully received.

@djohns: The first thing I did was just [...] Then I changed it to [...] Then I added some [...] Then I decided to stop playing and go to bed...

LOL This is WONDERFUL -- thank you so much -- I am snowed under with work at the moment -- fighting my way toward the weekend -- but I cannot wait to root through all of this and see what you did.

Also, I hjave to say that this ha sinspired me to "pull my finger out" and actually start constructing the display so I can start playing with these effects on the real display.

FYI Keep your eyes open for a follow-up blog later today on MUC Designline in which I will be pondering whether to use I2C or create a custom interface to link two MCUs (as part of my BADASS Display, of course)

Hi, here is link to some stuff I put together for fun illustrating fading pixels on and off and abstracting from the hardware. It turned out to be very flexible. The first thing I did was just select pixels at random and give them a random rgb color. If the same pixel was randomly selected later and given a new color, it would fade from the one color to the other. Then I changed it to draw light the pixels as a vertical bar from the bottom to the selected pixel and fade the bar out over time. Then I added some gravity effects so that the bar would fade out from the top down. Then I decided to stop playing and go to bed...

I just noticed something kind of horrifying. After I installed the program I linked you (which is everything I said it was), I tried doing a Google search and discovered that the "Conduit" search engine hijacker had been installed on my machine (even though I had in fact agreed to NOTHING other than the download itself)! Now "Conduit" itself is sort of nasty but it is after all just a kind of adware, so I did a search on the removal process and was able to get rid of it in a few short steps, and the instructions that I found online (the ones that invoke adwcleaner) worked rather well and I believe they removed the problem completely. What I wanted to say first is that this download site is owned by Cnet which I understand is itself owned by the CBS network, and it is the very height of corporate irresponsibility AND GREED that they feel they can maintain this site as a paid mechanism for infecting other PCs with unwelcome adware as a means of monetizing the website, and I think a wholesale boycott of CBS products would not be out of the question. It's probably also possible to download the program directly fron the developer's website, I haven't looked for it yet but if I find it I'll leave that as the download link. Anyway I feel just TERRIBLE about the distress that I may have caused some of you, and I wanted to get this warning out as soon as possible, thanks to all of you for your patience and understanding!