Pico Led Controller 1.0.3
A project to control LEDs using Raspberry Pi Pico W
ws2812b.h File Reference
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Functions

static void put_pixel (uint32_t pixel_grb)
 Put pixel to the LED strip. More...
 
void set_all_leds (uint32_t color, uint32_t len)
 Set all leds to the same color. More...
 
void turn_off_all (uint32_t len)
 Turn off all leds. More...
 
uint32_t get_red (uint8_t brightness)
 Get red color. More...
 
uint32_t get_green (uint8_t brightness)
 Get green color. More...
 
uint32_t get_blue (uint8_t brightness)
 Get blue color. More...
 
uint32_t get_white (uint8_t brightness)
 Get white color. More...
 
uint32_t get_purple (uint8_t brightness)
 Get purple color. More...
 
uint32_t get_yellow (uint8_t brightness)
 Get yellow color. More...
 
uint32_t get_cyan (uint8_t brightness)
 Get cyan color. More...
 
uint32_t get_orange (uint8_t brightness)
 Get orange color. More...
 
uint32_t get_pink (uint8_t brightness)
 Get pink color. More...
 
uint32_t get_turquoise (uint8_t brightness)
 Get turquoise color. More...
 
uint32_t get_magenta (uint8_t brightness)
 Get magenta color. More...
 
void set_all_red (uint32_t len, uint8_t brightness)
 Set all leds to red. More...
 
void set_all_green (uint32_t len, uint8_t brightness)
 Set all leds to green. More...
 
void set_all_blue (uint32_t len, uint8_t brightness)
 Set all leds to blue. More...
 
void set_all_white (uint32_t len, uint8_t brightness)
 Set all leds to white. More...
 
void set_rainbow_spectrum (uint32_t len)
 Set all leds to rainbow spectrum. More...
 
void set_all_purple (uint32_t len, uint8_t brightness)
 Set all leds purple. More...
 
void set_all_yellow (uint32_t len, uint8_t brightness)
 Set all leds yellow. More...
 
void set_all_cyan (uint32_t len, uint8_t brightness)
 Set all leds cyan. More...
 
void set_all_orange (uint32_t len, uint8_t brightness)
 Set all leds orange. More...
 
void set_all_pink (uint32_t len, uint8_t brightness)
 Set all leds pink. More...
 
void set_all_turquoise (uint32_t len, uint8_t brightness)
 Set all leds turquoise. More...
 
void set_all_magenta (uint32_t len, uint8_t brightness)
 Set all leds magenta. More...
 
void apply_rainbow_wheel_effect (uint32_t len, uint16_t *base_hue, uint8_t *speed_factor, uint8_t *density_factor, volatile uint8_t *brightness)
 Apply rainbow wheel effect. More...
 
void apply_rainbow_cycle_effect (uint32_t len, uint16_t *hue, uint8_t *speed_factor, volatile uint8_t *brightness)
 Apply rainbow cycle effect. More...
 
void apply_breathing_effect (uint32_t len, uint8_t *speed_factor, uint32_t *color, volatile uint8_t *base_brightness, uint8_t *brightness, bool *breathing_up)
 Apply breathing effect. More...
 
void apply_flashing_effect (uint32_t len, uint32_t *color)
 Apply flashing effect. More...
 

Function Documentation

◆ apply_breathing_effect()

void apply_breathing_effect ( uint32_t  len,
uint8_t *  speed_factor,
uint32_t *  color,
volatile uint8_t *  base_brightness,
uint8_t *  brightness,
bool *  breathing_up 
)

Apply breathing effect.

Parameters
lennumber of leds
speed_factorspeed factor
colorcolor
base_brightnessbase brightness
brightnessbrightness
breathing_upbreathing up

Definition at line 184 of file ws2812b.c.

185 {
186  uint8_t min_brightness = 15;
187  uint8_t max_brightness = *base_brightness;
188  if (max_brightness < min_brightness)
189  max_brightness = min_brightness;
190  for (uint32_t i = 0; i < len; ++i)
191  {
192  uint8_t r = URGB_R(*color);
193  uint8_t g = URGB_G(*color);
194  uint8_t b = URGB_B(*color);
195  uint32_t color = URGB(r * *brightness / 255, g * *brightness / 255, b * *brightness / 255);
196  put_pixel(color);
197  }
198  if (*brightness >= max_brightness)
199  {
200  *breathing_up = false;
201  *brightness = max_brightness;
202  }
203  else if (*brightness <= min_brightness)
204  {
205  *breathing_up = true;
206  *brightness = min_brightness;
207  }
208  if (*breathing_up)
209  *brightness += *speed_factor;
210  else
211  *brightness -= *speed_factor;
212 }
#define URGB_B(color)
Definition: urgb.h:9
#define URGB_R(color)
Definition: urgb.h:7
#define URGB(r, g, b)
Definition: urgb.h:6
#define URGB_G(color)
Definition: urgb.h:8
static void put_pixel(uint32_t pixel_grb)
Definition: ws2812b.c:10

References put_pixel(), URGB, URGB_B, URGB_G, and URGB_R.

Referenced by run_loop().

◆ apply_flashing_effect()

void apply_flashing_effect ( uint32_t  len,
uint32_t *  color 
)

Apply flashing effect.

Parameters
lennumber of leds
led_valuesled values
colorcolor

Definition at line 214 of file ws2812b.c.

215 {
216  srand(time(NULL));
217  int num_led_flash = 10;
218  int *random_indices = (int *)malloc(num_led_flash * sizeof(int));
219  for (int i = 0; i < num_led_flash; ++i)
220  random_indices[i] = rand() % len;
221  int leds_handled = 0;
222  while (leds_handled < len)
223  {
224  int i = 0;
225  if (random_indices[i] == leds_handled)
226  {
227  uint32_t new_color;
228  if (!color)
229  new_color = URGB(rand() % 256, rand() % 256, rand() % 256);
230  else
231  {
232  uint8_t r = URGB_R(*color);
233  uint8_t g = URGB_G(*color);
234  uint8_t b = URGB_B(*color);
235  new_color = URGB(r, g, b);
236  }
237  put_pixel(new_color);
238  i++;
239  if (i >= num_led_flash)
240  break;
241  }
242  else
243  put_pixel(0);
244  leds_handled++;
245  }
246 }

References put_pixel(), URGB, URGB_B, URGB_G, and URGB_R.

Referenced by run_loop().

◆ apply_rainbow_cycle_effect()

void apply_rainbow_cycle_effect ( uint32_t  len,
uint16_t *  hue,
uint8_t *  speed_factor,
volatile uint8_t *  brightness 
)

Apply rainbow cycle effect.

Parameters
lennumber of leds
huehue
speed_factorspeed factor
brightnessbrightness

Definition at line 174 of file ws2812b.c.

175 {
176  for (uint32_t i = 0; i < len; ++i)
177  {
178  uint32_t color = hsv_to_rgb(*hue, 255, *brightness);
179  put_pixel(color);
180  }
181  *hue += *speed_factor;
182 }
uint32_t hsv_to_rgb(uint16_t h, uint8_t s, uint8_t v)
Convert HSV to RGB.
Definition: hsv.c:5

References hsv_to_rgb(), and put_pixel().

Referenced by run_loop().

◆ apply_rainbow_wheel_effect()

void apply_rainbow_wheel_effect ( uint32_t  len,
uint16_t *  base_hue,
uint8_t *  speed_factor,
uint8_t *  density_factor,
volatile uint8_t *  brightness 
)

Apply rainbow wheel effect.

Parameters
lennumber of leds
base_huebase hue
speed_factorspeed factor
density_factordensity factor
brightnessbrightness

Definition at line 160 of file ws2812b.c.

161 {
162  for (uint32_t i = 0; i < len; ++i)
163  {
164  uint16_t hue = (*base_hue + i * 360 / len * *density_factor) % 360;
165  uint32_t color = hsv_to_rgb(hue, 255, *brightness);
166  put_pixel(color);
167  hue++;
168  }
169  *speed_factor = rand() % 3 + 6; // 6, 7, 8
170  *base_hue += *speed_factor;
171  *base_hue %= 360;
172 }

References hsv_to_rgb(), and put_pixel().

Referenced by run_loop().

◆ get_blue()

uint32_t get_blue ( uint8_t  brightness)

Get blue color.

Parameters
brightnessbrightness
Returns
blue color in RGB format

Definition at line 40 of file ws2812b.c.

41 {
42  return URGB(0, 0, brightness);
43 }

References URGB.

Referenced by get_current_color().

◆ get_cyan()

uint32_t get_cyan ( uint8_t  brightness)

Get cyan color.

Parameters
brightnessbrightness
Returns
cyan color in RGB format

Definition at line 60 of file ws2812b.c.

61 {
62  return URGB(0, brightness, brightness);
63 }

References URGB.

Referenced by get_current_color().

◆ get_green()

uint32_t get_green ( uint8_t  brightness)

Get green color.

Parameters
brightnessbrightness
Returns
green color in RGB format

Definition at line 35 of file ws2812b.c.

36 {
37  return URGB(0, brightness, 0);
38 }

References URGB.

Referenced by get_current_color().

◆ get_magenta()

uint32_t get_magenta ( uint8_t  brightness)

Get magenta color.

Parameters
brightnessbrightness
Returns
magenta color in RGB format

Definition at line 80 of file ws2812b.c.

81 {
82  return URGB(brightness, 0, brightness);
83 }

References URGB.

Referenced by get_current_color().

◆ get_orange()

uint32_t get_orange ( uint8_t  brightness)

Get orange color.

Parameters
brightnessbrightness
Returns
orange color in RGB format

Definition at line 65 of file ws2812b.c.

66 {
67  return URGB(brightness, brightness / 3, 0);
68 }

References URGB.

Referenced by get_current_color().

◆ get_pink()

uint32_t get_pink ( uint8_t  brightness)

Get pink color.

Parameters
brightnessbrightness
Returns
pink color in RGB format

Definition at line 70 of file ws2812b.c.

71 {
72  return URGB(brightness, brightness / 3, brightness / 3);
73 }

References URGB.

Referenced by get_current_color().

◆ get_purple()

uint32_t get_purple ( uint8_t  brightness)

Get purple color.

Parameters
brightnessbrightness
Returns
purple color in RGB format

Definition at line 50 of file ws2812b.c.

51 {
52  return URGB(brightness * 3 / 4, 0, brightness);
53 }

References URGB.

Referenced by get_current_color().

◆ get_red()

uint32_t get_red ( uint8_t  brightness)

Get red color.

Parameters
brightnessbrightness
Returns
red color in RGB format

Definition at line 30 of file ws2812b.c.

31 {
32  return URGB(brightness, 0, 0);
33 }

References URGB.

Referenced by get_current_color().

◆ get_turquoise()

uint32_t get_turquoise ( uint8_t  brightness)

Get turquoise color.

Parameters
brightnessbrightness
Returns
turquoise color in RGB format

Definition at line 75 of file ws2812b.c.

76 {
77  return URGB(brightness / 3, brightness * 2 / 3, brightness);
78 }

References URGB.

Referenced by get_current_color().

◆ get_white()

uint32_t get_white ( uint8_t  brightness)

Get white color.

Parameters
brightnessbrightness
Returns
white color in RGB format

Definition at line 45 of file ws2812b.c.

46 {
47  return URGB(brightness, brightness, brightness);
48 }

References URGB.

Referenced by get_current_color().

◆ get_yellow()

uint32_t get_yellow ( uint8_t  brightness)

Get yellow color.

Parameters
brightnessbrightness
Returns
yellow color in RGB format

Definition at line 55 of file ws2812b.c.

56 {
57  return URGB(brightness, brightness, 0);
58 }

References URGB.

Referenced by get_current_color().

◆ put_pixel()

static void put_pixel ( uint32_t  pixel_grb)
inlinestatic

Put pixel to the LED strip.

Parameters
pixel_grbpixel color in GRB format

◆ set_all_blue()

void set_all_blue ( uint32_t  len,
uint8_t  brightness 
)

Set all leds to blue.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 97 of file ws2812b.c.

98 {
99  uint32_t blue = URGB(0, 0, brightness);
100  set_all_leds(blue, len);
101 }
void set_all_leds(uint32_t color, uint32_t len)
Set all leds to the same color.
Definition: ws2812b.c:15

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_cyan()

void set_all_cyan ( uint32_t  len,
uint8_t  brightness 
)

Set all leds cyan.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 121 of file ws2812b.c.

122 {
123  uint32_t cyan = URGB(0, brightness, brightness);
124  set_all_leds(cyan, len);
125 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_green()

void set_all_green ( uint32_t  len,
uint8_t  brightness 
)

Set all leds to green.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 91 of file ws2812b.c.

92 {
93  uint32_t green = URGB(0, brightness, 0);
94  set_all_leds(green, len);
95 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_leds()

void set_all_leds ( uint32_t  color,
uint32_t  len 
)

Set all leds to the same color.

Parameters
colorcolor in RGB format
lennumber of leds

Definition at line 15 of file ws2812b.c.

16 {
17  for (uint32_t i = 0; i < len; ++i)
18  {
19  put_pixel(color);
20  }
21 }

References put_pixel().

Referenced by set_all_blue(), set_all_cyan(), set_all_green(), set_all_magenta(), set_all_orange(), set_all_pink(), set_all_purple(), set_all_red(), set_all_turquoise(), set_all_white(), set_all_yellow(), and turn_off_all().

◆ set_all_magenta()

void set_all_magenta ( uint32_t  len,
uint8_t  brightness 
)

Set all leds magenta.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 145 of file ws2812b.c.

146 {
147  uint32_t magenta = URGB(brightness, 0, brightness);
148  set_all_leds(magenta, len);
149 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_orange()

void set_all_orange ( uint32_t  len,
uint8_t  brightness 
)

Set all leds orange.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 127 of file ws2812b.c.

128 {
129  uint32_t orange = URGB(brightness, brightness / 3, 0);
130  set_all_leds(orange, len);
131 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_pink()

void set_all_pink ( uint32_t  len,
uint8_t  brightness 
)

Set all leds pink.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 133 of file ws2812b.c.

134 {
135  uint32_t pink = URGB(brightness, brightness / 3, brightness / 3);
136  set_all_leds(pink, len);
137 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_purple()

void set_all_purple ( uint32_t  len,
uint8_t  brightness 
)

Set all leds purple.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 109 of file ws2812b.c.

110 {
111  uint32_t purple = URGB(brightness * 3 / 4, 0, brightness);
112  set_all_leds(purple, len);
113 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_red()

void set_all_red ( uint32_t  len,
uint8_t  brightness 
)

Set all leds to red.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 85 of file ws2812b.c.

86 {
87  uint32_t red = URGB(brightness, 0, 0);
88  set_all_leds(red, len);
89 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_turquoise()

void set_all_turquoise ( uint32_t  len,
uint8_t  brightness 
)

Set all leds turquoise.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 139 of file ws2812b.c.

140 {
141  uint32_t turquoise = URGB(brightness / 3, brightness * 2 / 3, brightness);
142  set_all_leds(turquoise, len);
143 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_white()

void set_all_white ( uint32_t  len,
uint8_t  brightness 
)

Set all leds to white.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 103 of file ws2812b.c.

104 {
105  uint32_t white = URGB(brightness, brightness, brightness);
106  set_all_leds(white, len);
107 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_all_yellow()

void set_all_yellow ( uint32_t  len,
uint8_t  brightness 
)

Set all leds yellow.

Parameters
lennumber of leds
brightnessbrightness

Definition at line 115 of file ws2812b.c.

116 {
117  uint32_t yellow = URGB(brightness, brightness, 0);
118  set_all_leds(yellow, len);
119 }

References set_all_leds(), and URGB.

Referenced by set_light_color().

◆ set_rainbow_spectrum()

void set_rainbow_spectrum ( uint32_t  len)

Set all leds to rainbow spectrum.

Parameters
lennumber of leds

Definition at line 151 of file ws2812b.c.

152 {
153  for (uint32_t i = 0; i < len; ++i)
154  {
155  uint32_t color = hsv_to_rgb(i * 360 / len, 255, 255);
156  put_pixel(color);
157  }
158 }

References hsv_to_rgb(), and put_pixel().

◆ turn_off_all()

void turn_off_all ( uint32_t  len)

Turn off all leds.

Parameters
lennumber of leds

Definition at line 23 of file ws2812b.c.

24 {
25  uint32_t off = URGB(0, 0, 0);
26  set_all_leds(off, len);
27 }

References set_all_leds(), and URGB.

Referenced by cgi_led_brightness_handler(), cgi_led_handler(), run_loop(), and toggle_light_state().