2019-01-13 23:51:13 -05:00
|
|
|
/*
|
2021-05-06 09:22:35 -04:00
|
|
|
* This file is part of the CircuitPython project, https://github.com/adafruit/circuitpython
|
2019-01-13 23:51:13 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2021-07-08 17:31:10 -04:00
|
|
|
* Copyright (c) 2018 Rose Hooper
|
2019-01-13 23:51:13 -05:00
|
|
|
*
|
|
|
|
* 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/objarray.h"
|
2019-10-05 13:47:40 -04:00
|
|
|
#include "py/objtype.h"
|
2019-01-13 23:51:13 -05:00
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/gc.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-07-16 13:31:40 -04:00
|
|
|
#include "shared-bindings/adafruit_pixelbuf/PixelBuf.h"
|
|
|
|
#include "shared-module/adafruit_pixelbuf/PixelBuf.h"
|
2019-01-13 23:51:13 -05:00
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
|
|
|
|
2023-01-28 14:31:59 -05:00
|
|
|
#if CIRCUITPY_ULAB
|
2020-08-07 18:42:06 -04:00
|
|
|
#include "extmod/ulab/code/ndarray.h"
|
|
|
|
#endif
|
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
static NORETURN void invalid_byteorder(void) {
|
|
|
|
mp_arg_error_invalid(MP_QSTR_byteorder);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t *parsed);
|
2020-01-24 21:23:07 -05:00
|
|
|
|
2020-05-08 16:03:39 -04:00
|
|
|
//| class PixelBuf:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """A fast RGB[W] pixel buffer for LED and similar devices."""
|
2019-01-13 23:51:13 -05:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def __init__(
|
|
|
|
//| self,
|
|
|
|
//| size: int,
|
|
|
|
//| *,
|
|
|
|
//| byteorder: str = "BGR",
|
|
|
|
//| brightness: float = 0,
|
|
|
|
//| auto_write: bool = False,
|
|
|
|
//| header: ReadableBuffer = b"",
|
|
|
|
//| trailer: ReadableBuffer = b""
|
|
|
|
//| ) -> None:
|
2020-05-08 16:03:39 -04:00
|
|
|
//| """Create a PixelBuf object of the specified size, byteorder, and bits per pixel.
|
2019-01-13 23:51:13 -05:00
|
|
|
//|
|
2020-05-08 16:03:39 -04:00
|
|
|
//| When brightness is less than 1.0, a second buffer will be used to store the color values
|
|
|
|
//| before they are adjusted for brightness.
|
2019-01-13 23:51:13 -05:00
|
|
|
//|
|
2020-05-24 20:39:53 -04:00
|
|
|
//| When ``P`` (PWM duration) is present as the 4th character of the byteorder
|
2020-05-08 16:03:39 -04:00
|
|
|
//| string, the 4th value in the tuple/list for a pixel is the individual pixel
|
2020-05-30 05:44:13 -04:00
|
|
|
//| brightness (0.0-1.0) and will enable a Dotstar compatible 1st byte for each
|
2020-05-24 20:39:53 -04:00
|
|
|
//| pixel.
|
2019-01-13 23:51:13 -05:00
|
|
|
//|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| :param int size: Number of pixels
|
|
|
|
//| :param str byteorder: Byte order string (such as "RGB", "RGBW" or "PBGR")
|
|
|
|
//| :param float brightness: Brightness (0 to 1.0, default 1.0)
|
|
|
|
//| :param bool auto_write: Whether to automatically write pixels (Default False)
|
2021-12-21 15:28:49 -05:00
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer header: Sequence of bytes to always send before pixel values.
|
2023-02-01 03:08:41 -05:00
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer trailer: Sequence of bytes to always send after pixel values.
|
|
|
|
//| """
|
2020-05-08 16:03:39 -04:00
|
|
|
//| ...
|
2021-10-15 14:43:12 -04:00
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2020-01-24 21:23:07 -05:00
|
|
|
enum { ARG_size, ARG_byteorder, ARG_brightness, ARG_auto_write, ARG_header, ARG_trailer };
|
2019-01-13 23:51:13 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_size, MP_ARG_REQUIRED | MP_ARG_INT },
|
2020-01-24 21:23:07 -05:00
|
|
|
{ MP_QSTR_byteorder, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_BGR) } },
|
|
|
|
{ MP_QSTR_brightness, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
|
|
|
{ MP_QSTR_auto_write, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
{ MP_QSTR_header, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
|
|
|
{ MP_QSTR_trailer, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
|
2019-01-13 23:51:13 -05:00
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2019-07-26 18:52:22 -04:00
|
|
|
pixelbuf_byteorder_details_t byteorder_details;
|
2019-01-13 23:51:13 -05:00
|
|
|
|
2020-01-24 21:23:07 -05:00
|
|
|
parse_byteorder(args[ARG_byteorder].u_obj, &byteorder_details);
|
|
|
|
|
|
|
|
mp_buffer_info_t header_bufinfo;
|
|
|
|
mp_buffer_info_t trailer_bufinfo;
|
|
|
|
|
|
|
|
if (!mp_get_buffer(args[ARG_header].u_obj, &header_bufinfo, MP_BUFFER_READ)) {
|
|
|
|
header_bufinfo.buf = NULL;
|
|
|
|
header_bufinfo.len = 0;
|
|
|
|
}
|
|
|
|
if (!mp_get_buffer(args[ARG_trailer].u_obj, &trailer_bufinfo, MP_BUFFER_READ)) {
|
|
|
|
trailer_bufinfo.buf = NULL;
|
|
|
|
trailer_bufinfo.len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float brightness = 1.0;
|
|
|
|
if (args[ARG_brightness].u_obj != mp_const_none) {
|
|
|
|
brightness = mp_obj_get_float(args[ARG_brightness].u_obj);
|
|
|
|
if (brightness < 0) {
|
|
|
|
brightness = 0;
|
|
|
|
} else if (brightness > 1) {
|
|
|
|
brightness = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation complete, allocate and populate object.
|
|
|
|
pixelbuf_pixelbuf_obj_t *self = m_new_obj(pixelbuf_pixelbuf_obj_t);
|
|
|
|
self->base.type = &pixelbuf_pixelbuf_type;
|
2021-07-16 14:08:39 -04:00
|
|
|
common_hal_adafruit_pixelbuf_pixelbuf_construct(self, args[ARG_size].u_int,
|
2021-03-15 09:57:36 -04:00
|
|
|
&byteorder_details, brightness, args[ARG_auto_write].u_bool, header_bufinfo.buf,
|
|
|
|
header_bufinfo.len, trailer_bufinfo.buf, trailer_bufinfo.len);
|
2020-01-24 21:23:07 -05:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t *parsed) {
|
2022-11-08 16:05:33 -05:00
|
|
|
mp_arg_validate_type_string(byteorder_obj, MP_QSTR_byteorder);
|
2019-01-13 23:51:13 -05:00
|
|
|
|
2020-01-24 21:23:07 -05:00
|
|
|
size_t bo_len;
|
|
|
|
const char *byteorder = mp_obj_str_get_data(byteorder_obj, &bo_len);
|
|
|
|
if (bo_len < 3 || bo_len > 4) {
|
2022-05-13 15:33:43 -04:00
|
|
|
invalid_byteorder();
|
2020-01-24 21:23:07 -05:00
|
|
|
}
|
|
|
|
parsed->order_string = byteorder_obj;
|
2019-08-03 13:35:11 -04:00
|
|
|
|
2020-01-24 21:23:07 -05:00
|
|
|
parsed->bpp = bo_len;
|
2019-10-06 22:44:00 -04:00
|
|
|
char *dotstar = strchr(byteorder, 'P');
|
2019-07-26 18:52:22 -04:00
|
|
|
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);
|
2020-01-24 21:23:07 -05:00
|
|
|
if ((num_chars < parsed->bpp) || !(r && b && g)) {
|
2022-05-13 15:33:43 -04:00
|
|
|
invalid_byteorder();
|
2020-01-24 21:23:07 -05:00
|
|
|
}
|
|
|
|
parsed->is_dotstar = dotstar ? true : false;
|
|
|
|
parsed->has_white = w ? true : false;
|
|
|
|
parsed->byteorder.r = r - byteorder;
|
|
|
|
parsed->byteorder.g = g - byteorder;
|
|
|
|
parsed->byteorder.b = b - byteorder;
|
|
|
|
parsed->byteorder.w = w ? w - byteorder : 0;
|
2019-07-26 18:52:22 -04:00
|
|
|
// The dotstar brightness byte is always first (as it goes with the pixel start bits)
|
2019-10-06 22:44:00 -04:00
|
|
|
if (dotstar && byteorder[0] != 'P') {
|
2022-05-13 15:33:43 -04:00
|
|
|
invalid_byteorder();
|
2019-07-26 18:52:22 -04:00
|
|
|
}
|
2020-01-24 21:23:07 -05:00
|
|
|
if (parsed->has_white && parsed->is_dotstar) {
|
2022-05-13 15:33:43 -04:00
|
|
|
invalid_byteorder();
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
2019-10-05 13:47:40 -04:00
|
|
|
}
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| bpp: int
|
2020-05-08 16:03:39 -04:00
|
|
|
//| """The number of bytes per pixel in the buffer (read-only)"""
|
2019-01-13 23:51:13 -05:00
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_bpp(mp_obj_t self_in) {
|
2021-07-16 14:08:39 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_adafruit_pixelbuf_pixelbuf_get_bpp(self_in));
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_bpp_obj, pixelbuf_pixelbuf_obj_get_bpp);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(pixelbuf_pixelbuf_bpp_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&pixelbuf_pixelbuf_get_bpp_obj);
|
2019-01-13 23:51:13 -05:00
|
|
|
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| brightness: float
|
2020-05-08 16:03:39 -04:00
|
|
|
//| """Float value between 0 and 1. Output brightness.
|
2020-01-24 21:23:07 -05:00
|
|
|
//|
|
|
|
|
//| When brightness is less than 1.0, a second buffer will be used to store the color values
|
2020-05-08 16:03:39 -04:00
|
|
|
//| before they are adjusted for brightness."""
|
2019-01-13 23:51:13 -05:00
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_brightness(mp_obj_t self_in) {
|
2021-07-16 14:08:39 -04:00
|
|
|
return mp_obj_new_float(common_hal_adafruit_pixelbuf_pixelbuf_get_brightness(self_in));
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_brightness_obj, pixelbuf_pixelbuf_obj_get_brightness);
|
|
|
|
|
|
|
|
|
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_obj_set_brightness(mp_obj_t self_in, mp_obj_t value) {
|
2021-01-01 19:49:20 -05:00
|
|
|
mp_float_t brightness = mp_obj_get_float(value);
|
2020-01-24 21:23:07 -05:00
|
|
|
if (brightness > 1) {
|
|
|
|
brightness = 1;
|
|
|
|
} else if (brightness < 0) {
|
|
|
|
brightness = 0;
|
|
|
|
}
|
2021-07-16 14:08:39 -04:00
|
|
|
common_hal_adafruit_pixelbuf_pixelbuf_set_brightness(self_in, brightness);
|
2019-01-13 23:51:13 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(pixelbuf_pixelbuf_set_brightness_obj, pixelbuf_pixelbuf_obj_set_brightness);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(pixelbuf_pixelbuf_brightness_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&pixelbuf_pixelbuf_get_brightness_obj,
|
|
|
|
(mp_obj_t)&pixelbuf_pixelbuf_set_brightness_obj);
|
2019-01-13 23:51:13 -05:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| auto_write: bool
|
2020-05-08 16:03:39 -04:00
|
|
|
//| """Whether to automatically write the pixels after each update."""
|
2019-01-13 23:51:13 -05:00
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_auto_write(mp_obj_t self_in) {
|
2021-07-16 14:08:39 -04:00
|
|
|
return mp_obj_new_bool(common_hal_adafruit_pixelbuf_pixelbuf_get_auto_write(self_in));
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_auto_write_obj, pixelbuf_pixelbuf_obj_get_auto_write);
|
|
|
|
|
|
|
|
|
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_obj_set_auto_write(mp_obj_t self_in, mp_obj_t value) {
|
2021-07-16 14:08:39 -04:00
|
|
|
common_hal_adafruit_pixelbuf_pixelbuf_set_auto_write(self_in, mp_obj_is_true(value));
|
2019-01-13 23:51:13 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(pixelbuf_pixelbuf_set_auto_write_obj, pixelbuf_pixelbuf_obj_set_auto_write);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(pixelbuf_pixelbuf_auto_write_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&pixelbuf_pixelbuf_get_auto_write_obj,
|
|
|
|
(mp_obj_t)&pixelbuf_pixelbuf_set_auto_write_obj);
|
2019-01-13 23:51:13 -05:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| byteorder: str
|
2020-05-08 16:03:39 -04:00
|
|
|
//| """byteorder string for the buffer (read-only)"""
|
2019-01-13 23:51:13 -05:00
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_byteorder(mp_obj_t self_in) {
|
2021-07-16 14:08:39 -04:00
|
|
|
return common_hal_adafruit_pixelbuf_pixelbuf_get_byteorder_string(self_in);
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
2019-08-03 13:35:11 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_get_byteorder_str, pixelbuf_pixelbuf_obj_get_byteorder);
|
2019-01-13 23:51:13 -05:00
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(pixelbuf_pixelbuf_byteorder_str,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&pixelbuf_pixelbuf_get_byteorder_str);
|
2019-01-13 23:51:13 -05:00
|
|
|
|
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|
|
|
switch (op) {
|
2021-03-15 09:57:36 -04:00
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
return mp_const_true;
|
2020-01-24 21:23:07 -05:00
|
|
|
case MP_UNARY_OP_LEN:
|
2021-07-16 14:08:39 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_adafruit_pixelbuf_pixelbuf_get_len(self_in));
|
2021-03-15 09:57:36 -04:00
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 10:47:44 -04:00
|
|
|
//| def show(self) -> None:
|
2020-05-08 16:03:39 -04:00
|
|
|
//| """Transmits the color data to the pixels so that they are shown. This is done automatically
|
|
|
|
//| when `auto_write` is True."""
|
|
|
|
//| ...
|
2019-01-13 23:51:13 -05:00
|
|
|
|
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_show(mp_obj_t self_in) {
|
2021-07-16 14:08:39 -04:00
|
|
|
common_hal_adafruit_pixelbuf_pixelbuf_show(self_in);
|
2020-01-24 21:23:07 -05:00
|
|
|
return mp_const_none;
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_show_obj, pixelbuf_pixelbuf_show);
|
|
|
|
|
Add adafruit_pixelmap.PixelMap
.. a fast helper for animations. It is similar to and inspired by the
PixelMap helper in Adafruit LED Animation library, but with an extremely
fast 'paste' method for setting a series of pixels. This is a common
operation for many animations, and can give a substantial speed improvement.
It's named `adafruit_pixelmap` so that we can package a compatible version
in pure Python for systems that can't fit it in C in flash, or for
Blinka.
This is a proof of concept and can make a very fast comet animation:
```python
import time
import adafruit_pixelbuf
import adafruti_pixelmap
import board
import neopixel
from supervisor import ticks_ms
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation import color
pixel_pin = board.GP0
pixel_num = 96
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False, pixel_order="RGB")
evens = adafruit_pixelmap.PixelMap(pixels, tuple(range(0, pixel_num, 2)))
odd_indices = tuple((i, i+2) for i in range(1, pixel_num, 4))
print(odd_indices)
odds = adafruit_pixelbuf.PixelMap(pixels, odd_indices)
assert len(odds) == len(odd_indices)
comet_length = 16
comet1 = [color.calculate_intensity(color.GREEN, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
comet2 = [color.calculate_intensity(color.PURPLE, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
pos1 = 0
pos2 = 96//4
while True:
evens.paste(comet1, pos1, wrap=True, reverse=False, others=0)
pos1 = (pos1 + 1) % len(evens)
odds.paste(comet2, pos2, wrap=True, reverse=True, others=0)
pos2 = (pos2 - 1) % len(odds)
pixels.show()
m = ticks_ms()
if m % 2000 > 1000:
time.sleep(.02)
```
2022-11-10 12:02:31 -05:00
|
|
|
//| def fill(self, color: PixelType) -> None:
|
2020-07-03 10:47:44 -04:00
|
|
|
//| """Fills the given pixelbuf with the given color."""
|
|
|
|
//| ...
|
2020-01-24 21:23:07 -05:00
|
|
|
|
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_fill(mp_obj_t self_in, mp_obj_t value) {
|
2021-07-16 14:08:39 -04:00
|
|
|
common_hal_adafruit_pixelbuf_pixelbuf_fill(self_in, value);
|
2020-01-24 21:23:07 -05:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pixelbuf_pixelbuf_fill_obj, pixelbuf_pixelbuf_fill);
|
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| @overload
|
Add adafruit_pixelmap.PixelMap
.. a fast helper for animations. It is similar to and inspired by the
PixelMap helper in Adafruit LED Animation library, but with an extremely
fast 'paste' method for setting a series of pixels. This is a common
operation for many animations, and can give a substantial speed improvement.
It's named `adafruit_pixelmap` so that we can package a compatible version
in pure Python for systems that can't fit it in C in flash, or for
Blinka.
This is a proof of concept and can make a very fast comet animation:
```python
import time
import adafruit_pixelbuf
import adafruti_pixelmap
import board
import neopixel
from supervisor import ticks_ms
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation import color
pixel_pin = board.GP0
pixel_num = 96
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False, pixel_order="RGB")
evens = adafruit_pixelmap.PixelMap(pixels, tuple(range(0, pixel_num, 2)))
odd_indices = tuple((i, i+2) for i in range(1, pixel_num, 4))
print(odd_indices)
odds = adafruit_pixelbuf.PixelMap(pixels, odd_indices)
assert len(odds) == len(odd_indices)
comet_length = 16
comet1 = [color.calculate_intensity(color.GREEN, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
comet2 = [color.calculate_intensity(color.PURPLE, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
pos1 = 0
pos2 = 96//4
while True:
evens.paste(comet1, pos1, wrap=True, reverse=False, others=0)
pos1 = (pos1 + 1) % len(evens)
odds.paste(comet2, pos2, wrap=True, reverse=True, others=0)
pos2 = (pos2 - 1) % len(odds)
pixels.show()
m = ticks_ms()
if m % 2000 > 1000:
time.sleep(.02)
```
2022-11-10 12:02:31 -05:00
|
|
|
//| def __getitem__(self, index: slice) -> PixelReturnSequence:
|
2020-05-08 16:03:39 -04:00
|
|
|
//| """Returns the pixel value at the given index as a tuple of (Red, Green, Blue[, White]) values
|
2020-05-24 20:39:53 -04:00
|
|
|
//| between 0 and 255. When in PWM (DotStar) mode, the 4th tuple value is a float of the pixel
|
|
|
|
//| intensity from 0-1.0."""
|
2020-05-08 16:03:39 -04:00
|
|
|
//| ...
|
2020-07-23 14:22:41 -04:00
|
|
|
//| @overload
|
Add adafruit_pixelmap.PixelMap
.. a fast helper for animations. It is similar to and inspired by the
PixelMap helper in Adafruit LED Animation library, but with an extremely
fast 'paste' method for setting a series of pixels. This is a common
operation for many animations, and can give a substantial speed improvement.
It's named `adafruit_pixelmap` so that we can package a compatible version
in pure Python for systems that can't fit it in C in flash, or for
Blinka.
This is a proof of concept and can make a very fast comet animation:
```python
import time
import adafruit_pixelbuf
import adafruti_pixelmap
import board
import neopixel
from supervisor import ticks_ms
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation import color
pixel_pin = board.GP0
pixel_num = 96
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False, pixel_order="RGB")
evens = adafruit_pixelmap.PixelMap(pixels, tuple(range(0, pixel_num, 2)))
odd_indices = tuple((i, i+2) for i in range(1, pixel_num, 4))
print(odd_indices)
odds = adafruit_pixelbuf.PixelMap(pixels, odd_indices)
assert len(odds) == len(odd_indices)
comet_length = 16
comet1 = [color.calculate_intensity(color.GREEN, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
comet2 = [color.calculate_intensity(color.PURPLE, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
pos1 = 0
pos2 = 96//4
while True:
evens.paste(comet1, pos1, wrap=True, reverse=False, others=0)
pos1 = (pos1 + 1) % len(evens)
odds.paste(comet2, pos2, wrap=True, reverse=True, others=0)
pos2 = (pos2 - 1) % len(odds)
pixels.show()
m = ticks_ms()
if m % 2000 > 1000:
time.sleep(.02)
```
2022-11-10 12:02:31 -05:00
|
|
|
//| def __getitem__(self, index: int) -> PixelReturnType:
|
|
|
|
//| """Returns the pixel value at the given index as a tuple of (Red, Green, Blue[, White]) values
|
|
|
|
//| between 0 and 255. When in PWM (DotStar) mode, the 4th tuple value is a float of the pixel
|
|
|
|
//| intensity from 0-1.0."""
|
|
|
|
//| ...
|
2020-08-03 00:35:43 -04:00
|
|
|
//| @overload
|
Add adafruit_pixelmap.PixelMap
.. a fast helper for animations. It is similar to and inspired by the
PixelMap helper in Adafruit LED Animation library, but with an extremely
fast 'paste' method for setting a series of pixels. This is a common
operation for many animations, and can give a substantial speed improvement.
It's named `adafruit_pixelmap` so that we can package a compatible version
in pure Python for systems that can't fit it in C in flash, or for
Blinka.
This is a proof of concept and can make a very fast comet animation:
```python
import time
import adafruit_pixelbuf
import adafruti_pixelmap
import board
import neopixel
from supervisor import ticks_ms
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation import color
pixel_pin = board.GP0
pixel_num = 96
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False, pixel_order="RGB")
evens = adafruit_pixelmap.PixelMap(pixels, tuple(range(0, pixel_num, 2)))
odd_indices = tuple((i, i+2) for i in range(1, pixel_num, 4))
print(odd_indices)
odds = adafruit_pixelbuf.PixelMap(pixels, odd_indices)
assert len(odds) == len(odd_indices)
comet_length = 16
comet1 = [color.calculate_intensity(color.GREEN, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
comet2 = [color.calculate_intensity(color.PURPLE, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
pos1 = 0
pos2 = 96//4
while True:
evens.paste(comet1, pos1, wrap=True, reverse=False, others=0)
pos1 = (pos1 + 1) % len(evens)
odds.paste(comet2, pos2, wrap=True, reverse=True, others=0)
pos2 = (pos2 - 1) % len(odds)
pixels.show()
m = ticks_ms()
if m % 2000 > 1000:
time.sleep(.02)
```
2022-11-10 12:02:31 -05:00
|
|
|
//| def __setitem__(self, index: slice, value: PixelSequence) -> None: ...
|
2020-07-23 14:22:41 -04:00
|
|
|
//| @overload
|
Add adafruit_pixelmap.PixelMap
.. a fast helper for animations. It is similar to and inspired by the
PixelMap helper in Adafruit LED Animation library, but with an extremely
fast 'paste' method for setting a series of pixels. This is a common
operation for many animations, and can give a substantial speed improvement.
It's named `adafruit_pixelmap` so that we can package a compatible version
in pure Python for systems that can't fit it in C in flash, or for
Blinka.
This is a proof of concept and can make a very fast comet animation:
```python
import time
import adafruit_pixelbuf
import adafruti_pixelmap
import board
import neopixel
from supervisor import ticks_ms
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation import color
pixel_pin = board.GP0
pixel_num = 96
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False, pixel_order="RGB")
evens = adafruit_pixelmap.PixelMap(pixels, tuple(range(0, pixel_num, 2)))
odd_indices = tuple((i, i+2) for i in range(1, pixel_num, 4))
print(odd_indices)
odds = adafruit_pixelbuf.PixelMap(pixels, odd_indices)
assert len(odds) == len(odd_indices)
comet_length = 16
comet1 = [color.calculate_intensity(color.GREEN, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
comet2 = [color.calculate_intensity(color.PURPLE, ((1+i) / comet_length) ** 2.4)
for i in range(comet_length)]
pos1 = 0
pos2 = 96//4
while True:
evens.paste(comet1, pos1, wrap=True, reverse=False, others=0)
pos1 = (pos1 + 1) % len(evens)
odds.paste(comet2, pos2, wrap=True, reverse=True, others=0)
pos2 = (pos2 - 1) % len(odds)
pixels.show()
m = ticks_ms()
if m % 2000 > 1000:
time.sleep(.02)
```
2022-11-10 12:02:31 -05:00
|
|
|
//| def __setitem__(self, index: int, value: PixelType) -> None:
|
2020-05-24 20:39:53 -04:00
|
|
|
//| """Sets the pixel value at the given index. Value can either be a tuple or integer. Tuples are
|
|
|
|
//| The individual (Red, Green, Blue[, White]) values between 0 and 255. If given an integer, the
|
|
|
|
//| red, green and blue values are packed into the lower three bytes (0xRRGGBB).
|
|
|
|
//| For RGBW byteorders, if given only RGB values either as an int or as a tuple, the white value
|
|
|
|
//| is used instead when the red, green, and blue values are the same."""
|
2020-05-08 16:03:39 -04:00
|
|
|
//| ...
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2019-12-13 14:29:15 -05:00
|
|
|
STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
|
2019-01-13 23:51:13 -05:00
|
|
|
if (value == MP_OBJ_NULL) {
|
|
|
|
// delete item
|
|
|
|
// slice deletion
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2019-05-30 22:01:27 -04:00
|
|
|
}
|
2019-01-13 23:51:13 -05:00
|
|
|
|
|
|
|
if (0) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_SLICE
|
2021-04-22 20:55:39 -04:00
|
|
|
} else if (mp_obj_is_type(index_in, &mp_type_slice)) {
|
2019-01-13 23:51:13 -05:00
|
|
|
mp_bound_slice_t slice;
|
|
|
|
|
2021-07-16 14:08:39 -04:00
|
|
|
size_t length = common_hal_adafruit_pixelbuf_pixelbuf_get_len(self_in);
|
2020-01-24 21:23:07 -05:00
|
|
|
mp_seq_get_fast_slice_indexes(length, index_in, &slice);
|
2020-08-10 18:05:13 -04:00
|
|
|
static mp_obj_tuple_t flat_item_tuple = {
|
|
|
|
.base = {&mp_type_tuple},
|
|
|
|
.len = 0,
|
|
|
|
.items = {
|
|
|
|
mp_const_none,
|
|
|
|
mp_const_none,
|
|
|
|
mp_const_none,
|
|
|
|
mp_const_none,
|
|
|
|
}
|
|
|
|
};
|
2019-10-20 19:54:25 -04:00
|
|
|
|
2020-05-21 21:57:45 -04:00
|
|
|
size_t slice_len;
|
|
|
|
if (slice.step > 0) {
|
|
|
|
slice_len = slice.stop - slice.start;
|
2020-05-22 16:28:09 -04:00
|
|
|
} else {
|
2020-05-21 21:57:45 -04:00
|
|
|
slice_len = 1 + slice.start - slice.stop;
|
|
|
|
}
|
|
|
|
if (slice.step > 1 || slice.step < -1) {
|
|
|
|
size_t step = slice.step > 0 ? slice.step : slice.step * -1;
|
|
|
|
slice_len = (slice_len / step) + (slice_len % step ? 1 : 0);
|
2020-01-24 21:23:07 -05:00
|
|
|
}
|
2019-01-13 23:51:13 -05:00
|
|
|
|
|
|
|
if (value == MP_OBJ_SENTINEL) { // Get
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(slice_len, NULL));
|
2020-05-21 21:57:45 -04:00
|
|
|
for (uint i = 0; i < slice_len; i++) {
|
2021-07-16 14:08:39 -04:00
|
|
|
t->items[i] = common_hal_adafruit_pixelbuf_pixelbuf_get_pixel(self_in, i * slice.step + slice.start);
|
2020-01-24 21:23:07 -05:00
|
|
|
}
|
|
|
|
return MP_OBJ_FROM_PTR(t);
|
2019-01-13 23:51:13 -05:00
|
|
|
} else { // Set
|
|
|
|
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
|
|
|
|
|
2020-08-07 18:42:06 -04:00
|
|
|
size_t num_items = mp_obj_get_int(mp_obj_len(value));
|
2020-08-10 12:04:44 -04:00
|
|
|
|
2021-07-16 14:08:39 -04:00
|
|
|
if (num_items != slice_len && num_items != (slice_len * common_hal_adafruit_pixelbuf_pixelbuf_get_bpp(self_in))) {
|
2020-08-08 18:40:45 -04:00
|
|
|
mp_raise_ValueError_varg(translate("Unmatched number of items on RHS (expected %d, got %d)."), slice_len, num_items);
|
2019-01-13 23:51:13 -05:00
|
|
|
}
|
2021-07-16 14:08:39 -04:00
|
|
|
common_hal_adafruit_pixelbuf_pixelbuf_set_pixels(self_in, slice.start, slice.step, slice_len, value,
|
2020-08-10 18:05:13 -04:00
|
|
|
num_items != slice_len ? &flat_item_tuple : mp_const_none);
|
2019-01-13 23:51:13 -05:00
|
|
|
return mp_const_none;
|
|
|
|
#else
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
#endif
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-01-13 23:51:13 -05:00
|
|
|
} else { // Single index rather than slice.
|
2021-07-16 14:08:39 -04:00
|
|
|
size_t length = common_hal_adafruit_pixelbuf_pixelbuf_get_len(self_in);
|
2020-01-24 21:23:07 -05:00
|
|
|
size_t index = mp_get_index(mp_obj_get_type(self_in), length, index_in, false);
|
2019-01-13 23:51:13 -05:00
|
|
|
|
|
|
|
if (value == MP_OBJ_SENTINEL) { // Get
|
2021-07-16 14:08:39 -04:00
|
|
|
return common_hal_adafruit_pixelbuf_pixelbuf_get_pixel(self_in, index);
|
2019-01-13 23:51:13 -05:00
|
|
|
} else { // Store
|
2021-07-16 14:08:39 -04:00
|
|
|
common_hal_adafruit_pixelbuf_pixelbuf_set_pixel(self_in, index, value);
|
2019-01-13 23:51:13 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t pixelbuf_pixelbuf_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_auto_write), MP_ROM_PTR(&pixelbuf_pixelbuf_auto_write_obj)},
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_bpp), MP_ROM_PTR(&pixelbuf_pixelbuf_bpp_obj)},
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&pixelbuf_pixelbuf_brightness_obj)},
|
2019-08-03 13:35:11 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_byteorder), MP_ROM_PTR(&pixelbuf_pixelbuf_byteorder_str)},
|
2019-01-13 23:51:13 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&pixelbuf_pixelbuf_show_obj)},
|
2020-01-24 21:23:07 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&pixelbuf_pixelbuf_fill_obj)},
|
2019-01-13 23:51:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pixelbuf_pixelbuf_locals_dict, pixelbuf_pixelbuf_locals_dict_table);
|
|
|
|
|
|
|
|
|
|
|
|
const mp_obj_type_t pixelbuf_pixelbuf_type = {
|
2021-03-15 09:57:36 -04:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_PixelBuf,
|
2021-07-09 15:59:37 -04:00
|
|
|
.flags = MP_TYPE_FLAG_EXTENDED,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_t)&pixelbuf_pixelbuf_locals_dict,
|
2021-07-06 11:57:35 -04:00
|
|
|
.make_new = pixelbuf_pixelbuf_make_new,
|
2021-07-12 07:57:59 -04:00
|
|
|
MP_TYPE_EXTENDED_FIELDS(
|
2021-07-06 11:57:35 -04:00
|
|
|
.subscr = pixelbuf_pixelbuf_subscr,
|
|
|
|
.unary_op = pixelbuf_pixelbuf_unary_op,
|
|
|
|
.getiter = mp_obj_new_generic_iterator,
|
|
|
|
),
|
2019-01-13 23:51:13 -05:00
|
|
|
};
|