adafruit / Adafruit_DotStarMatrix

Adafruit_GFX-compatible library for DotStar matrices and grids.
GNU General Public License v3.0
19 stars 7 forks source link

Bug in HSV color support for DotStar Matrices? #4

Open dxhenshaw opened 4 years ago

dxhenshaw commented 4 years ago

I have assembled a six-panel DotStar Matrix and, while it works, I'm having trouble getting certain colors to show up when using HSV. Some basic colors show as expected (red, white) but others do not (green / magenta and others).

Code follows, then a picture of the result. I'm also seeing other weird behaviors - I have a script that cycles through hues, for example, and it is extremely "flashy" and will inexplicably go from blue to black to yellow while incrementing hue from 0 thru 65536. Decreasing Value also partially works, but also includes lots of flickering to black then back to a dimmer value.

So... I think there is a bug in the HSV color support for DotStar Matrices.

#pragma region Includes and Defines
#include <Adafruit_DotStarMatrix.h>   // For the DotStar Matrix and associated libraries
#include <Adafruit_GFX.h>
#include <Wire.h>
#pragma endregion
#                     
#pragma endregion
#pragma region Instantiations

Adafruit_DotStarMatrix clockDisplay = Adafruit_DotStarMatrix(
   8, 8,                        // single panel is 8 x 8 pixels
   6, 1,                        // the matrix is 6 panels wide and 1 tall
   16, 15,                        // data and clock pins (MOSI and SCK)
   DS_MATRIX_TOP +                  // matrix begins at topmost panel
   DS_MATRIX_LEFT +               // matrix begins on left hand side
   DS_MATRIX_COLUMNS +               // matrix is ordered via columns
   DS_MATRIX_PROGRESSIVE +
   DS_TILE_TOP +                  // first pixel on first panel is at the top
   DS_TILE_LEFT +                  // and leftmost side
   DS_TILE_COLUMNS +               // and is ordered by columns
   DS_TILE_PROGRESSIVE,
   DOTSTAR_BRG);                  // initial "BRG" | bgr doesn't work | rbg doesn't work
#pragma endregion

void setup() {
   clockDisplay.begin();
   clockDisplay.show();            // Initialize all pixels to 'off'
   clockDisplay.setBrightness(10);
}

void loop()
{
#pragma region Debug code

   showStaticColoredBoxes();

   delay(5000);
#pragma endregion
}
void showStaticColoredBoxes() {

   // Box 1: Red
   uint16_t hue = 182;         
   uint8_t saturation = 255;
   uint8_t value = 255;      
   uint32_t rgbcolor = clockDisplay.ColorHSV(hue, saturation, value);
   clockDisplay.fillRect(0, 0, 5, 8, rgbcolor);

   // Box 2: Yellow
    hue = 10922;
    saturation = 255;
    value = 255;
    rgbcolor = clockDisplay.ColorHSV(hue, saturation, value);
   clockDisplay.fillRect(5, 0, 5, 8, rgbcolor);

   // Box 3: Blue
    hue = 43873;
    saturation = 255;
    value = 255;
    rgbcolor = clockDisplay.ColorHSV(hue, saturation, value);
   clockDisplay.fillRect(10, 0, 5, 8, rgbcolor);

   // Box 4: Green
    hue = 20389;
    saturation = 255;
    value = 255;
    rgbcolor = clockDisplay.ColorHSV(hue, saturation, value);
   clockDisplay.fillRect(15, 0, 5, 8, rgbcolor);

   // Box 5: Magenta
   hue = 54795;
   saturation = 255;
   value = 255;
   rgbcolor = clockDisplay.ColorHSV(hue, saturation, value);
   clockDisplay.fillRect(20, 0, 5, 8, rgbcolor);

   // Box 6: White
   hue = 0;
   saturation = 0;
   value = 255;
   rgbcolor = clockDisplay.ColorHSV(hue, saturation, value);
   clockDisplay.fillRect(25, 0, 5, 8, rgbcolor);

   clockDisplay.show();
}

Picture of actual result:

dotstarmatrix photo

Magenta and Green are not the correct colors given the hue.

eqkessel commented 2 years ago

I think this has to do with the Adafruit GFX library expecting 16-bit color in the form of a uint16_t, while the ColorHSV method is inherited from the Adafruit_DotStar class and returns a 32-bit color in the form of a uint32_t. You should be able to work around this by writing a function to get the RGB values produced by the HSV function and pass that into the Color method of your matrix, since that is overloaded to produce the correct 16-bit colors.