Skip to content

Commit cf3bb7e

Browse files
committed
fix bugs and inline the byteorder code
1 parent 31e4591 commit cf3bb7e

1 file changed

Lines changed: 36 additions & 41 deletions

File tree

shared-bindings/_pixelbuf/PixelBuf.c

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,6 @@
4242

4343
extern const int32_t colorwheel(float pos);
4444

45-
void parse_byteorder_string(const char *byteorder, pixelbuf_byteorder_details_t details) {
46-
details.bpp = strlen(byteorder);
47-
char *dotstar = strchr(byteorder, 'D');
48-
char *r = strchr(byteorder, 'R');
49-
char *g = strchr(byteorder, 'G');
50-
char *b = strchr(byteorder, 'B');
51-
char *w = strchr(byteorder, 'W');
52-
int num_chars = (dotstar ? 1 : 0) + (w ? 1 : 0) + (r ? 1 : 0) + (g ? 1 : 0) + (b ? 1 : 0);
53-
if (num_chars < details.bpp)
54-
mp_raise_ValueError(translate("Unexpected character in byteorder"));
55-
if (!(r && b && g))
56-
mp_raise_ValueError(translate("Incomplete byteorder string"));
57-
details.is_dotstar = dotstar ? true : false;
58-
details.has_white = w ? true : false;
59-
details.byteorder.r = byteorder - r;
60-
details.byteorder.g = byteorder - g;
61-
details.byteorder.b = byteorder - b;
62-
if (w)
63-
details.byteorder.w = byteorder - w;
64-
// The dotstar brightness byte is always first (as it goes with the pixel start bits)
65-
// if 'D' is found at the end, adjust byte position
66-
// if 'D' is elsewhere, error out
67-
if (dotstar) {
68-
size_t dotstar_pos = dotstar - byteorder;
69-
if (dotstar_pos == 4) {
70-
details.byteorder.b += 1;
71-
details.byteorder.g += 1;
72-
details.byteorder.r += 1;
73-
} else if (dotstar_pos != 0) {
74-
mp_raise_ValueError(translate("Dotstar position invalid"));
75-
}
76-
}
77-
}
78-
7945
//| .. currentmodule:: pixelbuf
8046
//|
8147
//| :class:`PixelBuf` -- A fast RGB[W] pixel buffer for LED and similar devices
@@ -123,20 +89,49 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
12389
};
12490
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
12591
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
92+
const char *byteorder = NULL;
93+
pixelbuf_byteorder_details_t byteorder_details;
94+
size_t bo_len;
12695

12796
if (!MP_OBJ_IS_STR(args[ARG_byteorder].u_obj))
12897
mp_raise_TypeError(translate("byteorder is not a string"));
12998

130-
const char *byteorder_str = NULL;
131-
pixelbuf_byteorder_details_t byteorder_details;
132-
size_t bo_len;
13399
if (args[ARG_byteorder].u_obj == NULL)
134-
byteorder_str = "BGR";
100+
byteorder = "BGR";
135101
else
136-
byteorder_str = mp_obj_str_get_data(args[ARG_byteorder].u_obj, &bo_len);
137-
138-
parse_byteorder_string(byteorder_str, byteorder_details);
102+
byteorder = mp_obj_str_get_data(args[ARG_byteorder].u_obj, &bo_len);
139103

104+
byteorder_details.bpp = strlen(byteorder);
105+
char *dotstar = strchr(byteorder, 'D');
106+
char *r = strchr(byteorder, 'R');
107+
char *g = strchr(byteorder, 'G');
108+
char *b = strchr(byteorder, 'B');
109+
char *w = strchr(byteorder, 'W');
110+
int num_chars = (dotstar ? 1 : 0) + (w ? 1 : 0) + (r ? 1 : 0) + (g ? 1 : 0) + (b ? 1 : 0);
111+
if (num_chars < byteorder_details.bpp)
112+
mp_raise_ValueError(translate("Unexpected character in byteorder"));
113+
if (!(r && b && g))
114+
mp_raise_ValueError(translate("Incomplete byteorder string"));
115+
byteorder_details.is_dotstar = dotstar ? true : false;
116+
byteorder_details.has_white = w ? true : false;
117+
byteorder_details.byteorder.r = r - byteorder;
118+
byteorder_details.byteorder.g = g - byteorder;
119+
byteorder_details.byteorder.b = b - byteorder;
120+
if (w)
121+
byteorder_details.byteorder.w = w - byteorder;
122+
// The dotstar brightness byte is always first (as it goes with the pixel start bits)
123+
// if 'D' is found at the end, adjust byte position
124+
// if 'D' is elsewhere, error out
125+
if (dotstar) {
126+
size_t dotstar_pos = dotstar - byteorder;
127+
if (dotstar_pos == 4) {
128+
byteorder_details.byteorder.b += 1;
129+
byteorder_details.byteorder.g += 1;
130+
byteorder_details.byteorder.r += 1;
131+
} else if (dotstar_pos != 0) {
132+
mp_raise_ValueError(translate("Dotstar position invalid"));
133+
}
134+
}
140135
if (byteorder_details.has_white && byteorder_details.is_dotstar)
141136
mp_raise_ValueError(translate("Can not use dotstar with a white byte"));
142137

0 commit comments

Comments
 (0)