diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-21 22:48:37 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-21 23:18:02 +0000 |
commit | 05005f679e00241e15a87751d89327f2c4630cb6 (patch) | |
tree | 50319ab1dee5af8016e95e3c80a3b28346cdca21 /stmhal/i2c.c | |
parent | 0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (diff) |
py: Remove mp_obj_str_builder and use vstr instead.
With this patch str/bytes construction is streamlined. Always use a
vstr to build a str/bytes object. If the size is known beforehand then
use vstr_init_len to allocate only required memory. Otherwise use
vstr_init and the vstr will grow as needed. Then use
mp_obj_new_str_from_vstr to create a str/bytes object using the vstr
memory.
Saves code ROM: 68 bytes on stmhal, 108 bytes on bare-arm, and 336 bytes
on unix x64.
Diffstat (limited to 'stmhal/i2c.c')
-rw-r--r-- | stmhal/i2c.c | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/stmhal/i2c.c b/stmhal/i2c.c index e4d406b4a..80b40b887 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -418,8 +418,8 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_RECV_NUM_ARGS, pyb_i2c_recv_args, vals); // get the buffer to receive into - mp_buffer_info_t bufinfo; - mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &bufinfo); + vstr_t vstr; + mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr); // receive the data HAL_StatusTypeDef status; @@ -428,9 +428,9 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "addr argument required")); } mp_uint_t i2c_addr = vals[1].u_int << 1; - status = HAL_I2C_Master_Receive(self->i2c, i2c_addr, bufinfo.buf, bufinfo.len, vals[2].u_int); + status = HAL_I2C_Master_Receive(self->i2c, i2c_addr, (uint8_t*)vstr.buf, vstr.len, vals[2].u_int); } else { - status = HAL_I2C_Slave_Receive(self->i2c, bufinfo.buf, bufinfo.len, vals[2].u_int); + status = HAL_I2C_Slave_Receive(self->i2c, (uint8_t*)vstr.buf, vstr.len, vals[2].u_int); } if (status != HAL_OK) { @@ -438,10 +438,10 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k } // return the received data - if (o_ret == MP_OBJ_NULL) { - return vals[0].u_obj; + if (o_ret != MP_OBJ_NULL) { + return o_ret; } else { - return mp_obj_str_builder_end(o_ret); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv); @@ -479,8 +479,8 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_ mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_MEM_READ_NUM_ARGS, pyb_i2c_mem_read_args, vals); // get the buffer to read into - mp_buffer_info_t bufinfo; - mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &bufinfo); + vstr_t vstr; + mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr); // get the addresses mp_uint_t i2c_addr = vals[1].u_int << 1; @@ -491,17 +491,17 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_ mem_addr_size = I2C_MEMADD_SIZE_16BIT; } - HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, vals[3].u_int); + HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, (uint8_t*)vstr.buf, vstr.len, vals[3].u_int); if (status != HAL_OK) { mp_hal_raise(status); } // return the read data - if (o_ret == MP_OBJ_NULL) { - return vals[0].u_obj; + if (o_ret != MP_OBJ_NULL) { + return o_ret; } else { - return mp_obj_str_builder_end(o_ret); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read); |