Browse Source

Add HSV color mode.

master
Mateusz Salamon 7 years ago
parent
commit
548ad5d804
  1. 1
      Inc/ws2812b.h
  2. 55
      Src/main.c
  3. 65
      Src/ws2812b.c

1
Inc/ws2812b.h

@ -22,6 +22,7 @@ typedef struct ws2812b_color { @@ -22,6 +22,7 @@ typedef struct ws2812b_color {
void WS2812B_Init(SPI_HandleTypeDef * spi_handler);
void WS2812B_SetDiodeColor(int16_t diode_id, ws2812b_color color);
void WS2812B_SetDiodeRGB(int16_t diode_id, uint8_t R, uint8_t G, uint8_t B);
void WS2812B_SetDiodeHSV(int16_t diode_id, uint16_t Hue, uint8_t Saturation, uint8_t Brightness);
void WS2812B_Refresh();
#endif /* WS2812B_H_ */

55
Src/main.c

@ -123,6 +123,32 @@ int main(void) @@ -123,6 +123,32 @@ int main(void)
/* USER CODE BEGIN 3 */
// HSV Value manipulation demo
for(int loop = 0; loop < 10; loop++)
{
for(int v =0; v<256; v++)
{
for(uint8_t i = 0; i <= WS2812B_LEDS; i++)
{
j = (360/35) * i;
WS2812B_SetDiodeHSV(i, j, 255, v);
}
HAL_Delay(1);
WS2812B_Refresh();
}
for(int v =255; v>-1; v--)
{
for(uint8_t i = 0; i <= WS2812B_LEDS; i++)
{
j = 360/35 * i;
WS2812B_SetDiodeHSV(i, j, 255, v);
}
HAL_Delay(1);
WS2812B_Refresh();
}
}
// RGB manipulation demo
for(int v =0; v<30; v++)
{
@ -134,6 +160,35 @@ int main(void) @@ -134,6 +160,35 @@ int main(void)
WS2812B_Refresh();
}
// HSV Saturation manipulation demo
for(int loop = 0; loop < 10; loop++)
{
for(int v =0; v<256; v++)
{
for(uint8_t i = 0; i <= WS2812B_LEDS; i++)
{
j = (360/35) * i;
WS2812B_SetDiodeHSV(i, j, v, 255);
}
HAL_Delay(1);
WS2812B_Refresh();
}
for(int v =255; v>-1; v--)
{
for(uint8_t i = 0; i <= WS2812B_LEDS; i++)
{
j = 360/35 * i;
WS2812B_SetDiodeHSV(i, j, v, 255);
}
HAL_Delay(1);
WS2812B_Refresh();
}
}
HAL_Delay(10);
}
/* USER CODE END 3 */
}

65
Src/ws2812b.c

@ -45,6 +45,71 @@ void WS2812B_SetDiodeRGB(int16_t diode_id, uint8_t R, uint8_t G, uint8_t B) @@ -45,6 +45,71 @@ void WS2812B_SetDiodeRGB(int16_t diode_id, uint8_t R, uint8_t G, uint8_t B)
ws2812b_array[diode_id].blue = B;
}
//
// Set diode with HSV model
//
// Hue 0-359
// Saturation 0-255
// Birghtness(Value) 0-255
//
void WS2812B_SetDiodeHSV(int16_t diode_id, uint16_t Hue, uint8_t Saturation, uint8_t Brightness)
{
if(diode_id >= WS2812B_LEDS || diode_id < 0) return;
uint16_t Sector, Fracts, p, q, t;
if(Saturation == 0)
{
ws2812b_array[diode_id].red = Brightness;
ws2812b_array[diode_id].green = Brightness;
ws2812b_array[diode_id]. blue = Brightness;
}
else
{
if(Hue >= 360) Hue = 359;
Sector = Hue / 60; // Sector 0 to 5
Fracts = Hue % 60;
p = (Brightness * (255 - Saturation)) / 256;
q = (Brightness * (255 - (Saturation * Fracts)/360)) / 256;
t = (Brightness * (255 - (Saturation * (360 - Fracts))/360)) / 256;
switch(Sector)
{
case 0:
ws2812b_array[diode_id].red = Brightness;
ws2812b_array[diode_id].green = (uint8_t)t;
ws2812b_array[diode_id]. blue = (uint8_t)p;
break;
case 1:
ws2812b_array[diode_id].red = (uint8_t)q;
ws2812b_array[diode_id].green = Brightness;
ws2812b_array[diode_id]. blue = (uint8_t)p;
break;
case 2:
ws2812b_array[diode_id].red = (uint8_t)p;
ws2812b_array[diode_id].green = Brightness;
ws2812b_array[diode_id]. blue = (uint8_t)t;
break;
case 3:
ws2812b_array[diode_id].red = (uint8_t)p;
ws2812b_array[diode_id].green = (uint8_t)q;
ws2812b_array[diode_id]. blue = Brightness;
break;
case 4:
ws2812b_array[diode_id].red = (uint8_t)t;
ws2812b_array[diode_id].green = (uint8_t)p;
ws2812b_array[diode_id]. blue = Brightness;
break;
default: // case 5:
ws2812b_array[diode_id].red = Brightness;
ws2812b_array[diode_id].green = (uint8_t)p;
ws2812b_array[diode_id]. blue = (uint8_t)q;
break;
}
}
}
void WS2812B_Refresh()
{
CurrentLed = 0;

Loading…
Cancel
Save