@@ -66,9 +66,9 @@ void common_hal_rgbmatrix_rgbmatrix_construct(rgbmatrix_rgbmatrix_obj_t *self, i
66
66
}
67
67
68
68
void common_hal_rgbmatrix_rgbmatrix_reconstruct (rgbmatrix_rgbmatrix_obj_t * self , mp_obj_t framebuffer ) {
69
+ common_hal_rgbmatrix_timer_disable (self -> timer );
69
70
if (framebuffer ) {
70
71
self -> framebuffer = framebuffer ;
71
- framebuffer = mp_obj_new_bytearray_of_zeros (self -> bufsize );
72
72
mp_get_buffer_raise (self -> framebuffer , & self -> bufinfo , MP_BUFFER_READ );
73
73
if (mp_get_buffer (self -> framebuffer , & self -> bufinfo , MP_BUFFER_RW )) {
74
74
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,
79
79
mp_get_index (mp_obj_get_type (self -> framebuffer ), self -> bufinfo .len , MP_OBJ_NEW_SMALL_INT (self -> bufsize - 1 ), false);
80
80
} else {
81
81
_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 );
85
85
86
86
self -> framebuffer = NULL ;
87
- self -> bufinfo .buf = _PM_allocator_impl (self -> bufsize );
87
+ self -> bufinfo .buf = common_hal_rgbmatrix_allocator_impl (self -> bufsize );
88
88
self -> bufinfo .len = self -> bufsize ;
89
89
self -> bufinfo .typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW ;
90
90
}
91
91
92
- ProtomatterStatus stat = _PM_init (& self -> core ,
92
+ memset (& self -> protomatter , 0 , sizeof (self -> protomatter ));
93
+ ProtomatterStatus stat = _PM_init (& self -> protomatter ,
93
94
self -> width , self -> bit_depth ,
94
95
self -> rgb_count /6 , self -> rgb_pins ,
95
96
self -> addr_count , self -> addr_pins ,
96
97
self -> clock_pin , self -> latch_pin , self -> oe_pin ,
97
98
self -> doublebuffer , self -> timer );
98
99
99
100
if (stat == PROTOMATTER_OK ) {
100
- _PM_protoPtr = & self -> core ;
101
+ _PM_protoPtr = & self -> protomatter ;
101
102
common_hal_mcu_disable_interrupts ();
102
103
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
+ }
105
109
common_hal_mcu_enable_interrupts ();
106
- _PM_swapbuffer_maybe (& self -> core );
110
+ if (stat == PROTOMATTER_OK ) {
111
+ _PM_swapbuffer_maybe (& self -> protomatter );
112
+ }
107
113
}
108
114
109
115
if (stat != PROTOMATTER_OK ) {
110
- // XXX this deinit() actually makes crashy-crashy
111
- // can trigger it by sending inappropriate pins
112
116
common_hal_rgbmatrix_rgbmatrix_deinit (self );
113
117
switch (stat ) {
114
118
case PROTOMATTER_ERR_PINS :
@@ -117,7 +121,9 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t* self,
117
121
case PROTOMATTER_ERR_ARG :
118
122
mp_raise_ValueError (translate ("Invalid argument" ));
119
123
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 ;
121
127
default :
122
128
mp_raise_msg_varg (& mp_type_RuntimeError ,
123
129
translate ("Internal error #%d" ), (int )stat );
@@ -126,7 +132,6 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t* self,
126
132
}
127
133
128
134
self -> paused = 0 ;
129
-
130
135
}
131
136
132
137
STATIC void free_pin (uint8_t * pin ) {
@@ -148,7 +153,7 @@ void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
148
153
self -> timer = 0 ;
149
154
}
150
155
151
- if (_PM_protoPtr == & self -> core ) {
156
+ if (_PM_protoPtr == & self -> protomatter ) {
152
157
_PM_protoPtr = NULL ;
153
158
}
154
159
@@ -158,10 +163,10 @@ void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
158
163
free_pin (& self -> latch_pin );
159
164
free_pin (& self -> oe_pin );
160
165
161
- if (self -> core .rgbPins ) {
162
- _PM_free (& self -> core );
166
+ if (self -> protomatter .rgbPins ) {
167
+ _PM_free (& self -> protomatter );
163
168
}
164
- memset (& self -> core , 0 , sizeof (self -> core ));
169
+ memset (& self -> protomatter , 0 , sizeof (self -> protomatter ));
165
170
166
171
// If it was supervisor-allocated, it is supervisor-freed and the pointer
167
172
// is zeroed, otherwise the pointer is just zeroed
@@ -175,16 +180,16 @@ void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
175
180
176
181
void rgbmatrix_rgbmatrix_collect_ptrs (rgbmatrix_rgbmatrix_obj_t * self ) {
177
182
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 );
181
186
}
182
187
183
188
void common_hal_rgbmatrix_rgbmatrix_set_paused (rgbmatrix_rgbmatrix_obj_t * self , bool paused ) {
184
189
if (paused && !self -> paused ) {
185
- _PM_stop (& self -> core );
190
+ _PM_stop (& self -> protomatter );
186
191
} else if (!paused && self -> paused ) {
187
- _PM_resume (& self -> core );
192
+ _PM_resume (& self -> protomatter );
188
193
}
189
194
self -> paused = paused ;
190
195
}
@@ -194,8 +199,8 @@ bool common_hal_rgbmatrix_rgbmatrix_get_paused(rgbmatrix_rgbmatrix_obj_t* self)
194
199
}
195
200
196
201
void 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 );
199
204
}
200
205
201
206
int 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) {
206
211
int computed_height = (self -> rgb_count / 3 ) << (self -> addr_count );
207
212
return computed_height ;
208
213
}
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