原创 Pointer problems

2014-5-2 16:10 2387 24 25 分类: 消费电子

Jack Ganssle posted a 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 actually using Adafruit's NeoPixel Strips for this little beauty, with the control of the display being performed by an Arduino Mega microcontroller development board.

 

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.

文章评论1条评论)

登录后参与讨论

用户1406868 2016-5-2 08:50

You delieitnfy know how to bring an issue to light and make it important. I cant believe youre not more popular because you definitely have the gift.
相关推荐阅读
用户3826190 2016-04-29 17:59
Can you improve this BMP display?
Our Caveman Diorama is going to boast a Time Portal, thereby explaining my presence in the scene. ...
用户3826190 2016-04-29 17:56
In progress: Home-made Nixie tube clock
Just to make sure we're all tap dancing to the same drum beat, let's remind ourselves that, someti...
用户3826190 2016-04-29 17:51
Incorporating medication delivery into smartphones
I don’t know about you, but whenever I have to take medications over a period of time, I have a tr...
用户3826190 2016-04-15 17:20
Incredibly handy electronic component testers
Several weeks ago, I was walking around my office building when I ran across my chums Ivan and Dar...
用户3826190 2016-04-15 17:17
A look at electronic component testers
Several weeks ago I was meandering my way around my office building when I ran across my chums Iva...
用户3826190 2016-03-24 11:48
Let Jolly Roger Telephone Co deal with telemarketers
I don’t know about you, but I receive a staggering number of unsolicited telemarketer calls these ...
我要评论
1
24
关闭 站长推荐上一条 /2 下一条