py: Fix mp_get_buffer, and use it in more places.

Must use mp_obj_get_type to get the type of an object.  Can't assume
mp_obj_t is castable to mp_obj_base_t.
This commit is contained in:
Damien George 2014-04-13 12:08:52 +01:00
parent 4b01de44ba
commit 8a1cab952f
4 changed files with 16 additions and 22 deletions

View File

@ -333,11 +333,11 @@ mp_obj_t mp_identity(mp_obj_t self) {
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity); MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) { bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) {
mp_obj_base_t *o = (mp_obj_base_t *)obj; mp_obj_type_t *type = mp_obj_get_type(obj);
if (o->type->buffer_p.get_buffer == NULL) { if (type->buffer_p.get_buffer == NULL) {
return false; return false;
} }
o->type->buffer_p.get_buffer(o, bufinfo, BUFFER_READ); type->buffer_p.get_buffer(obj, bufinfo, BUFFER_READ);
if (bufinfo->buf == NULL) { if (bufinfo->buf == NULL) {
return false; return false;
} }

View File

@ -442,14 +442,15 @@ STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
mp_obj_t *items; mp_obj_t *items;
mp_obj_list_get(obj, &len, &items); mp_obj_list_get(obj, &len, &items);
return (machine_uint_t)items; return (machine_uint_t)items;
} else if (type->buffer_p.get_buffer != NULL) {
// supports the buffer protocol, get a pointer to the data
buffer_info_t bufinfo;
type->buffer_p.get_buffer(obj, &bufinfo, BUFFER_READ);
return (machine_uint_t)bufinfo.buf;
} else { } else {
// just pass along a pointer to the object buffer_info_t bufinfo;
return (machine_uint_t)obj; if (mp_get_buffer(obj, &bufinfo)) {
// supports the buffer protocol, return a pointer to the data
return (machine_uint_t)bufinfo.buf;
} else {
// just pass along a pointer to the object
return (machine_uint_t)obj;
}
} }
} }
} }

View File

@ -180,12 +180,8 @@ mp_obj_t pyb_dac_dma(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
// set TIM6 to trigger the DAC at the given frequency // set TIM6 to trigger the DAC at the given frequency
TIM6_Config(mp_obj_get_int(args[2])); TIM6_Config(mp_obj_get_int(args[2]));
mp_obj_type_t *type = mp_obj_get_type(args[1]);
if (type->buffer_p.get_buffer == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "buffer argument must support buffer protocol"));
}
buffer_info_t bufinfo; buffer_info_t bufinfo;
type->buffer_p.get_buffer(args[1], &bufinfo, BUFFER_READ); mp_get_buffer_raise(args[1], &bufinfo);
__DMA1_CLK_ENABLE(); __DMA1_CLK_ENABLE();

View File

@ -143,16 +143,13 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args) {
machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1; machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1;
machine_uint_t mem_addr = mp_obj_get_int(args[2]); machine_uint_t mem_addr = mp_obj_get_int(args[2]);
HAL_StatusTypeDef status; HAL_StatusTypeDef status;
mp_obj_type_t *type = mp_obj_get_type(args[3]); if (MP_OBJ_IS_INT(args[3])) {
if (type->buffer_p.get_buffer != NULL) {
buffer_info_t bufinfo;
type->buffer_p.get_buffer(args[3], &bufinfo, BUFFER_READ);
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
} else if (MP_OBJ_IS_INT(args[3])) {
uint8_t data[1] = {mp_obj_get_int(args[3])}; uint8_t data[1] = {mp_obj_get_int(args[3])};
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200); status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
} else { } else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "data argument must be an integer or support the buffer protocol")); buffer_info_t bufinfo;
mp_get_buffer_raise(args[3], &bufinfo);
status = HAL_I2C_Mem_Write(self->i2c_handle, i2c_addr, mem_addr, I2C_MEMADD_SIZE_8BIT, bufinfo.buf, bufinfo.len, 200);
} }
//printf("Write got %d\n", status); //printf("Write got %d\n", status);