-
Notifications
You must be signed in to change notification settings - Fork 7
Neopixels
Neopixels are strings of small, very bright, multicoloured (RGB) LEDs that are individually addressable using a fast, custom one wire protocol.
They can easily be programmed in Swift for Arduino using built in functions in the standard hardware library (the AVR
module).
For example, to write all the neopixels on a 20 neopixel string connected to pin 5 to show a dim red colour...
let iLEDPin = 5
pinMode(pin: iLEDPin, mode: OUTPUT)
iLEDFastSetup(pin: iLEDPin, pixelCount: 20, hasWhite: false, grbOrdered: true)
let dimRed = iLEDFastMakeColor(red: 10, green: 0, blue: 0, white: 0)
for _ in 1...20 {
iLEDFastWritePixel(color: dimRed)
}
// Latch so color is displayed
delay(microseconds: 6)
Or as an alternative you can take a "buffered" approach, where you create a RAM buffer that retains a colour for each one of your neopixel string (up to 255 pixels). Then you can define any pattern you require, update the RAM buffer for each pixel and finally write the whole buffer in one atomic operation. This approach is particularly good for animated displays, such as a rolling or pulsing animation where you may want to update the contents of neopixel based on another.
let iLEDPin = 5
let pixelCount = 20
pinMode(pin: iLEDPin, mode: OUTPUT)
guard let buffer = iLEDFastSetupBuffered (pin: iLEDPin, pixelCount: pixelCount, hasWhite: false, grbOrdered: true) else {
fatalError()
}
// setup the buffer with a rainbow...
let hueDeltaPerPixel = 255 / pixelCount
for i in 0..<pixelCount {
// note: we are using an UnsafeMutablePointer due to limitations in the current v1 compiler/runtime/stdlib
// so be careful! make sure to follow the above example thoughtfully and avoid buffer overrun
// it is perfectly possible to write to buffer[-1] with potentially devastating consequences!
buffer[Int(i)] = iLEDFastMakeColor(hue: i &* hueDeltaPerPixel, saturation: 255, value: 255, white: 0)
}
iLEDFastWriteBuffer()
func rollBuffer() {
let firstPixel = buffer[0]
for i in 0..<pixelCount {
// note: we are using an UnsafeMutablePointer due to limitations in the current v1 compiler/runtime/stdlib
// so be careful! make sure to follow the above example thoughtfully and avoid buffer overrun
// it is perfectly possible to write to buffer[-1] with potentially devastating consequences!
buffer[Int(i)] = buffer[Int(i&+1)]
}
buffer[Int(pixelCount-1)] = firstPixel
iLEDFastWriteBuffer()
}
while true {
delay(ms: 100)
rollBuffer()
}
The original LEDs were made by World Semiconductor, in Shenzen, China and most are still made there. They came to fame sold by Adafruit, who call them "neopixels" and the name is widely used for all such LEDs. In the S4A documentation, we tend to use the term "intelligent LEDs" or "iLED", to reflect the fact that actually "neopixel" is a trademark of Adafruit, but most people will colloquially call them "neopixels".
A discussion on how neopixels are programmed...
Some simple projects...
TBA