Page 1 of 1

Dimming Neopixels, Delays<Microseconds

Posted: Wed May 01, 2019 4:32 pm
by HelWeb
Neopixels have great possibilties. With a normal LEDs you can show the state of a system (blue= too cold, red= too hot). With a neopixel you can show values in between with smoothly changing colors from for instance blue ... many colors ... red.

Dimming an LED may be done with PWM - not so with a Neopixel.
And Neopixels have crude timings. (400 ns)

Here is a very small RTOS-task dimming a Neopixel with different colors.
BTW It can be used for very short delays below 1 microsecond.

Have fun ;)
  1. // ----------- NeoPixel -------------
  2. //---------- WS2812B.pdf ------------
  3.  
  4. inline void BitL(int lp) {
  5. int ii;
  6.   gpio_set_level( RTOS_PinNeo, 0);
  7.   for (ii=0; ii<lp; ii++) {
  8.     asm(" nop");
  9.   }
  10.  
  11. }
  12.  
  13. inline void BitH(int lp) {
  14. int ii;
  15.   gpio_set_level( RTOS_PinNeo, 1);
  16.   for (ii=0; ii<lp; ii++) {
  17.     asm(" nop");
  18.   }
  19. }
  20.  
  21. inline void Bit1() {
  22.   BitH(21);            
  23.   BitL(6); // 21/6      // from experiments
  24. }
  25.  
  26. inline void Bit0() {
  27.   BitH(8);
  28.   BitL(20); // 8/20
  29. }
  30.  
  31.  
  32. inline void Color( uint32_t c) {
  33. int i;
  34.   for (i=0;i<24;i++) {
  35.     if (c & 0x800000 ) Bit1();
  36.     else Bit0();
  37.     c<<=1;
  38.   }
  39. }
  40.  
  41. /**
  42.  * \name NeoPix
  43.  * @{
  44.  * Dimming Blink of NeoPixel, 3 colors: red, green, blue
  45.  *
  46.  * \hideinitializer
  47.  *
  48.  */
  49.  
  50. void NeoPix( void * pvParameters ) {
  51. uint32_t c=0, cc=0;
  52. int up=1;
  53. unsigned int state=0;
  54.   gpio_set_direction(RTOS_PinNeo, GPIO_MODE_OUTPUT );  // Data-Pin of NeoPixel
  55.   c=0x01;
  56.   while(1) {
  57.       portDISABLE_INTERRUPTS();
  58.       Color(cc);
  59.       BitL(2);
  60.  
  61.       //SentColors++; // for testing
  62.       // Dimming and blinking ...
  63.       if (up) { c+=1;  if (c>=0xfe)     up=0;}
  64.       else    { c-=1;  if (c==0x000000) { up=1; state+=8;} } // BRG: shift  0, 8, 16
  65.       if (state>16) state=0;
  66.       cc = (c<<state);
  67.       portENABLE_INTERRUPTS();
  68.       vTaskDelay(1);
  69.   }
  70.  
  71. }
  72.  
  73. // --- End NeoPixel
  74. /** @} */
  75.  

Re: Dimming Neopixels, Delays<Microseconds

Posted: Thu May 02, 2019 2:37 am
by ESP_Sprite
FWIW, while this may be an interesting example, the ESP32 has a bunch of peripherals that are better at doing this than dedicating a core to it... the RMT, I2S and SPI peripherals all have been used to do this with minimal CPU overhead; various libraries are available for this.

Re: Dimming Neopixels, Delays<Microseconds

Posted: Thu Jun 06, 2019 1:53 pm
by fasani
Very interesting thanks for your code example.
I've also started a project using Neopixels to receive short UDP messages and launch fast animations
https://github.com/martinberlin/Remora

I would like to send signals from Orca sequencer

Re: Dimming Neopixels, Delays<Microseconds

Posted: Mon Oct 07, 2019 3:55 pm
by fasani
Totally agree with ESP_Sprite.

Note that if you are using Neopixels Makuna library there is a I2S bug that is sometimes not refreshing the leds correctly. Use the RMT methods for the ESP32. Then you can have as much as 8 channels and control a lot of pixels.