From 9cf7d73c6c71df1e43d11d4f2f604a76f59365a8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 16 Feb 2021 18:37:11 -0600 Subject: [PATCH 1/9] core: add bit_transpose function .. this version can only handle exactly 8 bits "across". The restriction may be relaxed in a future revision. --- locale/circuitpython.pot | 8 +++ ports/raspberrypi/mpconfigport.mk | 1 + py/circuitpy_defns.mk | 5 ++ py/circuitpy_mpconfig.h | 9 +++ py/circuitpy_mpconfig.mk | 3 + shared-bindings/_bit_transpose/__init__.c | 87 +++++++++++++++++++++++ shared-bindings/_bit_transpose/__init__.h | 32 +++++++++ shared-module/_bit_transpose/__init__.c | 83 +++++++++++++++++++++ shared-module/_bit_transpose/__init__.h | 27 +++++++ 9 files changed, 255 insertions(+) create mode 100644 shared-bindings/_bit_transpose/__init__.c create mode 100644 shared-bindings/_bit_transpose/__init__.h create mode 100644 shared-module/_bit_transpose/__init__.c create mode 100644 shared-module/_bit_transpose/__init__.h diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index e936dd1f83..11335fd325 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1092,6 +1092,10 @@ msgstr "" msgid "Initialization failed due to lack of memory" msgstr "" +#: shared-bindings/_bit_transpose/__init__.c +msgid "Input buffer must be a multiple of 8 bytes" +msgstr "" + #: ports/atmel-samd/common-hal/pulseio/PulseIn.c msgid "Input taking too long" msgstr "" @@ -1659,6 +1663,10 @@ msgstr "" msgid "Out of sockets" msgstr "" +#: shared-bindings/_bit_transpose/__init__.c +msgid "Output buffer must be at least as big as input buffer" +msgstr "" + #: shared-bindings/audiobusio/PDMIn.c msgid "Oversample must be multiple of 8." msgstr "" diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index dac7231dac..6bd617f370 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -24,6 +24,7 @@ CIRCUITPY_NEOPIXEL_WRITE = 0 endif CIRCUITPY_FULL_BUILD = 1 +CIRCUITPY_BIT_TRANSPOSE = 1 CIRCUITPY_PWMIO = 1 # Things that need to be implemented. diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index f907bf7ae6..e04031e5a6 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -132,6 +132,10 @@ endif ifeq ($(CIRCUITPY_AUDIOMP3),1) SRC_PATTERNS += audiomp3/% endif +ifeq ($(CIRCUITPY_BIT_TRANSPOSE),1) +$(info BIT_TRANSPOSE enabled) +SRC_PATTERNS += _bit_transpose/% +endif ifeq ($(CIRCUITPY_BITBANGIO),1) SRC_PATTERNS += bitbangio/% endif @@ -440,6 +444,7 @@ SRC_BINDINGS_ENUMS += \ util.c SRC_SHARED_MODULE_ALL = \ + _bit_transpose/__init__.c \ _bleio/Address.c \ _bleio/Attribute.c \ _bleio/ScanEntry.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index ee23c71569..be95eabe2e 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -299,6 +299,14 @@ extern const struct _mp_obj_module_t audiopwmio_module; #define BINASCII_MODULE #endif +#if CIRCUITPY_BIT_TRANSPOSE +extern const struct _mp_obj_module_t bit_transpose_module; +#define BIT_TRANSPOSE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__bit_transpose),(mp_obj_t)&bit_transpose_module }, +#else +#define BIT_TRANSPOSE_MODULE +#endif + + #if CIRCUITPY_BITBANGIO #define BITBANGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, extern const struct _mp_obj_module_t bitbangio_module; @@ -819,6 +827,7 @@ extern const struct _mp_obj_module_t msgpack_module; AUDIOMP3_MODULE \ AUDIOPWMIO_MODULE \ BINASCII_MODULE \ + BIT_TRANSPOSE_MODULE \ BITBANGIO_MODULE \ BLEIO_MODULE \ BOARD_MODULE \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index d9c7c0d331..f06e99b57c 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -89,6 +89,9 @@ CFLAGS += -DCIRCUITPY_AUDIOMP3=$(CIRCUITPY_AUDIOMP3) CIRCUITPY_BINASCII ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_BINASCII=$(CIRCUITPY_BINASCII) +CIRCUITPY_BIT_TRANSPOSE ?= 0 +CFLAGS += -DCIRCUITPY_BIT_TRANSPOSE=$(CIRCUITPY_BIT_TRANSPOSE) + CIRCUITPY_BITBANGIO ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO) diff --git a/shared-bindings/_bit_transpose/__init__.c b/shared-bindings/_bit_transpose/__init__.c new file mode 100644 index 0000000000..38f66eb778 --- /dev/null +++ b/shared-bindings/_bit_transpose/__init__.c @@ -0,0 +1,87 @@ +/* + * This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Roy Hooper + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/_bit_transpose/__init__.h" + +//| """A fast bit transposition function for parallel NeoPixel strips +//| +//| When driving multiple NeoPixel strips from a shift register, the bits +//| must be re-ordered in a specific way. This module offers a low-level +//| routine for performing the transformation.""" +//| + +//| def bit_transpose(input: _typing.ReadableBuffer, *, output: Optional[_typing.WritableBuffer]=None): +//| """Convert a sequence of 8*N pixel values into a single stream of bytes suitable for sending via a parallel conversion method (PioPixl8) +//| +//| Returns the output buffer if specified (which must be big enough to hold the result), otherwise a freshly allocated buffer.""" +//| ... +//| +STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_input, ARG_output }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED, {} }, + { MP_QSTR_output, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } }, + }; + 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); + + mp_buffer_info_t input_bufinfo; + mp_buffer_info_t output_bufinfo; + + mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ); + int n = input_bufinfo.len; + if (n % 8 != 0) { + mp_raise_ValueError(translate("Input buffer must be a multiple of 8 bytes")); + } + mp_obj_t output = args[ARG_output].u_obj; + + if (!output || output == mp_const_none) { + output = mp_obj_new_bytearray_of_zeros(n); + } + mp_get_buffer_raise(output, &output_bufinfo, MP_BUFFER_WRITE); + int m = output_bufinfo.len; + if (m < n) { + mp_raise_ValueError(translate("Output buffer must be at least as big as input buffer")); + } + common_hal_bit_transpose_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, input_bufinfo.len); + return output; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bit_transpose_bit_transpose_obj, 1, bit_transpose); + +STATIC const mp_rom_map_elem_t bit_transpose_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bit_transpose) }, + { MP_ROM_QSTR(MP_QSTR_bit_transpose), MP_ROM_PTR(&bit_transpose_bit_transpose_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(bit_transpose_module_globals, bit_transpose_module_globals_table); + +const mp_obj_module_t bit_transpose_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&bit_transpose_module_globals, +}; diff --git a/shared-bindings/_bit_transpose/__init__.h b/shared-bindings/_bit_transpose/__init__.h new file mode 100644 index 0000000000..75d57d980c --- /dev/null +++ b/shared-bindings/_bit_transpose/__init__.h @@ -0,0 +1,32 @@ +/* + * This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Jeff Epler + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include +#include + +void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t n); diff --git a/shared-module/_bit_transpose/__init__.c b/shared-module/_bit_transpose/__init__.c new file mode 100644 index 0000000000..e30f989b93 --- /dev/null +++ b/shared-module/_bit_transpose/__init__.c @@ -0,0 +1,83 @@ +/* + * This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Jeff Epler + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/_bit_transpose/__init__.h" + +#include +#include +#include + +// adapted from "Hacker's Delight" - Figure 7-2 Transposing an 8x8-bit matrix +// basic idea is: +// > First, treat the 8x8-bit matrix as 16 2x2-bit matrices, and transpose each +// > of the 16 2x2-bit matrices. Second, treat the matrix as four 2x2 submatrices +// > whose elements are 2x2-bit matrices and transpose each of the four 2x2 +// > submatrices. Finally, treat the matrix as a 2x2 matrix whose elements are +// > 4x4-bit matrices, and transpose the 2x2 matrix. These transformations are +// > illustrated below. +// We want a different definition of bit/byte order, deal with strides differently, etc. +// so the code is heavily re-worked compared to the original. +static void transpose8(uint32_t *result, const uint8_t *src, int src_stride) { + uint32_t x, y, t; + + y = *src; src += src_stride; + y |= (*src << 8); src += src_stride; + y |= (*src << 16); src += src_stride; + y |= (*src << 24); src += src_stride; + x = *src; src += src_stride; + x |= (*src << 8); src += src_stride; + x |= (*src << 16); src += src_stride; + x |= (*src << 24); src += src_stride; + + t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7); + t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7); + + t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14); + t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14); + + t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F); + y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F); + x = t; + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + x = __builtin_bswap32(x); + y = __builtin_bswap32(y); +#endif + result[0] = x; + result[1] = y; +} + +static void bit_transpose(uint32_t *result, const uint8_t *src, size_t src_stride, size_t n) { + for(size_t i=0; i Date: Thu, 18 Feb 2021 11:33:13 -0600 Subject: [PATCH 2/9] bit_transpose: Support from 2 to 7 strands, not just 8 --- locale/circuitpython.pot | 10 ++- shared-bindings/_bit_transpose/__init__.c | 26 +++++--- shared-bindings/_bit_transpose/__init__.h | 2 +- shared-module/_bit_transpose/__init__.c | 78 +++++++++++++++++++++-- 4 files changed, 99 insertions(+), 17 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 11335fd325..eef1b07f29 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1093,7 +1093,8 @@ msgid "Initialization failed due to lack of memory" msgstr "" #: shared-bindings/_bit_transpose/__init__.c -msgid "Input buffer must be a multiple of 8 bytes" +#, c-format +msgid "Input buffer length (%d) must be a multiple of the strand count (%d)" msgstr "" #: ports/atmel-samd/common-hal/pulseio/PulseIn.c @@ -1664,7 +1665,8 @@ msgid "Out of sockets" msgstr "" #: shared-bindings/_bit_transpose/__init__.c -msgid "Output buffer must be at least as big as input buffer" +#, c-format +msgid "Output buffer must be at least %d bytes" msgstr "" #: shared-bindings/audiobusio/PDMIn.c @@ -3473,6 +3475,10 @@ msgstr "" msgid "not enough arguments for format string" msgstr "" +#: shared-bindings/_bit_transpose/__init__.c +msgid "num_strands must be from 2 to 8 (inclusive)" +msgstr "" + #: extmod/ulab/code/ulab_create.c msgid "number of points must be at least 2" msgstr "" diff --git a/shared-bindings/_bit_transpose/__init__.c b/shared-bindings/_bit_transpose/__init__.c index 38f66eb778..979dff1452 100644 --- a/shared-bindings/_bit_transpose/__init__.c +++ b/shared-bindings/_bit_transpose/__init__.c @@ -43,9 +43,10 @@ //| ... //| STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_input, ARG_output }; + enum { ARG_input, ARG_num_strands, ARG_output }; static const mp_arg_t allowed_args[] = { { MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED, {} }, + { MP_QSTR_num_strands, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 8 } }, { MP_QSTR_output, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -55,21 +56,28 @@ STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t mp_buffer_info_t output_bufinfo; mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ); - int n = input_bufinfo.len; - if (n % 8 != 0) { - mp_raise_ValueError(translate("Input buffer must be a multiple of 8 bytes")); + int num_strands = args[ARG_num_strands].u_int; + + if (num_strands < 2 || num_strands > 8) { + mp_raise_ValueError(translate("num_strands must be from 2 to 8 (inclusive)")); + } + + int inlen = input_bufinfo.len; + if (inlen % num_strands != 0) { + mp_raise_ValueError_varg(translate("Input buffer length (%d) must be a multiple of the strand count (%d)"), inlen, num_strands); } mp_obj_t output = args[ARG_output].u_obj; + int outlen = 8 * (inlen / num_strands); if (!output || output == mp_const_none) { - output = mp_obj_new_bytearray_of_zeros(n); + output = mp_obj_new_bytearray_of_zeros(outlen); } mp_get_buffer_raise(output, &output_bufinfo, MP_BUFFER_WRITE); - int m = output_bufinfo.len; - if (m < n) { - mp_raise_ValueError(translate("Output buffer must be at least as big as input buffer")); + int avail = output_bufinfo.len; + if (avail < outlen) { + mp_raise_ValueError_varg(translate("Output buffer must be at least %d bytes"), outlen); } - common_hal_bit_transpose_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, input_bufinfo.len); + common_hal_bit_transpose_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, inlen, num_strands); return output; } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bit_transpose_bit_transpose_obj, 1, bit_transpose); diff --git a/shared-bindings/_bit_transpose/__init__.h b/shared-bindings/_bit_transpose/__init__.h index 75d57d980c..5fcf11d45b 100644 --- a/shared-bindings/_bit_transpose/__init__.h +++ b/shared-bindings/_bit_transpose/__init__.h @@ -29,4 +29,4 @@ #include #include -void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t n); +void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands); diff --git a/shared-module/_bit_transpose/__init__.c b/shared-module/_bit_transpose/__init__.c index e30f989b93..517d2d840e 100644 --- a/shared-module/_bit_transpose/__init__.c +++ b/shared-module/_bit_transpose/__init__.c @@ -30,6 +30,12 @@ #include #include +#ifdef __GNUC__ +#define FALLTHROUGH __attribute__((fallthrough)) +#else +#define FALLTHROUGH ((void)0) /* FALLTHROUGH */ +#endif + // adapted from "Hacker's Delight" - Figure 7-2 Transposing an 8x8-bit matrix // basic idea is: // > First, treat the 8x8-bit matrix as 16 2x2-bit matrices, and transpose each @@ -40,7 +46,57 @@ // > illustrated below. // We want a different definition of bit/byte order, deal with strides differently, etc. // so the code is heavily re-worked compared to the original. -static void transpose8(uint32_t *result, const uint8_t *src, int src_stride) { +static void transpose_var(uint32_t *result, const uint8_t *src, int src_stride, int num_strands) { + uint32_t x = 0, y = 0, t; + + src += (num_strands-1) * src_stride; + + switch(num_strands) { + case 7: + x |= *src << 16; + src -= src_stride; + FALLTHROUGH; + case 6: + x |= *src << 8; + src -= src_stride; + FALLTHROUGH; + case 5: + x |= *src; + src -= src_stride; + FALLTHROUGH; + case 4: + y |= *src << 24; + src -= src_stride; + FALLTHROUGH; + case 3: + y |= *src << 16; + src -= src_stride; + FALLTHROUGH; + case 2: + y |= *src << 8; + src -= src_stride; + y |= *src; + } + + t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7); + t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7); + + t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14); + t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14); + + t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F); + y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F); + x = t; + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + x = __builtin_bswap32(x); + y = __builtin_bswap32(y); +#endif + result[0] = x; + result[1] = y; +} + +static void transpose_8(uint32_t *result, const uint8_t *src, int src_stride) { uint32_t x, y, t; y = *src; src += src_stride; @@ -70,14 +126,26 @@ static void transpose8(uint32_t *result, const uint8_t *src, int src_stride) { result[1] = y; } -static void bit_transpose(uint32_t *result, const uint8_t *src, size_t src_stride, size_t n) { +static void bit_transpose_8(uint32_t *result, const uint8_t *src, size_t src_stride, size_t n) { for(size_t i=0; i Date: Thu, 18 Feb 2021 11:43:43 -0600 Subject: [PATCH 3/9] _bit_transpose: fix docs --- shared-bindings/_bit_transpose/__init__.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/shared-bindings/_bit_transpose/__init__.c b/shared-bindings/_bit_transpose/__init__.c index 979dff1452..0b9d31fbef 100644 --- a/shared-bindings/_bit_transpose/__init__.c +++ b/shared-bindings/_bit_transpose/__init__.c @@ -36,8 +36,13 @@ //| routine for performing the transformation.""" //| -//| def bit_transpose(input: _typing.ReadableBuffer, *, output: Optional[_typing.WritableBuffer]=None): -//| """Convert a sequence of 8*N pixel values into a single stream of bytes suitable for sending via a parallel conversion method (PioPixl8) +//| def bit_transpose(input: _typing.ReadableBuffer, *, num_strands:int = 8, output: Optional[_typing.WriteableBuffer]=None) -> WriteableBuffer: +//| """Convert a sequence of pixel values into a single stream of bytes suitable for sending via a parallel conversion method (PioPixl8) +//| +//| The number of bytes in the input buffer must be a multiple of the number of strands. +//| +//| If specified output buffer must be big enough for the output, ``len(input) * 8 // num_strands``. To get a properly sized buffer, +//| you can also pass in ``output=None`` once, then re-use the return value in future calls. //| //| Returns the output buffer if specified (which must be big enough to hold the result), otherwise a freshly allocated buffer.""" //| ... From 7fd45678939147da1c3034f212331d1d755ddae6 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Feb 2021 15:41:23 -0600 Subject: [PATCH 4/9] bitops: rename from _bit_transpose, describe the algorithm --- locale/circuitpython.pot | 12 +-- ports/raspberrypi/mpconfigport.mk | 2 +- py/circuitpy_defns.mk | 8 +- py/circuitpy_mpconfig.h | 10 +- py/circuitpy_mpconfig.mk | 4 +- shared-bindings/_bit_transpose/__init__.c | 100 ----------------- shared-bindings/bitops/__init__.c | 101 ++++++++++++++++++ .../{_bit_transpose => bitops}/__init__.h | 2 +- .../{_bit_transpose => bitops}/__init__.c | 4 +- .../{_bit_transpose => bitops}/__init__.h | 0 10 files changed, 122 insertions(+), 121 deletions(-) delete mode 100644 shared-bindings/_bit_transpose/__init__.c create mode 100644 shared-bindings/bitops/__init__.c rename shared-bindings/{_bit_transpose => bitops}/__init__.h (91%) rename shared-module/{_bit_transpose => bitops}/__init__.c (96%) rename shared-module/{_bit_transpose => bitops}/__init__.h (100%) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index eef1b07f29..b1bc1a390c 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1092,7 +1092,7 @@ msgstr "" msgid "Initialization failed due to lack of memory" msgstr "" -#: shared-bindings/_bit_transpose/__init__.c +#: shared-bindings/bitops/__init__.c #, c-format msgid "Input buffer length (%d) must be a multiple of the strand count (%d)" msgstr "" @@ -1664,7 +1664,7 @@ msgstr "" msgid "Out of sockets" msgstr "" -#: shared-bindings/_bit_transpose/__init__.c +#: shared-bindings/bitops/__init__.c #, c-format msgid "Output buffer must be at least %d bytes" msgstr "" @@ -3475,10 +3475,6 @@ msgstr "" msgid "not enough arguments for format string" msgstr "" -#: shared-bindings/_bit_transpose/__init__.c -msgid "num_strands must be from 2 to 8 (inclusive)" -msgstr "" - #: extmod/ulab/code/ulab_create.c msgid "number of points must be at least 2" msgstr "" @@ -4115,6 +4111,10 @@ msgstr "" msgid "watchdog timeout must be greater than 0" msgstr "" +#: shared-bindings/bitops/__init__.c +msgid "width must be from 2 to 8 (inclusive)" +msgstr "" + #: shared-bindings/rgbmatrix/RGBMatrix.c msgid "width must be greater than zero" msgstr "" diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index 6bd617f370..b93e4dcc7f 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -24,7 +24,7 @@ CIRCUITPY_NEOPIXEL_WRITE = 0 endif CIRCUITPY_FULL_BUILD = 1 -CIRCUITPY_BIT_TRANSPOSE = 1 +CIRCUITPY_BITOPS = 1 CIRCUITPY_PWMIO = 1 # Things that need to be implemented. diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index e04031e5a6..c1cf863621 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -132,9 +132,9 @@ endif ifeq ($(CIRCUITPY_AUDIOMP3),1) SRC_PATTERNS += audiomp3/% endif -ifeq ($(CIRCUITPY_BIT_TRANSPOSE),1) -$(info BIT_TRANSPOSE enabled) -SRC_PATTERNS += _bit_transpose/% +ifeq ($(CIRCUITPY_BITOPS),1) +$(info BITOPS enabled) +SRC_PATTERNS += bitops/% endif ifeq ($(CIRCUITPY_BITBANGIO),1) SRC_PATTERNS += bitbangio/% @@ -444,7 +444,6 @@ SRC_BINDINGS_ENUMS += \ util.c SRC_SHARED_MODULE_ALL = \ - _bit_transpose/__init__.c \ _bleio/Address.c \ _bleio/Attribute.c \ _bleio/ScanEntry.c \ @@ -471,6 +470,7 @@ SRC_SHARED_MODULE_ALL = \ bitbangio/OneWire.c \ bitbangio/SPI.c \ bitbangio/__init__.c \ + bitops/__init__.c \ board/__init__.c \ adafruit_bus_device/__init__.c \ adafruit_bus_device/I2CDevice.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index be95eabe2e..d72146c426 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -299,11 +299,11 @@ extern const struct _mp_obj_module_t audiopwmio_module; #define BINASCII_MODULE #endif -#if CIRCUITPY_BIT_TRANSPOSE -extern const struct _mp_obj_module_t bit_transpose_module; -#define BIT_TRANSPOSE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR__bit_transpose),(mp_obj_t)&bit_transpose_module }, +#if CIRCUITPY_BITOPS +extern const struct _mp_obj_module_t bitops_module; +#define BITOPS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_bitops),(mp_obj_t)&bitops_module }, #else -#define BIT_TRANSPOSE_MODULE +#define BITOPS_MODULE #endif @@ -827,7 +827,7 @@ extern const struct _mp_obj_module_t msgpack_module; AUDIOMP3_MODULE \ AUDIOPWMIO_MODULE \ BINASCII_MODULE \ - BIT_TRANSPOSE_MODULE \ + BITOPS_MODULE \ BITBANGIO_MODULE \ BLEIO_MODULE \ BOARD_MODULE \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index f06e99b57c..6244ed1366 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -89,8 +89,8 @@ CFLAGS += -DCIRCUITPY_AUDIOMP3=$(CIRCUITPY_AUDIOMP3) CIRCUITPY_BINASCII ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_BINASCII=$(CIRCUITPY_BINASCII) -CIRCUITPY_BIT_TRANSPOSE ?= 0 -CFLAGS += -DCIRCUITPY_BIT_TRANSPOSE=$(CIRCUITPY_BIT_TRANSPOSE) +CIRCUITPY_BITOPS ?= 0 +CFLAGS += -DCIRCUITPY_BITOPS=$(CIRCUITPY_BITOPS) CIRCUITPY_BITBANGIO ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO) diff --git a/shared-bindings/_bit_transpose/__init__.c b/shared-bindings/_bit_transpose/__init__.c deleted file mode 100644 index 0b9d31fbef..0000000000 --- a/shared-bindings/_bit_transpose/__init__.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - * This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Roy Hooper - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "py/obj.h" -#include "py/runtime.h" - -#include "shared-bindings/_bit_transpose/__init__.h" - -//| """A fast bit transposition function for parallel NeoPixel strips -//| -//| When driving multiple NeoPixel strips from a shift register, the bits -//| must be re-ordered in a specific way. This module offers a low-level -//| routine for performing the transformation.""" -//| - -//| def bit_transpose(input: _typing.ReadableBuffer, *, num_strands:int = 8, output: Optional[_typing.WriteableBuffer]=None) -> WriteableBuffer: -//| """Convert a sequence of pixel values into a single stream of bytes suitable for sending via a parallel conversion method (PioPixl8) -//| -//| The number of bytes in the input buffer must be a multiple of the number of strands. -//| -//| If specified output buffer must be big enough for the output, ``len(input) * 8 // num_strands``. To get a properly sized buffer, -//| you can also pass in ``output=None`` once, then re-use the return value in future calls. -//| -//| Returns the output buffer if specified (which must be big enough to hold the result), otherwise a freshly allocated buffer.""" -//| ... -//| -STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_input, ARG_num_strands, ARG_output }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED, {} }, - { MP_QSTR_num_strands, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 8 } }, - { MP_QSTR_output, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } }, - }; - 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); - - mp_buffer_info_t input_bufinfo; - mp_buffer_info_t output_bufinfo; - - mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ); - int num_strands = args[ARG_num_strands].u_int; - - if (num_strands < 2 || num_strands > 8) { - mp_raise_ValueError(translate("num_strands must be from 2 to 8 (inclusive)")); - } - - int inlen = input_bufinfo.len; - if (inlen % num_strands != 0) { - mp_raise_ValueError_varg(translate("Input buffer length (%d) must be a multiple of the strand count (%d)"), inlen, num_strands); - } - mp_obj_t output = args[ARG_output].u_obj; - - int outlen = 8 * (inlen / num_strands); - if (!output || output == mp_const_none) { - output = mp_obj_new_bytearray_of_zeros(outlen); - } - mp_get_buffer_raise(output, &output_bufinfo, MP_BUFFER_WRITE); - int avail = output_bufinfo.len; - if (avail < outlen) { - mp_raise_ValueError_varg(translate("Output buffer must be at least %d bytes"), outlen); - } - common_hal_bit_transpose_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, inlen, num_strands); - return output; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bit_transpose_bit_transpose_obj, 1, bit_transpose); - -STATIC const mp_rom_map_elem_t bit_transpose_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__bit_transpose) }, - { MP_ROM_QSTR(MP_QSTR_bit_transpose), MP_ROM_PTR(&bit_transpose_bit_transpose_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(bit_transpose_module_globals, bit_transpose_module_globals_table); - -const mp_obj_module_t bit_transpose_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&bit_transpose_module_globals, -}; diff --git a/shared-bindings/bitops/__init__.c b/shared-bindings/bitops/__init__.c new file mode 100644 index 0000000000..fc57842590 --- /dev/null +++ b/shared-bindings/bitops/__init__.c @@ -0,0 +1,101 @@ +/* + * This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Roy Hooper + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/bitops/__init__.h" + +//| """Routines for low-level manipulation of binary data""" +//| +//| + +//| def bit_transpose(input: _typing.ReadableBuffer, *, width:int = 8, output: Optional[_typing.WriteableBuffer]=None) -> WriteableBuffer: +//| """"Transpose" a buffer by assembling each output byte with bits taken from each of ``width`` different input bytes. +//| +//| This can be useful to convert a sequence of pixel values into a single +//| stream of bytes suitable for sending via a parallel conversion method. +//| +//| The number of bytes in the input buffer must be a multiple of the width, +//| and the width can be any value from 2 to 8. If the width is fewer than 8, +//| then the remaining (less significant) bits of the output are set to zero. +//| +//| Let ``stride = len(input)//width``. Then the first byte is made out of the +//| most significant bits of ``[input[0], input[stride], input[2*stride], ...]``. +//| The second byte is made out of the second bits, and so on until the 8th output +//| byte which is made of the first bits of ``input[1], input[1+stride, +//| input[2*stride], ...]``. +//| +//| The required output buffer size is ``len(input) * 8 // width``. +//| +//| Returns the output buffer.""" +//| ... + +STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_input, ARG_width, ARG_output }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_output, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_width, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 8 } }, + }; + 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); + + int width = args[ARG_width].u_int; + if (width < 2 || width > 8) { + mp_raise_ValueError(translate("width must be from 2 to 8 (inclusive)")); + } + + mp_buffer_info_t input_bufinfo; + mp_get_buffer_raise(args[ARG_input].u_obj, &input_bufinfo, MP_BUFFER_READ); + int inlen = input_bufinfo.len; + if (inlen % width != 0) { + mp_raise_ValueError_varg(translate("Input buffer length (%d) must be a multiple of the strand count (%d)"), inlen, width); + } + + mp_buffer_info_t output_bufinfo; + mp_get_buffer_raise(args[ARG_output].u_obj, &output_bufinfo, MP_BUFFER_WRITE); + int avail = output_bufinfo.len; + int outlen = 8 * (inlen / width); + if (avail < outlen) { + mp_raise_ValueError_varg(translate("Output buffer must be at least %d bytes"), outlen); + } + common_hal_bitops_bit_transpose(output_bufinfo.buf, input_bufinfo.buf, inlen, width); + return args[ARG_output].u_obj; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bitops_bit_transpose_obj, 1, bit_transpose); + +STATIC const mp_rom_map_elem_t bitops_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bitops) }, + { MP_ROM_QSTR(MP_QSTR_bit_transpose), MP_ROM_PTR(&bitops_bit_transpose_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(bitops_module_globals, bitops_module_globals_table); + +const mp_obj_module_t bitops_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&bitops_module_globals, +}; diff --git a/shared-bindings/_bit_transpose/__init__.h b/shared-bindings/bitops/__init__.h similarity index 91% rename from shared-bindings/_bit_transpose/__init__.h rename to shared-bindings/bitops/__init__.h index 5fcf11d45b..6654cac5e9 100644 --- a/shared-bindings/_bit_transpose/__init__.h +++ b/shared-bindings/bitops/__init__.h @@ -29,4 +29,4 @@ #include #include -void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands); +void common_hal_bitops_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands); diff --git a/shared-module/_bit_transpose/__init__.c b/shared-module/bitops/__init__.c similarity index 96% rename from shared-module/_bit_transpose/__init__.c rename to shared-module/bitops/__init__.c index 517d2d840e..66898e5bee 100644 --- a/shared-module/_bit_transpose/__init__.c +++ b/shared-module/bitops/__init__.c @@ -24,7 +24,7 @@ * THE SOFTWARE. */ -#include "shared-bindings/_bit_transpose/__init__.h" +#include "shared-bindings/bitops/__init__.h" #include #include @@ -142,7 +142,7 @@ static void bit_transpose_var(uint32_t *result, const uint8_t *src, size_t src_s } } -void common_hal_bit_transpose_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands) { +void common_hal_bitops_bit_transpose(uint8_t *result, const uint8_t *src, size_t inlen, size_t num_strands) { if(num_strands == 8) { bit_transpose_8((uint32_t*)(void*)result, src, inlen/8, inlen/8); } else { diff --git a/shared-module/_bit_transpose/__init__.h b/shared-module/bitops/__init__.h similarity index 100% rename from shared-module/_bit_transpose/__init__.h rename to shared-module/bitops/__init__.h From d56fe7691d262e80254aaab3c6335479fa3f6ccf Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Feb 2021 15:44:51 -0600 Subject: [PATCH 5/9] bitops: doc correction --- shared-bindings/bitops/__init__.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/bitops/__init__.c b/shared-bindings/bitops/__init__.c index fc57842590..0ea556fa24 100644 --- a/shared-bindings/bitops/__init__.c +++ b/shared-bindings/bitops/__init__.c @@ -33,7 +33,7 @@ //| //| -//| def bit_transpose(input: _typing.ReadableBuffer, *, width:int = 8, output: Optional[_typing.WriteableBuffer]=None) -> WriteableBuffer: +//| def bit_transpose(input: _typing.ReadableBuffer, output: _typing.WriteableBuffer, width:int = 8) -> WriteableBuffer: //| """"Transpose" a buffer by assembling each output byte with bits taken from each of ``width`` different input bytes. //| //| This can be useful to convert a sequence of pixel values into a single @@ -59,7 +59,7 @@ STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t static const mp_arg_t allowed_args[] = { { MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED }, { MP_QSTR_output, MP_ARG_OBJ | MP_ARG_REQUIRED }, - { MP_QSTR_width, MP_ARG_INT | MP_ARG_KW_ONLY, { .u_int = 8 } }, + { MP_QSTR_width, MP_ARG_INT, { .u_int = 8 } }, }; 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); From 72e8fb085cc802cac098747f9b8355ee140009c2 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Feb 2021 16:01:23 -0600 Subject: [PATCH 6/9] remove debug print during build --- py/circuitpy_defns.mk | 1 - 1 file changed, 1 deletion(-) diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index c1cf863621..47870ede10 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -133,7 +133,6 @@ ifeq ($(CIRCUITPY_AUDIOMP3),1) SRC_PATTERNS += audiomp3/% endif ifeq ($(CIRCUITPY_BITOPS),1) -$(info BITOPS enabled) SRC_PATTERNS += bitops/% endif ifeq ($(CIRCUITPY_BITBANGIO),1) From ffae89b1914272cdb5978a0837991e33732bbe59 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Feb 2021 16:10:57 -0600 Subject: [PATCH 7/9] bitops: fix argument parsing --- shared-bindings/bitops/__init__.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/bitops/__init__.c b/shared-bindings/bitops/__init__.c index 0ea556fa24..0d40e53ca8 100644 --- a/shared-bindings/bitops/__init__.c +++ b/shared-bindings/bitops/__init__.c @@ -55,7 +55,7 @@ //| ... STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_input, ARG_width, ARG_output }; + enum { ARG_input, ARG_output, ARG_width }; static const mp_arg_t allowed_args[] = { { MP_QSTR_input, MP_ARG_OBJ | MP_ARG_REQUIRED }, { MP_QSTR_output, MP_ARG_OBJ | MP_ARG_REQUIRED }, @@ -66,7 +66,7 @@ STATIC mp_obj_t bit_transpose(size_t n_args, const mp_obj_t *pos_args, mp_map_t int width = args[ARG_width].u_int; if (width < 2 || width > 8) { - mp_raise_ValueError(translate("width must be from 2 to 8 (inclusive)")); + mp_raise_ValueError_varg(translate("width must be from 2 to 8 (inclusive), not %d"), width); } mp_buffer_info_t input_bufinfo; From 5c758523c0cd967beccc5e84457b154d9c9e071a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Feb 2021 17:19:34 -0600 Subject: [PATCH 8/9] requested changes --- shared-bindings/bitops/__init__.c | 4 ++-- shared-module/bitops/__init__.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shared-bindings/bitops/__init__.c b/shared-bindings/bitops/__init__.c index 0d40e53ca8..3c567b8ed9 100644 --- a/shared-bindings/bitops/__init__.c +++ b/shared-bindings/bitops/__init__.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2018 Roy Hooper + * Copyright (c) 2021 Jeff Epler for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -33,7 +33,7 @@ //| //| -//| def bit_transpose(input: _typing.ReadableBuffer, output: _typing.WriteableBuffer, width:int = 8) -> WriteableBuffer: +//| def bit_transpose(input: ReadableBuffer, output: WriteableBuffer, width:int = 8) -> WriteableBuffer: //| """"Transpose" a buffer by assembling each output byte with bits taken from each of ``width`` different input bytes. //| //| This can be useful to convert a sequence of pixel values into a single diff --git a/shared-module/bitops/__init__.c b/shared-module/bitops/__init__.c index 66898e5bee..cf0ac7bd1f 100644 --- a/shared-module/bitops/__init__.c +++ b/shared-module/bitops/__init__.c @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (c) 2021 Jeff Epler + * Copyright (c) 2021 Jeff Epler for Adafruit Industries * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal From b854c06a0ca31e97b4c6998ff8ef5b314ebf49e4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 18 Feb 2021 17:23:55 -0600 Subject: [PATCH 9/9] make translate --- locale/circuitpython.pot | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index b1bc1a390c..0a79520b88 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -4112,7 +4112,8 @@ msgid "watchdog timeout must be greater than 0" msgstr "" #: shared-bindings/bitops/__init__.c -msgid "width must be from 2 to 8 (inclusive)" +#, c-format +msgid "width must be from 2 to 8 (inclusive), not %d" msgstr "" #: shared-bindings/rgbmatrix/RGBMatrix.c