|
30 | 30 | #include "shared-bindings/microcontroller/Pin.h" |
31 | 31 | #include "shared-bindings/busio/I2C.h" |
32 | 32 |
|
| 33 | +#include "lib/utils/buffer_helper.h" |
33 | 34 | #include "lib/utils/context_manager_helpers.h" |
34 | 35 | #include "py/runtime.h" |
35 | 36 | //| .. currentmodule:: busio |
@@ -187,18 +188,11 @@ STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, |
187 | 188 |
|
188 | 189 | mp_buffer_info_t bufinfo; |
189 | 190 | mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE); |
190 | | - int32_t end = args[ARG_end].u_int; |
191 | | - if (end < 0) { |
192 | | - end += bufinfo.len; |
193 | | - } |
194 | | - uint32_t start = args[ARG_start].u_int; |
195 | | - uint32_t len = end - start; |
196 | | - if ((uint32_t) end < start) { |
197 | | - len = 0; |
198 | | - } else if (len > bufinfo.len) { |
199 | | - len = bufinfo.len; |
200 | | - } |
201 | | - uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len); |
| 191 | + |
| 192 | + int32_t start = args[ARG_start].u_int; |
| 193 | + uint32_t length = bufinfo.len; |
| 194 | + normalize_buffer_bounds(&start, args[ARG_end].u_int, &length); |
| 195 | + uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, length); |
202 | 196 | if (status != 0) { |
203 | 197 | mp_raise_OSError(status); |
204 | 198 | } |
@@ -241,21 +235,14 @@ STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_ma |
241 | 235 | mp_buffer_info_t bufinfo; |
242 | 236 | mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_READ); |
243 | 237 |
|
244 | | - int32_t end = args[ARG_end].u_int; |
245 | | - if (end < 0) { |
246 | | - end += bufinfo.len; |
247 | | - } |
248 | | - uint32_t start = args[ARG_start].u_int; |
249 | | - uint32_t len = end - start; |
250 | | - if ((uint32_t) end < start) { |
251 | | - len = 0; |
252 | | - } else if (len > bufinfo.len) { |
253 | | - len = bufinfo.len; |
254 | | - } |
| 238 | + |
| 239 | + int32_t start = args[ARG_start].u_int; |
| 240 | + uint32_t length = bufinfo.len; |
| 241 | + normalize_buffer_bounds(&start, args[ARG_end].u_int, &length); |
255 | 242 |
|
256 | 243 | // do the transfer |
257 | 244 | uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int, |
258 | | - ((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool); |
| 245 | + ((uint8_t*) bufinfo.buf) + start, length, args[ARG_stop].u_bool); |
259 | 246 | if (status != 0) { |
260 | 247 | mp_raise_OSError(status); |
261 | 248 | } |
|
0 commit comments