WIP: refactor _pixelbuf to use strings instead of classes

This commit is contained in:
Roy Hooper 2019-07-21 16:21:39 -04:00
parent a98bfa628e
commit db84445a62
5 changed files with 88 additions and 125 deletions

View File

@ -40,10 +40,42 @@
#include "../../shared-module/_pixelbuf/PixelBuf.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
extern const pixelbuf_byteorder_obj_t byteorder_BGR;
extern const mp_obj_type_t pixelbuf_byteorder_type;
extern const int32_t colorwheel(float pos);
int parse_byteorder_string(const char *byteorder, pixelbuf_byteorder_details_t details) {
details.bpp = strlen(byteorder);
char *dotstar = strchr(byteorder, 'D');
char *r = strchr(byteorder, 'R');
char *g = strchr(byteorder, 'G');
char *b = strchr(byteorder, 'B');
char *w = strchr(byteorder, 'W');
int num_chars = (dotstar ? 1 : 0) + (w ? 1 : 0) + (r ? 1 : 0) + (g ? 1 : 0) + (b ? 1 : 0);
if (num_chars < details.bpp)
mp_raise_ValueError(translate("Unexpected character in byteorder"));
if (!(r && b && g))
mp_raise_ValueError(translate("Incomplete byteorder string"));
details.is_dotstar = dotstar ? true : false;
details.has_white = w ? true : false;
details.byteorder.r = byteorder - r;
details.byteorder.g = byteorder - g;
details.byteorder.b = byteorder - b;
if (w)
details.byteorder.w = byteorder - w;
// The dotstar brightness byte is always first (as it goes with the pixel start bits)
// if 'D' is found at the end, adjust byte position
// if 'D' is elsewhere, error out
if (dotstar) {
size_t dotstar_pos = dotstar - byteorder;
if (dotstar_pos == 4) {
details.byteorder.b += 1;
details.byteorder.g += 1;
details.byteorder.r += 1;
} else if (dotstar_pos != 0) {
mp_raise_ValueError(translate("Dotstar position invalid"));
}
}
}
//| .. currentmodule:: pixelbuf
//|
//| :class:`PixelBuf` -- A fast RGB[W] pixel buffer for LED and similar devices
@ -51,7 +83,7 @@ extern const int32_t colorwheel(float pos);
//|
//| :class:`~_pixelbuf.PixelBuf` implements an RGB[W] bytearray abstraction.
//|
//| .. class:: PixelBuf(size, buf, byteorder=BGR, brightness=0, rawbuf=None, offset=0, dotstar=False, auto_write=False, write_function=None, write_args=None)
//| .. class:: PixelBuf(size, buf, byteorder="BGR", brightness=0, rawbuf=None, offset=0, auto_write=False, write_function=None, write_args=None)
//|
//| Create a PixelBuf object of the specified size, byteorder, and bits per pixel.
//|
@ -60,17 +92,15 @@ extern const int32_t colorwheel(float pos);
//|
//| When only given ``buf``, ``brightness`` applies to the next pixel assignment.
//|
//| When ``dotstar`` is True, and ``bpp`` is 4, the 4th value in a tuple/list
//| is the individual pixel brightness (0-1). Not compatible with RGBW Byteorders.
//| Compatible `ByteOrder` classes are bpp=3, or bpp=4 and has_luminosity=True (g LBGR).
//| When ``D`` (dotstar mode) is present in the byteorder configuration, the
//| 4th value in a tuple/list is the individual pixel brightness (0-1).
//|
//| :param ~int size: Number of pixelsx
//| :param ~bytearray buf: Bytearray to store pixel data in
//| :param ~_pixelbuf.ByteOrder byteorder: Byte order constant from `_pixelbuf`
//| :param ~bytearray buf: Bytearray in which to store pixel data
//| :param ~str byteorder: Byte order string (such as "BGR" or "BGRD")
//| :param ~float brightness: Brightness (0 to 1.0, default 1.0)
//| :param ~bytearray rawbuf: Bytearray to store raw pixel colors in
//| :param ~bytearray rawbuf: Bytearray in which to store raw pixel data (before brightness adjustment)
//| :param ~int offset: Offset from start of buffer (default 0)
//| :param ~bool dotstar: Dotstar mode (default False)
//| :param ~bool auto_write: Whether to automatically write pixels (Default False)
//| :param ~callable write_function: (optional) Callable to use to send pixels
//| :param ~list write_args: (optional) Tuple or list of args to pass to ``write_function``. The
@ -78,7 +108,7 @@ extern const int32_t colorwheel(float pos);
//|
STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
mp_arg_check_num(n_args, kw_args, 2, MP_OBJ_FUN_ARGS_MAX, true);
enum { ARG_size, ARG_buf, ARG_byteorder, ARG_brightness, ARG_rawbuf, ARG_offset, ARG_dotstar,
enum { ARG_size, ARG_buf, ARG_byteorder, ARG_brightness, ARG_rawbuf, ARG_offset,
ARG_auto_write, ARG_write_function, ARG_write_args };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_INT },
@ -87,7 +117,6 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
{ MP_QSTR_brightness, MP_ARG_OBJ, { .u_obj = mp_const_none } },
{ MP_QSTR_rawbuf, MP_ARG_OBJ, { .u_obj = mp_const_none } },
{ MP_QSTR_offset, MP_ARG_INT, { .u_int = 0 } },
{ MP_QSTR_dotstar, MP_ARG_BOOL, { .u_bool = false } },
{ MP_QSTR_auto_write, MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_write_function, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_write_args, MP_ARG_OBJ, {.u_obj = mp_const_none} },
@ -95,15 +124,23 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (mp_obj_is_subclass_fast(args[ARG_byteorder].u_obj, &pixelbuf_byteorder_type))
mp_raise_TypeError_varg(translate("byteorder is not an instance of ByteOrder (got a %s)"), mp_obj_get_type_str(args[ARG_byteorder].u_obj));
if (!MP_OBJ_IS_STR(args[ARG_byteorder].u_obj))
mp_raise_TypeError(translate("byteorder is not a string"));
pixelbuf_byteorder_obj_t *byteorder = (args[ARG_byteorder].u_obj == mp_const_none) ? MP_OBJ_FROM_PTR(&byteorder_BGR) : args[ARG_byteorder].u_obj;
const char *byteorder_str = NULL;
pixelbuf_byteorder_details_t byteorder_details;
size_t bo_len;
if (args[ARG_byteorder].u_obj == NULL)
byteorder_str = "BGR";
else
byteorder_str = mp_obj_str_get_data(byteorder_str, bo_len);
if (byteorder->has_white && args[ARG_dotstar].u_bool)
mp_raise_ValueError_varg(translate("Can not use dotstar with %s"), mp_obj_get_type_str(byteorder));
parse_byteorder_string(byteorder_str, byteorder_details);
size_t effective_bpp = args[ARG_dotstar].u_bool ? 4 : byteorder->bpp; // Always 4 for DotStar
if (byteorder_details.has_white && byteorder_details.is_dotstar)
mp_raise_ValueError(translate("Can not use dotstar with a white byte"));
size_t effective_bpp = byteorder_details.is_dotstar ? 4 : byteorder_details.bpp; // Always 4 for DotStar
size_t bytes = args[ARG_size].u_int * effective_bpp;
size_t offset = args[ARG_offset].u_int;
mp_buffer_info_t bufinfo, rawbufinfo;
@ -133,28 +170,16 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
self->base.type = &pixelbuf_pixelbuf_type;
self->pixels = args[ARG_size].u_int;
self->bytes = bytes;
self->byteorder = *byteorder; // Copied because we modify for dotstar
self->byteorder = byteorder_details; // Copied because we modify for dotstar
self->bytearray = args[ARG_buf].u_obj;
self->two_buffers = two_buffers;
self->rawbytearray = two_buffers ? args[ARG_rawbuf].u_obj : NULL;
self->offset = offset;
self->dotstar_mode = args[ARG_dotstar].u_bool;
self->buf = (uint8_t *)bufinfo.buf + offset;
self->rawbuf = two_buffers ? (uint8_t *)rawbufinfo.buf + offset : NULL;
self->pixel_step = effective_bpp;
self->auto_write = args[ARG_auto_write].u_bool;
if (self->dotstar_mode) {
// Ensure sane configuration
if (!self->byteorder.has_luminosity) {
self->byteorder.has_luminosity = true;
self->byteorder.byteorder.b += 1;
self->byteorder.byteorder.g += 1;
self->byteorder.byteorder.r += 1;
}
self->byteorder.byteorder.w = 0;
}
// Show/auto-write callbacks
self->write_function = args[ARG_write_function].u_obj;
mp_obj_t function_args = args[ARG_write_args].u_obj;
@ -187,7 +212,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
self->brightness = 1;
}
if (self->dotstar_mode) {
if (self->byteorder.is_dotstar) {
// Initialize the buffer with the dotstar start bytes.
// Header and end must be setup by caller
for (uint i = 0; i < self->pixels * 4; i += 4) {
@ -266,7 +291,7 @@ void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self) {
// Compensate for shifted buffer (bpp=3 dotstar)
for (uint i = 0; i < self->bytes; i++) {
// Don't adjust per-pixel luminance bytes in dotstar mode
if (!self->dotstar_mode || (i % 4 != 0))
if (!self->byteorder.is_dotstar || (i % 4 != 0))
buf[i] = rawbuf[i] * self->brightness;
}
}
@ -321,7 +346,7 @@ const mp_obj_property_t pixelbuf_pixelbuf_buf_obj = {
//| .. attribute:: byteorder
//|
//| `ByteOrder` class for the buffer (read-only)
//| byteorder string for the buffer (read-only)
//|
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_byteorder(mp_obj_t self_in) {
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_pixelbuf_type));
@ -397,7 +422,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
if (value == MP_OBJ_SENTINEL) { // Get
size_t len = slice.stop - slice.start;
return pixelbuf_get_pixel_array((uint8_t *) self->buf + slice.start, len, &self->byteorder, self->pixel_step, self->dotstar_mode);
return pixelbuf_get_pixel_array((uint8_t *) self->buf + slice.start, len, &self->byteorder, self->pixel_step, self->byteorder.is_dotstar);
} else { // Set
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
@ -426,7 +451,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
if (MP_OBJ_IS_TYPE(value, &mp_type_list) || MP_OBJ_IS_TYPE(value, &mp_type_tuple) || MP_OBJ_IS_INT(value)) {
pixelbuf_set_pixel(self->buf + (i * self->pixel_step),
self->two_buffers ? self->rawbuf + (i * self->pixel_step) : NULL,
self->brightness, item, &self->byteorder, self->dotstar_mode);
self->brightness, item, &self->byteorder, self->byteorder.is_dotstar);
}
}
if (self->auto_write)
@ -445,10 +470,10 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
if (value == MP_OBJ_SENTINEL) { // Get
uint8_t *pixelstart = (uint8_t *)(self->two_buffers ? self->rawbuf : self->buf) + offset;
return pixelbuf_get_pixel(pixelstart, &self->byteorder, self->dotstar_mode);
return pixelbuf_get_pixel(pixelstart, &self->byteorder, self->byteorder.is_dotstar);
} else { // Store
pixelbuf_set_pixel(self->buf + offset, self->two_buffers ? self->rawbuf + offset : NULL,
self->brightness, value, &self->byteorder, self->dotstar_mode);
self->brightness, value, &self->byteorder, self->byteorder.is_dotstar);
if (self->auto_write)
call_write_function(self);
return mp_const_none;

View File

@ -36,13 +36,12 @@ typedef struct {
size_t pixels;
size_t bytes;
size_t pixel_step;
pixelbuf_byteorder_obj_t byteorder;
pixelbuf_byteorder_details_t byteorder;
mp_obj_t bytearray;
mp_obj_t rawbytearray;
mp_float_t brightness;
bool two_buffers;
size_t offset;
bool dotstar_mode;
uint8_t *rawbuf;
uint8_t *buf;
mp_obj_t write_function;

View File

@ -42,9 +42,11 @@
//| .. module:: _pixelbuf
//| :synopsis: A fast RGB(W) pixel buffer library for like NeoPixel and DotStar.
//|
//| The `_pixelbuf` module provides :py:class:`PixelBuf` and :py:class:`ByteOrder` classes to accelerate
//| The `_pixelbuf` module provides the :py:class:`PixelBuf` class to accelerate
//| RGB(W) strip/matrix manipulation, such as DotStar and Neopixel.
//|
//| Byteorders are configured with strings, such as "RGB" or "RGBD".
//| TODO: Pull in docs from pypixelbuf.
//| Libraries
//|
@ -53,31 +55,6 @@
//|
//| PixelBuf
//| .. class:: ByteOrder()
//|
//| Classes representing byteorders for circuitpython
//| .. attribute:: bpp
//|
//| The number of bytes per pixel (read-only)
//|
//| .. attribute:: has_white
//|
//| Whether the pixel has white (in addition to RGB)
//|
//| .. attribute:: has_luminosity
//|
//| Whether the pixel has luminosity (in addition to RGB)
//|
//| .. attribute:: byteorder
//|
//| Tuple of byte order (r, g, b) or (r, g, b, w) or (r, g, b, l)
//|
STATIC void pixelbuf_byteorder_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
mp_check_self(MP_OBJ_IS_TYPE(self_in, &pixelbuf_byteorder_type));
@ -113,33 +90,6 @@ STATIC void pixelbuf_byteorder_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest)
}
}
STATIC mp_obj_t pixelbuf_byteorder_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
pixelbuf_byteorder_obj_t *self = MP_OBJ_TO_PTR(self_in);
switch (op) {
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->bpp);
default: return MP_OBJ_NULL; // op not supported
}
}
const mp_obj_type_t pixelbuf_byteorder_type = {
{ &mp_type_type },
.name = MP_QSTR_ByteOrder,
.print = pixelbuf_byteorder_print,
.unary_op = pixelbuf_byteorder_unary_op,
.attr = pixelbuf_byteorder_attr,
};
// This macro is used to simplify RGB subclass definition
#define PIXELBUF_BYTEORDER(p_name, p_bpp, p_r, p_g, p_b, p_w, p_has_white, p_has_luminosity) \
const pixelbuf_byteorder_obj_t byteorder_## p_name = { \
{ &pixelbuf_byteorder_type }, \
.name = MP_QSTR_## p_name, \
.bpp = p_bpp, \
.byteorder = { p_r, p_g, p_b, p_w }, \
.has_white = p_has_white, \
.has_luminosity = p_has_luminosity, \
};
//| .. function:: wheel(n)
//|
@ -290,36 +240,29 @@ PIXELBUF_BYTEORDER(LBGR, 4, 3, 2, 1, 0, false, true)
STATIC const mp_rom_map_elem_t pixelbuf_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__pixelbuf) },
{ MP_ROM_QSTR(MP_QSTR_PixelBuf), MP_ROM_PTR(&pixelbuf_pixelbuf_type) },
{ MP_ROM_QSTR(MP_QSTR_ByteOrder), MP_ROM_PTR(&pixelbuf_byteorder_type) },
{ MP_ROM_QSTR(MP_QSTR_RGB), MP_ROM_PTR(&byteorder_RGB) },
{ MP_ROM_QSTR(MP_QSTR_RBG), MP_ROM_PTR(&byteorder_RBG) },
{ MP_ROM_QSTR(MP_QSTR_GRB), MP_ROM_PTR(&byteorder_GRB) },
{ MP_ROM_QSTR(MP_QSTR_GBR), MP_ROM_PTR(&byteorder_GBR) },
{ MP_ROM_QSTR(MP_QSTR_BRG), MP_ROM_PTR(&byteorder_BRG) },
{ MP_ROM_QSTR(MP_QSTR_BGR), MP_ROM_PTR(&byteorder_BGR) },
{ MP_ROM_QSTR(MP_QSTR_RGBW), MP_ROM_PTR(&byteorder_RGBW) },
{ MP_ROM_QSTR(MP_QSTR_RBGW), MP_ROM_PTR(&byteorder_RBGW) },
{ MP_ROM_QSTR(MP_QSTR_GRBW), MP_ROM_PTR(&byteorder_GRBW) },
{ MP_ROM_QSTR(MP_QSTR_GBRW), MP_ROM_PTR(&byteorder_GBRW) },
{ MP_ROM_QSTR(MP_QSTR_BRGW), MP_ROM_PTR(&byteorder_BRGW) },
{ MP_ROM_QSTR(MP_QSTR_BGRW), MP_ROM_PTR(&byteorder_BGRW) },
{ MP_ROM_QSTR(MP_QSTR_LRGB), MP_ROM_PTR(&byteorder_LRGB) },
{ MP_ROM_QSTR(MP_QSTR_LRBG), MP_ROM_PTR(&byteorder_LRBG) },
{ MP_ROM_QSTR(MP_QSTR_LGRB), MP_ROM_PTR(&byteorder_LGRB) },
{ MP_ROM_QSTR(MP_QSTR_LGBR), MP_ROM_PTR(&byteorder_LGBR) },
{ MP_ROM_QSTR(MP_QSTR_LBRG), MP_ROM_PTR(&byteorder_LBRG) },
{ MP_ROM_QSTR(MP_QSTR_LBGR), MP_ROM_PTR(&byteorder_LBGR) },
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_wheel_obj) },
{ MP_ROM_QSTR(MP_QSTR_RGB), MP_ROM_QSTR(MP_QSTR_RGB) },
{ MP_ROM_QSTR(MP_QSTR_RBG), MP_ROM_QSTR(MP_QSTR_RBG) },
{ MP_ROM_QSTR(MP_QSTR_GRB), MP_ROM_QSTR(MP_QSTR_GRB) },
{ MP_ROM_QSTR(MP_QSTR_GBR), MP_ROM_QSTR(MP_QSTR_GBR) },
{ MP_ROM_QSTR(MP_QSTR_BRG), MP_ROM_QSTR(MP_QSTR_BRG) },
{ MP_ROM_QSTR(MP_QSTR_BGR), MP_ROM_QSTR(MP_QSTR_BGR) },
{ MP_ROM_QSTR(MP_QSTR_RGBW), MP_ROM_QSTR(MP_QSTR_RGBW) },
{ MP_ROM_QSTR(MP_QSTR_RBGW), MP_ROM_QSTR(MP_QSTR_RBGW) },
{ MP_ROM_QSTR(MP_QSTR_GRBW), MP_ROM_QSTR(MP_QSTR_GRBW) },
{ MP_ROM_QSTR(MP_QSTR_GBRW), MP_ROM_QSTR(MP_QSTR_GBRW) },
{ MP_ROM_QSTR(MP_QSTR_BRGW), MP_ROM_QSTR(MP_QSTR_BRGW) },
{ MP_ROM_QSTR(MP_QSTR_BGRW), MP_ROM_QSTR(MP_QSTR_BGRW) },
{ MP_ROM_QSTR(MP_QSTR_RGBD), MP_ROM_QSTR(MP_QSTR_RGBD) },
{ MP_ROM_QSTR(MP_QSTR_RBGD), MP_ROM_QSTR(MP_QSTR_RBGD) },
{ MP_ROM_QSTR(MP_QSTR_GRBD), MP_ROM_QSTR(MP_QSTR_GRBD) },
{ MP_ROM_QSTR(MP_QSTR_GBRD), MP_ROM_QSTR(MP_QSTR_GBRD) },
{ MP_ROM_QSTR(MP_QSTR_BRGD), MP_ROM_QSTR(MP_QSTR_BRGD) },
{ MP_ROM_QSTR(MP_QSTR_BGRD), MP_ROM_QSTR(MP_QSTR_BGRD) },
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_QSTR(&pixelbuf_wheel_obj) },
};
STATIC MP_DEFINE_CONST_DICT(pixelbuf_module_globals, pixelbuf_module_globals_table);
STATIC void pixelbuf_byteorder_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pixelbuf_byteorder_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "%q.%q", MP_QSTR__pixelbuf, self->name);
return;
}
const mp_obj_module_t pixelbuf_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&pixelbuf_module_globals,

View File

@ -29,9 +29,7 @@
#include "common-hal/digitalio/DigitalInOut.h"
STATIC void pixelbuf_byteorder_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
const int32_t colorwheel(float pos);
const mp_obj_type_t pixelbuf_byteorder_type;
extern void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes);
#endif //CP_SHARED_BINDINGS_PIXELBUF_INIT_H

View File

@ -37,12 +37,10 @@ typedef struct {
} pixelbuf_rgbw_t;
typedef struct {
mp_obj_base_t base;
qstr name;
uint8_t bpp;
pixelbuf_rgbw_t byteorder;
bool has_white;
bool has_luminosity;
} pixelbuf_byteorder_obj_t;
bool is_dotstar;
} pixelbuf_byteorder_details_t;
#endif // CIRCUITPYTHON_PIXELBUF_TYPES_H