Adding rainbow module.

This commit is contained in:
Kattni Rembor 2021-07-08 15:55:13 -04:00
parent e51d5e4df2
commit 289575a811
9 changed files with 144 additions and 37 deletions

View File

@ -229,6 +229,9 @@ endif
ifeq ($(CIRCUITPY_PIXELBUF),1)
SRC_PATTERNS += _pixelbuf/%
endif
ifeq ($(CIRCUITPY_RAINBOW),1)
SRC_PATTERNS += rainbow/%
endif
ifeq ($(CIRCUITPY_RGBMATRIX),1)
SRC_PATTERNS += rgbmatrix/%
endif
@ -533,6 +536,7 @@ SRC_SHARED_MODULE_ALL = \
network/__init__.c \
msgpack/__init__.c \
os/__init__.c \
rainbow/__init__.c \
random/__init__.c \
rgbmatrix/RGBMatrix.c \
rgbmatrix/__init__.c \

View File

@ -644,6 +644,13 @@ extern const struct _mp_obj_module_t pwmio_module;
#define PWMIO_MODULE
#endif
#if CIRCUITPY_RAINBOW
extern const struct _mp_obj_module_t rainbow_module;
#define RAINBOW_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_rainbow), (mp_obj_t)&rainbow_module },
#else
#define RAINBOW_MODULE
#endif
#if CIRCUITPY_RANDOM
extern const struct _mp_obj_module_t random_module;
#define RANDOM_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module },
@ -918,6 +925,7 @@ extern const struct _mp_obj_module_t msgpack_module;
PS2IO_MODULE \
PULSEIO_MODULE \
PWMIO_MODULE \
RAINBOW_MODULE \
RANDOM_MODULE \
RE_MODULE \
RGBMATRIX_MODULE \

View File

@ -246,6 +246,9 @@ CFLAGS += -DCIRCUITPY_PULSEIO=$(CIRCUITPY_PULSEIO)
CIRCUITPY_PWMIO ?= 1
CFLAGS += -DCIRCUITPY_PWMIO=$(CIRCUITPY_PWMIO)
CIRCUITPY_RAINBOW ?= 1
CFLAGS += -DCIRCUITPY_RAINBOW=$(CIRCUITPY_RAINBOW)
CIRCUITPY_RANDOM ?= 1
CFLAGS += -DCIRCUITPY_RANDOM=$(CIRCUITPY_RANDOM)

View File

@ -44,8 +44,6 @@
#include "extmod/ulab/code/ndarray.h"
#endif
extern const int32_t colorwheel(float pos);
static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t *parsed);
//| class PixelBuf:

View File

@ -41,40 +41,9 @@
//| Byteorders are configured with strings, such as "RGB" or "RGBD"."""
// TODO: Pull in docs from pypixelbuf.
//| def colorwheel(n: float) -> int:
//| """C implementation of the common wheel() function found in many examples.
//| Returns the colorwheel RGB value as an integer value for n (usable in :py:class:`PixelBuf`, neopixel, and dotstar)."""
//| ...
//|
//| def wheel(n: float) -> int:
//| """Use of wheel() is deprecated. Please use colorwheel()."""
//|
STATIC mp_obj_t pixelbuf_colorwheel(mp_obj_t n) {
return MP_OBJ_NEW_SMALL_INT(colorwheel(mp_obj_is_small_int(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_get_float(n)));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_colorwheel_obj, pixelbuf_colorwheel);
const int32_t colorwheel(float pos) {
if (pos > 255) {
pos = pos - ((uint32_t)(pos / 256) * 256);
}
if (pos < 85) {
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
} else if (pos < 170) {
pos -= 85;
return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
} else {
pos -= 170;
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
}
}
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_wheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
{ MP_ROM_QSTR(MP_QSTR_colorwheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
};
STATIC MP_DEFINE_CONST_DICT(pixelbuf_module_globals, pixelbuf_module_globals_table);

View File

@ -27,8 +27,4 @@
#ifndef CP_SHARED_BINDINGS_PIXELBUF_INIT_H
#define CP_SHARED_BINDINGS_PIXELBUF_INIT_H
#include "common-hal/digitalio/DigitalInOut.h"
const int32_t colorwheel(float pos);
#endif // CP_SHARED_BINDINGS_PIXELBUF_INIT_H

View File

@ -0,0 +1,54 @@
/*
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Kattni Rembor
*
* 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/rainbow/__init__.h"
#include "py/mpconfig.h"
#include "py/obj.h"
//| """`rainbow` module.
//|
//| Provides the `colorwheel()` function."""
//|
//| def colorwheel(n: float) -> int:
//| """C implementation of the common colorwheel() function found in many examples.
//| Returns the colorwheel RGB value as an integer value for n (usable in neopixel and dotstar)."""
//| ...
//|
STATIC mp_obj_t rainbow_colorwheel(mp_obj_t n) {
return MP_OBJ_NEW_SMALL_INT(colorwheel(mp_obj_is_small_int(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_get_float(n)));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(rainbow_colorwheel_obj, rainbow_colorwheel);
STATIC const mp_rom_map_elem_t rainbow_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rainbow) },
{ MP_ROM_QSTR(MP_QSTR_colorwheel), MP_ROM_PTR(&rainbow_colorwheel_obj) },
};
STATIC MP_DEFINE_CONST_DICT(rainbow_module_globals, rainbow_module_globals_table);
const mp_obj_module_t rainbow_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&rainbow_module_globals,
};

View File

@ -0,0 +1,33 @@
/*
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Kattni Rembor
*
* 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.
*/
#ifndef CP_SHARED_BINDINGS_RAINBOW_INIT_H
#define CP_SHARED_BINDINGS_RAINBOW_INIT_H
#include <stdint.h>
const int32_t colorwheel(float pos);
#endif // CP_SHARED_BINDINGS_RAINBOW_INIT_H

View File

@ -0,0 +1,42 @@
/*
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Kattni Rembor
*
* 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/rainbow/__init__.h"
const int32_t colorwheel(float pos) {
if (pos > 255) {
pos = pos - ((uint32_t)(pos / 256) * 256);
}
if (pos < 85) {
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
} else if (pos < 170) {
pos -= 85;
return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
} else {
pos -= 170;
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
}
}