467浏览
查看: 467|回复: 22

[ESP8266/ESP32] 【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏

[复制链接]
手头有64位和256位WS2812B的硬屏各一片,现在尝试点亮它们

17.3-58.jpg

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 15:21:08

  【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  实验程序五:TwinkleFOX:淡入淡出的闪烁“节日灯“

  1. /*
  2.   【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  3.   实验程序五:TwinkleFOX:淡入淡出的闪烁“节日灯“
  4. */
  5. #include "FastLED.h"
  6. #define NUM_LEDS      256
  7. #define LED_TYPE   WS2811
  8. #define COLOR_ORDER   GRB
  9. #define DATA_PIN        9
  10. //#define CLK_PIN       4
  11. #define VOLTS          12
  12. #define MAX_MA       4000
  13. //  TwinkleFOX: Twinkling 'holiday' lights that fade in and out.
  14. //  Colors are chosen from a palette; a few palettes are provided.
  15. //
  16. //  This December 2015 implementation improves on the December 2014 version
  17. //  in several ways:
  18. //  - smoother fading, compatible with any colors and any palettes
  19. //  - easier control of twinkle speed and twinkle density
  20. //  - supports an optional 'background color'
  21. //  - takes even less RAM: zero RAM overhead per pixel
  22. //  - illustrates a couple of interesting techniques (uh oh...)
  23. //
  24. //  The idea behind this (new) implementation is that there's one
  25. //  basic, repeating pattern that each pixel follows like a waveform:
  26. //  The brightness rises from 0..255 and then falls back down to 0.
  27. //  The brightness at any given point in time can be determined as
  28. //  as a function of time, for example:
  29. //    brightness = sine( time ); // a sine wave of brightness over time
  30. //
  31. //  So the way this implementation works is that every pixel follows
  32. //  the exact same wave function over time.  In this particular case,
  33. //  I chose a sawtooth triangle wave (triwave8) rather than a sine wave,
  34. //  but the idea is the same: brightness = triwave8( time ).
  35. //
  36. //  Of course, if all the pixels used the exact same wave form, and
  37. //  if they all used the exact same 'clock' for their 'time base', all
  38. //  the pixels would brighten and dim at once -- which does not look
  39. //  like twinkling at all.
  40. //
  41. //  So to achieve random-looking twinkling, each pixel is given a
  42. //  slightly different 'clock' signal.  Some of the clocks run faster,
  43. //  some run slower, and each 'clock' also has a random offset from zero.
  44. //  The net result is that the 'clocks' for all the pixels are always out
  45. //  of sync from each other, producing a nice random distribution
  46. //  of twinkles.
  47. //
  48. //  The 'clock speed adjustment' and 'time offset' for each pixel
  49. //  are generated randomly.  One (normal) approach to implementing that
  50. //  would be to randomly generate the clock parameters for each pixel
  51. //  at startup, and store them in some arrays.  However, that consumes
  52. //  a great deal of precious RAM, and it turns out to be totally
  53. //  unnessary!  If the random number generate is 'seeded' with the
  54. //  same starting value every time, it will generate the same sequence
  55. //  of values every time.  So the clock adjustment parameters for each
  56. //  pixel are 'stored' in a pseudo-random number generator!  The PRNG
  57. //  is reset, and then the first numbers out of it are the clock
  58. //  adjustment parameters for the first pixel, the second numbers out
  59. //  of it are the parameters for the second pixel, and so on.
  60. //  In this way, we can 'store' a stable sequence of thousands of
  61. //  random clock adjustment parameters in literally two bytes of RAM.
  62. //
  63. //  There's a little bit of fixed-point math involved in applying the
  64. //  clock speed adjustments, which are expressed in eighths.  Each pixel's
  65. //  clock speed ranges from 8/8ths of the system clock (i.e. 1x) to
  66. //  23/8ths of the system clock (i.e. nearly 3x).
  67. //
  68. //  On a basic Arduino Uno or Leonardo, this code can twinkle 300+ pixels
  69. //  smoothly at over 50 updates per seond.
  70. //
  71. //  -Mark Kriegsman, December 2015
  72. CRGBArray<NUM_LEDS> leds;
  73. // Overall twinkle speed.
  74. // 0 (VERY slow) to 8 (VERY fast).
  75. // 4, 5, and 6 are recommended, default is 4.
  76. #define TWINKLE_SPEED 8
  77. // Overall twinkle density.
  78. // 0 (NONE lit) to 8 (ALL lit at once).
  79. // Default is 5.
  80. #define TWINKLE_DENSITY 5
  81. // How often to change color palettes.
  82. #define SECONDS_PER_PALETTE  10
  83. // Also: toward the bottom of the file is an array
  84. // called "ActivePaletteList" which controls which color
  85. // palettes are used; you can add or remove color palettes
  86. // from there freely.
  87. // Background color for 'unlit' pixels
  88. // Can be set to CRGB::Black if desired.
  89. CRGB gBackgroundColor = CRGB::Black;
  90. // Example of dim incandescent fairy light background color
  91. // CRGB gBackgroundColor = CRGB(CRGB::FairyLight).nscale8_video(16);
  92. // If AUTO_SELECT_BACKGROUND_COLOR is set to 1,
  93. // then for any palette where the first two entries
  94. // are the same, a dimmed version of that color will
  95. // automatically be used as the background color.
  96. #define AUTO_SELECT_BACKGROUND_COLOR 0
  97. // If COOL_LIKE_INCANDESCENT is set to 1, colors will
  98. // fade out slighted 'reddened', similar to how
  99. // incandescent bulbs change color as they get dim down.
  100. #define COOL_LIKE_INCANDESCENT 1
  101. CRGBPalette16 gCurrentPalette;
  102. CRGBPalette16 gTargetPalette;
  103. void setup() {
  104.   delay( 2000 ); //safety startup delay
  105.   FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
  106.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
  107.   .setCorrection(TypicalLEDStrip);
  108.   FastLED.setBrightness(22);
  109.   chooseNextColorPalette(gTargetPalette);
  110. }
  111. void loop(){
  112.   EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
  113.     chooseNextColorPalette( gTargetPalette );
  114.   }
  115.   EVERY_N_MILLISECONDS( 10 ) {
  116.     nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 12);
  117.   }
  118.   drawTwinkles( leds);
  119.   FastLED.show();
  120. }
  121. //  This function loops over each pixel, calculates the
  122. //  adjusted 'clock' that this pixel should use, and calls
  123. //  "CalculateOneTwinkle" on each pixel.  It then displays
  124. //  either the twinkle color of the background color,
  125. //  whichever is brighter.
  126. void drawTwinkles( CRGBSet& L)
  127. {
  128.   // "PRNG16" is the pseudorandom number generator
  129.   // It MUST be reset to the same starting value each time
  130.   // this function is called, so that the sequence of 'random'
  131.   // numbers that it generates is (paradoxically) stable.
  132.   uint16_t PRNG16 = 11337;
  133.   uint32_t clock32 = millis();
  134.   // Set up the background color, "bg".
  135.   // if AUTO_SELECT_BACKGROUND_COLOR == 1, and the first two colors of
  136.   // the current palette are identical, then a deeply faded version of
  137.   // that color is used for the background color
  138.   CRGB bg;
  139.   if ( (AUTO_SELECT_BACKGROUND_COLOR == 1) &&
  140.        (gCurrentPalette[0] == gCurrentPalette[1] )) {
  141.     bg = gCurrentPalette[0];
  142.     uint8_t bglight = bg.getAverageLight();
  143.     if ( bglight > 64) {
  144.       bg.nscale8_video( 16); // very bright, so scale to 1/16th
  145.     } else if ( bglight > 16) {
  146.       bg.nscale8_video( 64); // not that bright, so scale to 1/4th
  147.     } else {
  148.       bg.nscale8_video( 86); // dim, scale to 1/3rd.
  149.     }
  150.   } else {
  151.     bg = gBackgroundColor; // just use the explicitly defined background color
  152.   }
  153.   uint8_t backgroundBrightness = bg.getAverageLight();
  154.   for ( CRGB& pixel : L) {
  155.     PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
  156.     uint16_t myclockoffset16 = PRNG16; // use that number as clock offset
  157.     PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
  158.     // use that number as clock speed adjustment factor (in 8ths, from 8/8ths to 23/8ths)
  159.     uint8_t myspeedmultiplierQ5_3 =  ((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
  160.     uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
  161.     uint8_t  myunique8 = PRNG16 >> 8; // get 'salt' value for this pixel
  162.     // We now have the adjusted 'clock' for this pixel, now we call
  163.     // the function that computes what color the pixel should be based
  164.     // on the "brightness = f( time )" idea.
  165.     CRGB c = computeOneTwinkle( myclock30, myunique8);
  166.     uint8_t cbright = c.getAverageLight();
  167.     int16_t deltabright = cbright - backgroundBrightness;
  168.     if ( deltabright >= 32 || (!bg)) {
  169.       // If the new pixel is significantly brighter than the background color,
  170.       // use the new color.
  171.       pixel = c;
  172.     } else if ( deltabright > 0 ) {
  173.       // If the new pixel is just slightly brighter than the background color,
  174.       // mix a blend of the new color and the background color
  175.       pixel = blend( bg, c, deltabright * 8);
  176.     } else {
  177.       // if the new pixel is not at all brighter than the background color,
  178.       // just use the background color.
  179.       pixel = bg;
  180.     }
  181.   }
  182. }
  183. //  This function takes a time in pseudo-milliseconds,
  184. //  figures out brightness = f( time ), and also hue = f( time )
  185. //  The 'low digits' of the millisecond time are used as
  186. //  input to the brightness wave function.
  187. //  The 'high digits' are used to select a color, so that the color
  188. //  does not change over the course of the fade-in, fade-out
  189. //  of one cycle of the brightness wave function.
  190. //  The 'high digits' are also used to determine whether this pixel
  191. //  should light at all during this cycle, based on the TWINKLE_DENSITY.
  192. CRGB computeOneTwinkle( uint32_t ms, uint8_t salt)
  193. {
  194.   uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
  195.   uint8_t fastcycle8 = ticks;
  196.   uint16_t slowcycle16 = (ticks >> 8) + salt;
  197.   slowcycle16 += sin8( slowcycle16);
  198.   slowcycle16 =  (slowcycle16 * 2053) + 1384;
  199.   uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);
  200.   uint8_t bright = 0;
  201.   if ( ((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
  202.     bright = attackDecayWave8( fastcycle8);
  203.   }
  204.   uint8_t hue = slowcycle8 - salt;
  205.   CRGB c;
  206.   if ( bright > 0) {
  207.     c = ColorFromPalette( gCurrentPalette, hue, bright, NOBLEND);
  208.     if ( COOL_LIKE_INCANDESCENT == 1 ) {
  209.       coolLikeIncandescent( c, fastcycle8);
  210.     }
  211.   } else {
  212.     c = CRGB::Black;
  213.   }
  214.   return c;
  215. }
  216. // This function is like 'triwave8', which produces a
  217. // symmetrical up-and-down triangle sawtooth waveform, except that this
  218. // function produces a triangle wave with a faster attack and a slower decay:
  219. //
  220. //     / \
  221. //    /     \
  222. //   /         \
  223. //  /             \
  224. //
  225. uint8_t attackDecayWave8( uint8_t i)
  226. {
  227.   if ( i < 86) {
  228.     return i * 3;
  229.   } else {
  230.     i -= 86;
  231.     return 255 - (i + (i / 2));
  232.   }
  233. }
  234. // This function takes a pixel, and if its in the 'fading down'
  235. // part of the cycle, it adjusts the color a little bit like the
  236. // way that incandescent bulbs fade toward 'red' as they dim.
  237. void coolLikeIncandescent( CRGB& c, uint8_t phase)
  238. {
  239.   if ( phase < 128) return;
  240.   uint8_t cooling = (phase - 128) >> 4;
  241.   c.g = qsub8( c.g, cooling);
  242.   c.b = qsub8( c.b, cooling * 2);
  243. }
  244. // A mostly red palette with green accents and white trim.
  245. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  246. const TProgmemRGBPalette16 RedGreenWhite_p FL_PROGMEM =
  247. { CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
  248.   CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
  249.   CRGB::Red, CRGB::Red, CRGB::Gray, CRGB::Gray,
  250.   CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green
  251. };
  252. // A mostly (dark) green palette with red berries.
  253. #define Holly_Green 0x00580c
  254. #define Holly_Red   0xB00402
  255. const TProgmemRGBPalette16 Holly_p FL_PROGMEM =
  256. { Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  257.   Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  258.   Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  259.   Holly_Green, Holly_Green, Holly_Green, Holly_Red
  260. };
  261. // A red and white striped palette
  262. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  263. const TProgmemRGBPalette16 RedWhite_p FL_PROGMEM =
  264. { CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red,
  265.   CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray,
  266.   CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red,
  267.   CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray
  268. };
  269. // A mostly blue palette with white accents.
  270. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  271. const TProgmemRGBPalette16 BlueWhite_p FL_PROGMEM =
  272. { CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  273.   CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  274.   CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  275.   CRGB::Blue, CRGB::Gray, CRGB::Gray, CRGB::Gray
  276. };
  277. // A pure "fairy light" palette with some brightness variations
  278. #define HALFFAIRY ((CRGB::FairyLight & 0xFEFEFE) / 2)
  279. #define QUARTERFAIRY ((CRGB::FairyLight & 0xFCFCFC) / 4)
  280. const TProgmemRGBPalette16 FairyLight_p FL_PROGMEM =
  281. { CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight,
  282.   HALFFAIRY,        HALFFAIRY,        CRGB::FairyLight, CRGB::FairyLight,
  283.   QUARTERFAIRY,     QUARTERFAIRY,     CRGB::FairyLight, CRGB::FairyLight,
  284.   CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight
  285. };
  286. // A palette of soft snowflakes with the occasional bright one
  287. const TProgmemRGBPalette16 Snow_p FL_PROGMEM =
  288. { 0x304048, 0x304048, 0x304048, 0x304048,
  289.   0x304048, 0x304048, 0x304048, 0x304048,
  290.   0x304048, 0x304048, 0x304048, 0x304048,
  291.   0x304048, 0x304048, 0x304048, 0xE0F0FF
  292. };
  293. // A palette reminiscent of large 'old-school' C9-size tree lights
  294. // in the five classic colors: red, orange, green, blue, and white.
  295. #define C9_Red    0xB80400
  296. #define C9_Orange 0x902C02
  297. #define C9_Green  0x046002
  298. #define C9_Blue   0x070758
  299. #define C9_White  0x606820
  300. const TProgmemRGBPalette16 RetroC9_p FL_PROGMEM =
  301. { C9_Red,    C9_Orange, C9_Red,    C9_Orange,
  302.   C9_Orange, C9_Red,    C9_Orange, C9_Red,
  303.   C9_Green,  C9_Green,  C9_Green,  C9_Green,
  304.   C9_Blue,   C9_Blue,   C9_Blue,
  305.   C9_White
  306. };
  307. // A cold, icy pale blue palette
  308. #define Ice_Blue1 0x0C1040
  309. #define Ice_Blue2 0x182080
  310. #define Ice_Blue3 0x5080C0
  311. const TProgmemRGBPalette16 Ice_p FL_PROGMEM =
  312. {
  313.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  314.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  315.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  316.   Ice_Blue2, Ice_Blue2, Ice_Blue2, Ice_Blue3
  317. };
  318. // Add or remove palette names from this list to control which color
  319. // palettes are used, and in what order.
  320. const TProgmemRGBPalette16* ActivePaletteList[] = {
  321.   &RetroC9_p,
  322.   &BlueWhite_p,
  323.   &RainbowColors_p,
  324.   &FairyLight_p,
  325.   &RedGreenWhite_p,
  326.   &PartyColors_p,
  327.   &RedWhite_p,
  328.   &Snow_p,
  329.   &Holly_p,
  330.   &Ice_p
  331. };
  332. // Advance to the next color palette in the list (above).
  333. void chooseNextColorPalette( CRGBPalette16& pal)
  334. {
  335.   const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList[0]);
  336.   static uint8_t whichPalette = -1;
  337.   whichPalette = addmod8( whichPalette, 1, numberOfPalettes);
  338.   pal = *(ActivePaletteList[whichPalette]);
  339. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 14:28:00

  【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  实验程序三:256位显示 RGBW 的 WHITE 通道的测试使用

  1. /*
  2.   【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  3.   实验程序三:256位显示 RGBW 的 WHITE 通道的测试使用
  4. */
  5. #include <Adafruit_NeoPixel.h>
  6. #define LED_PIN 9
  7. #define LED_COUNT 256
  8. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + 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.   colorWipe(strip.Color(  0,   0,   0, 255), 50); // True white (not RGB white)
  20.   whiteOverRainbow(75, 5);
  21.   pulseWhite(5);
  22.   rainbowFade2White(3, 3, 1);
  23. }
  24. // Fill strip pixels one after another with a color. Strip is NOT cleared
  25. // first; anything there will be covered pixel by pixel. Pass in color
  26. // (as a single 'packed' 32-bit value, which you can get by calling
  27. // strip.Color(red, green, blue) as shown in the loop() function above),
  28. // and a delay time (in milliseconds) between pixels.
  29. void colorWipe(uint32_t color, int wait) {
  30.   for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  31.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  32.     strip.show();                          //  Update strip to match
  33.     delay(wait);                           //  Pause for a moment
  34.   }
  35. }
  36. void whiteOverRainbow(int whiteSpeed, int whiteLength) {
  37.   if (whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;
  38.   int      head          = whiteLength - 1;
  39.   int      tail          = 0;
  40.   int      loops         = 3;
  41.   int      loopNum       = 0;
  42.   uint32_t lastTime      = millis();
  43.   uint32_t firstPixelHue = 0;
  44.   for (;;) { // Repeat forever (or until a 'break' or 'return')
  45.     for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  46.       if (((i >= tail) && (i <= head)) ||     //  If between head & tail...
  47.           ((tail > head) && ((i >= tail) || (i <= head)))) {
  48.         strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
  49.       } else {                                             // else set rainbow
  50.         int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  51.         strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  52.       }
  53.     }
  54.     strip.show(); // Update strip with new contents
  55.     // There's no delay here, it just runs full-tilt until the timer and
  56.     // counter combination below runs out.
  57.     firstPixelHue += 40; // Advance just a little along the color wheel
  58.     if ((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
  59.       if (++head >= strip.numPixels()) {     // Advance head, wrap around
  60.         head = 0;
  61.         if (++loopNum >= loops) return;
  62.       }
  63.       if (++tail >= strip.numPixels()) {     // Advance tail, wrap around
  64.         tail = 0;
  65.       }
  66.       lastTime = millis();                   // Save time of last movement
  67.     }
  68.   }
  69. }
  70. void pulseWhite(uint8_t wait) {
  71.   for (int j = 0; j < 256; j++) { // Ramp up from 0 to 255
  72.     // Fill entire strip with white at gamma-corrected brightness level 'j':
  73.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  74.     strip.show();
  75.     delay(wait);
  76.   }
  77.   for (int j = 255; j >= 0; j--) { // Ramp down from 255 to 0
  78.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  79.     strip.show();
  80.     delay(wait);
  81.   }
  82. }
  83. void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
  84.   int fadeVal = 0, fadeMax = 100;
  85.   // Hue of first pixel runs 'rainbowLoops' complete loops through the color
  86.   // wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
  87.   // just count from 0 to rainbowLoops*65536, using steps of 256 so we
  88.   // advance around the wheel at a decent clip.
  89.   for (uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops * 65536;
  90.        firstPixelHue += 256) {
  91.     for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  92.       // Offset pixel hue by an amount to make one full revolution of the
  93.       // color wheel (range of 65536) along the length of the strip
  94.       // (strip.numPixels() steps):
  95.       uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  96.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  97.       // optionally add saturation and value (brightness) (each 0 to 255).
  98.       // Here we're using just the three-argument variant, though the
  99.       // second value (saturation) is a constant 255.
  100.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
  101.                                            255 * fadeVal / fadeMax)));
  102.     }
  103.     strip.show();
  104.     delay(wait);
  105.     if (firstPixelHue < 65536) {                             // First loop,
  106.       if (fadeVal < fadeMax) fadeVal++;                      // fade in
  107.     } else if (firstPixelHue >= ((rainbowLoops - 1) * 65536)) { // Last loop,
  108.       if (fadeVal > 0) fadeVal--;                            // fade out
  109.     } else {
  110.       fadeVal = fadeMax; // Interim loop, make sure fade is at max
  111.     }
  112.   }
  113.   for (int k = 0; k < whiteLoops; k++) {
  114.     for (int j = 0; j < 256; j++) { // Ramp up 0 to 255
  115.       // Fill entire strip with white at gamma-corrected brightness level 'j':
  116.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  117.       strip.show();
  118.     }
  119.     delay(100); // Pause 1 second
  120.     for (int j = 255; j >= 0; j--) { // Ramp down 255 to 0
  121.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  122.       strip.show();
  123.     }
  124.   }
  125.   delay(50); // Pause 1/2 second
  126. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 12:45:06

  【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  实验程序二:一个基本的NeoPixel 灯板灯条测试程序

  1. /*
  2.   【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  3.   实验程序二:一个基本的NeoPixel 灯板灯条测试程序
  4. */
  5. #include <Adafruit_NeoPixel.h>
  6. #define PIN 9
  7. #define NUMPIXELS 64
  8. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  9. void setup() {
  10.   strip.setBrightness(50);
  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-12 10:16:16

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


0-.jpg
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 10:21:33

WS2812模块电原理图


07.jpg
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 10:33:57

合宙CORE ESP32-C3核心板,是基于乐鑫ESP32-C3芯片进行设计的一款开发板。尺寸仅有21mm×51mm,板边采用邮票孔设计,板载 Wi-Fi/BLE天线,方便开发者在不同场景下的使用。核心板支持UART、GPIO、SPI、I2C、ADC、PWM等接口,可根据实际需要选用。

1路SPI FLASH,板载4MB,支持最高 16MB
2路UART接口,UART0~UART1,其中下载口为UART0
6 路 12 比特 ADC,最高采样率 100KSPS
1路低速SPI接口,支持主模式
1路IIC控制器
4路PWM接口
GPIO外部管脚15路,可复用
2路贴片LED指示灯
1路复位按键+1路BOOT按键
1路USB转TTL下载调试口
2.4G PCB板载天线


17.3-09.jpg
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 11:00:28

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

17.3-59.jpg

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 11:35:22

  【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  实验程序一:64位绿色上漂灯

  1. /*
  2.   【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  3.   实验程序一:64位绿色上漂灯
  4. */
  5. #include <Adafruit_NeoPixel.h>
  6. #define PIN 9
  7. #define MAX_LED 64
  8. int val = 0;
  9. Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_RGB + NEO_KHZ800 );
  10. void setup() {
  11.   strip.setBrightness(150);
  12.   strip.begin();
  13.   strip.show();
  14. }
  15. void loop() {
  16.   uint8_t i, a = 0;
  17.   uint32_t color = strip.Color(160, 10, 10);
  18.   while (a < 64)
  19.   {
  20.     for (i = 0; i < 64; i++)
  21.     {
  22.       if (i == a) strip.setPixelColor(i, color);
  23.       else strip.setPixelColor(i, 0);
  24.     }
  25.     strip.show();
  26.     delay(6);
  27.     a++;
  28.   }
  29. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 11:40:47

实验串口返回情况

17.3-60.jpg
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 11:47:11

实验场景图  动态图

动画11.gif
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 13:55:01

实验场景图  动态图

动画13.gif
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 13:57:29

实验场景图  动态图

动画12.gif
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 13:59:45

实验场景图  动态图

动画14.gif
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 14:35:42

实验场景图

17.3-61.jpg
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 14:41:41

实验场景图  动态图

动画15.gif
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 14:53:57

  【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  实验程序四:256位使用FastLED库的八种快速调色板

  1. /*
  2.   【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  3.   实验程序四:256位使用FastLED库的八种快速调色板
  4. */
  5. #include <FastLED.h>
  6. #define LED_PIN     9
  7. #define NUM_LEDS    256
  8. #define BRIGHTNESS  20
  9. #define LED_TYPE    WS2811
  10. #define COLOR_ORDER GRB
  11. CRGB leds[NUM_LEDS];
  12. #define UPDATES_PER_SECOND 100
  13. CRGBPalette16 currentPalette;
  14. TBlendType    currentBlending;
  15. extern CRGBPalette16 myRedWhiteBluePalette;
  16. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  17. void setup() {
  18.   delay( 3000 ); // power-up safety delay
  19.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  20.   FastLED.setBrightness(  BRIGHTNESS );
  21.   currentPalette = RainbowColors_p;
  22.   currentBlending = LINEARBLEND;
  23. }
  24. void loop() {
  25.   ChangePalettePeriodically();
  26.   static uint8_t startIndex = 0;
  27.   startIndex = startIndex + 1; /* motion speed */
  28.   FillLEDsFromPaletteColors( startIndex);
  29.   FastLED.show();
  30.   FastLED.delay(1000 / UPDATES_PER_SECOND);
  31. }
  32. void FillLEDsFromPaletteColors( uint8_t colorIndex) {
  33.   uint8_t brightness = 255;
  34.   for ( int i = 0; i < NUM_LEDS; ++i) {
  35.     leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  36.     colorIndex += 3;
  37.   }
  38. }
  39. void ChangePalettePeriodically() {
  40.   uint8_t secondHand = (millis() / 1000) % 60;
  41.   static uint8_t lastSecond = 99;
  42.   if ( lastSecond != secondHand) {
  43.     lastSecond = secondHand;
  44.     if ( secondHand ==  0)  {
  45.       currentPalette = RainbowColors_p;
  46.       currentBlending = LINEARBLEND;
  47.     }
  48.     if ( secondHand == 10)  {
  49.       currentPalette = RainbowStripeColors_p;
  50.       currentBlending = NOBLEND;
  51.     }
  52.     if ( secondHand == 15)  {
  53.       currentPalette = RainbowStripeColors_p;
  54.       currentBlending = LINEARBLEND;
  55.     }
  56.     if ( secondHand == 20)  {
  57.       SetupPurpleAndGreenPalette();
  58.       currentBlending = LINEARBLEND;
  59.     }
  60.     if ( secondHand == 25)  {
  61.       SetupTotallyRandomPalette();
  62.       currentBlending = LINEARBLEND;
  63.     }
  64.     if ( secondHand == 30)  {
  65.       SetupBlackAndWhiteStripedPalette();
  66.       currentBlending = NOBLEND;
  67.     }
  68.     if ( secondHand == 35)  {
  69.       SetupBlackAndWhiteStripedPalette();
  70.       currentBlending = LINEARBLEND;
  71.     }
  72.     if ( secondHand == 40)  {
  73.       currentPalette = CloudColors_p;
  74.       currentBlending = LINEARBLEND;
  75.     }
  76.     if ( secondHand == 45)  {
  77.       currentPalette = PartyColors_p;
  78.       currentBlending = LINEARBLEND;
  79.     }
  80.     if ( secondHand == 50)  {
  81.       currentPalette = myRedWhiteBluePalette_p;
  82.       currentBlending = NOBLEND;
  83.     }
  84.     if ( secondHand == 55)  {
  85.       currentPalette = myRedWhiteBluePalette_p;
  86.       currentBlending = LINEARBLEND;
  87.     }
  88.   }
  89. }
  90. // This function fills the palette with totally random colors.
  91. void SetupTotallyRandomPalette() {
  92.   for ( int i = 0; i < 16; ++i) {
  93.     currentPalette[i] = CHSV( random8(), 255, random8());
  94.   }
  95. }
  96. void SetupBlackAndWhiteStripedPalette() {
  97.   // 'black out' all 16 palette entries...
  98.   fill_solid( currentPalette, 16, CRGB::Black);
  99.   // and set every fourth one to white.
  100.   currentPalette[0] = CRGB::White;
  101.   currentPalette[4] = CRGB::White;
  102.   currentPalette[8] = CRGB::White;
  103.   currentPalette[12] = CRGB::White;
  104. }
  105. // This function sets up a palette of purple and green stripes.
  106. void SetupPurpleAndGreenPalette() {
  107.   CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  108.   CRGB green  = CHSV( HUE_GREEN, 255, 255);
  109.   CRGB black  = CRGB::Black;
  110.   currentPalette = CRGBPalette16(
  111.                      green,  green,  black,  black,
  112.                      purple, purple, black,  black,
  113.                      green,  green,  black,  black,
  114.                      purple, purple, black,  black );
  115. }
  116. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  117. {
  118.   CRGB::Red,
  119.   CRGB::Gray, // 'white' is too bright compared to red and blue
  120.   CRGB::Blue,
  121.   CRGB::Black,
  122.   CRGB::Red,
  123.   CRGB::Gray,
  124.   CRGB::Blue,
  125.   CRGB::Black,
  126.   CRGB::Red,
  127.   CRGB::Red,
  128.   CRGB::Gray,
  129.   CRGB::Gray,
  130.   CRGB::Blue,
  131.   CRGB::Blue,
  132.   CRGB::Black,
  133.   CRGB::Black
  134. };
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 14:59:19

实验场景图  

17.3-62.jpg
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 15:03:20

实验场景图  动态图

动画16.gif
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 15:24:36

实验场景图  动态图

动画17.gif
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-12 16:03:52

  【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  实验程序六:FastLED“100行代码”演示卷轴动画效果

  1. /*
  2.   【花雕体验】19 合宙ESP32_C3点亮WS2812B硬屏
  3.   实验程序六:FastLED“100行代码”演示卷轴动画效果
  4. */
  5. #include <FastLED.h>
  6. FASTLED_USING_NAMESPACE
  7. // FastLED "100-lines-of-code" demo reel, showing just a few
  8. // of the kinds of animation patterns you can quickly and easily
  9. // compose using FastLED.
  10. //
  11. // This example also shows one easy way to define multiple
  12. // animations patterns and have them automatically rotate.
  13. //
  14. // -Mark Kriegsman, December 2014
  15. #define DATA_PIN    9
  16. //#define CLK_PIN   4
  17. #define LED_TYPE    WS2811
  18. #define COLOR_ORDER GRB
  19. #define NUM_LEDS    256
  20. CRGB leds[NUM_LEDS];
  21. #define BRIGHTNESS          22
  22. #define FRAMES_PER_SECOND  120
  23. void setup() {
  24.   delay(1000); // 3 second delay for recovery
  25.   // tell FastLED about the LED strip configuration
  26.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  27.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  28.   // set master brightness control
  29.   FastLED.setBrightness(BRIGHTNESS);
  30. }
  31. // List of patterns to cycle through.  Each is defined as a separate function below.
  32. typedef void (*SimplePatternList[])();
  33. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
  34. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  35. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  36. void loop()
  37. {
  38.   // Call the current pattern function once, updating the 'leds' array
  39.   gPatterns[gCurrentPatternNumber]();
  40.   // send the 'leds' array out to the actual LED strip
  41.   FastLED.show();
  42.   // insert a delay to keep the framerate modest
  43.   FastLED.delay(1000 / FRAMES_PER_SECOND);
  44.   // do some periodic updates
  45.   EVERY_N_MILLISECONDS( 20 ) {
  46.     gHue++;  // slowly cycle the "base color" through the rainbow
  47.   }
  48.   EVERY_N_SECONDS( 10 ) {
  49.     nextPattern();  // change patterns periodically
  50.   }
  51. }
  52. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  53. void nextPattern()
  54. {
  55.   // add one to the current pattern number, and wrap around at the end
  56.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  57. }
  58. void rainbow()
  59. {
  60.   // FastLED's built-in rainbow generator
  61.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  62. }
  63. void rainbowWithGlitter()
  64. {
  65.   // built-in FastLED rainbow, plus some random sparkly glitter
  66.   rainbow();
  67.   addGlitter(80);
  68. }
  69. void addGlitter( fract8 chanceOfGlitter)
  70. {
  71.   if ( random8() < chanceOfGlitter) {
  72.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  73.   }
  74. }
  75. void confetti()
  76. {
  77.   // random colored speckles that blink in and fade smoothly
  78.   fadeToBlackBy( leds, NUM_LEDS, 10);
  79.   int pos = random16(NUM_LEDS);
  80.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  81. }
  82. void sinelon()
  83. {
  84.   // a colored dot sweeping back and forth, with fading trails
  85.   fadeToBlackBy( leds, NUM_LEDS, 20);
  86.   int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
  87.   leds[pos] += CHSV( gHue, 255, 192);
  88. }
  89. void bpm()
  90. {
  91.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  92.   uint8_t BeatsPerMinute = 62;
  93.   CRGBPalette16 palette = PartyColors_p;
  94.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  95.   for ( int i = 0; i < NUM_LEDS; i++) { //9948
  96.     leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
  97.   }
  98. }
  99. void juggle() {
  100.   // eight colored dots, weaving in and out of sync with each other
  101.   fadeToBlackBy( leds, NUM_LEDS, 20);
  102.   uint8_t dothue = 0;
  103.   for ( int i = 0; i < 8; i++) {
  104.     leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
  105.     dothue += 32;
  106.   }
  107. }
复制代码


回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail