@@ -59,12 +59,14 @@ extern const int32_t colorwheel(float pos);
5959//|
6060//| When only given ``buf``, ``brightness`` applies to the next pixel assignment.
6161//|
62- //| When ``D`` (dotstar mode) is present in the byteorder configuration, the
63- //| 4th value in a tuple/list is the individual pixel brightness (0-1).
62+ //| When ``P`` (pwm duration) is present as the 4th character of the byteorder
63+ //| string, the 4th value in the tuple/list for a pixel is the individual pixel
64+ //| brightness (0.0-1.0) and will enable a Dotstar compatible 1st byte in the
65+ //| output buffer (``buf``).
6466//|
6567//| :param ~int size: Number of pixelsx
6668//| :param ~bytearray buf: Bytearray in which to store pixel data
67- //| :param ~str byteorder: Byte order string (such as "BGR" or "BGRD ")
69+ //| :param ~str byteorder: Byte order string (such as "BGR" or "DBGR ")
6870//| :param ~float brightness: Brightness (0 to 1.0, default 1.0)
6971//| :param ~bytearray rawbuf: Bytearray in which to store raw pixel data (before brightness adjustment)
7072//| :param ~int offset: Offset from start of buffer (default 0)
@@ -77,7 +79,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
7779 static const mp_arg_t allowed_args [] = {
7880 { MP_QSTR_size , MP_ARG_REQUIRED | MP_ARG_INT },
7981 { MP_QSTR_buf , MP_ARG_REQUIRED | MP_ARG_OBJ },
80- { MP_QSTR_byteorder , MP_ARG_OBJ , { .u_obj = mp_const_none } },
82+ { MP_QSTR_byteorder , MP_ARG_OBJ , { .u_obj = MP_OBJ_NEW_QSTR ( MP_QSTR_BGR ) } },
8183 { MP_QSTR_brightness , MP_ARG_OBJ , { .u_obj = mp_const_none } },
8284 { MP_QSTR_rawbuf , MP_ARG_OBJ , { .u_obj = mp_const_none } },
8385 { MP_QSTR_offset , MP_ARG_INT , { .u_int = 0 } },
@@ -92,17 +94,13 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
9294 if (!MP_OBJ_IS_STR (args [ARG_byteorder ].u_obj ))
9395 mp_raise_TypeError (translate ("byteorder is not a string" ));
9496
95- if (args [ARG_byteorder ].u_obj == NULL )
96- byteorder = "BGR" ;
97- else
98- byteorder = mp_obj_str_get_data (args [ARG_byteorder ].u_obj , & bo_len );
99-
97+ byteorder = mp_obj_str_get_data (args [ARG_byteorder ].u_obj , & bo_len );
10098 if (bo_len < 3 || bo_len > 4 )
10199 mp_raise_ValueError (translate ("Invalid byteorder string" ));
102- strncpy ( byteorder_details .order , byteorder , sizeof ( byteorder_details . order )) ;
100+ byteorder_details .order = args [ ARG_byteorder ]. u_obj ;
103101
104102 byteorder_details .bpp = bo_len ;
105- char * dotstar = strchr (byteorder , 'D ' );
103+ char * dotstar = strchr (byteorder , 'P ' );
106104 char * r = strchr (byteorder , 'R' );
107105 char * g = strchr (byteorder , 'G' );
108106 char * b = strchr (byteorder , 'B' );
@@ -117,18 +115,8 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
117115 byteorder_details .byteorder .b = b - byteorder ;
118116 byteorder_details .byteorder .w = w ? w - byteorder : 0 ;
119117 // The dotstar brightness byte is always first (as it goes with the pixel start bits)
120- // if 'D' is found at the end, adjust byte position
121- // if 'D' is elsewhere, error out
122- if (dotstar ) {
123- size_t dotstar_pos = dotstar - byteorder ;
124- if (dotstar_pos == 3 ) {
125- byteorder_details .byteorder .b += 1 ;
126- byteorder_details .byteorder .g += 1 ;
127- byteorder_details .byteorder .r += 1 ;
128- byteorder_details .byteorder .w = 0 ;
129- } else if (dotstar_pos != 0 ) {
130- mp_raise_ValueError (translate ("Invalid byteorder string" ));
131- }
118+ if (dotstar && byteorder [0 ] != 'P' ) {
119+ mp_raise_ValueError (translate ("Invalid byteorder string" ));
132120 }
133121 if (byteorder_details .has_white && byteorder_details .is_dotstar )
134122 mp_raise_ValueError (translate ("Invalid byteorder string" ));
@@ -178,7 +166,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_make_new(const mp_obj_type_t *type, size_t n_a
178166
179167 if (self -> byteorder .is_dotstar ) {
180168 // Initialize the buffer with the dotstar start bytes.
181- // Header and end must be setup by caller
169+ // Note: Header and end must be setup by caller
182170 for (uint i = 0 ; i < self -> pixels * 4 ; i += 4 ) {
183171 self -> buf [i ] = DOTSTAR_LED_START_FULL_BRIGHT ;
184172 if (two_buffers ) {
@@ -268,9 +256,7 @@ void pixelbuf_recalculate_brightness(pixelbuf_pixelbuf_obj_t *self) {
268256mp_obj_t call_show (mp_obj_t self_in ) {
269257 mp_obj_t dest [2 ];
270258 mp_load_method (self_in , MP_QSTR_show , dest );
271- if (dest [0 ] == MP_OBJ_NULL )
272- return mp_const_none ;
273- return mp_call_method_self_n_kw (dest [0 ], self_in , 0 , 0 , mp_const_none );
259+ return mp_call_method_n_kw (0 , 0 , dest );
274260}
275261
276262//| .. attribute:: auto_write
@@ -324,7 +310,7 @@ const mp_obj_property_t pixelbuf_pixelbuf_buf_obj = {
324310//|
325311STATIC mp_obj_t pixelbuf_pixelbuf_obj_get_byteorder (mp_obj_t self_in ) {
326312 pixelbuf_pixelbuf_obj_t * self = native_pixelbuf (self_in );
327- return mp_obj_new_str ( self -> byteorder .order , strlen ( self -> byteorder . order )) ;
313+ return self -> byteorder .order ;
328314}
329315MP_DEFINE_CONST_FUN_OBJ_1 (pixelbuf_pixelbuf_get_byteorder_str , pixelbuf_pixelbuf_obj_get_byteorder );
330316
@@ -376,13 +362,15 @@ STATIC mp_obj_t pixelbuf_pixelbuf_subscr(mp_obj_t self_in, mp_obj_t index_in, mp
376362 mp_bound_slice_t slice ;
377363
378364 if (!mp_seq_get_fast_slice_indexes (self -> bytes , index_in , & slice ))
365+ // TODO support stepping!!!
379366 mp_raise_NotImplementedError (translate ("Only slices with step=1 (aka None) are supported" ));
380367 if ((slice .stop * self -> pixel_step ) > self -> bytes )
381368 mp_raise_IndexError (translate ("Range out of bounds" ));
382369
383370 if (value == MP_OBJ_SENTINEL ) { // Get
384371 size_t len = slice .stop - slice .start ;
385- return pixelbuf_get_pixel_array ((uint8_t * ) self -> buf + slice .start , len , & self -> byteorder , self -> pixel_step , self -> byteorder .is_dotstar );
372+ uint8_t * readbuf = self -> two_buffers ? self -> rawbuf : self -> buf ;
373+ return pixelbuf_get_pixel_array (readbuf + slice .start , len , & self -> byteorder , self -> pixel_step , self -> byteorder .is_dotstar );
386374 } else { // Set
387375 #if MICROPY_PY_ARRAY_SLICE_ASSIGN
388376
0 commit comments