try to revert pixelbuf merge brokenness. remove second color_u def.

This commit is contained in:
foamyguy 2022-11-22 18:47:25 -06:00
parent 19f1119994
commit e5ea1d2247
2 changed files with 39 additions and 34 deletions

View File

@ -154,56 +154,55 @@ STATIC uint8_t _pixelbuf_get_as_uint8(mp_obj_t obj) {
translate("can't convert %q to %q"), mp_obj_get_type_qstr(obj), MP_QSTR_int);
}
STATIC color_u _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t *self, mp_obj_t color) {
color_u result;
static void pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t *self, mp_obj_t color, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *w) {
pixelbuf_byteorder_details_t *byteorder = &self->byteorder;
// w is shared between white in NeoPixels and brightness in dotstars (so that DotStars can have
// per-pixel brightness). Set the defaults here in case it isn't set below.
if (byteorder->is_dotstar) {
result.w = 255;
*w = 255;
} else {
result.w = 0;
*w = 0;
}
if (mp_obj_is_int(color) || mp_obj_is_float(color)) {
mp_int_t value = mp_obj_is_int(color) ? mp_obj_get_int_truncated(color) : (mp_int_t)mp_obj_get_float(color);
result.r = value >> 16 & 0xff;
result.g = (value >> 8) & 0xff;
result.b = value & 0xff;
*r = value >> 16 & 0xff;
*g = (value >> 8) & 0xff;
*b = value & 0xff;
} else {
mp_obj_t *items;
size_t len;
mp_obj_get_array(color, &len, &items);
mp_arg_validate_length_range(len, 3, 4, MP_QSTR_color);
result.r = _pixelbuf_get_as_uint8(items[PIXEL_R]);
result.g = _pixelbuf_get_as_uint8(items[PIXEL_G]);
result.b = _pixelbuf_get_as_uint8(items[PIXEL_B]);
*r = _pixelbuf_get_as_uint8(items[PIXEL_R]);
*g = _pixelbuf_get_as_uint8(items[PIXEL_G]);
*b = _pixelbuf_get_as_uint8(items[PIXEL_B]);
if (len > 3) {
if (mp_obj_is_float(items[PIXEL_W])) {
result.w = 255 * mp_obj_get_float(items[PIXEL_W]);
*w = 255 * mp_obj_get_float(items[PIXEL_W]);
} else {
result.w = mp_obj_get_int_truncated(items[PIXEL_W]);
*w = mp_obj_get_int_truncated(items[PIXEL_W]);
}
return result;
return;
}
}
// Int colors can't set white directly so convert to white when all components are equal.
// Also handles RGBW values assigned an RGB tuple.
if (!byteorder->is_dotstar && byteorder->bpp == 4 && byteorder->has_white && result.r == result.g && result.r == result.b) {
result.w = result.r;
result.r = 0;
result.g = 0;
result.b = 0;
if (!byteorder->is_dotstar && byteorder->bpp == 4 && byteorder->has_white && *r == *g && *r == *b) {
*w = *r;
*r = 0;
*g = 0;
*b = 0;
}
return result;
}
STATIC void _pixelbuf_set_pixel_color(pixelbuf_pixelbuf_obj_t *self, size_t index, color_u rgbw) {
uint8_t r = rgbw.r;
uint8_t g = rgbw.g;
uint8_t b = rgbw.b;
uint8_t w = rgbw.w;
void common_hal_adafruit_pixelbuf_pixelbuf_parse_color(mp_obj_t self_in, mp_obj_t color, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *w) {
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
pixelbuf_parse_color(self, color, r, g, b, w);
}
static void pixelbuf_set_pixel_color(pixelbuf_pixelbuf_obj_t *self, size_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
// DotStars don't have white, instead they have 5 bit brightness so pack it into w. Shift right
// by three to leave the top five bits.
if (self->bytes_per_pixel == 4 && self->byteorder.is_dotstar) {
@ -240,10 +239,18 @@ STATIC void _pixelbuf_set_pixel_color(pixelbuf_pixelbuf_obj_t *self, size_t inde
scaled_buffer[rgbw_order->b] = (b * self->scaled_brightness) / 256;
}
}
void common_hal_adafruit_pixelbuf_pixelbuf_set_pixel_color(mp_obj_t self_in, size_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
pixelbuf_set_pixel_color(self, index, r, g, b, w);
}
STATIC void _pixelbuf_set_pixel(pixelbuf_pixelbuf_obj_t *self, size_t index, mp_obj_t value) {
color_u rgbw = _pixelbuf_parse_color(self, value);
_pixelbuf_set_pixel_color(self, index, rgbw);
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t w;
common_hal_adafruit_pixelbuf_pixelbuf_parse_color(self, value, &r, &g, &b, &w);
common_hal_adafruit_pixelbuf_pixelbuf_set_pixel_color(self, index, r, g, b, w);
}
void common_hal_adafruit_pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, mp_int_t step, size_t slice_len, mp_obj_t *values,
@ -322,10 +329,14 @@ void common_hal_adafruit_pixelbuf_pixelbuf_show(mp_obj_t self_in) {
void common_hal_adafruit_pixelbuf_pixelbuf_fill(mp_obj_t self_in, mp_obj_t fill_color) {
pixelbuf_pixelbuf_obj_t *self = native_pixelbuf(self_in);
color_u rgbw = _pixelbuf_parse_color(self, fill_color);
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t w;
common_hal_adafruit_pixelbuf_pixelbuf_parse_color(self, fill_color, &r, &g, &b, &w);
for (size_t i = 0; i < self->pixel_count; i++) {
_pixelbuf_set_pixel_color(self, i, rgbw);
common_hal_adafruit_pixelbuf_pixelbuf_set_pixel_color(self, i, r, g, b, w);
}
if (self->auto_write) {
common_hal_adafruit_pixelbuf_pixelbuf_show(self_in);

View File

@ -32,12 +32,6 @@
#include "shared-bindings/adafruit_pixelbuf/PixelBuf.h"
#include "shared-module/adafruit_pixelmap/PixelMap.h"
typedef union {
uint32_t rgbw;
struct {
uint8_t r, g, b, w;
};
} color_u;
static void pixelmap_set_pixel_rgbw(pixelmap_pixelmap_obj_t *self, size_t i, color_u rgbw) {
mp_arg_validate_index_range(i, 0, self->len, MP_QSTR_index);