fastLED库函数介绍

一、基础函数

参考:FastLED库 (taichi-maker.com)

1. FastLED.addLeds<WS2812,LED_PIN,GRB>(leds , NUM_LEDS)

初始化LED控制光带

  • WS2812:控制灯珠的类型
  • LED_PIN:控制板上使用的IO端口
  • GRB:色彩类型
  • leds:光带
  • NUM_LEDS:灯珠的总数量

2. FastLED.show();

刷新灯的色彩显示

3. FastLED.clear();

将所有灯熄灭

4. FastLED.setBrightness(30)

设置灯带亮度,最大为255

5. fill_solid(leds, 30, CRGB::Red)

将leds光带的从头数前30个灯珠设置为红色

  • leds:光带
  • 30:从起始位置开始算的数量
  • CRGB::Red:设置的颜色值

fill_solid(leds+5, 3, CRGB::Red)
设置leds光带从头数第6个光珠到后面3个光珠颜色为红色

6. fill_rainbow(leds, 30, beginHue, deltaHue)

为一系列 LED 装上彩虹色。组成彩虹的颜色饱和度和亮度均为满值。
将leds光带的从头数30个灯珠设置为渐变彩虹色

  • leds:光带
  • 30:从起始位置开始算的数量
  • beginHue:彩虹色的起始色调值,最大为255
  • deltaHue:相邻灯珠的色调差,最大为255

fill_rainbow(leds+5, 30, beginHue, deltaHue)
设置leds光带从头数第6个光珠到后面的30个光珠颜色的彩虹色

7. fill_gradient_RGB(leds, 0, CRGB::Red, 29, CRGB::Blue)

将leds光带的从头数30个灯珠设置为渐变色

  • leds:光带
  • 0:开始的灯珠编号
  • CRGB::Red:开始的颜色
  • 29:最后一个灯珠的编号
  • CRGB::Blue:结束的颜色

用法
fill_gradient_RGB(leds, 0, CRGB::Red, 29, CRGB::Blue)
以上语句将leds光带的从头数30个灯珠设置为渐变色。灯带头部起始颜色为红色。灯带尾部结束颜色为蓝色。灯带中间为由红色到蓝色的渐变色。

fill_gradient(leds, 0, CHSV(50, 255,255) , 29, CHSV(150,255,255), SHORTEST_HUES)
以上语句将leds光带的从头数30个灯珠设置为渐变色。灯带头部起始颜色为HSV(50, 255,255)色彩。灯带尾部结束颜色为CHSV(150,255,255)色彩。灯带颜色过度采用短色调过渡方式。

fill_gradient(leds, 0, CHSV(50, 255,255) , 29, CHSV(150,255,255), LONGEST_HUES)
以上语句将leds光带的从头数30个灯珠设置为渐变色。灯带头部起始颜色为HSV(50, 255,255)色彩。灯带尾部结束颜色为CHSV(150,255,255)色彩。灯带颜色过度采用长色调过渡方式。


#include "FastLED.h"            // 此示例程序需要使用FastLED库
 
#define NUM_LEDS 30             // LED灯珠数量
#define LED_DT 9                // Arduino输出控制信号引脚
#define LED_TYPE WS2812         // LED灯带型号
#define COLOR_ORDER GRB         // RGB灯珠中红色、绿色、蓝色LED的排列顺序
 
uint8_t max_bright = 128;       // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
 
CRGB leds[NUM_LEDS];            // 建立光带leds
 
void setup() { 
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带  
  FastLED.setBrightness(max_bright);                            // 设置光带亮度
}
 
void loop () {       
  // fill_gradient 显示两种颜色  
  //fill_gradient(leds, 0, CHSV(50, 255,255) , 29, CHSV(150,255,255), SHORTEST_HUES);
  fill_gradient(leds, 0, CHSV(50, 255,255) , 29, CHSV(150,255,255), LONGEST_HUES);
  FastLED.show();
  delay(50);
} 

8. fill_gradient(leds, 0, CHSV(50, 255,255) , 29, CHSV(150,255,255), SHORTEST_HUES)

  • CHSV(50, 255,255):起始色彩,HSV格式
  • CHSV(150,255,255):结束色彩
  • SHORTEST_HUES:采用短色过渡方式,两个颜色值之间最短的颜色过渡
fill_gradient(leds, 0, CHSV(50, 255,255) , 29, CHSV(150,255,255),LONGEST_HUES)

LONGEST_HUES:采用长色过渡方式,两个颜色值之间最长的颜色过渡

注意:使用SHORTEST_HUES和LONGEST_HUES的时候必须采用CHVS方式填色

9. fill_palette (leds, 30, 0, 8, OceanColors_p, 255, LINEARBLEND);

将leds光带的从头数30个灯珠设置为渐变色。渐变色的色彩全部来自FastLED预设色板OceanColors_p

  • leds:灯带
  • 30:从起始位置开始算的数量
  • 0:灯带头部第一个灯珠的色板色彩序号
  • 8:相邻两个灯珠见色板色彩需要相差8
  • OceanColors_p:预设色板
  • 255:亮度最高255
  • LINEARBLEND:线性过渡效果
fill_palette (leds+5, 15, 31, 5, OceanColors_p, 255, LINEARBLEND);

设置光带开头第6个灯珠到后面的15个灯珠为OceanColors_p色板所设定的颜色,颜色编号从第31开始,间隔5的线性渐变

预设色板有:CloudColors_p、LavaColors_p、OceanColors_p、ForestColors_p、RainbowColors_p、RainbowStripeColors_p、PartyColors_p、HeatColors_p

10. ColorFromPalette

ColorFromPalette函数返回指定色板中的颜色。我们可以将ColorFromPalette的返回值设置LED色彩。

用法
leds[0] = ColorFromPalette( RainbowColors_p, 0, 128, LINEARBLEND);
以上语句将leds光带的第一个灯珠设置为RainbowColors_p色板中颜色序号为0的颜色(红色)。LED亮度为128。色彩过渡为线性过渡效果(LINEARBLEND)。

leds[9] = ColorFromPalette( OceanColors_p, 120, 255, LINEARBLEND);
以上语句将leds光带的第十个灯珠设置为OceanColors_p色板中颜色序号为120的颜色。LED亮度为255。色彩过渡为线性过渡效果(LINEARBLEND)。

如需了解更多FastLED库色板的知识,请点击这里


#include "FastLED.h"            // 此示例程序需要使用FastLED库
 
#define NUM_LEDS 30             // LED灯珠数量
#define DATA_PIN 9              // Arduino输出控制信号引脚
#define LED_TYPE WS2812         // LED灯带型号
#define COLOR_ORDER GRB         // RGB灯珠中红色、绿色、蓝色LED的排列顺序
 
#define twinkleInterval  100    //  闪烁间隔时间 (毫秒)
#define twinkleChance 80        //  闪烁数量,数值越大闪烁越多(0-255) 
 
uint8_t max_bright = 128;       // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
 
CRGB leds[NUM_LEDS];            // 建立光带leds
 
void setup() { 
  Serial.begin(9600);           // 启动串行通讯
  delay(1000);                  // 稳定性等待
 
  LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带
  
  FastLED.setBrightness(max_bright);                            // 设置光带亮度
}
 
void loop() {      
  int pos = random8(NUM_LEDS);
  EVERY_N_MILLISECONDS( twinkleInterval ) { 
    if( random8() < twinkleChance) {
      leds[pos] = ColorFromPalette( PartyColors_p, random8(255), 128, LINEARBLEND);
    }   
  }
  EVERY_N_MILLISECONDS( 20 ) { 
    fadeToBlackBy( leds, NUM_LEDS, 10);
  }    
  FastLED.show(); 
  delay(50);       
}

11. fadeToBlackBy()fadeLightBy()

void fadeToBlackBy(CRGB *leds,uint16_t num_leds,uint8_t 	 fadeBy )
void fadeLightBy(CRGB *leds,uint16_t num_leds,uint8_t 	 fadeBy )

两个函数都是一次性降低像素阵列的亮度。fadeToBlackBy函数将逐渐熄灭LED灯带。fadeLightBy()并不会熄灭灯带


Example showing how fadeToBlackBy works. Open the serial monitor to see r,g,b values.
Each time fadeToBlackBy(X) is called it will fade a pixel’s r,g,b values by a certain percentage. Calling it repeatedly will eventually fade to black (where r,g,b = 0,0,0).
Note that the specified number is a percentage out of 255, so for example, using 64 would be 64/255 = 25%, and using 10 would be 10/255 = 4%. 0 would be no fading.

示例显示fadeToBlackBy如何工作。打开串行显示器,查看r,g,b的值。
每次调用fadeToBlackBy(X)时,它将使像素的r,g,b值按一定百分比褪色。反复调用它将最终消失为黑色(其中r,g,b = 0,0,0)。
注意,指定的数字是255的百分比,因此,例如,使用64将是64/255 = 25%,使用10将是10/255 = 4%。0表示没有衰落。
It can be used to fade all leds of a CRGB array like:它可以用来减弱CRGB阵列的所有led,比如:

fadeToBlackBy(leds, NUM_LEDS, 128); // 128/255 = 50%
Or can be used to fade a specific pixel.或可用于使特定像素褪色。
leds[i].fadeToBlackBy(128); // fade by 50%

用法
fadeToBlackBy( leds, 30, 10);
以上语句将leds光带的30个灯珠亮度调低10。(注:LED亮度值可选数值范围为0 – 255)

示例程序


#include "FastLED.h"            // 此示例程序需要使用FastLED库
 
#define NUM_LEDS 30             // LED灯珠数量
#define DATA_PIN 9              // Arduino输出控制信号引脚
#define LED_TYPE WS2812         // LED灯带型号
#define COLOR_ORDER GRB         // RGB灯珠中红色、绿色、蓝色LED的排列顺序
 
#define twinkleInterval  100    //  闪烁间隔时间 (毫秒)
#define twinkleChance 80        //  闪烁数量,数值越大闪烁越多(0-255) 
 
uint8_t max_bright = 128;       // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
 
CRGB leds[NUM_LEDS];            // 建立光带leds
 
void setup() { 
  Serial.begin(9600);           // 启动串行通讯
  delay(1000);                  // 稳定性等待
 
  LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带
  
  FastLED.setBrightness(max_bright);                            // 设置光带亮度
}
 
void loop() {      
  int pos = random8(NUM_LEDS);
  EVERY_N_MILLISECONDS( twinkleInterval ) { 
    if( random8() < twinkleChance) {
      leds[pos] = ColorFromPalette( PartyColors_p, random8(255), 128, LINEARBLEND);
    }   
  }
  EVERY_N_MILLISECONDS( 20 ) { 
    fadeToBlackBy( leds, NUM_LEDS, 10);
  }    
  FastLED.show(); 
  delay(50);       
}

12. fadeUsingColor()

fadeUsingColor(CRGB *leds, uint16_t numLeds, const CRGB &colormask)

降低像素阵列的亮度,就像通过指定颜色的透明滤镜看到的一样。

例如,如果颜色掩码为 CRGB(200,100,50),那么像素的红色将淡化为 256 分之 200,绿色将淡化为 256 分之 100,蓝色将淡化为 256 分之 50。这个例子将呈现出 "热褪色 "的效果,即白色褪成黄色,然后是红色,最后是黑色。您也可以使用 CRGB::Blue 这样的色彩任务,将红色和绿色元素清零,而蓝色(大体上)保持不变。

13. nscale8()

void nscale8(CRGB * leds, uint16_t num_leds, uint8_t scale )

一次性缩放像素阵列的亮度。即使 "scale "不为零,该函数最终也会逐渐变黑。

参数
leds: 一个指向要缩放的 LED 阵列的指针
num-leds: 要缩放的 LED 数量
scale 每个 LED 的缩放比例

// 将颜色减至先前值的 75%(192/256 分之一),最终变为全黑
  leds[i].nscale8( 192);

14. nscale8_video()

void nscale8_video(CRGB * leds, uint16_t num_leds, uint8_t scale )

一次性缩放像素阵列的亮度。即使 "scale "不为零,该函数保证不会完全变黑。

15. scale8()

LIB8STATIC_ALWAYS_INLINE uint8_t scale8 ( uint8_t i,fract8 scale )
将一个字节按第二个字节的比例缩放,将其视为分母为 256 的分数的分子。

换句话说,计算 i * (scale / 256)

  • 参数
    i 要缩放的输入值
    scale,单位为 n/256
  • 返回值
    缩放值

二、灯的操作

1. leds[dot] = CRGB::Blue

将某个LED设置为指定颜色

leds[dot].r=128

将灯的r,g,b颜色进行分别设置,最大为255

2. leds[0] = ColorFromPalette( RainbowColors_p, 0, 128, LINEARBLEND)

将灯带中第一个灯珠的颜色设置为RainbowColors_p色板指定的颜色

  • RainbowColors_p:使用色板
  • 0:颜色序号
  • 128:亮度
  • LINEARBLEND:线性渐变

三、其他函数

0.

以给定的 “每分钟节拍数”(BPM)复位的波形发生器。
标准的 "节拍 "功能可产生 “锯齿波”,从 0 上升到最大值,然后复位,以指定的频率(BPM)不断重复该周期。
sin "版本的功能类似,但会以指定频率产生正弦振荡波。

提供 BPM 的方式有两种。比较简单的方式是将 BPM 指定为 1-255 之间的简单 8 位整数(如 “120”)。指定 BPM 的更复杂方式是提供分数 "Q8.8 "定点数(accum88),其中包括 8 位整数部分和 8 位分数部分。最简单的方法是将浮点 BPM 值(如 120.3)乘以 256(如本例中的 30796),然后将其作为 16 位 BPM 参数传递。

这些函数最初是为了让整个动画项目产生亮度脉冲而设计的。为了达到这一效果,请在现有的 "FastLED.show() "调用之上添加这一行:

uint8_t bright = beatsin8( 60 /*BPM*/, 192 /*dimmest*/, 255 /*brightest*/ ));   // dimmest 最暗
FastLED.setBrightness( bright );
FastLED.show();

The entire animation will now pulse between brightness 192 and 255 once per second.
整个动画将每秒在亮度 192 和 255 之间跳动一次。

1. beatsin

原文:http://www.taichi-maker.com/homepage/reference-index/arduino-library-index/fastled-library/beatsin8/

beatsin8


LIB8STATIC uint8_t beatsin8	(	accum88 	beats_per_minute,
							uint16_t 	lowest = 0,
							uint16_t 	highest = 255,
							uint32_t 	timebase = 0,
							uint16_t 	phase_offset = 0 
)	

参数

beats_per_minute 波形的频率,十进制
lowest 正弦波的最低输出值
highest 正弦波的最高输出值
timebase 波形与 millis() 定时器的时间偏移量
phase_offset 波形与当前位置的相位偏移

beatsin8可以视作一个信号发生器。它的返回值会沿着正弦曲线返回数值。

用法

beatsin8(beats_per_minute, lowest, highest)

参数

beats_per_minute: 每分钟产生循环次数
lowest: 最小值
lowest: 最大值

beatsin16

以给定的 BPM 生成 16 位正弦波,并在给定范围内振荡。
用法

LIB8STATIC uint16_t beatsin16	(	accum88 	beats_per_minute,
							uint16_t 	lowest = 0,
							uint16_t 	highest = 65535,
							uint32_t 	timebase = 0,
							uint16_t 	phase_offset = 0 
)	

LIB8STATIC  将 LIB8TION 成员函数定义为带 "未使用 "属性的静态内联函数。

参数

beats_per_minute 波形的频率,十进制
lowest 正弦波的最低输出值
highest 正弦波的最高输出值
timebase 波形与 millis() 定时器的时间偏移量
phase_offset 波形与当前位置的相位偏移

示例语句

以下图示为 beatsin8(10, 0, 255) 返回值示意图。↓
这一示例语句中第一个参数为10。这说明此函数所产生的正弦曲线周期时间为6秒(1分钟有60秒,60除以参数10等于6秒钟)。第二个参数为0。这说明此函数所产生的正弦曲线最小返回值为0(如图中绿色数字所示)。第三个参数为255。这说明此函数所产生的正弦曲线最大返回值为255(如图中红色数字所示)。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. random8

random8()
函数将返回0-255之间的随机数值。返回值最大值可以设置为自定义的0-255间数值
random8(uint_8 lim)
生成介于 0 和 lim 之间的 8 位随机数。
`random8(uint8_t min, uint_8 lim)``
在给定范围内生成一个 8 位随机数。

参数
min 随机数的下限,包含在内
lim 随机数的上限,不含

用法
random8()
函数将返回0-255之间的随机数值。返回值最大值可以设置为自定义的0-255间数值

random8(10)
以上语句将返回0-9之间的随机数值。

示例程序
Arduino

#include "FastLED.h"  // FastLED库
 
void setup() {
  Serial.begin(9600);                                
}
 
void loop() {
  //串口监视器输出random8函数返回数值
  Serial.println(random8(10));
  delay(50);
}

3. EVERY_N_SECONDS

EVERY_N_SECONDS函数所包含的程序内容将定时执行。执行时间间隔由函数参数决定。

用法
EVERY_N_SECONDS(10)
以上函数所包含的程序内容将定时执行,执行时间间隔为10秒钟。

EVERY_N_SECONDS(1)
以上函数所包含的程序内容将定时执行,执行时间间隔为1秒钟。

示例程序

 
#include "FastLED.h"            // 此示例程序需要使用FastLED库
 
#define NUM_LEDS 30             // LED灯珠数量
#define DATA_PIN 9              // Arduino输出控制信号引脚
#define LED_TYPE WS2812         // LED灯带型号
#define COLOR_ORDER GRB         // RGB灯珠中红色、绿色、蓝色LED的排列顺序
 
#define twinkleInterval  100    //  闪烁间隔时间 (毫秒)
#define twinkleChance 80        //  闪烁数量,数值越大闪烁越多(0-255) 
 
uint8_t max_bright = 128;       // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
 
CRGB leds[NUM_LEDS];            // 建立光带leds
 
void setup() { 
  Serial.begin(9600);           // 启动串行通讯
  delay(1000);                  // 稳定性等待
 
  LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带
  
  FastLED.setBrightness(max_bright);                            // 设置光带亮度
}
 
void loop() {      
  int pos = random8(NUM_LEDS);
  EVERY_N_MILLISECONDS( twinkleInterval ) { 
    if( random8() < twinkleChance) {
      leds[pos] = ColorFromPalette( PartyColors_p, random8(255), 128, LINEARBLEND);
    }   
  }
  EVERY_N_MILLISECONDS( 20 ) { 
    fadeToBlackBy( leds, NUM_LEDS, 10);
  }    
  FastLED.show(); 
  delay(50);       
}

4. EVERY_N_MILLISECONDS

EVERY_N_SECONDS函数所包含的程序内容将定时执行。执行时间间隔由函数参数决定。

用法
EVERY_N_MILLISECONDS(100)
以上函数所包含的程序内容将定时执行,执行时间间隔为100毫秒。

EVERY_N_MILLISECONDS(500)
以上函数所包含的程序内容将定时执行,执行时间间隔为500毫秒。

** 示例程序**


#include "FastLED.h"            // 此示例程序需要使用FastLED库

#define NUM_LEDS 30             // LED灯珠数量
#define DATA_PIN 9              // Arduino输出控制信号引脚
#define LED_TYPE WS2812         // LED灯带型号
#define COLOR_ORDER GRB         // RGB灯珠中红色、绿色、蓝色LED的排列顺序

#define twinkleInterval  100    //  闪烁间隔时间 (毫秒)
#define twinkleChance 80        //  闪烁数量,数值越大闪烁越多(0-255) 

uint8_t max_bright = 128;       // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高

CRGB leds[NUM_LEDS];            // 建立光带leds

void setup() { 
  Serial.begin(9600);           // 启动串行通讯
  delay(1000);                  // 稳定性等待

  LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带
  
  FastLED.setBrightness(max_bright);                            // 设置光带亮度
}

void loop() {      
  int pos = random8(NUM_LEDS);
  EVERY_N_MILLISECONDS( twinkleInterval ) { 
    if( random8() < twinkleChance) {
      leds[pos] = ColorFromPalette( PartyColors_p, random8(255), 128, LINEARBLEND);
    }   
  }
  EVERY_N_MILLISECONDS( 20 ) { 
    fadeToBlackBy( leds, NUM_LEDS, 10);
  }    
  FastLED.show(); 
  delay(50);       
}

问题解答

Neopexel官方获取能得到一些启发,请读这里Adafruit NeoPixel 超级指南

1. 我的Arduino伺服代码在与NeoPixels结合使用时停止工作!

Unfortunately the NeoPixel and Servo libraries don’t play nice together; one is dependent on periodically disabling interrupts, the other absolutely requires interrupts. There are a couple of options here:
不幸的是,NeoPixel 和 Servo 库不能很好地结合在一起;一个依赖于定期禁用的中断,另一个绝对需要中断。这里有几个选项:

  • Use a dedicated servo control shield or breakout board, offloading that task from the processor so interrupts are a non-issue.
    使用专用的伺服控制扩展板或分线板,从处理器中卸载该任务,因此不会出现中断问题。
  • Use a hardware-PWM-based servo library rather than the stock Arduino Servo library. This can provide rock-steady servo timing without interrupts, but can only control a very limited number of servos (2-3), and only on very specific pins.
    使用基于硬件PWM的伺服库,而不是库存的Arduino伺服库。这可以在不中断的情况下提供坚如磐石的伺服定时,但只能控制非常有限的伺服系统(2-3个),并且只能控制非常特定的引脚。
2. 驱动 NeoPixels 时,我无法在红外接收器上接收红外代码!

Just like servos, the infrared library uses software interrupts to poll the IR LED, while the standard NeoPixel library blocks interrupts while NeoPixel are being updated.
就像舵机一样,红外库使用软件中断来轮询红外 LED,而标准 NeoPixel 库在 NeoPixel 更新时阻止中断。

If you don’t constantly update the NeoPixel, IR will work in between updates, but if you update them all the time, you will need to use another library and a microcontroller more capable than an Uno or Mega. Ideally one with DMA so that NeoPixels don’t take up any CPU cycles.
如果您不经常更新 NeoPixel,IR 将在更新之间工作,但如果您一直更新它们,您将需要使用另一个库和比 Uno 或 Mega 更强大的微控制器。理想情况下,带有 DMA 的 NeoPixel 不会占用任何 CPU 周期。

Marc MERLIN explains how to this depending on what chip you have (Teensy, ESP8266 or ESP32):
Marc MERLIN 根据您拥有的芯片(Teensy、ESP8266 或 ESP32)解释了如何做到这一点:
http://marc.merlins.org/perso/arduino/post_2017-04-03_Arduino-328P-Uno-Teensy3_1-ESP8266-ESP32-IR-and-Neopixels.html

3. 刷新一串 (N)灯带的速度有多快?

NeoPixels receive data from a fixed-frequency 800 KHz datastream (except for “V1” Flora pixels, which use 400 KHz). Each bit of data therefore requires 1/800,000 sec — 1.25 microseconds. One pixel requires 24 bits (8 bits each for red, green blue) — 30 microseconds. After the last pixel’s worth of data is issued, the stream must stop for at least 50 microseconds for the new colors to “latch.”
NeoPixels 从固定频率的 800 KHz 数据流接收数据(“V1”Flora 像素除外,它使用 400 KHz)。因此,每比特数据需要 1/800,000 秒 — 1.25 微秒。一个像素需要 24 位(红色、绿色、蓝色各 8 位)——30 微秒。在发出最后一个像素的数据后,流必须停止至少 50 微秒才能“锁定”新颜色。

For a strip of 100 pixels, that’s (100 * 30) + 50, or 3,050 microseconds. 1,000,000 / 3,050 = 328 updates per second, approximately.
对于 100 像素的条带,即 (100 * 30) + 50,即 3,050 微秒。大约每秒 1,000,000 / 3,050 = 328 次更新。

However… 然而。。。

That’s only the time needed to push the bits down the wire. The actual refresh rate will be something less than this, and can’t be estimated as a single number for all cases. It takes time to process each “frame” of animation. How much time depends on the complexity of the math and the efficiency of the code (for example, floating-point calculations can be relatively slow). The formula above gives a maximum theoretical rate, but that’s just a starting point. Reality in some cases could fall an order of magnitude (or more) below this.
这只是将钻头推下电线所需的时间。实际刷新率将低于此值,并且不能在所有情况下都估计为单个数字。处理动画的每个“帧”都需要时间。时间取决于数学的复杂性和代码的效率(例如,浮点计算可能相对较慢)。上面的公式给出了最大理论速率,但这只是一个起点。在某些情况下,现实可能比这低一个数量级(或更多)。

For exploratory benchmarking, you can always write code as if a large number of pixels were present, and time the result. The extra output bits will simply be ignored by the strip (or you can even test with no NeoPixels connected at all).
对于探索性基准测试,您始终可以编写代码,就好像存在大量像素一样,并对结果进行计时。额外的输出位将被条带忽略(或者您甚至可以在根本没有连接 NeoPixel 的情况下进行测试)。

That won’t do. Now what?
那不行,现在怎么办?

Because NeoPixels use a fixed-frequency clock, options are limited. You can’t switch out for a faster microcontroller and expect substantially different results.
由于 NeoPixel 使用固定频率时钟,因此选项有限。您不能换成更快的微控制器,并期望得到截然不同的结果。

One option is to use a different LED type, such as our DotStar or LPD8806 strips, or WS2801 pixels. These can be driven at higher data rates, though they do have some other tradeoffs with respect to NeoPixels (cost, color resolution and/or pixel density).
一种选择是使用不同的 LED 类型,例如我们的 DotStar 或LPD8806灯条或 WS2801 像素。这些可以以更高的数据速率驱动,尽管它们在NeoPixel方面确实有一些其他的权衡(成本、颜色分辨率和/或像素密度)。

Another is to develop your own code on a more capable microcontroller or an FPGA that drives multiple NeoPixel strips in parallel. One such project — OctoWS2811 for the Teensy 3 microcontroller — is shown later. This sort of thing is a complex undertaking and not recommended for beginners. And even among more experienced programmers, there’s often an unreasonable over-emphasis on data rates when the real bottlenecks lie elsewhere…don’t dwell on this too much unless you can confirm it’s the root of the problem.
另一种方法是在功能更强大的微控制器或并行驱动多个 NeoPixel 条带的 FPGA 上开发自己的代码。一个这样的项目——用于 Teensy 3 微控制器的 OctoWS2811——将在后面展示。这种事情是一项复杂的工作,不建议初学者使用。即使在更有经验的程序员中,当真正的瓶颈在其他地方时,也经常会过度强调数据速率…除非你能确认这是问题的根源,否则不要过多地纠结于此。

4. 我可以使用 (Board X) 控制 NeoPixels 吗?

We currently only offer an Arduino library. See the links later for other devices. For anything beyond this, if considering writing your own library, understand that some processors are better suited to the task than others. Read through the timing requirements shown below and determine if the chip in question can synthesize a signal meeting those specifications. An 8 MHz AVR can just barely keep up…anything slower may have trouble, though some hardware-specific hacks (like clever use of SPI) might make it possible. In many cases, assembly language is required.
我们目前只提供Arduino库。请参阅稍后的其他设备的链接。对于除此之外的任何事情,如果考虑编写自己的库,请了解某些处理器比其他处理器更适合该任务。仔细阅读下面显示的时序要求,并确定相关芯片是否可以合成满足这些规格的信号。8 MHz AVR 几乎跟不上…任何较慢的东西都可能遇到麻烦,尽管一些特定于硬件的黑客(如巧妙地使用SPI)可能会使它成为可能。在许多情况下,汇编语言是必需的。

1. Raspberry Pi 是怎么回事?

This guide focuses mostly on Arduino and similar microcontroller boards where NeoPixel support is most robust. Raspberry Pi control of NeoPixels is possible, but requires special libraries and/or is limited to specific pins…it’s all a bit beyond the scope of this guide. The wiring and power guidance here is valid for any device though.
本指南主要关注Arduino和类似的微控制器板,其中NeoPixel支持最强大。NeoPixels 的 Raspberry Pi 控制是可能的,但需要特殊的库和/或仅限于特定的引脚…这一切都超出了本指南的范围。不过,此处的接线和电源指南对任何设备都有效。

We tend toward DotStar LEDs in Raspberry Pi projects, as in this Raspberry Pi light painter.
在 Raspberry Pi 项目中,我们倾向于使用 DotStar LED,就像这个 Raspberry Pi 光画家一样。

#  DMA NeoPixels for ARM Cortex-M0 Boards 用于 ARM Cortex-M0 板的 DMA NeoPixels

If you’re using a recent “M0” development board such as the Adafruit Feather M0, Circuit Playground Express or Arduino Zero, an alternate NeoPixel library exploits these devices’ direct memory access (DMA) feature to operate more smoothly. Advanced Arduino sketches can then use interrupts with impunity, and code that depends on the millis() or micros() functions will not lose time.
如果您使用的是最新的“M0”开发板,例如 Adafruit Feather M0、Circuit Playground Express 或 Arduino Zero,则备用 NeoPixel 库可利用这些设备的直接内存访问 (DMA) 功能来更流畅地运行。然后,高级Arduino草图可以不受惩罚地使用中断,并且依赖于millis()或micros()函数的代码不会浪费时间。

There’s a corresponding DMA version of the NeoMatrix library as well.
NeoMatrix 库也有相应的 DMA 版本。

Plus a super potent 8-way concurrent NeoPixel DMA library. We offer a companion FeatherWing and breakout board to make connections and level-shifting easier!
外加一个超级强大的 8 路并发 NeoPixel DMA 库。我们提供配套的 FeatherWing 和分线板,使连接和级别转换更容易!

Third-Party Libraries 第三方库

In addition to the previously-mentioned FastLED library, NeoPixel-compatible libraries have been developed for devices beyond Arduino. Please keep in mind that Adafruit did not develop any of this code and can’t fix bugs or offer technical help. This is Wild West stuff.
除了前面提到的FastLED库外,还为Arduino以外的设备开发了兼容NeoPixel的库。请记住,Adafruit 没有开发任何此类代码,也无法修复错误或提供技术帮助。这是狂野西部的东西。

  • OctoWS2811: specifically for the PJRC Teensy 3.0 microcontroller board. Uses DMA to drive up to 8 NeoPixel strips concurrently with minimal processor load. Multiple boards can be cascaded for still larger displays.
    OctoWS2811:专门用于PJRC Teensy 3.0微控制器板。使用 DMA 以最小的处理器负载同时驱动多达 8 个 NeoPixel 灯带。多个板可以级联以获得更大的显示器。
  • FadeCandy: also for Teensy 3.0. Doesn’t support as many pixels as OctoWS2811, but adds dithering and smooth interpolation for color purists.
    FadeCandy:也适用于 Teensy 3.0。不支持像 OctoWS2811 那样多的像素,但为色彩纯粹主义者添加了抖动和平滑插值。
  • LEDscape: specifically for BeagleBone Black. Although the BeagleBone is a multitasking Linux system like the not-NeoPixel-compatible Raspberry Pi, this code exploits hardware features specific to the BeagleBone Black to drive hundreds of meters of NeoPixel strip with virtually no processor load.
    LEDscape:专门用于 BeagleBone Black。尽管 BeagleBone 是一个多任务 Linux 系统,就像不兼容 NeoPixel 的 Raspberry Pi 一样,但此代码利用 BeagleBone Black 特有的硬件功能来驱动数百米长的 NeoPixel 条带,几乎没有处理器负载。
  • WS2812 LED Driver for Parallax Propeller.
    WS2812 用于视差螺旋桨的 LED 驱动器。
  • xCORE NeoPixel test code for the XMOS xCORE startKIT.
    XMOS xCORE startKIT 的 xCORE NeoPixel 测试代码。
  • light_ws2812: an alternate Arduino library for AVR and ARM microcontrollers.
    light_ws2812:用于 AVR 和 ARM 微控制器的备用 Arduino 库。

Some of these are 3.3V devices. See the “Powering NeoPixel” page for notes on controlling 5V NeoPixels from 3.3V microcontrollers.
其中一些是 3.3V 器件。有关从 3.3V 微控制器控制 5V NeoPixel 的说明,请参阅“为 NeoPixel 供电”页面。

1. WS2811?WS2812?SK6812?为什么我会看到不同的名称被提及?

NeoPixels have evolved through several generations:
NeoPixels已经发展了几代:

  • WS2811 is a driver chip separate from the RGB LED
    WS2811是独立于RGB LED的驱动芯片
  • WS2812 integrated the driver and RGB LED in a single package
    WS2812 将驱动器和 RGB LED 集成在单个封装中
  • WS2812B is a newer generation WS2812 — the color balance is slightly different and the data signal “latch” time is longer
    WS2812B是新一代的WS2812,色彩平衡略有不同,数据信号“锁存”时间更长
  • SK6812 is a “WS2812-compatible” part from other manufacturers — we’ve adopted these for most NeoPixel-branded products as they better tolerate different voltages.
    SK6812 是来自其他制造商的“WS2812 兼容”部件——我们已将这些部件用于大多数 NeoPixel 品牌产品,因为它们可以更好地承受不同的电压。

On the original WS2811, chip, the data stream runs at half the speed. When the WS2812 came along, a lot of code and projects had already built up around the WS2811 name — so, for instance, you’ll see projects like “OctoWS2811” and others but they’ll really work with all of these!
在原来的WS2811芯片上,数据流以一半的速度运行。当 WS2812 出现时,许多代码和项目已经围绕 WS2811 名称构建起来——因此,例如,您会看到像“OctoWS2811”这样的项目和其他项目,但它们将真正适用于所有这些项目!

  • 25
    点赞
  • 241
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔚蓝慕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值