@@ -137,12 +137,52 @@ STATIC mp_obj_t displayio_tilegrid_make_new(const mp_obj_type_t *type, size_t n_
137
137
return MP_OBJ_FROM_PTR (self );
138
138
}
139
139
140
+
140
141
// Helper to ensure we have the native super class instead of a subclass.
141
142
static displayio_tilegrid_t * native_tilegrid (mp_obj_t tilegrid_obj ) {
142
143
mp_obj_t native_tilegrid = mp_obj_cast_to_native_base (tilegrid_obj , & displayio_tilegrid_type );
143
144
mp_obj_assert_native_inited (native_tilegrid );
144
145
return MP_OBJ_TO_PTR (native_tilegrid );
145
146
}
147
+
148
+ static void enforce_bitmap_size (mp_obj_t self_in , mp_obj_t bitmap ) {
149
+ displayio_tilegrid_t * self = native_tilegrid (self_in );
150
+ uint16_t bitmap_width ;
151
+ uint16_t bitmap_height ;
152
+ mp_obj_t native = mp_obj_cast_to_native_base (bitmap , & displayio_shape_type );
153
+ if (native != MP_OBJ_NULL ) {
154
+ displayio_shape_t * bmp = MP_OBJ_TO_PTR (native );
155
+ bitmap_width = bmp -> width ;
156
+ bitmap_height = bmp -> height ;
157
+ } else if (mp_obj_is_type (bitmap , & displayio_bitmap_type )) {
158
+ displayio_bitmap_t * bmp = MP_OBJ_TO_PTR (bitmap );
159
+ native = bitmap ;
160
+ bitmap_width = bmp -> width ;
161
+ bitmap_height = bmp -> height ;
162
+ } else if (mp_obj_is_type (bitmap , & displayio_ondiskbitmap_type )) {
163
+ displayio_ondiskbitmap_t * bmp = MP_OBJ_TO_PTR (bitmap );
164
+ native = bitmap ;
165
+ bitmap_width = bmp -> width ;
166
+ bitmap_height = bmp -> height ;
167
+ } else {
168
+ mp_raise_TypeError_varg (translate ("unsupported %q type" ), MP_QSTR_bitmap );
169
+ }
170
+ uint16_t tile_width = self -> tile_width ;
171
+ if (tile_width == 0 ) {
172
+ tile_width = bitmap_width ;
173
+ }
174
+ uint16_t tile_height = self -> tile_height ;
175
+ if (tile_height == 0 ) {
176
+ tile_height = bitmap_height ;
177
+ }
178
+ if (bitmap_width % tile_width != 0 ) {
179
+ mp_raise_ValueError (translate ("Tile width must exactly divide bitmap width" ));
180
+ }
181
+ if (bitmap_height % tile_height != 0 ) {
182
+ mp_raise_ValueError (translate ("Tile height must exactly divide bitmap height" ));
183
+ }
184
+ }
185
+
146
186
//| hidden: bool
147
187
//| """True when the TileGrid is hidden. This may be False even when a part of a hidden Group."""
148
188
//|
@@ -397,6 +437,14 @@ STATIC mp_obj_t displayio_tilegrid_obj_set_bitmap(mp_obj_t self_in, mp_obj_t bit
397
437
mp_raise_TypeError (translate ("bitmap must be displayio.Bitmap, displayio.Shape, or displayio.OnDiskBitmap" ));
398
438
}
399
439
440
+ /*if (bitmap->width % self->tile_width != 0) {
441
+ mp_raise_ValueError(translate("Tile width must exactly divide bitmap width"));
442
+ }
443
+ if (bitmap->height % self->tile_height != 0) {
444
+ mp_raise_ValueError(translate("Tile height must exactly divide bitmap height"));
445
+ }*/
446
+ enforce_bitmap_size (self_in , bitmap );
447
+
400
448
common_hal_displayio_tilegrid_set_bitmap (self , bitmap );
401
449
402
450
return mp_const_none ;
0 commit comments