cleanup post instance parameter removal

This commit is contained in:
microDev 2022-01-20 20:00:02 +05:30
parent 2f6ef766ea
commit 30c0891fba
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
3 changed files with 25 additions and 48 deletions

View File

@ -51,42 +51,21 @@
//| circuitpython, as well as on circuitpython.org.
//| Example: "hallowing_m0_express"."""
#if CIRCUITPY_BOARD_I2C || CIRCUITPY_BOARD_SPI || CIRCUITPY_BOARD_UART
STATIC mp_int_t board_get_instance(size_t n_args, const mp_obj_t *args, const mp_int_t bus_in) {
if (n_args == 0) {
return 0;
}
const mp_int_t instance = mp_obj_get_int(args[0]);
if (instance >= bus_in || instance < 0) {
mp_raise_ValueError_varg(translate("No default %q bus"), MP_QSTR_UART);
}
return instance;
}
#endif
//| def I2C() -> busio.I2C:
//| """Returns the `busio.I2C` object for the board's designated I2C bus(es).
//| The object created is a singleton, and uses the default parameter values for `busio.I2C`."""
//| ...
//|
#if CIRCUITPY_BOARD_I2C
mp_obj_t board_i2c(size_t n_args, const mp_obj_t *args) {
const mp_int_t instance = board_get_instance(n_args, args, CIRCUITPY_BOARD_I2C);
const mp_obj_t singleton = common_hal_board_get_i2c(instance);
if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
return singleton;
}
return common_hal_board_create_i2c(instance);
STATIC mp_obj_t board_i2c_0(void) {
return common_hal_board_create_i2c(0);
}
#else
mp_obj_t board_i2c(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t board_i2c_0(void) {
mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_I2C);
return MP_ROM_NONE;
}
#endif
STATIC mp_obj_t board_i2c_0(void) {
return board_i2c(0, NULL);
}
MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c_0);
//| def SPI() -> busio.SPI:
@ -95,23 +74,15 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c_0);
//| ...
//|
#if CIRCUITPY_BOARD_SPI
mp_obj_t board_spi(size_t n_args, const mp_obj_t *args) {
const mp_int_t instance = board_get_instance(n_args, args, CIRCUITPY_BOARD_SPI);
const mp_obj_t singleton = common_hal_board_get_spi(instance);
if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
return singleton;
}
return common_hal_board_create_spi(instance);
STATIC mp_obj_t board_spi_0(void) {
return common_hal_board_create_spi(0);
}
#else
mp_obj_t board_spi(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t board_spi_0(void) {
mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_SPI);
return MP_ROM_NONE;
}
#endif
STATIC mp_obj_t board_spi_0(void) {
return board_spi(0, NULL);
}
MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi_0);
//| def UART() -> busio.UART:
@ -120,23 +91,15 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi_0);
//| ...
//|
#if CIRCUITPY_BOARD_UART
mp_obj_t board_uart(size_t n_args, const mp_obj_t *args) {
const mp_int_t instance = board_get_instance(n_args, args, CIRCUITPY_BOARD_UART);
const mp_obj_t singleton = common_hal_board_get_uart(instance);
if (singleton != NULL && !common_hal_busio_uart_deinited(singleton)) {
return singleton;
}
return common_hal_board_create_uart(instance);
STATIC mp_obj_t board_uart_0(void) {
return common_hal_board_create_uart(0);
}
#else
mp_obj_t board_uart(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t board_uart_0(void) {
mp_raise_NotImplementedError_varg(translate("No default %q bus"), MP_QSTR_UART);
return MP_ROM_NONE;
}
#endif
STATIC mp_obj_t board_uart_0(void) {
return board_uart(0, NULL);
}
MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart_0);
const mp_obj_module_t board_module = {

View File

@ -55,8 +55,7 @@ MP_DECLARE_CONST_FUN_OBJ_0(board_uart_obj);
#define CIRCUITPY_BOARD_BUS_SINGLETON(name, bus, instance) \
STATIC mp_obj_t board_##name(void) { \
const mp_obj_t inst = mp_obj_new_int_from_uint(instance); \
return board_##bus(1, &inst); \
return common_hal_board_create_##bus(instance); \
} \
MP_DEFINE_CONST_FUN_OBJ_0(board_##name##_obj, board_##name);

View File

@ -72,6 +72,11 @@ mp_obj_t common_hal_board_get_i2c(const mp_int_t instance) {
}
mp_obj_t common_hal_board_create_i2c(const mp_int_t instance) {
const mp_obj_t singleton = common_hal_board_get_i2c(instance);
if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
return singleton;
}
busio_i2c_obj_t *self = &i2c_obj[instance];
self->base.type = &busio_i2c_type;
@ -111,6 +116,11 @@ mp_obj_t common_hal_board_get_spi(const mp_int_t instance) {
}
mp_obj_t common_hal_board_create_spi(const mp_int_t instance) {
const mp_obj_t singleton = common_hal_board_get_spi(instance);
if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
return singleton;
}
busio_spi_obj_t *self = &spi_obj[instance];
self->base.type = &busio_spi_type;
@ -148,6 +158,11 @@ mp_obj_t common_hal_board_get_uart(const mp_int_t instance) {
}
mp_obj_t common_hal_board_create_uart(const mp_int_t instance) {
const mp_obj_t singleton = common_hal_board_get_uart(instance);
if (singleton != NULL && !common_hal_busio_uart_deinited(singleton)) {
return singleton;
}
busio_uart_obj_t *self = &uart_obj[instance];
self->base.type = &busio_uart_type;