diff --git a/shared-bindings/displayio/FourWire.c b/shared-bindings/displayio/FourWire.c index 77329578a4..1cd51b2e2d 100644 --- a/shared-bindings/displayio/FourWire.c +++ b/shared-bindings/displayio/FourWire.c @@ -82,19 +82,8 @@ STATIC mp_obj_t displayio_fourwire_make_new(const mp_obj_type_t *type, size_t n_ mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj); mcu_pin_obj_t *reset = validate_obj_is_free_pin_or_none(args[ARG_reset].u_obj); - displayio_fourwire_obj_t* self = NULL; mp_obj_t spi = args[ARG_spi_bus].u_obj; - for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { - if (displays[i].fourwire_bus.base.type == NULL || - displays[i].fourwire_bus.base.type == &mp_type_NoneType) { - self = &displays[i].fourwire_bus; - self->base.type = &displayio_fourwire_type; - break; - } - } - if (self == NULL) { - mp_raise_RuntimeError(translate("Too many display busses")); - } + displayio_fourwire_obj_t* self = &allocate_display_bus_or_raise()->fourwire_bus; uint8_t polarity = args[ARG_polarity].u_int; if (polarity != 0 && polarity != 1) { diff --git a/shared-bindings/displayio/I2CDisplay.c b/shared-bindings/displayio/I2CDisplay.c index 2e312aa14e..dd3333f6de 100644 --- a/shared-bindings/displayio/I2CDisplay.c +++ b/shared-bindings/displayio/I2CDisplay.c @@ -71,19 +71,8 @@ STATIC mp_obj_t displayio_i2cdisplay_make_new(const mp_obj_type_t *type, size_t mcu_pin_obj_t *reset = validate_obj_is_free_pin_or_none(args[ARG_reset].u_obj); - displayio_i2cdisplay_obj_t* self = NULL; mp_obj_t i2c = args[ARG_i2c_bus].u_obj; - for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { - if (displays[i].i2cdisplay_bus.base.type == NULL || - displays[i].i2cdisplay_bus.base.type == &mp_type_NoneType) { - self = &displays[i].i2cdisplay_bus; - self->base.type = &displayio_i2cdisplay_type; - break; - } - } - if (self == NULL) { - mp_raise_RuntimeError(translate("Too many display busses")); - } + displayio_i2cdisplay_obj_t* self = &allocate_display_bus_or_raise()->i2cdisplay_bus; common_hal_displayio_i2cdisplay_construct(self, MP_OBJ_TO_PTR(i2c), args[ARG_device_address].u_int, reset); diff --git a/shared-bindings/displayio/ParallelBus.c b/shared-bindings/displayio/ParallelBus.c index b3a876f903..4d9ffb0a1e 100644 --- a/shared-bindings/displayio/ParallelBus.c +++ b/shared-bindings/displayio/ParallelBus.c @@ -83,18 +83,7 @@ STATIC mp_obj_t displayio_parallelbus_make_new(const mp_obj_type_t *type, size_t mcu_pin_obj_t *read = validate_obj_is_free_pin(args[ARG_read].u_obj); mcu_pin_obj_t *reset = validate_obj_is_free_pin(args[ARG_reset].u_obj); - displayio_parallelbus_obj_t* self = NULL; - for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { - if (displays[i].parallel_bus.base.type== NULL || - displays[i].parallel_bus.base.type == &mp_type_NoneType) { - self = &displays[i].parallel_bus; - self->base.type = &displayio_parallelbus_type; - break; - } - } - if (self == NULL) { - mp_raise_RuntimeError(translate("Too many display busses")); - } + displayio_parallelbus_obj_t* self = &allocate_display_bus_or_raise()->parallel_bus; common_hal_displayio_parallelbus_construct(self, data0, command, chip_select, write, read, reset); return self; diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index fd58a0322a..4844dfe60b 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -326,3 +326,20 @@ primary_display_t *allocate_display_or_raise() { } } +primary_display_t *allocate_display_bus(void) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t display_type = displays[i].display.base.type; + if (display_type == NULL || display_type == &mp_type_NoneType) { + return &displays[i]; + } + } + return NULL; +} + +primary_display_t *allocate_display_bus_or_raise(void) { + primary_display_t *result = allocate_display_bus(); + if (result) { + return result; + } + mp_raise_RuntimeError(translate("Too many display busses")); +} diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h index 4e1297c8d1..638c2091e0 100644 --- a/shared-module/displayio/__init__.h +++ b/shared-module/displayio/__init__.h @@ -56,5 +56,7 @@ void displayio_gc_collect(void); primary_display_t *allocate_display(void); primary_display_t *allocate_display_or_raise(void); +primary_display_t *allocate_display_bus(void); +primary_display_t *allocate_display_bus_or_raise(void); #endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H