RGB to HSV conversion function destroying my Pixelblaze?

Hello all,

As I’m starting patterns that get DMX channels for color information, I get on my Pixelblaze r,g,b information when using a color picker from most DMX consoles (using Resolume and QLC+ so far). I intend to create it as a framework that would allow any pattern to be controlled through DMX/Artnet/Flamecaster

But I would like to transform this data into h,s,v values to be used in my existing patterns, since they almost all use HSV for practical reasons.

I have asked my friend chatGPT to cook me a nice function using the code reference, and it looks correct (as in “no bug detected”), but executing it absolutely crashes my Pixelblaze and restarts it with a “No LED” configuration and very hot temperature.

What did we mess up ? I don’t see anything that could lead to a crash, and even though I’m not sure exactly about the math between, it looks correct.

Thank you !


export var rgb        = [0, 1, 1]  // Some RGB values
export var hsvValues  = array(3)



function rgb2hsv(r, g, b) {
  
  RGBmax = max(r, g)
  RGBmax = max(RGBmax, b)
  
  RGBmin = min(r, g)
  RGBmin = max(RGBmin, b)
  
  deltaRGB = RGBmax - RGBmin
  h = 0
  s = (RGBmax == 0 ? 0 : deltaRGB / RGBmax)
  v = RGBmax

  if (RGBmax != RGBmin) {
    if (RGBmax == r) {
      h = (g - b) / deltaRGB + (g < b ? 6 : 0)
    } else if (RGBmax == g) {
      h = (b - r) / deltaRGB + 2
    } else if (RGBmax == b) {
      h = (r - g) / deltaRGB + 4
    }
    h /= 6
  }
  
  return [h, s, v]
}



export function beforeRender(delta) {
  //t1 = time(.1)
  
  hsvValues = rgb2hsv(rgb[0], rgb[1], rgb[2])
  
}

export function render2D(index,x,y) {
  
  hsv(hsvValues[0], hsvValues[1], hsvValues[2])
  
  //h = t1 + index/pixelCount
  //s = 1
  //v = 1
  //hsv(h, s, v)
}


I would check the forums first before ChatGPT. That implementation is creating an array for every call.

Ah apologies, I actually did a search on the forum but failed to find this topic. Sorry.
On a more general topic, I understand it creates an array for every call because we return an array every time ? It which means it completely saturates the RAM of the ESP32 and leads to a crash/heating ?

Is there a way to purge the variable once used, or force the code to use always the same array ? I can see returning an array from a function could be useful. Sorry if it’s a basic question.

Thank you

Nothing in software should be changing the heat profile in a noticeable way. The difference between an idle cpu and a busy one is a handful of milliamps and PB pretty much always uses the full cpu for fps.

But it might cause a crash. I have check in place to try to avoid memory overrunning and causing problems, but still possible. Usually you get an error back in the editor.

PB doesn’t currently have garbage collection, so it can’t free up any unused arrays. You can return an array by reference. So eg make a global and keep using that. The array.replace() function makes it efficient to reuse them and easily set all of their elements at once.

1 Like

It works great ! Thank you.

I’ll be back soon with some patterns adapted to be controllable from a DMX console, and a framework to handle this in a standardized way !