Skip to content

Commit

Permalink
Merge pull request #528 from pimoroni/patch-plasma-alloc
Browse files Browse the repository at this point in the history
Plasma: Use m_new to alloc buffer on gc_heap if not supplied.
  • Loading branch information
Gadgetoid authored Oct 6, 2022
2 parents 1ac55c4 + eaf9fcd commit 9878e4b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions micropython/modules/plasma/plasma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ mp_obj_t PlasmaWS2812_make_new(const mp_obj_type_t *type, size_t n_args, size_t
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_RW);
buffer = bufinfo.buf;
if(bufinfo.len < (size_t)(num_leds * 4)) {
if(bufinfo.len < (size_t)(num_leds * sizeof(WS2812::RGB))) {
mp_raise_ValueError("Supplied buffer is too small for LED count!");
}
} else {
buffer = m_new(WS2812::RGB, num_leds);
}

self = m_new_obj_with_finaliser(_PlasmaWS2812_obj_t);
Expand Down Expand Up @@ -294,13 +296,15 @@ mp_obj_t PlasmaAPA102_make_new(const mp_obj_type_t *type, size_t n_args, size_t
if(bufinfo.len < (size_t)(num_leds * 4)) {
mp_raise_ValueError("Supplied buffer is too small for LED count!");
}
// If a bytearray is supplied it'll be raw, uninitialized bytes
// iterate through the RGB elements and call "brightness"
// to set up the SOF bytes, otherwise a flickery mess will happen!
// Oh for such niceties as "placement new"...
for(auto i = 0; i < num_leds; i++) {
buffer[i].brightness(15);
}
} else {
buffer = m_new(APA102::RGB, num_leds);
}

// A supplied bytearray or new buffer will be raw, uninitialized bytes
// iterate through the RGB elements and call "brightness"
// to set up the SOF bytes, otherwise a flickery mess will happen!
for(auto i = 0; i < num_leds; i++) {
buffer[i].brightness(15);
}

self = m_new_obj_with_finaliser(_PlasmaAPA102_obj_t);
Expand Down

0 comments on commit 9878e4b

Please sign in to comment.