[Info] WS2812 (NeoPixels) C++ class for ESP32

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Info] WS2812 (NeoPixels) C++ class for ESP32

Postby kolban » Mon Nov 28, 2016 1:32 am

https://github.com/nkolban/esp32-snippe ... /neopixels

Here is a sample C++ class for driving WS2812 (NeoPixels). It uses the RMT technology. To use, we create an instance of the WS2812() object specifying the RMT channel number, the GPIO pin for data out and the number of pixels. Now we can call one of the setPixel() methods to set a pixel's color and finally call the show() method to cause the pixels to light.

See also http://esp32.com/viewtopic.php?f=17&t=589 for another ESP32 WS2812 library.
Last edited by kolban on Fri Jan 20, 2017 10:28 am, edited 1 time in total.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

uhrheber
Posts: 16
Joined: Sat Nov 12, 2016 12:07 pm

Re: WS2812 (NeoPixels) C++ class for ESP32

Postby uhrheber » Tue Nov 29, 2016 7:09 am

Kolban,

great!
Many thanks.
If only I had more time...

Uhrheber

jumjum123
Posts: 199
Joined: Mon Oct 17, 2016 3:11 pm

Re: WS2812 (NeoPixels) C++ class for ESP32

Postby jumjum123 » Tue Dec 06, 2016 3:52 pm

Is there a limit for no. of pixels ?
As far as I understand, for an array of 8 by 8 pixels, it would need 64 * 24 * size_of_rmt_item32_t
Does rmt support this amount of memory ?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: WS2812 (NeoPixels) C++ class for ESP32

Postby kolban » Tue Dec 06, 2016 4:09 pm

My understanding is that the for each RMT channel, there is a buffer that stores the data that will be pushed out by RMT interpretation. Each "item" contains the signal level and how many clock ticks that signal level is to be maintained for. There appear to be 8 buffers available to the RMT channels and lower channel numbers appear to be able to use the buffers for the channels above as long as those channels aren't used. However, as you have noted, that still puts an upper bound on the size of items that can be used ... specifically if one used channel 0 (of 8 in total) to output then it would appear that you only have 8 x sizeof(RMT buffer) number of items. However, RMT appears to have considered this notion and appears to have "threshold interrupts" that are called when the number of items in the buffer is about to be exhausted without having reached the end. My loose reading says that RMT invokes an interrupt that is responsible for replenishing the buffer form (ideally) RAM so that it continues to be fed. As such, the amount of items that can be sent by RMT appears to be unlimited as long as the interrupt service routine can move data into RMT buffers fast enough.

It is also my loose understanding that the RMT drivers "take care of this for us" ... so when we provide a buffer of items to RMT through the driver, the wrapping logic of the driver will honor whatever length of items we have supplied.

I could (as usual) be all washed up on my thinking ... but this is my best guess at the moment.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

peter_
Posts: 13
Joined: Tue Jan 31, 2017 9:57 am

Re: [Info] WS2812 (NeoPixels) C++ class for ESP32

Postby peter_ » Wed Feb 15, 2017 2:31 pm

Hello kolban,
thank you for this snippet. It looks like there is a little bug. You have to change the MSB and LSB (particularly in the RGB set function).
I do this in that way (it is maybe not the best but it works):

Code: Select all

void WS2812::setPixel(uint16_t index, uint8_t red, uint8_t green,
		uint8_t blue) {
	if (index >= this->pixelCount) {
		ESP_LOGE(tag, "setPixel: index out of range: %d", index);
		return;
	}
  uint8_t red_swapped = 0, green_swapped = 0, blue_swapped = 0;
  for(uint8_t i=0;i<8;i++) {
      red_swapped >>= 1;
      red_swapped |= (red & 0x80);
      red <<= 1;
  }

  for(uint8_t i=0;i<8;i++) {
      green_swapped >>= 1;
      green_swapped |= (blue & 0x80);
      blue <<= 1;
  }

  for(uint8_t i=0;i<8;i++) {
      blue_swapped >>= 1;
      blue_swapped |= (green & 0x80);
      green <<= 1;
  }

  this->pixels[index].red = red_swapped;
  this->pixels[index].green = green_swapped;
  this->pixels[index].blue = blue_swapped;
} // setPixel

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Info] WS2812 (NeoPixels) C++ class for ESP32

Postby kolban » Wed Feb 15, 2017 4:15 pm

Thank you kind sir ... there is an open issue to support distinct RGB combinations and I have been tardy in addressing that. Ill take your code and get to work on it tonight (for hopefully the benefit of all).

I'll update this post when done.

2017-02-15: 10:18pm - Change posted ... details here ... https://github.com/nkolban/esp32-snippets/issues/1
Neil
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: No registered users and 19 guests