mirror of
https://github.com/micropython/micropython.git
synced 2026-01-08 13:10:21 +01:00
py/obj: Add accessors for type slots and use everywhere.
This is a no-op, but sets the stage for changing the mp_obj_type_t representation. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
committed by
Damien George
parent
e8355eb163
commit
a52cd5b07d
@@ -273,7 +273,7 @@ int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n
|
||||
}
|
||||
}
|
||||
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
int ret = i2c_p->transfer_single(self, addr, len, buf, flags);
|
||||
|
||||
if (n > 1) {
|
||||
@@ -292,14 +292,14 @@ int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n
|
||||
}
|
||||
|
||||
STATIC int mp_machine_i2c_readfrom(mp_obj_base_t *self, uint16_t addr, uint8_t *dest, size_t len, bool stop) {
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
mp_machine_i2c_buf_t buf = {.len = len, .buf = dest};
|
||||
unsigned int flags = MP_MACHINE_I2C_FLAG_READ | (stop ? MP_MACHINE_I2C_FLAG_STOP : 0);
|
||||
return i2c_p->transfer(self, addr, 1, &buf, flags);
|
||||
}
|
||||
|
||||
STATIC int mp_machine_i2c_writeto(mp_obj_base_t *self, uint16_t addr, const uint8_t *src, size_t len, bool stop) {
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
mp_machine_i2c_buf_t buf = {.len = len, .buf = (uint8_t *)src};
|
||||
unsigned int flags = stop ? MP_MACHINE_I2C_FLAG_STOP : 0;
|
||||
return i2c_p->transfer(self, addr, 1, &buf, flags);
|
||||
@@ -310,7 +310,7 @@ STATIC int mp_machine_i2c_writeto(mp_obj_base_t *self, uint16_t addr, const uint
|
||||
|
||||
STATIC mp_obj_t machine_i2c_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
if (i2c_p->init == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported"));
|
||||
}
|
||||
@@ -338,7 +338,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_scan_obj, machine_i2c_scan);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_start(mp_obj_t self_in) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
if (i2c_p->start == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported"));
|
||||
}
|
||||
@@ -352,7 +352,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_start_obj, machine_i2c_start);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_stop(mp_obj_t self_in) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
if (i2c_p->stop == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported"));
|
||||
}
|
||||
@@ -366,7 +366,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_stop_obj, machine_i2c_stop);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_readinto(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
if (i2c_p->read == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported"));
|
||||
}
|
||||
@@ -390,7 +390,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readinto_obj, 2, 3, machine_i2c_
|
||||
|
||||
STATIC mp_obj_t machine_i2c_write(mp_obj_t self_in, mp_obj_t buf_in) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
if (i2c_p->write == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("I2C operation not supported"));
|
||||
}
|
||||
@@ -486,7 +486,7 @@ STATIC mp_obj_t machine_i2c_writevto(size_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
|
||||
// Do the I2C transfer
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
int ret = i2c_p->transfer(self, addr, nbufs, bufs, stop ? MP_MACHINE_I2C_FLAG_STOP : 0);
|
||||
mp_local_free(bufs);
|
||||
|
||||
@@ -519,7 +519,7 @@ STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t a
|
||||
|
||||
#if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1
|
||||
// The I2C transfer function may support the MP_MACHINE_I2C_FLAG_WRITE1 option
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
if (i2c_p->transfer_supports_write1) {
|
||||
// Create partial write and read buffers
|
||||
mp_machine_i2c_buf_t bufs[2] = {
|
||||
@@ -556,7 +556,7 @@ STATIC int write_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t
|
||||
};
|
||||
|
||||
// Do I2C transfer
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)MP_OBJ_TYPE_GET_SLOT(self->type, protocol);
|
||||
return i2c_p->transfer(self, addr, 2, bufs, MP_MACHINE_I2C_FLAG_STOP);
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t
|
||||
|
||||
if (n_args > 0 && mp_obj_is_obj(args[0])) {
|
||||
mp_obj_base_t *pin_base = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
|
||||
pin_p = (mp_pin_p_t *)pin_base->type->protocol;
|
||||
pin_p = (mp_pin_p_t *)MP_OBJ_TYPE_GET_SLOT_OR_NULL(pin_base->type, protocol);
|
||||
}
|
||||
|
||||
if (pin_p == NULL) {
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
|
||||
STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol;
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
|
||||
spi_p->init(s, n_args - 1, args + 1, kw_args);
|
||||
return mp_const_none;
|
||||
}
|
||||
@@ -51,7 +51,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
|
||||
|
||||
STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self);
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol;
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
|
||||
if (spi_p->deinit != NULL) {
|
||||
spi_p->deinit(s);
|
||||
}
|
||||
@@ -61,7 +61,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
|
||||
|
||||
STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self);
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)s->type->protocol;
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
|
||||
spi_p->transfer(s, len, src, dest);
|
||||
}
|
||||
|
||||
|
||||
@@ -744,7 +744,7 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
|
||||
wiznet5k_obj.base.type = (mp_obj_type_t *)&mod_network_nic_type_wiznet5k;
|
||||
wiznet5k_obj.cris_state = 0;
|
||||
wiznet5k_obj.spi = spi;
|
||||
wiznet5k_obj.spi_transfer = ((mp_machine_spi_p_t *)spi->type->protocol)->transfer;
|
||||
wiznet5k_obj.spi_transfer = ((mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(spi->type, protocol))->transfer;
|
||||
wiznet5k_obj.cs = cs;
|
||||
wiznet5k_obj.rst = rst;
|
||||
#if WIZNET5K_WITH_LWIP_STACK
|
||||
|
||||
@@ -132,8 +132,9 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) {
|
||||
}
|
||||
|
||||
// If the mounted object has the VFS protocol, call its import_stat helper
|
||||
const mp_vfs_proto_t *proto = mp_obj_get_type(vfs->obj)->protocol;
|
||||
if (proto != NULL) {
|
||||
const mp_obj_type_t *type = mp_obj_get_type(vfs->obj);
|
||||
if (MP_OBJ_TYPE_HAS_SLOT(type, protocol)) {
|
||||
const mp_vfs_proto_t *proto = MP_OBJ_TYPE_GET_SLOT(type, protocol);
|
||||
return proto->import_stat(MP_OBJ_TO_PTR(vfs->obj), path_out);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,12 +28,12 @@
|
||||
|
||||
int mp_virtual_pin_read(mp_obj_t pin) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(pin);
|
||||
mp_pin_p_t *pin_p = (mp_pin_p_t *)s->type->protocol;
|
||||
mp_pin_p_t *pin_p = (mp_pin_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
|
||||
return pin_p->ioctl(pin, MP_PIN_READ, 0, NULL);
|
||||
}
|
||||
|
||||
void mp_virtual_pin_write(mp_obj_t pin, int value) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(pin);
|
||||
mp_pin_p_t *pin_p = (mp_pin_p_t *)s->type->protocol;
|
||||
mp_pin_p_t *pin_p = (mp_pin_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
|
||||
pin_p->ioctl(pin, MP_PIN_WRITE, value, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user