Skip to content

Commit 3997179

Browse files
committed
Encapsulate buffers inside PixelBuf and refactor it.
1 parent 81c3bc4 commit 3997179

File tree

7 files changed

+407
-374
lines changed

7 files changed

+407
-374
lines changed

shared-bindings/_pixelbuf/PixelBuf.c

Lines changed: 127 additions & 200 deletions
Large diffs are not rendered by default.

shared-bindings/_pixelbuf/PixelBuf.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,26 @@
2727
#ifndef CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H
2828
#define CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H
2929

30-
#include "shared-bindings/_pixelbuf/types.h"
30+
#include "shared-module/_pixelbuf/PixelBuf.h"
3131

3232
const mp_obj_type_t pixelbuf_pixelbuf_type;
3333

34-
typedef struct {
35-
mp_obj_base_t base;
36-
size_t pixels;
37-
size_t bytes;
38-
size_t pixel_step;
39-
pixelbuf_byteorder_details_t byteorder;
40-
mp_obj_t bytearray;
41-
mp_obj_t rawbytearray;
42-
mp_float_t brightness;
43-
bool two_buffers;
44-
size_t offset;
45-
uint8_t *rawbuf;
46-
uint8_t *buf;
47-
bool auto_write;
48-
} pixelbuf_pixelbuf_obj_t;
34+
void common_hal__pixelbuf_pixelbuf_construct(pixelbuf_pixelbuf_obj_t *self, size_t n,
35+
pixelbuf_byteorder_details_t* byteorder, mp_float_t brightness, bool auto_write, uint8_t* header,
36+
size_t header_len, uint8_t* trailer, size_t trailer_len);
4937

50-
void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self);
51-
mp_obj_t pixelbuf_call_show(mp_obj_t self_in);
38+
// These take mp_obj_t because they are called on subclasses of PixelBuf.
39+
uint8_t common_hal__pixelbuf_pixelbuf_get_bpp(mp_obj_t self);
40+
mp_float_t common_hal__pixelbuf_pixelbuf_get_brightness(mp_obj_t self);
41+
void common_hal__pixelbuf_pixelbuf_set_brightness(mp_obj_t self, mp_float_t brightness);
42+
bool common_hal__pixelbuf_pixelbuf_get_auto_write(mp_obj_t self);
43+
void common_hal__pixelbuf_pixelbuf_set_auto_write(mp_obj_t self, bool auto_write);
44+
size_t common_hal__pixelbuf_pixelbuf_get_len(mp_obj_t self_in);
45+
mp_obj_t common_hal__pixelbuf_pixelbuf_get_byteorder_string(mp_obj_t self);
46+
void common_hal__pixelbuf_pixelbuf_fill(mp_obj_t self, mp_obj_t item);
47+
void common_hal__pixelbuf_pixelbuf_show(mp_obj_t self);
48+
mp_obj_t common_hal__pixelbuf_pixelbuf_get_pixel(mp_obj_t self, size_t index);
49+
void common_hal__pixelbuf_pixelbuf_set_pixel(mp_obj_t self, size_t index, mp_obj_t item);
50+
void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, size_t stop, size_t step, mp_obj_t* values);
5251

5352
#endif // CP_SHARED_BINDINGS_PIXELBUF_PIXELBUF_H

shared-bindings/_pixelbuf/__init__.c

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@
2929
#include "py/runtime.h"
3030
#include "py/objproperty.h"
3131

32-
#include "types.h"
33-
#include "__init__.h"
34-
35-
#include "PixelBuf.h"
36-
#include "../../shared-module/_pixelbuf/PixelBuf.h"
32+
#include "shared-bindings/_pixelbuf/__init__.h"
33+
#include "shared-bindings/_pixelbuf/PixelBuf.h"
3734

3835

3936
//| :mod:`_pixelbuf` --- Fast RGB(W) pixel buffer and helpers
@@ -82,39 +79,15 @@ const int32_t colorwheel(float pos) {
8279
}
8380
}
8481

85-
86-
//| .. function:: fill(pixelbuf, color)
87-
//|
88-
//| Fills the given pixelbuf with the given color.
89-
//|
90-
91-
STATIC mp_obj_t pixelbuf_fill(mp_obj_t pixelbuf_in, mp_obj_t value) {
92-
mp_obj_t obj = mp_instance_cast_to_native_base(pixelbuf_in, &pixelbuf_pixelbuf_type);
93-
if (obj == MP_OBJ_NULL)
94-
mp_raise_TypeError(translate("Expected a PixelBuf instance"));
95-
pixelbuf_pixelbuf_obj_t *pixelbuf = MP_OBJ_TO_PTR(obj);
96-
97-
for (size_t offset = 0; offset < pixelbuf->bytes; offset+= pixelbuf->pixel_step) {
98-
pixelbuf_set_pixel(pixelbuf->buf + offset, pixelbuf->two_buffers ? (pixelbuf->rawbuf + offset) : NULL,
99-
pixelbuf->brightness, value, &pixelbuf->byteorder, pixelbuf->byteorder.is_dotstar);
100-
}
101-
if (pixelbuf->auto_write)
102-
pixelbuf_call_show(pixelbuf_in);
103-
return mp_const_none;
104-
}
105-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pixelbuf_fill_obj, pixelbuf_fill);
106-
107-
10882
STATIC const mp_rom_map_elem_t pixelbuf_module_globals_table[] = {
10983
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__pixelbuf) },
11084
{ MP_ROM_QSTR(MP_QSTR_PixelBuf), MP_ROM_PTR(&pixelbuf_pixelbuf_type) },
11185
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_wheel_obj) },
112-
{ MP_ROM_QSTR(MP_QSTR_fill), MP_ROM_PTR(&pixelbuf_fill_obj) },
11386
};
11487

11588
STATIC MP_DEFINE_CONST_DICT(pixelbuf_module_globals, pixelbuf_module_globals_table);
11689

11790
const mp_obj_module_t pixelbuf_module = {
118-
.base = { &mp_type_module },
119-
.globals = (mp_obj_dict_t*)&pixelbuf_module_globals,
91+
.base = { &mp_type_module },
92+
.globals = (mp_obj_dict_t*)&pixelbuf_module_globals,
12093
};

shared-bindings/_pixelbuf/__init__.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@
3030
#include "common-hal/digitalio/DigitalInOut.h"
3131

3232
const int32_t colorwheel(float pos);
33-
extern void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes);
3433

3534
#endif //CP_SHARED_BINDINGS_PIXELBUF_INIT_H

shared-bindings/_pixelbuf/types.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)