FastLED库基础
FastLED库HSV颜色
HSV颜色基本概念
HSV颜色简介
HSV(Hue, Saturation, Value)是根据颜色的直观特性由A. R. Smith在1978年创建的一种颜色表达方法。该方法中的三个参数分别是:色调(H),饱和度(S),明亮度(V)。
HSV参数
色调H
色调参数取值范围为0~255。如上图所示,从红色开始按逆时针方向计算。红色为0,绿色为85,蓝色为170。
饱和度S
饱和度S表示颜色接近光谱色的程度。取值范围为0~255,值越大,颜色越饱和(越接近本色)。值越小,颜色越接近白色。当数值为零时,颜色为白色。
明亮度V
明亮度V表示颜色明亮的程度,取值范围为0~255。对于FastLED库来说,该数值越大,则控制光带的亮度越亮。反之数值越小,则控制光带的亮度越低。当数值为零时,光带完全熄灭。
使用HSV颜色控制光带
语法
CHSV (hVal, sVal, vVal)
或
CHSV HSVcolorName(hVal, sVal, vVal)
参数
hVal – 色调数值
sVal – 饱和度数值
vVal – 明亮度数值
备注
HSVcolorName.h 代表HSVcolorName颜色的色调数值。可对其进行操作。
Quick Examples 简单示例
How easy is the library to use? Here’s a quick example providing some blinking code:
该库的易用性如何?下面是一个提供一些闪烁代码的快速示例:
#include "FastLED.h"
CRGB leds[1];
void setup() {
FastLED.addLeds<NEOPIXEL, 6>(leds, 1);
}
void loop() {
leds[0] = CRGB::White;
FastLED.show();
delay(30);
leds[0] = CRGB::Black;
FastLED.show();
delay(30);
}
HSV Library HSV文库
Unsatisfied with the state of a lot of the HSV and color wheel libraries, we put a lot of work into providing an HSV to RGB conversion library that is fast and adjusted for human color perception. Why use the HSV color space? It’s a bit easier to navigate and provide transitions between colors than using RGB. When defining colors with RGB you’re mixing the Red, Green, and Blue color values. When using HSV, instead, you’re defining the hue of the color (that is, where it is on the color wheel), how saturated it is, and how bright it is. For example, here’s some simple code that will cycle through the colors of a rainbow:
由于对许多 HSV 和色轮库的状态不满意,我们投入了大量工作来提供 HSV 到 RGB 的转换库,该库可以快速并针对人类的颜色感知进行调整。为什么要使用 HSV 色彩空间?与使用 RGB 相比,导航和提供颜色之间的过渡要容易一些。使用 RGB 定义颜色时,您需要混合红色、绿色和蓝色值。相反,使用 HSV 时,您要定义颜色的色调(即它在色轮上的位置)、饱和度和亮度。例如,下面是一些简单的代码,这些代码将循环显示彩虹的颜色:
#include "FastLED.h"
CRGB leds[60];
void setup() {
FastLED.addLeds<NEOPIXEL, 6>(leds, 60);
}
void loop() {
static uint8_t hue = 0;
FastLED.showColor(CHSV(hue++, 255, 255));
delay(10);
}
Math Library 数学库
When doing LED programming, a lot of times you want to do math on the rgb or hsv values to help provide your transitions in brightness and color. However, the AVR/arduino platform isn’t exactly known for the fastest math out there. To help out with this, the library provides a number of math functions tuned for 8-bit operations, including scaling functions, fast sin/cos functions, fast random number generators, and interpolation and memory management functions.
在进行 LED 编程时,很多时候您希望对 rgb 或 hsv 值进行数学运算,以帮助提供亮度和颜色的过渡。然而,AVR/arduino 平台并不以最快的数学运算而闻名。为了帮助解决这个问题,该库提供了许多针对 8 位运算进行调整的数学函数,包括缩放函数、快速正弦/余弦函数、快速随机数生成器以及插值和内存管理函数。
Low level device access 低级设备访问
Finally, to do a lot of the magic in writing to LEDs, the library has some generalized classes to provide high speed, flexible access to pins and SPI hardware. While write only at the moment, this code can help you access pins and devices quicker than the stock arduino libraries do, even moreso once read support is added to the pin and spi libraries.
最后,为了在写入 LED 时发挥很多魔力,该库提供了一些通用类,以提供对引脚和 SPI 硬件的高速、灵活访问。虽然目前只能写入,但此代码可以帮助您比库存 arduino 库更快地访问引脚和设备,一旦将读取支持添加到引脚和 spi 库中,则更是如此。
leds接线
官方连接
Introduction 介绍
Obviously, before you can go about writing LED programs you need to have some LEDs hooked up to your arduino. This page proves some notes and tips for hooking up said LEDs.
显然,在开始编写 LED 程序之前,您需要将一些 LED 连接到您的 arduino。本页演示了连接所述 LED 的一些注意事项和技巧。
The Wires 接线
Most modern LED chipsets come with 3 or 4 pins or connectors on them. Some chipsets, like the WS2801, use 4 pins: Power, Ground, Data, and Clock. Others, like the WS2812B only use three: Power, Ground, and Data. Note that Power and Ground are always present. These wires are what supply power to the LEDs and allow them to light up. The Data pin is how led data gets from your arduino or micro controller to the actual LEDs. Finally, for some chipsets, the clock pin is used in conjunction with the data pin for transmitting data. See the [ChipsetOverview Chipset Overview] page for specific information on the various chipsets and what they use.
大多数现代 LED 芯片组都带有 3 或 4 个引脚或连接器。某些芯片组(如 WS2801)使用 4 个引脚:电源、接地、数据和时钟。其他的,如WS2812B只使用三种:电源、接地和数据。请注意,电源和接地始终存在。这些电线为 LED 供电并允许它们点亮。数据引脚是 LED 数据从 arduino 或微控制器传输到 LED 的方式。最后,对于某些芯片组,时钟引脚与数据引脚结合使用以传输数据。有关各种芯片组及其用途的具体信息,请参阅 [芯片组概述 芯片组概述] 页面。
Connecting the wires 连接电线
The data and clock pins connect to their appropriate pins on the arduino. There’s a variety of ways to do this, depending on the specifics of your controller. You can use jumper cables, screw terminals, solder wires directly, etc…
数据和时钟引脚连接到arduino上的相应引脚。有多种方法可以做到这一点,具体取决于控制器的具体情况。您可以直接使用跨接电缆、螺丝端子、焊锡丝等…
The power and ground pins connect to positive(+) and negative(-) on your power supply to get power to the LEDs. This should also be pretty straightforward.
电源和接地引脚连接到电源上的正极 (+) 和负极 (-),以便为 LED 供电。这也应该非常简单。
In some instances, you may also need to connect ground from the led strips to a ground pin on the arduino. This can help with keeping the data signal nice and clean.
在某些情况下,您可能还需要将 LED 灯条的接地连接到 arduino 上的接地引脚。这有助于保持数据信号的良好和干净。
A Word on Power 关于供电
LED strips and pixels usually come in one of two varieties - 5 volt (also called 5v) and 12 volt (also called 12v). Be aware of what voltage your LED pixels want! Plugging a 12v power supply into the power/ground pins of a 5v strip of led pixels may damage your LED strip. Also - be aware of the power and ground connections. Power should go to Positive(+) on your power supply and Ground should go to Negative(-). While some chipsets may protect against accidentally wiring this backwards, many more done and, once again, doing so may damage your chips.
LED 灯条和像素通常有两种类型之一 - 5 伏和 12 伏。注意您的 LED 像素需要什么电压!将 12v 电源插入 5v LED 像素条的电源/接地引脚可能会损坏您的 LED 灯条。另外 - 请注意电源和接地连接。电源应变为正极 (+),接地应变为负极 (-)。虽然某些芯片组可以防止意外向后接线,但更多的芯片组这样做可能会损坏您的芯片。
The brighter your LEDs are run, the more power they are going to draw. Many arduino devices have a 5v power pin, and it is often tempting to just connect a line from there to your LEDs. However, it is very easy for LEDs to draw a lot of current, more than the arduino can push through its 5v line. The end result may either be your LEDs not being as bright as you like, or perhaps glitching out and glowing a dim red instead of the colors you want, or worse, possibly damaging your arduino by attempting to draw too much current through and burning something out on the board.
LED 运行得越亮,它们消耗的功率就越大。许多 arduino 设备都有一个 5v 引脚,通常从那里将一根线连接到您的 LED。然而,LED 很容易消耗大量电流,超过了 arduino 可以推动其 5v 线路的电流。最终结果可能是您的 LED 没有您喜欢的那么亮,或者可能会出现故障并发出暗红色而不是您想要的颜色,或者更糟糕的是,可能会损坏您的 arduino 试图通过过多的电流并在板上烧掉一些东西。
It is generally considered much better to wire power to the leds independently of the arduino. Most easily, this is done with a splitter from your power supply - with one power lead going to the arduino and the other power lead going to your leds. This will ensure that everything gets all the power that it wants!
通常认为将LED电源连接到独立于 arduino要好得多。最简单的是,这是通过电源的分路器完成的 - 一根电源线连接到 arduino,另一根电源线连接到您的 LED。这将确保一切都获得它想要的所有电力!