2476浏览
查看: 2476|回复: 20

[项目] 【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏

[复制链接]
本帖最后由 驴友花雕 于 2022-7-9 04:43 编辑

实验使用的硬件,软件平台使用Arduino IDE(见《【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境》)

【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 16:02:12

本帖最后由 驴友花雕 于 2022-7-8 16:12 编辑

WS2812B
是一个集控制电路与发光电路于一体的智能外控LED光源。其外型与一个5050LED灯珠相同,每个元件即为一个像素点。像素点内部包含了智能数字接口数据锁存信号整形放大驱动电路,还包含有高精度的内部振荡器和12V高压可编程定电流控制部分,有效保证了像素点光的颜色高度一致。数据协议采用单线归零码的通讯方式,像素点在上电复位以后,DIN端接受从控制器传输过来的数据,首先送过来的24bit数据被第一个像素点提取后,送到像素点内部的数据锁存器,剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点,每经过一个像素点的传输,信号减少24bit。像素点采用自动整形转发技术,使得该像素点的级联个数不受信号传送的限制,仅仅受限信号传输速度要求。


【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 16:09:37

主要特点
●     智能反接保护,电源反接不会损坏IC。
●     IC控制电路与LED点光源公用一个电源。
●     控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外控像素点。
●     内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
●     内置上电复位和掉电复位电路。
●     每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
●     串行级联接口,能通过一根信号线完成数据的接收与解码。
●     任意两点传传输距离在不超过5米时无需增加任何电路。
●     当刷新速率30帧/秒时,级联数不小于1024点。
●      数据发送速度可达800Kbps。
●      光的颜色高度一致,性价比高。


【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 16:15:53

WS2812B典型应用电路

【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 16:37:22

Beetle ESP32-C3是一款基于ESP32-C3 RISC-V 32位单核处理器芯片的主控板,专为物联网 (IoT) 设备而设计。Beetle ESP32-C3在仅硬币大小的体积上引出了多达13个IO口,制作项目时不必再担心IO口不够用的情况,同时主控板还集成锂电池充电管理功能,可以直接连接锂电池,不需要外围模块,同时保证应用体积和安全性。Beetle ESP32-C3配套的扩展板在未增加太大体积的情况下,引出了更多的电源,在制作项目时焊接更加方便,板载的GDI显示屏接口解决使用屏幕时的接线烦恼。实验使用的引脚为A0和D6。

【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 16:44:25

8X32灯板采用独立电源供电,这里使用华为5V2A手机电源

【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 17:05:22

打开Arduino IDE——工具——管理库,搜索Adafruit NeoPixel,并安装驱动库


【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 19:00:46

  【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
    实验程序一:逐一点亮256个绿色LED灯

  1. /*
  2.   【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  3.   实验程序一:逐一点亮256个绿色LED灯
  4. */
  5. #include <Adafruit_NeoPixel.h>
  6. #define PIN            6
  7. #define NUMPIXELS      256
  8. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  9. int delayval = 60;
  10. void setup() {
  11.   pixels.begin();
  12. }
  13. void loop() {
  14.   for (int i = 0; i < NUMPIXELS; i++) {
  15.     pixels.setPixelColor(i, pixels.Color(0, 30, 0));
  16.     pixels.show();
  17.     delay(delayval);
  18.   }
  19. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 19:03:47

实验场景图

【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 19:09:12

动态图


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 20:15:04

【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
    实验程序二:简单的流水变幻彩虹灯

  1. /*
  2.   【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  3.   实验程序二:简单的流水变幻彩虹灯
  4. */
  5. #include <Adafruit_NeoPixel.h>
  6. #define PIN 6
  7. #define BRIGHTNESS 128
  8. Adafruit_NeoPixel strip = Adafruit_NeoPixel(BRIGHTNESS, PIN, NEO_GRB + NEO_KHZ800);
  9. void setup() {
  10.   strip.setBrightness(BRIGHTNESS);
  11.   strip.begin();
  12.   strip.show();
  13. }
  14. void loop() {
  15.   colorWipe(strip.Color(40, 0, 0), 20); // Red
  16.   colorWipe(strip.Color(0, 40, 0), 20); // Green
  17.   colorWipe(strip.Color(0, 0, 50), 20); // Blue
  18.   colorWipe(strip.Color(40, 40, 50), 20); // BlueWite
  19.   rainbowCycle(1);
  20. }
  21. void colorWipe(uint32_t c, uint8_t wait) {
  22.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  23.     strip.setPixelColor(i, c);
  24.     strip.show();
  25.     delay(wait);
  26.   }
  27. }
  28. void rainbow(uint8_t wait) {
  29.   uint16_t i, j;
  30.   for (j = 0; j < 256; j++) {
  31.     for (i = 0; i < strip.numPixels(); i++) {
  32.       strip.setPixelColor(i, Wheel((i + j) & 255 ));
  33.     }
  34.     strip.show();
  35.     delay(wait);
  36.   }
  37. }
  38. void rainbowCycle(uint8_t wait) {
  39.   uint16_t i, j;
  40.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  41.     for (i = 0; i < strip.numPixels(); i++) {
  42.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  43.     }
  44.     strip.show();
  45.     delay(wait);
  46.   }
  47. }
  48. uint32_t Wheel(byte WheelPos) {
  49.   if (WheelPos < 85) {
  50.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  51.   } else if (WheelPos < 170) {
  52.     WheelPos -= 85;
  53.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  54.   } else {
  55.     WheelPos -= 170;
  56.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  57.   }
  58. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 20:30:37

动态图 【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 20:34:14

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-8 20:37:27

本帖最后由 驴友花雕 于 2022-7-8 20:42 编辑

实验场景图
【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-9 10:25:48

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-9 10:56:51

  【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  实验程序三:256位多彩流水灯变幻彩虹灯

  1. /*
  2.   【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  3.   实验程序三:256位多彩流水灯变幻彩虹灯
  4. */
  5. #include <Adafruit_NeoPixel.h>
  6. #define LED_PIN 6
  7. #define LED_COUNT 256
  8. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  9. void setup() {
  10.   strip.setBrightness(30);
  11.   strip.begin();
  12.   strip.show();
  13. }
  14. void loop() {
  15.   // Fill along the length of the strip in various colors...
  16.   colorWipe(strip.Color(255,   0,   0), 50); // Red
  17.   colorWipe(strip.Color(  0, 255,   0), 50); // Green
  18.   colorWipe(strip.Color(  0,   0, 255), 50); // Blue
  19.   // Do a theater marquee effect in various colors...
  20.   theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  21.   theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
  22.   theaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightness
  23.   rainbow(10);             // Flowing rainbow cycle along the whole strip
  24.   theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
  25. }
  26. // Some functions of our own for creating animated effects -----------------
  27. // Fill strip pixels one after another with a color. Strip is NOT cleared
  28. // first; anything there will be covered pixel by pixel. Pass in color
  29. // (as a single 'packed' 32-bit value, which you can get by calling
  30. // strip.Color(red, green, blue) as shown in the loop() function above),
  31. // and a delay time (in milliseconds) between pixels.
  32. void colorWipe(uint32_t color, int wait) {
  33.   for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  34.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  35.     strip.show();                          //  Update strip to match
  36.     delay(wait);                           //  Pause for a moment
  37.   }
  38. }
  39. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  40. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  41. // between frames.
  42. void theaterChase(uint32_t color, int wait) {
  43.   for (int a = 0; a < 10; a++) { // Repeat 10 times...
  44.     for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
  45.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  46.       // 'c' counts up from 'b' to end of strip in steps of 3...
  47.       for (int c = b; c < strip.numPixels(); c += 3) {
  48.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  49.       }
  50.       strip.show(); // Update strip with new contents
  51.       delay(wait);  // Pause for a moment
  52.     }
  53.   }
  54. }
  55. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  56. void rainbow(int wait) {
  57.   // Hue of first pixel runs 5 complete loops through the color wheel.
  58.   // Color wheel has a range of 65536 but it's OK if we roll over, so
  59.   // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  60.   // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  61.   for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
  62.     for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  63.       // Offset pixel hue by an amount to make one full revolution of the
  64.       // color wheel (range of 65536) along the length of the strip
  65.       // (strip.numPixels() steps):
  66.       int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  67.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  68.       // optionally add saturation and value (brightness) (each 0 to 255).
  69.       // Here we're using just the single-argument hue variant. The result
  70.       // is passed through strip.gamma32() to provide 'truer' colors
  71.       // before assigning to each pixel:
  72.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  73.     }
  74.     strip.show(); // Update strip with new contents
  75.     delay(wait);  // Pause for a moment
  76.   }
  77. }
  78. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  79. void theaterChaseRainbow(int wait) {
  80.   int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  81.   for (int a = 0; a < 30; a++) { // Repeat 30 times...
  82.     for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
  83.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  84.       // 'c' counts up from 'b' to end of strip in increments of 3...
  85.       for (int c = b; c < strip.numPixels(); c += 3) {
  86.         // hue of pixel 'c' is offset by an amount to make one full
  87.         // revolution of the color wheel (range 65536) along the length
  88.         // of the strip (strip.numPixels() steps):
  89.         int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
  90.         uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  91.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  92.       }
  93.       strip.show();                // Update strip with new contents
  94.       delay(wait);                 // Pause for a moment
  95.       firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  96.     }
  97.   }
  98. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-9 11:14:34

实验动态图 【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-9 12:53:26

  【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  实验程序四:256位多彩火焰灯

  1. /*
  2.   【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  3.   实验程序四:256位多彩火焰灯
  4. */
  5. #include <FastLED.h>
  6. #define LED_PIN     6
  7. #define COLOR_ORDER GRB
  8. #define CHIPSET     WS2811
  9. #define NUM_LEDS    256
  10. #define BRIGHTNESS  22
  11. #define FRAMES_PER_SECOND 60
  12. bool gReverseDirection = false;
  13. CRGB leds[NUM_LEDS];
  14. // Fire2012 with programmable Color Palette
  15. //
  16. // This code is the same fire simulation as the original "Fire2012",
  17. // but each heat cell's temperature is translated to color through a FastLED
  18. // programmable color palette, instead of through the "HeatColor(...)" function.
  19. //
  20. // Four different static color palettes are provided here, plus one dynamic one.
  21. //
  22. // The three static ones are:
  23. //   1. the FastLED built-in HeatColors_p -- this is the default, and it looks
  24. //      pretty much exactly like the original Fire2012.
  25. //
  26. //  To use any of the other palettes below, just "uncomment" the corresponding code.
  27. //
  28. //   2. a gradient from black to red to yellow to white, which is
  29. //      visually similar to the HeatColors_p, and helps to illustrate
  30. //      what the 'heat colors' palette is actually doing,
  31. //   3. a similar gradient, but in blue colors rather than red ones,
  32. //      i.e. from black to blue to aqua to white, which results in
  33. //      an "icy blue" fire effect,
  34. //   4. a simplified three-step gradient, from black to red to white, just to show
  35. //      that these gradients need not have four components; two or
  36. //      three are possible, too, even if they don't look quite as nice for fire.
  37. //
  38. // The dynamic palette shows how you can change the basic 'hue' of the
  39. // color palette every time through the loop, producing "rainbow fire".
  40. CRGBPalette16 gPal;
  41. void setup() {
  42.   delay(3000); // sanity delay
  43.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  44.   FastLED.setBrightness( BRIGHTNESS );
  45.   // This first palette is the basic 'black body radiation' colors,
  46.   // which run from black to red to bright yellow to white.
  47.   gPal = HeatColors_p;
  48.   
  49.   // These are other ways to set up the color palette for the 'fire'.
  50.   // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
  51.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
  52.   
  53.   // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
  54.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  55.   
  56.   // Third, here's a simpler, three-step gradient, from black to red to white
  57.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);
  58. }
  59. void loop()
  60. {
  61.   // Add entropy to random number generator; we use a lot of it.
  62.   random16_add_entropy( random());
  63.   // Fourth, the most sophisticated: this one sets up a new palette every
  64.   // time through the loop, based on a hue that changes every time.
  65.   // The palette is a gradient from black, to a dark color based on the hue,
  66.   // to a light color based on the hue, to white.
  67.   //
  68.   //   static uint8_t hue = 0;
  69.   //   hue++;
  70.   //   CRGB darkcolor  = CHSV(hue,255,192); // pure hue, three-quarters brightness
  71.   //   CRGB lightcolor = CHSV(hue,128,255); // half 'whitened', full brightness
  72.   //   gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);
  73.   Fire2012WithPalette(); // run simulation frame, using palette colors
  74.   
  75.   FastLED.show(); // display this frame
  76.   FastLED.delay(1000 / FRAMES_PER_SECOND);
  77. }
  78. // Fire2012 by Mark Kriegsman, July 2012
  79. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  80. ////
  81. // This basic one-dimensional 'fire' simulation works roughly as follows:
  82. // There's a underlying array of 'heat' cells, that model the temperature
  83. // at each point along the line.  Every cycle through the simulation,
  84. // four steps are performed:
  85. //  1) All cells cool down a little bit, losing heat to the air
  86. //  2) The heat from each cell drifts 'up' and diffuses a little
  87. //  3) Sometimes randomly new 'sparks' of heat are added at the bottom
  88. //  4) The heat from each cell is rendered as a color into the leds array
  89. //     The heat-to-color mapping uses a black-body radiation approximation.
  90. //
  91. // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
  92. //
  93. // This simulation scales it self a bit depending on NUM_LEDS; it should look
  94. // "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
  95. //
  96. // I recommend running this simulation at anywhere from 30-100 frames per second,
  97. // meaning an interframe delay of about 10-35 milliseconds.
  98. //
  99. // Looks best on a high-density LED setup (60+ pixels/meter).
  100. //
  101. //
  102. // There are two main parameters you can play with to control the look and
  103. // feel of your fire: COOLING (used in step 1 above), and SPARKING (used
  104. // in step 3 above).
  105. //
  106. // COOLING: How much does the air cool as it rises?
  107. // Less cooling = taller flames.  More cooling = shorter flames.
  108. // Default 55, suggested range 20-100
  109. #define COOLING  55
  110. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  111. // Higher chance = more roaring fire.  Lower chance = more flickery fire.
  112. // Default 120, suggested range 50-200.
  113. #define SPARKING 120
  114. void Fire2012WithPalette()
  115. {
  116. // Array of temperature readings at each simulation cell
  117.   static uint8_t heat[NUM_LEDS];
  118.   // Step 1.  Cool down every cell a little
  119.     for( int i = 0; i < NUM_LEDS; i++) {
  120.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  121.     }
  122.   
  123.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  124.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  125.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  126.     }
  127.    
  128.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  129.     if( random8() < SPARKING ) {
  130.       int y = random8(7);
  131.       heat[y] = qadd8( heat[y], random8(160,255) );
  132.     }
  133.     // Step 4.  Map from heat cells to LED colors
  134.     for( int j = 0; j < NUM_LEDS; j++) {
  135.       // Scale the heat value from 0-255 down to 0-240
  136.       // for best results with color palettes.
  137.       uint8_t colorindex = scale8( heat[j], 240);
  138.       CRGB color = ColorFromPalette( gPal, colorindex);
  139.       int pixelnumber;
  140.       if( gReverseDirection ) {
  141.         pixelnumber = (NUM_LEDS-1) - j;
  142.       } else {
  143.         pixelnumber = j;
  144.       }
  145.       leds[pixelnumber] = color;
  146.     }
  147. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-9 12:56:00

实验场景  动态图 【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812硬屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-9 14:23:41

  【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  实验程序五:FastLED的八种动态颜色的配色板

  1. /*
  2.   【花雕体验】16 使用Beetle ESP32 C3控制8X32位WS2812灯板
  3.   实验程序五:FastLED的八种动态颜色的配色板
  4. */
  5. #include <FastLED.h>
  6. #define LED_PIN     6
  7. #define NUM_LEDS    256
  8. #define BRIGHTNESS  22
  9. #define LED_TYPE    WS2811
  10. #define COLOR_ORDER GRB
  11. CRGB leds[NUM_LEDS];
  12. #define UPDATES_PER_SECOND 100 //定义每秒更新数
  13. // This example shows several ways to set up and use 'palettes' of colors
  14. // with FastLED.
  15. //
  16. // These compact palettes provide an easy way to re-colorize your
  17. // animation on the fly, quickly, easily, and with low overhead.
  18. //
  19. // USING palettes is MUCH simpler in practice than in theory, so first just
  20. // run this sketch, and watch the pretty lights as you then read through
  21. // the code.  Although this sketch has eight (or more) different color schemes,
  22. // the entire sketch compiles down to about 6.5K on AVR.
  23. //
  24. // FastLED provides a few pre-configured color palettes, and makes it
  25. // extremely easy to make up your own color schemes with palettes.
  26. //
  27. // Some notes on the more abstract 'theory and practice' of
  28. // FastLED compact palettes are at the bottom of this file.
  29. CRGBPalette16 currentPalette;
  30. TBlendType    currentBlending;
  31. extern CRGBPalette16 myRedWhiteBluePalette;
  32. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  33. void setup() {
  34.     delay( 3000 ); // power-up safety delay
  35.     FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  36.     FastLED.setBrightness(  BRIGHTNESS );
  37.     currentPalette = RainbowColors_p;
  38.     currentBlending = LINEARBLEND;
  39. }
  40. void loop(){
  41.     ChangePalettePeriodically();
  42.    
  43.     static uint8_t startIndex = 0;
  44.     startIndex = startIndex + 1; /* motion speed */
  45.    
  46.     FillLEDsFromPaletteColors( startIndex);
  47.    
  48.     FastLED.show();
  49.     FastLED.delay(1000 / UPDATES_PER_SECOND);
  50. }
  51. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  52. {
  53.     uint8_t brightness = 255;
  54.    
  55.     for( int i = 0; i < NUM_LEDS; ++i) {
  56.         leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  57.         colorIndex += 3;
  58.     }
  59. }
  60. // There are several different palettes of colors demonstrated here.
  61. //
  62. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  63. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  64. //
  65. // Additionally, you can manually define your own color palettes, or you can write
  66. // code that creates color palettes on the fly.  All are shown here.
  67. void ChangePalettePeriodically()
  68. {
  69.     uint8_t secondHand = (millis() / 1000) % 60;
  70.     static uint8_t lastSecond = 99;
  71.    
  72.     if( lastSecond != secondHand) {
  73.         lastSecond = secondHand;
  74.         if( secondHand ==  0)  { currentPalette = RainbowColors_p;         currentBlending = LINEARBLEND; }
  75.         if( secondHand == 10)  { currentPalette = RainbowStripeColors_p;   currentBlending = NOBLEND;  }
  76.         if( secondHand == 15)  { currentPalette = RainbowStripeColors_p;   currentBlending = LINEARBLEND; }
  77.         if( secondHand == 20)  { SetupPurpleAndGreenPalette();             currentBlending = LINEARBLEND; }
  78.         if( secondHand == 25)  { SetupTotallyRandomPalette();              currentBlending = LINEARBLEND; }
  79.         if( secondHand == 30)  { SetupBlackAndWhiteStripedPalette();       currentBlending = NOBLEND; }
  80.         if( secondHand == 35)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
  81.         if( secondHand == 40)  { currentPalette = CloudColors_p;           currentBlending = LINEARBLEND; }
  82.         if( secondHand == 45)  { currentPalette = PartyColors_p;           currentBlending = LINEARBLEND; }
  83.         if( secondHand == 50)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND;  }
  84.         if( secondHand == 55)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
  85.     }
  86. }
  87. // This function fills the palette with totally random colors.
  88. void SetupTotallyRandomPalette()
  89. {
  90.     for( int i = 0; i < 16; ++i) {
  91.         currentPalette[i] = CHSV( random8(), 255, random8());
  92.     }
  93. }
  94. // This function sets up a palette of black and white stripes,
  95. // using code.  Since the palette is effectively an array of
  96. // sixteen CRGB colors, the various fill_* functions can be used
  97. // to set them up.
  98. void SetupBlackAndWhiteStripedPalette()
  99. {
  100.     // 'black out' all 16 palette entries...
  101.     fill_solid( currentPalette, 16, CRGB::Black);
  102.     // and set every fourth one to white.
  103.     currentPalette[0] = CRGB::White;
  104.     currentPalette[4] = CRGB::White;
  105.     currentPalette[8] = CRGB::White;
  106.     currentPalette[12] = CRGB::White;
  107.    
  108. }
  109. // This function sets up a palette of purple and green stripes.
  110. void SetupPurpleAndGreenPalette()
  111. {
  112.     CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  113.     CRGB green  = CHSV( HUE_GREEN, 255, 255);
  114.     CRGB black  = CRGB::Black;
  115.    
  116.     currentPalette = CRGBPalette16(
  117.                                    green,  green,  black,  black,
  118.                                    purple, purple, black,  black,
  119.                                    green,  green,  black,  black,
  120.                                    purple, purple, black,  black );
  121. }
  122. // This example shows how to set up a static color palette
  123. // which is stored in PROGMEM (flash), which is almost always more
  124. // plentiful than RAM.  A static PROGMEM palette like this
  125. // takes up 64 bytes of flash.
  126. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  127. {
  128.     CRGB::Red,
  129.     CRGB::Gray, // 'white' is too bright compared to red and blue
  130.     CRGB::Blue,
  131.     CRGB::Black,
  132.    
  133.     CRGB::Red,
  134.     CRGB::Gray,
  135.     CRGB::Blue,
  136.     CRGB::Black,
  137.    
  138.     CRGB::Red,
  139.     CRGB::Red,
  140.     CRGB::Gray,
  141.     CRGB::Gray,
  142.     CRGB::Blue,
  143.     CRGB::Blue,
  144.     CRGB::Black,
  145.     CRGB::Black
  146. };
  147. // Additional notes on FastLED compact palettes:
  148. //
  149. // Normally, in computer graphics, the palette (or "color lookup table")
  150. // has 256 entries, each containing a specific 24-bit RGB color.  You can then
  151. // index into the color palette using a simple 8-bit (one byte) value.
  152. // A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
  153. // is quite possibly "too many" bytes.
  154. //
  155. // FastLED does offer traditional 256-element palettes, for setups that
  156. // can afford the 768-byte cost in RAM.
  157. //
  158. // However, FastLED also offers a compact alternative.  FastLED offers
  159. // palettes that store 16 distinct entries, but can be accessed AS IF
  160. // they actually have 256 entries; this is accomplished by interpolating
  161. // between the 16 explicit entries to create fifteen intermediate palette
  162. // entries between each pair.
  163. //
  164. // So for example, if you set the first two explicit entries of a compact
  165. // palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
  166. // the first sixteen entries from the virtual palette (of 256), you'd get
  167. // Green, followed by a smooth gradient from green-to-blue, and then Blue.
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-9 14:29:24

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail