@@ -66,9 +66,9 @@ void common_hal_rgbmatrix_rgbmatrix_construct(rgbmatrix_rgbmatrix_obj_t *self, i
6666}
6767
6868void common_hal_rgbmatrix_rgbmatrix_reconstruct (rgbmatrix_rgbmatrix_obj_t * self , mp_obj_t framebuffer ) {
69+ common_hal_rgbmatrix_timer_disable (self -> timer );
6970 if (framebuffer ) {
7071 self -> framebuffer = framebuffer ;
71- framebuffer = mp_obj_new_bytearray_of_zeros (self -> bufsize );
7272 mp_get_buffer_raise (self -> framebuffer , & self -> bufinfo , MP_BUFFER_READ );
7373 if (mp_get_buffer (self -> framebuffer , & self -> bufinfo , MP_BUFFER_RW )) {
7474 self -> bufinfo .typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW ;
@@ -79,36 +79,40 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t* self,
7979 mp_get_index (mp_obj_get_type (self -> framebuffer ), self -> bufinfo .len , MP_OBJ_NEW_SMALL_INT (self -> bufsize - 1 ), false);
8080 } else {
8181 _PM_FREE (self -> bufinfo .buf );
82- _PM_FREE (self -> core .rgbPins );
83- _PM_FREE (self -> core .addr );
84- _PM_FREE (self -> core .screenData );
82+ _PM_FREE (self -> protomatter .rgbPins );
83+ _PM_FREE (self -> protomatter .addr );
84+ _PM_FREE (self -> protomatter .screenData );
8585
8686 self -> framebuffer = NULL ;
87- self -> bufinfo .buf = _PM_allocator_impl (self -> bufsize );
87+ self -> bufinfo .buf = common_hal_rgbmatrix_allocator_impl (self -> bufsize );
8888 self -> bufinfo .len = self -> bufsize ;
8989 self -> bufinfo .typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW ;
9090 }
9191
92- ProtomatterStatus stat = _PM_init (& self -> core ,
92+ memset (& self -> protomatter , 0 , sizeof (self -> protomatter ));
93+ ProtomatterStatus stat = _PM_init (& self -> protomatter ,
9394 self -> width , self -> bit_depth ,
9495 self -> rgb_count /6 , self -> rgb_pins ,
9596 self -> addr_count , self -> addr_pins ,
9697 self -> clock_pin , self -> latch_pin , self -> oe_pin ,
9798 self -> doublebuffer , self -> timer );
9899
99100 if (stat == PROTOMATTER_OK ) {
100- _PM_protoPtr = & self -> core ;
101+ _PM_protoPtr = & self -> protomatter ;
101102 common_hal_mcu_disable_interrupts ();
102103 common_hal_rgbmatrix_timer_enable (self -> timer );
103- stat = _PM_begin (& self -> core );
104- _PM_convert_565 (& self -> core , self -> bufinfo .buf , self -> width );
104+ stat = _PM_begin (& self -> protomatter );
105+
106+ if (stat == PROTOMATTER_OK ) {
107+ _PM_convert_565 (& self -> protomatter , self -> bufinfo .buf , self -> width );
108+ }
105109 common_hal_mcu_enable_interrupts ();
106- _PM_swapbuffer_maybe (& self -> core );
110+ if (stat == PROTOMATTER_OK ) {
111+ _PM_swapbuffer_maybe (& self -> protomatter );
112+ }
107113 }
108114
109115 if (stat != PROTOMATTER_OK ) {
110- // XXX this deinit() actually makes crashy-crashy
111- // can trigger it by sending inappropriate pins
112116 common_hal_rgbmatrix_rgbmatrix_deinit (self );
113117 switch (stat ) {
114118 case PROTOMATTER_ERR_PINS :
@@ -117,7 +121,9 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t* self,
117121 case PROTOMATTER_ERR_ARG :
118122 mp_raise_ValueError (translate ("Invalid argument" ));
119123 break ;
120- case PROTOMATTER_ERR_MALLOC : /// should have already been signaled as NLR
124+ case PROTOMATTER_ERR_MALLOC :
125+ mp_raise_msg (& mp_type_MemoryError , NULL );
126+ break ;
121127 default :
122128 mp_raise_msg_varg (& mp_type_RuntimeError ,
123129 translate ("Internal error #%d" ), (int )stat );
@@ -126,7 +132,6 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t* self,
126132 }
127133
128134 self -> paused = 0 ;
129-
130135}
131136
132137STATIC void free_pin (uint8_t * pin ) {
@@ -148,7 +153,7 @@ void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
148153 self -> timer = 0 ;
149154 }
150155
151- if (_PM_protoPtr == & self -> core ) {
156+ if (_PM_protoPtr == & self -> protomatter ) {
152157 _PM_protoPtr = NULL ;
153158 }
154159
@@ -158,10 +163,10 @@ void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
158163 free_pin (& self -> latch_pin );
159164 free_pin (& self -> oe_pin );
160165
161- if (self -> core .rgbPins ) {
162- _PM_free (& self -> core );
166+ if (self -> protomatter .rgbPins ) {
167+ _PM_free (& self -> protomatter );
163168 }
164- memset (& self -> core , 0 , sizeof (self -> core ));
169+ memset (& self -> protomatter , 0 , sizeof (self -> protomatter ));
165170
166171 // If it was supervisor-allocated, it is supervisor-freed and the pointer
167172 // is zeroed, otherwise the pointer is just zeroed
@@ -175,16 +180,16 @@ void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
175180
176181void rgbmatrix_rgbmatrix_collect_ptrs (rgbmatrix_rgbmatrix_obj_t * self ) {
177182 gc_collect_ptr (self -> framebuffer );
178- gc_collect_ptr (self -> core .rgbPins );
179- gc_collect_ptr (self -> core .addr );
180- gc_collect_ptr (self -> core .screenData );
183+ gc_collect_ptr (self -> protomatter .rgbPins );
184+ gc_collect_ptr (self -> protomatter .addr );
185+ gc_collect_ptr (self -> protomatter .screenData );
181186}
182187
183188void common_hal_rgbmatrix_rgbmatrix_set_paused (rgbmatrix_rgbmatrix_obj_t * self , bool paused ) {
184189 if (paused && !self -> paused ) {
185- _PM_stop (& self -> core );
190+ _PM_stop (& self -> protomatter );
186191 } else if (!paused && self -> paused ) {
187- _PM_resume (& self -> core );
192+ _PM_resume (& self -> protomatter );
188193 }
189194 self -> paused = paused ;
190195}
@@ -194,8 +199,8 @@ bool common_hal_rgbmatrix_rgbmatrix_get_paused(rgbmatrix_rgbmatrix_obj_t* self)
194199}
195200
196201void common_hal_rgbmatrix_rgbmatrix_refresh (rgbmatrix_rgbmatrix_obj_t * self ) {
197- _PM_convert_565 (& self -> core , self -> bufinfo .buf , self -> width );
198- _PM_swapbuffer_maybe (& self -> core );
202+ _PM_convert_565 (& self -> protomatter , self -> bufinfo .buf , self -> width );
203+ _PM_swapbuffer_maybe (& self -> protomatter );
199204}
200205
201206int common_hal_rgbmatrix_rgbmatrix_get_width (rgbmatrix_rgbmatrix_obj_t * self ) {
@@ -206,3 +211,20 @@ int common_hal_rgbmatrix_rgbmatrix_get_height(rgbmatrix_rgbmatrix_obj_t* self) {
206211 int computed_height = (self -> rgb_count / 3 ) << (self -> addr_count );
207212 return computed_height ;
208213}
214+
215+ void * common_hal_rgbmatrix_allocator_impl (size_t sz ) {
216+ if (gc_alloc_possible ()) {
217+ return m_malloc_maybe (sz + sizeof (void * ), true);
218+ } else {
219+ supervisor_allocation * allocation = allocate_memory (align32_size (sz ), false);
220+ return allocation ? allocation -> ptr : NULL ;
221+ }
222+ }
223+
224+ void common_hal_rgbmatrix_free_impl (void * ptr_in ) {
225+ supervisor_allocation * allocation = allocation_from_ptr (ptr_in );
226+
227+ if (allocation ) {
228+ free_memory (allocation );
229+ }
230+ }
0 commit comments