13. FastLED 示例3则

Best of FastLED Discussions

1. Fire2012: FastLED 火灾模拟器

// Fire2012: a basic fire simulation for a one-dimensional string of LEDs
// Mark Kriegsman, July 2012.
//
// Compiled size for Arduino/AVR is about 3,968 bytes.#include <FastLED.h>#define LED_PIN     5
#define COLOR_ORDER GRB
#define CHIPSET     WS2811
#define NUM_LEDS    50#define BRIGHTNESS  200
#define FRAMES_PER_SECOND 60CRGB leds[NUM_LEDS];void setup() {delay(3000); // sanity delayFastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);FastLED.setBrightness( BRIGHTNESS );
}void loop()
{// Add entropy to random number generator; we use a lot of it.random16_add_entropy( random());Fire2012(); // run simulation frameFastLED.show(); // display this frame#if defined(FASTLED_VERSION) && (FASTLED_VERSION >= 2001000)FastLED.delay(1000 / FRAMES_PER_SECOND);
#else  delay(1000 / FRAMES_PER_SECOND);
#endif  
}// Fire2012 by Mark Kriegsman, July 2012
// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
//
// This basic one-dimensional 'fire' simulation works roughly as follows:
// There's a underlying array of 'heat' cells, that model the temperature
// at each point along the line.  Every cycle through the simulation, 
// four steps are performed:
//  1) All cells cool down a little bit, losing heat to the air
//  2) The heat from each cell drifts 'up' and diffuses a little
//  3) Sometimes randomly new 'sparks' of heat are added at the bottom
//  4) The heat from each cell is rendered as a color into the leds array
//     The heat-to-color mapping uses a black-body radiation approximation.
//
// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
//
// This simulation scales it self a bit depending on NUM_LEDS; it should look
// "OK" on anywhere from 20 to 100 LEDs without too much tweaking. 
//
// I recommend running this simulation at anywhere from 30-100 frames per second,
// meaning an interframe delay of about 10-35 milliseconds.
//
//
// There are two main parameters you can play with to control the look and
// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
// in step 3 above).
//
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames.  More cooling = shorter flames.
// Default 55, suggested range 20-100 
#define COOLING  55// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire.  Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120void Fire2012()
{
// Array of temperature readings at each simulation cellstatic byte heat[NUM_LEDS];// Step 1.  Cool down every cell a littlefor( int i = 0; i < NUM_LEDS; i++) {heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));}// Step 2.  Heat from each cell drifts 'up' and diffuses a littlefor( int k= NUM_LEDS - 3; k > 0; k--) {heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;}// Step 3.  Randomly ignite new 'sparks' of heat near the bottomif( random8() < SPARKING ) {int y = random8(7);heat[y] = qadd8( heat[y], random8(160,255) );}// Step 4.  Map from heat cells to LED colorsfor( int j = 0; j < NUM_LEDS; j++) {leds[j] = HeatColor( heat[j]);}
}// CRGB HeatColor( uint8_t temperature)
// [to be included in the forthcoming FastLED v2.1]
//
// Approximates a 'black body radiation' spectrum for 
// a given 'heat' level.  This is useful for animations of 'fire'.
// Heat is specified as an arbitrary scale from 0 (cool) to 255 (hot).
// This is NOT a chromatically correct 'black body radiation' 
// spectrum, but it's surprisingly close, and it's extremely fast and small.
//
// On AVR/Arduino, this typically takes around 70 bytes of program memory, 
// versus 768 bytes for a full 256-entry RGB lookup table.CRGB HeatColor( uint8_t temperature)
{CRGB heatcolor;// Scale 'heat' down from 0-255 to 0-191,// which can then be easily divided into three// equal 'thirds' of 64 units each.uint8_t t192 = scale8_video( temperature, 192);// calculate a value that ramps up from// zero to 255 in each 'third' of the scale.uint8_t heatramp = t192 & 0x3F; // 0..63heatramp <<= 2; // scale up to 0..252// now figure out which third of the spectrum we're in:if( t192 & 0x80) {// we're in the hottest thirdheatcolor.r = 255; // full redheatcolor.g = 255; // full greenheatcolor.b = heatramp; // ramp up blue} else if( t192 & 0x40 ) {// we're in the middle thirdheatcolor.r = 255; // full redheatcolor.g = heatramp; // ramp up greenheatcolor.b = 0; // no blue} else {// we're in the coolest thirdheatcolor.r = heatramp; // ramp up redheatcolor.g = 0; // no greenheatcolor.b = 0; // no blue}return heatcolor;
}

2. 二维 XY 矩阵示例

#include <FastLED.h>#define LED_PIN 5
#define COLOR_ORDER GRB
#define CHIPSET WS2811#define BRIGHTNESS 32const uint8_t kMatrixWidth = 16;
const uint8_t kMatrixHeight = 16;
const bool kMatrixSerpentineLayout = true;#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds[NUM_LEDS];void setup() {FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);FastLED.setBrightness(BRIGHTNESS);
}void loop() {uint32_t ms = millis();int32_t yHueDelta32 = ((int32_t)cos16(ms * 27) * (350 / kMatrixWidth));int32_t xHueDelta32 = ((int32_t)cos16(ms * 39) * (310 / kMatrixHeight));DrawOneFrame(ms / 65536, yHueDelta32 / 32768, xHueDelta32 / 32768);FastLED.show();
}void DrawOneFrame(byte startHue8, int8_t yHueDelta8, int8_t xHueDelta8) {byte lineStartHue = startHue8;for (byte y = 0; y < kMatrixHeight; y++) {lineStartHue += yHueDelta8;byte pixelHue = lineStartHue;for (byte x = 0; x < kMatrixWidth; x++) {pixelHue += xHueDelta8;leds[XY(x, y)] = CHSV(pixelHue, 255, 255);}}
}// Helper function that translates from x, y into an index into the LED array
// Handles both ‘row order’ and ‘serpentine’ pixel layouts.
uint16_t XY(uint8_t x, uint8_t y) {uint16_t i;if (kMatrixSerpentineLayout == false) {i = (y * kMatrixWidth) + x;} else {if (y & 0x01) {// Odd rows run backwardsuint8_t reverseX = (kMatrixWidth - 1) - x;i = (y * kMatrixWidth) + reverseX;} else {// Even rows run forwardsi = (y * kMatrixWidth) + x;}}return i;
}

3. 小数像素渲染

#include <FastLED.h>#define DATA_PIN 6
#define NUM_LEDS 30
CRGB leds[NUM_LEDS];// Anti-aliased light bar example
//   v1 by Mark Kriegsman <kriegsman@tr.org>, November 29, 2013
//
// This example shows the basics of using variable pixel brightness
// as a form of anti-aliasing to animate effects on a sub-pixel level, 
// which is useful for effects you want to be particularly "smooth".
//
// This animation shows two bars looping around an LED strip, one moving
// in blocky whole-pixel "integer" steps, and the other one moving 
// by smoothly anti-aliased "fractional" (1/16th pixel) steps.
// The use of "16ths" (vs, say, 10ths) is totally arbitrary, but 16ths are
// a good balance of data size, expressive range, and code size and speed.
//
// Notionally, "I" is the Integer Bar, "F" is the Fractional Bar.int     Ipos   = NUM_LEDS / 2; // position of the "integer-based bar"
int     Idelta = 1; // how many pixels to move the Integer Bar
uint8_t Ihue = 20; // color for Integer Barint     F16pos = 0; // position of the "fraction-based bar"
int     F16delta = 1; // how many 16ths of a pixel to move the Fractional Bar
uint8_t Fhue = 20; // color for Fractional Barint Width  = 4; // width of each light bar, in whole pixelsint InterframeDelay = 40; //msvoid setup() {delay(3000); // setup guardFastLED.addLeds<WS2811, DATA_PIN, GRB>(leds, NUM_LEDS);FastLED.setBrightness(128);
}// Draw an "Integer Bar" of light starting at pixel 'pos', with given 
// width (in whole pixels) and hue.
// This is not the interesting code.
void drawIntegerBar( int intpos, int width, uint8_t hue)
{int i = intpos; // start drawing at "I"for( int n = 0; n < width; n++) {leds[i] += CHSV( hue, 255, 255);i++; if( i == NUM_LEDS) i = 0; // wrap around}
}// Draw a "Fractional Bar" of light starting at position 'pos16', which is counted in
// sixteenths of a pixel from the start of the strip.  Fractional positions are 
// rendered using 'anti-aliasing' of pixel brightness.
// The bar width is specified in whole pixels.
// Arguably, this is the interesting code.
void drawFractionalBar( int pos16, int width, uint8_t hue)
{int i = pos16 / 16; // convert from pos to raw pixel numberuint8_t frac = pos16 & 0x0F; // extract the 'factional' part of the position // brightness of the first pixel in the bar is 1.0 - (fractional part of position)// e.g., if the light bar starts drawing at pixel "57.9", then // pixel #57 should only be lit at 10% brightness, because only 1/10th of it// is "in" the light bar:////                       57.9 . . . . . . . . . . . . . . . . . 61.9//                        v                                      v//  ---+---56----+---57----+---58----+---59----+---60----+---61----+---62---->//     |         |        X|XXXXXXXXX|XXXXXXXXX|XXXXXXXXX|XXXXXXXX |  //  ---+---------+---------+---------+---------+---------+---------+--------->//                   10%       100%      100%      100%      90%        //// the fraction we get is in 16ths and needs to be converted to 256ths,// so we multiply by 16.  We subtract from 255 because we want a high// fraction (e.g. 0.9) to turn into a low brightness (e.g. 0.1)uint8_t firstpixelbrightness = 255 - (frac * 16);// if the bar is of integer length, the last pixel's brightness is the // reverse of the first pixel's; see illustration above.uint8_t lastpixelbrightness  = 255 - firstpixelbrightness;// For a bar of width "N", the code has to consider "N+1" pixel positions,// which is why the "<= width" below instead of "< width".uint8_t bright;for( int n = 0; n <= width; n++) {if( n == 0) {// first pixel in the barbright = firstpixelbrightness;} else if( n == width ) {// last pixel in the barbright = lastpixelbrightness;} else {// middle pixelsbright = 255;}leds[i] += CHSV( hue, 255, bright);i++; if( i == NUM_LEDS) i = 0; // wrap around}
}void loop()
{// Update the "Fraction Bar" by 1/16th pixel every timeF16pos += F16delta;// wrap around at end// remember that F16pos contains position in "16ths of a pixel"// so the 'end of the strip' is (NUM_LEDS * 16)if( F16pos >= (NUM_LEDS * 16)) {F16pos -= (NUM_LEDS * 16);}// For this demo, we want the Integer Bar and the Fraciton Bar// to move at the same speed down the strip.// The Fraction Bar moves 1/16th of a pixel each time through the // loop, so to get the same speed on the strip for the Integer Bar, // we need to move it by 1 full pixel -- but only every 16 times// through the loop.  'countdown' is used to tell when it's time// to advance the Integer Bar position again.static byte countdown = 0;if( countdown == 0) {countdown = 16; // reset countdown// advance Integer Bar one full pixel nowIpos += 1;// wrap around at endif( Ipos >= NUM_LEDS) {Ipos -= NUM_LEDS;   } }// countdown is decremented every time through the loopcountdown -= 1;// Draw everything:// clear the pixel buffermemset8( leds, 0, NUM_LEDS * sizeof(CRGB));// draw the Integer Bar, length=4px, hue=180drawIntegerBar( Ipos, Width, Ihue);// draw the Fractional Bar, length=4px, hue=180drawFractionalBar( F16pos, Width, Fhue);FastLED.show();delay(InterframeDelay);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/26900.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于springboot实现入校申报审批系统项目【项目源码+论文说明】计算机毕业设计

基于springboot实现入校申报审批系统演示 摘要 传统办法管理信息首先需要花费的时间比较多&#xff0c;其次数据出错率比较高&#xff0c;而且对错误的数据进行更改也比较困难&#xff0c;最后&#xff0c;检索数据费事费力。因此&#xff0c;在计算机上安装入校申报审批系统软…

【最新鸿蒙应用开发】——Context上下文对象

应用上下文Context 1. 概述 应用上下文&#xff08;Context&#xff09;是应用程序的全局信息的接口。它是一个抽象类&#xff0c;提供了访问应用程序环境的方法和资源的方法。应用上下文可以用于获取应用程序的资源、启动Ability、发送广播等。每个应用程序都有一个应用上下…

【机器学习】机器学习赋能医疗健康:从诊断到治疗的智能化革命

&#x1f4dd;个人主页&#x1f339;&#xff1a;Eternity._ &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; ❀目录 &#x1f4d2;1. 引言&#x1f4d9;2. 机器学习在疾病诊断中的应用&#x1f9e9;医学影像分析&#xff1a;从X光到3D成像带代码&#x1…

NDS域名解析服务

3.1BIND域名服务基础 3.1.1 DNS系统的作用及类型 正向解析&#xff1a;根据域名查IP&#xff0c;即将指定的域名解析为相对应的IP地址。 反向解析&#xff1a;根据IP地址查域名&#xff0c;即将指定的IP地址解析为相对应的域名。 每一台DNS服务器都只负责管理一个有限范围内…

UltraISO 未找到虚拟光驱

VMWare Win7 虚拟机用 UltraISO 加载镜像时&#xff0c;提示“未找到虚拟光驱”&#xff1f; 1. 安装签名更新程序 https://www.catalog.update.microsoft.com/Search.aspx?qkb4474419 https://blog.csdn.net/m0_52072919/article/details/118895862 2. 打开 CMD, 进入…

C++面试准备

变量作用&#xff1a;给一段指定的内存空间起名&#xff0c;方便操作这段内存。 常量&#xff1a;用于记录程序中不可更改的数据。 #include <iostream> using namespace std;#define DAY 7 int main() {cout << "一周有" << DAY << "…

【Linux之·软件更新源】

系列文章目录 文章目录 前言一、 ARM架构二、x86架构总结 前言 一、 ARM架构 armv7l&#xff08; Ubuntu 14.04.4 LTS&#xff09; deb http://ports.ubuntu.com/ubuntu-ports/ trusty main restricted universe multiverse deb http://ports.ubuntu.com/ubuntu-ports/ trust…

路由器怎么设置局域网?

局域网&#xff08;Local Area Network&#xff0c;LAN&#xff09;是指在一个相对较小的地理范围内&#xff0c;如家庭、办公室或学校等&#xff0c;通过路由器等设备连接起来的计算机网络。设置局域网可以方便地实现内部资源共享和信息交流。本文将介绍如何设置局域网以及一个…

maven 显式依赖包包含隐式依赖包,引起依赖包冲突

问题&#xff1a;FlinkCDC 3.0.1 代码 maven依赖包冲突 什么是依赖冲突 依赖冲突是指项目依赖的某一个jar包&#xff0c;有多个不同的版本&#xff0c;因而造成类包版本冲突 依赖冲突的原因 依赖冲突很经常是类包之间的间接依赖引起的。每个显式声明的类包都会依赖于一些其它…

Redis缓存(笔记三:Redis6新数据类型)

目录 1. 介绍 2. Bitmaps&#xff08;可以称为字符串&#xff0c;专门进行位操作的字符串&#xff09; 2.1 概念 2.2 setbit&#xff08;设置Bitmaps中某个偏移量的值&#xff09; 2.3 getbit&#xff08;获取Bitmaps中某个偏移量的值&#xff09; 2.4 bitcount&#xff…

Java I/O操作

引言 在Java编程中&#xff0c;输入和输出&#xff08;I/O&#xff09;操作是必不可少的部分。Java I/O通过一系列流&#xff08;Stream&#xff09;类和方法&#xff0c;支持文件操作、控制台输入输出、网络I/O等多种I/O操作。本文将详细介绍Java I/O的基础概念、文件操作、字…

vue实现类似微信按住发送语音给后端

一、需求是点击发送按钮说话获取语音文件发送给后台、鼠标移除即发送,限制时长30s 代码如下 <ttemplete><div class="bottom"><el-button type="primary" size="small" @mousedown.native="startRecord" @mouseup.n…

第二十七章 使用 MTOM 进行附件

文章目录 第二十七章 使用 MTOM 进行附件附件和 SOAP 消息打包具有全部内联部分的 SOAP 消息&#xff08;默认&#xff09; 第二十七章 使用 MTOM 进行附件 可以在 SOAP 请求和响应消息中包含附件。首选方法是使用 IRIS 数据平台对 MTOM&#xff08;消息传输优化机制&#xff…

FPGA专项课程即将开课,颁发AMD官方证书

社区成立以来&#xff0c;一直致力于为广大工程师提供优质的技术培训和资源&#xff0c;得到了众多用户的喜爱与支持。为了满足用户需求&#xff0c;我们特别推出了“基于Vitis HLS的高层次综合及图像处理开发”课程。 本次课程旨在帮助企业工程师掌握前沿的FPGA技术&#xff…

携程任我行礼品卡怎么用?

前两天我用积分兑了张携程卡&#xff0c;但是最近有没有假期&#xff0c;这张携程卡又不知道咋用 最后朋友告诉我可以在收卡云上转让&#xff0c; 去收卡云看了一眼&#xff0c;携程卡现在99.1折&#xff0c;果断出了&#xff0c;到账速度好快啊

Java多线程编程与并发处理

引言 在现代编程中&#xff0c;多线程和并发处理是提高程序运行效率和资源利用率的重要方法。Java提供了丰富的多线程编程支持&#xff0c;包括线程的创建与生命周期管理、线程同步与锁机制、并发库和高级并发工具等。本文将详细介绍这些内容&#xff0c;并通过表格进行总结和…

MySQL----主键、唯一、普通索引的创建与删除

创建索引 CREATE INDEX index_name ON table_name (column1 [ASC|DESC], column2 [ASC|DESC], ...);CREATE INDEX: 用于创建普通索引的关键字。index_name: 指定要创建的索引的名称。索引名称在表中必须是唯一的。table_name: 指定要在哪个表上创建索引。(column1, column2, ……

IntelliJ IDEA 用maven创建web项目

前言 已经安装并配置好Tomcat。具体步骤&#xff1a;Tomcat安装及环境变量配置(一看就会)-CSDN博客​编辑https://blog.csdn.net/longyongyyds/article/details/135825647 具体步骤 1.新建一个maven项目 2&#xff0c;配置好tomcat服务器 3.运行测试一下 maven教程&#xf…

前端面试题(四)|求职季面试分享|答案版

面试形式&#xff1a;线上面试&#xff1a;&#xff08;一面&#xff1a;电话&#xff09;&#xff1a;时长10分钟 面试评价&#xff1a;面试官介绍公司业务面试者倾听介绍 面试官&#xff1a;技术人员 面试官的提问大纲&#xff1a;本公司招聘要求 面试官提问&#xff08;面试…

使用PHP对接企业微信审批接口的流程和基本接口(一)

在现代企业中&#xff0c;审批流程是非常重要的一环&#xff0c;它涉及到企业内部各种业务流程的规范和高效运转。而随着企业微信的流行&#xff0c;许多企业希望将审批流程整合到企业微信中&#xff0c;以实现更便捷的审批操作。本文将介绍如何使用PHP对接企业微信审批接口&am…