Neopixel rainbow

Thanks to great work by Mark Swanson, neopixel support is now available for S4A!

We needed a small upgrade to the S4A component library, AVR to give some timing functions, otherwise the work is all done in swift. You can see some great demo videos.

Spurred on by this I decided to add some optional extra low level support for many basic classes of neopixels, to give developers two options how to get started.

Here is an example of making a rainbow using the new "Fast iLED" components.

https://www.youtube.com/watch?v=e3nqyxCACKA&feature=youtu.be


And all this in just a few lines of simple Swift code!

import AVR
typealias IntegerLiteralType = UInt8
 
let ledPin = 4
let pixels = 60
 
var buffer = iLED_Setup_Buffered(pin: ledPin, count: pixels, hasWhiteChip: true) 
func rainbow() {
    let hueDeltaPerPixel = 255 / pixels
    for i in 0..<pixels {
        buffer[Int(i)] = iLEDFastMakeColor(hue: i &* hueDeltaPerPixel, saturation: 255, value: 255, white: 0)
    }
    iLEDFastWriteBuffer()
}
rainbow()
func rollBuffer() {
    let firstPixel = buffer[0]
    for i in 0..<pixels {
        buffer[Int(i)] = buffer[Int(i&+1)]
    }
    buffer[Int(pixels&-1)] = firstPixel
    iLEDFastWriteBuffer()
}
delay(ms: 1000)
while(true) {
    rollBuffer()
    delay(ms: 1)
}
You can see a basic setup, following the usual pattern. Then we call the setup function (we have actually another bit of setup wrapped in a function not shown here but all it really does is call the setup function then clear the bits).

In Mark's view (and I think he's right), most neopixels you buy, that run on the standard one wire neopixel protocols, take the values as GRB or GRBW if they have an added white LED (like mine do). In the above setup, because it's overwhelmingly true, we default grb order to true.

This function does a malloc under the hood so in the unlikely event you wanted to create a buffer then finished with it later, you'd use iLEDFastTeardown. But in most cases this would be a bad idea. atmega chips have extremely limited RAM and if you fragment and reuse them you will very quickly run out of it and bad things will happen.

So generally you'll do a setup, make the buffer, create any colours you want and then start setting pixels.

This one has two simple functions.

The first makes a simple rainbow by using the HSV version of iLEDFastMakeColor to make each pixel one of the steps on a hue cylinder. Finally you write all the pixels in RAM down the wire to the neopixels using iLEDFastWriteBuffer, this displays your dazzlingly bright rainbow on the strip.

The next function simply rolls the buffer around by one then sends it down to the neopixels.

Finally to cap it all off, we call the roll buffer function once every ms (or slower if you prefer).

And that's it! Have fun playing and trying new things and let everyone know what you find out in the comments below!

Carl



Parts you will need for this project:

1) Adafruit neopixel strip - there are many varieties of this, too many to list, the one in my demo had 60 pixels, is about 1m long and cost £20 (from DigiKey)... you'll probably end up spending between $10-$40 for this, it's likely to be the most expensive component in the project

2) Arduino UNO or clone - around $20-25

3) Power supply. I used a bench power supply in this for test purposes but any 5v switching power supply capable of supplying up to 1 amp should work. See Adafruit site for more info. $5-10

4) Swift for Arduino: one time license cost is $10 including unlimited upgrades forever.

5) Some fly leads/wires to hook it all up. You probably already have these or a few dollars if not.


Total price: around $40 - $85.

Comments

Popular posts from this blog

Halloween LED lights on a plastic trick or treat cauldron

All about bootloaders

code signing, entitlements, bundles, sandboxes, hardened runtime, notarisation, app store security