File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed
ports/nrf/common-hal/neopixel_write Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -111,18 +111,23 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout
111
111
//
112
112
// If there is not enough memory, we will fall back to cycle counter
113
113
// using DWT
114
- uint32_t pattern_size = numBytes * 8 * sizeof (uint16_t ) + 2 * sizeof (uint16_t );
114
+
115
+ #define PATTERN_SIZE (numBytes ) (numBytes * 8 * sizeof(uint16_t) + 2 * sizeof(uint16_t))
116
+
117
+ uint32_t pattern_size = PATTERN_SIZE (numBytes );
115
118
uint16_t * pixels_pattern = NULL ;
116
119
bool pattern_on_heap = false;
117
120
118
121
// Use the stack to store 1 pixels worth of PWM data for the status led. uint32_t to ensure alignment.
119
- uint32_t one_pixel [8 * sizeof (uint16_t ) + 1 ];
122
+ // Make it at least as big as PATTERN_SIZE(3), for one pixel of RGB data.
123
+ // PATTERN_SIZE is a multiple of 4, so we don't need round up to make sure one_pixel is large enough.
124
+ uint32_t one_pixel [PATTERN_SIZE (3 )/sizeof (uint32_t )];
120
125
121
126
NRF_PWM_Type * pwm = find_free_pwm ();
122
127
123
128
// only malloc if there is PWM device available
124
129
if ( pwm != NULL ) {
125
- if (pattern_size <= sizeof (one_pixel ) * sizeof ( uint32_t ) ) {
130
+ if (pattern_size <= sizeof (one_pixel )) {
126
131
pixels_pattern = (uint16_t * ) one_pixel ;
127
132
} else {
128
133
pixels_pattern = (uint16_t * ) m_malloc_maybe (pattern_size , false);
You can’t perform that action at this time.
0 commit comments