*************************************************************************** */
/*
- * Support for the WS2812 Chainable RGB Leds
+ * Support for the WS2812 and WS2812B Chainable RGB Leds
*
* WS2812 protocol can be found here : https://www.adafruit.com/datasheets/WS2812.pdf
*
}
static uint8_t led_data[NB_LEDS * 3];
-static uint16_t max_led = 0;
+static int16_t max_led = -1;
static uint32_t nb_bytes = 0;
return -1;
}
if (nb_leds == 0) {
+ if (max_led == -1) {
+ return 0;
+ }
nb_leds = max_led;
+ /* All leds set previously will be sent, back to no leds set */
+ max_led = -1;
}
nb_bytes = (nb_leds + 1) * 3;
ws2812_bit_sender();
}
return 0;
}
+/* Get a pixel (led) color from the data buffer */
+int ws2812_get_pixel(uint16_t pixel_num, uint8_t* red, uint8_t* green, uint8_t* blue)
+{
+ if (pixel_num >= NB_LEDS) {
+ return -1;
+ }
+ *green = led_data[ ((pixel_num * 3) + 0) ];
+ *red = led_data[ ((pixel_num * 3) + 1) ];
+ *blue = led_data[ ((pixel_num * 3) + 2) ];
+ return 0;
+}
/* Clear the internal data buffer. */
void ws2812_clear_buffer(void)
{
memset(led_data, 0, (NB_LEDS * 3));
- max_led = 0;
+ max_led = -1;
}
/* Clear the internal data buffer and send it to the Leds, turning them all off */
/* Start at first led and send all leds off */
ws2812_clear_buffer();
ws2812_send_frame(NB_LEDS);
- max_led = 0;
+ max_led = -1;
}
void ws2812_stop(void) __attribute__ ((alias ("ws2812_clear")));
*/
int ws2812_set_pixel(uint16_t pixel_num, uint8_t red, uint8_t green, uint8_t blue);
+/* Read the value of a pixel (led) color from the data buffer (frame)
+ * The returned value is the last value set using ws2812_set_pixel(), but not
+ * necessarily the actual color of the led if ws2812_send_frame() has not been
+ * called with a big enough value. It is the value that would be set when using
+ * ws2812_send_frame() with 0 or with a value equal to or above the pixel number
+ * in the ws2812_get_pixel() call.
+ */
+int ws2812_get_pixel(uint16_t pixel_num, uint8_t* red, uint8_t* green, uint8_t* blue);
+
/* Clear the internal data buffer. */
void ws2812_clear_buffer(void);