- WS281x led control
- HSV to RGB and RGB to HSV
- Filling and Clearing
- Brightness control
Currently, the library is tested on STM32F4 and STM32H7 microcontrollers, but there's a good chance it will be compatible with other STM32 microcontrollers.
The library has been tested with WS2815 and WS2812 LEDs. It is highly likely to also work with WS2813 and WS2812B, or similar variants. It might not work with WS2811 LEDs.
In testing, the library demonstrated a refresh rate of approximately 19.5 LEDs per millisecond.
Enable SPI you want and set settings to:
These are tested settings, I encourage you to experiment with them.
The Baud Rate is the important setting for Clock.
Connect the strip control pin to the pin described as SPIx_MOSI in the pinout view.
To install simply choose version for your CPU and insert the library among your other program files.
Also install the files from the color
folder.
To use this library, create an object of the class strip from the swietlib namespace:
swietlib::strip ledstrip(&hspi2, 120);
Replace &hspi2
with a reference to the SPI handler you have chosen.
(Remember to include spi.h
)
Set LEDs color with this:
ledstrip.setPixel(10, swietlib::rgbColor(255, 128, 0));
or
ledstrip.setPixel(10, 255, 128, 0);
First argument means which LED in turn to change color,
To clear the strip use:
ledstrip.clear();
To fill all the strip use:
ledstrip.fill(255, 128, 0);
To refresh strip (send data to strip) you need to use:
ledstrip.refresh();
More info you can find there.
Simple moving rainbow effect using this library.
#include <cstdint>
#include "swietlib.hpp"
#include "swietlibColor.hpp"
#include "stm32f4xx.h"
#include "spi.h"
#define LED_COUNT 120
...
swietlib::strip ledstrip(&hspi2, LED_COUNT);
// Change it to modify the density of the rainbow ↓↓↓
for (uint16_t hue = 0, i = 0; i <= LED_COUNT; i++, hue += (65535 / (LED_COUNT / 2))) {
ledstrip.setPixel(i, swietlib::hsvColor(hue, 255, 255).toRGB());
}
ledstrip.setBrightness(64);
uint16_t offset = 1;
while (1) {
ledstrip.refreshOffset(offset);
HAL_Delay(20);
if (offset >= LED_COUNT) offset = 0;
offset++;
}