Skip to content

Commit 7c2fd27

Browse files
authored
Merge pull request #4976 from kattni/rainbow
Adding rainbow module.
2 parents e51d5e4 + 825a706 commit 7c2fd27

File tree

13 files changed

+152
-43
lines changed

13 files changed

+152
-43
lines changed

ports/atmel-samd/boards/escornabot_makech/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
99
INTERNAL_FLASH_FILESYSTEM = 1
1010
LONGINT_IMPL = NONE
1111
CIRCUITPY_FULL_BUILD = 0
12+
13+
CIRCUITPY_USB_MIDI = 0

py/circuitpy_defns.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ endif
229229
ifeq ($(CIRCUITPY_PIXELBUF),1)
230230
SRC_PATTERNS += _pixelbuf/%
231231
endif
232+
ifeq ($(CIRCUITPY_RAINBOW),1)
233+
SRC_PATTERNS += rainbow/%
234+
endif
232235
ifeq ($(CIRCUITPY_RGBMATRIX),1)
233236
SRC_PATTERNS += rgbmatrix/%
234237
endif
@@ -533,6 +536,7 @@ SRC_SHARED_MODULE_ALL = \
533536
network/__init__.c \
534537
msgpack/__init__.c \
535538
os/__init__.c \
539+
rainbow/__init__.c \
536540
random/__init__.c \
537541
rgbmatrix/RGBMatrix.c \
538542
rgbmatrix/__init__.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,13 @@ extern const struct _mp_obj_module_t pwmio_module;
644644
#define PWMIO_MODULE
645645
#endif
646646

647+
#if CIRCUITPY_RAINBOW
648+
extern const struct _mp_obj_module_t rainbow_module;
649+
#define RAINBOW_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_rainbow), (mp_obj_t)&rainbow_module },
650+
#else
651+
#define RAINBOW_MODULE
652+
#endif
653+
647654
#if CIRCUITPY_RANDOM
648655
extern const struct _mp_obj_module_t random_module;
649656
#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;
918925
PS2IO_MODULE \
919926
PULSEIO_MODULE \
920927
PWMIO_MODULE \
928+
RAINBOW_MODULE \
921929
RANDOM_MODULE \
922930
RE_MODULE \
923931
RGBMATRIX_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ CFLAGS += -DCIRCUITPY_PULSEIO=$(CIRCUITPY_PULSEIO)
246246
CIRCUITPY_PWMIO ?= 1
247247
CFLAGS += -DCIRCUITPY_PWMIO=$(CIRCUITPY_PWMIO)
248248

249+
CIRCUITPY_RAINBOW ?= 1
250+
CFLAGS += -DCIRCUITPY_RAINBOW=$(CIRCUITPY_RAINBOW)
251+
249252
CIRCUITPY_RANDOM ?= 1
250253
CFLAGS += -DCIRCUITPY_RANDOM=$(CIRCUITPY_RANDOM)
251254

shared-bindings/_pixelbuf/PixelBuf.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Roy Hooper
6+
* Copyright (c) 2018 Rose Hooper
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -44,8 +44,6 @@
4444
#include "extmod/ulab/code/ndarray.h"
4545
#endif
4646

47-
extern const int32_t colorwheel(float pos);
48-
4947
static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t *parsed);
5048

5149
//| class PixelBuf:

shared-bindings/_pixelbuf/PixelBuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Roy Hooper
6+
* Copyright (c) 2018 Rose Hooper
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal

shared-bindings/_pixelbuf/__init__.c

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Roy Hooper
6+
* Copyright (c) 2018 Rose Hooper
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -41,40 +41,9 @@
4141
//| Byteorders are configured with strings, such as "RGB" or "RGBD"."""
4242
// TODO: Pull in docs from pypixelbuf.
4343

44-
//| def colorwheel(n: float) -> int:
45-
//| """C implementation of the common wheel() function found in many examples.
46-
//| Returns the colorwheel RGB value as an integer value for n (usable in :py:class:`PixelBuf`, neopixel, and dotstar)."""
47-
//| ...
48-
//|
49-
//| def wheel(n: float) -> int:
50-
//| """Use of wheel() is deprecated. Please use colorwheel()."""
51-
//|
52-
53-
STATIC mp_obj_t pixelbuf_colorwheel(mp_obj_t n) {
54-
return MP_OBJ_NEW_SMALL_INT(colorwheel(mp_obj_is_small_int(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_get_float(n)));
55-
}
56-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_colorwheel_obj, pixelbuf_colorwheel);
57-
58-
const int32_t colorwheel(float pos) {
59-
if (pos > 255) {
60-
pos = pos - ((uint32_t)(pos / 256) * 256);
61-
}
62-
if (pos < 85) {
63-
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
64-
} else if (pos < 170) {
65-
pos -= 85;
66-
return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
67-
} else {
68-
pos -= 170;
69-
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
70-
}
71-
}
72-
7344
STATIC const mp_rom_map_elem_t pixelbuf_module_globals_table[] = {
7445
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__pixelbuf) },
7546
{ MP_ROM_QSTR(MP_QSTR_PixelBuf), MP_ROM_PTR(&pixelbuf_pixelbuf_type) },
76-
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
77-
{ MP_ROM_QSTR(MP_QSTR_colorwheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
7847
};
7948

8049
STATIC MP_DEFINE_CONST_DICT(pixelbuf_module_globals, pixelbuf_module_globals_table);

shared-bindings/_pixelbuf/__init__.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Roy Hooper
6+
* Copyright (c) 2018 Rose Hooper
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -27,8 +27,4 @@
2727
#ifndef CP_SHARED_BINDINGS_PIXELBUF_INIT_H
2828
#define CP_SHARED_BINDINGS_PIXELBUF_INIT_H
2929

30-
#include "common-hal/digitalio/DigitalInOut.h"
31-
32-
const int32_t colorwheel(float pos);
33-
3430
#endif // CP_SHARED_BINDINGS_PIXELBUF_INIT_H

shared-bindings/rainbow/__init__.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Kattni Rembor
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/rainbow/__init__.h"
28+
#include "py/mpconfig.h"
29+
#include "py/obj.h"
30+
//| """`rainbow` module.
31+
//|
32+
//| Provides the `colorwheel()` function."""
33+
//|
34+
//| def colorwheel(n: float) -> int:
35+
//| """C implementation of the common colorwheel() function found in many examples.
36+
//| Returns the colorwheel RGB value as an integer value for n (usable in neopixel and dotstar)."""
37+
//| ...
38+
//|
39+
STATIC mp_obj_t rainbow_colorwheel(mp_obj_t n) {
40+
return MP_OBJ_NEW_SMALL_INT(colorwheel(mp_obj_is_small_int(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_get_float(n)));
41+
}
42+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(rainbow_colorwheel_obj, rainbow_colorwheel);
43+
44+
STATIC const mp_rom_map_elem_t rainbow_module_globals_table[] = {
45+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rainbow) },
46+
{ MP_ROM_QSTR(MP_QSTR_colorwheel), MP_ROM_PTR(&rainbow_colorwheel_obj) },
47+
};
48+
49+
STATIC MP_DEFINE_CONST_DICT(rainbow_module_globals, rainbow_module_globals_table);
50+
51+
const mp_obj_module_t rainbow_module = {
52+
.base = { &mp_type_module },
53+
.globals = (mp_obj_dict_t *)&rainbow_module_globals,
54+
};

shared-bindings/rainbow/__init__.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Kattni Rembor
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef CP_SHARED_BINDINGS_RAINBOW_INIT_H
28+
#define CP_SHARED_BINDINGS_RAINBOW_INIT_H
29+
#include <stdint.h>
30+
31+
const int32_t colorwheel(float pos);
32+
33+
#endif // CP_SHARED_BINDINGS_RAINBOW_INIT_H

shared-module/_pixelbuf/PixelBuf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Roy Hooper
6+
* Copyright (c) 2018 Rose Hooper
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal

shared-module/_pixelbuf/PixelBuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2018 Roy Hooper
6+
* Copyright (c) 2018 Rose Hooper
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal

shared-module/rainbow/__init__.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Kattni Rembor
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/rainbow/__init__.h"
28+
29+
const int32_t colorwheel(float pos) {
30+
if (pos > 255) {
31+
pos = pos - ((uint32_t)(pos / 256) * 256);
32+
}
33+
if (pos < 85) {
34+
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
35+
} else if (pos < 170) {
36+
pos -= 85;
37+
return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
38+
} else {
39+
pos -= 170;
40+
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
41+
}
42+
}

0 commit comments

Comments
 (0)