/SwiftLED

SwiftLED is a high-level library to control addressable LEDs (e.g. WS2812/NeoPixel) with animations and effects. It's designed to run on a Raspberry Pi.

Primary LanguageSwift

SwiftLED

SwiftLED is a high-level library to control addressable LEDs (e.g. WS2812/NeoPixel) with animations and effects. It's designed to run on a Raspberry Pi.

There is a companion app for MacOS called SwiftLEDSimulator that allows you to test out your LED programs before sending your code to the embedded device (e.g. Raspberry Pi). The animations below are screenshots from this app.

  1. Static Examples
  2. Animation Examples
  3. Supported Hardware
  4. Installation
  5. Usage
  6. How It Works

Static Examples:

Set Single LED Color

ledStrip.leds[5].fill(.orange)


Set All LED's Color

ledStrip.fill(Color(red: 0, green: 100, blue: 155))


Set Every Fifth LED's Color

let leds = stride(from: 0, to: ledStrip.leds.count, by: 5).map { ledStrip.leds[$0] }
leds.fill(.green)


Fill All LEDs with Gradient

ledStrip.fill(Gradient(.red, .orange, .yellow, .orange, .red))


Animation Examples:

Single Color LED Move

ledStrip.repeatForever {
  ledStrip.animate(.red, start: 0..<1, end: 45..<46, duration: 2.0)
  ledStrip.animate(.green, start: 0..<1, end: 45..<46, duration: 2.0)
  ledStrip.animate(.blue, start: 0..<1, end: 45..<46, duration: 2.0)
}


Dual Color LED Move

ledStrip.repeatForever {
  ledStrip.animate(.red, start: 45..<47, end: 0..<2, duration: 2.0)
  ledStrip.animate(.green, start: 0..<2, end: 45..<47, duration: 2.0)
  ledStrip.animate(.blue, start: 45..<47, end: 0..<2, duration: 2.0)
}


Color Wipes

ledStrip.sequence {
  ledStrip.animate(.red, start: 0..<1, end: 0..<45, duration: 1.0)
  ledStrip.animate(.green, start: 0..<1, end: 0..<45, duration: 1.0)
  ledStrip.animate(.blue, start: 0..<1, end: 0..<45, duration: 1.0)
}


Comet

ledStrip.repeat(3) {
  ledStrip.animate(Gradient(.black, .white), start: 0..<6, end: 45..<51, duration: 2)
}


Rainbow Gradient Color Wipe

let rainbow = Gradient(.red, .green, .blue, .red)

ledStrip.repeat(2) {
  ledStrip.animate(rainbow, start: 0..<1, end: 0..<180, duration: 2.0, fillSize: 180)
}


Moving Rainbow Gradient

let rainbow = Gradient(.red, .green, .blue, .red)

ledStrip.repeat(2) {
  ledStrip.animate(rainbow, start: 0..<45, end: 45..<90, duration: 2.0)
}


Theatre Chase

let rainbow = Gradient(.red, .green, .blue, .red)

ledStrip.sequence {
  ledStrip.threatreChase(.red, repeatCount: 30)
  ledStrip.threatreChase(rainbow, repeatCount: 30)
}


Pulse Effect stacked with Popcorn Effect

ledStrip.repeatForever {
    ledStrip.animate(Color(red: 200, green: 0, blue: 0), duration: 3.0, curve: .easeOut)
    ledStrip.animate(Color(red: 20, green: 0, blue: 0), duration: 3.0, curve: .easeIn)
}
ledStrip.popcorn(Color(red: 255, green: 0, blue: 0), averagePopsPerSecond: 6, averagePopDuration: 0.6)

Supported Hardware:

LEDS:

  1. WS2812 / WS2812b (aka NeoPixel)

Boards:

  1. Raspberry Pi 1/2/3/4

Installation

  1. Install Swift on your Raspberry Pi (https://github.com/uraimo/buildSwiftOnARM) or (https://lickability.com/blog/swift-on-raspberry-pi/)
  2. Create swift project with a dependency for this package

Usage

How it works