Skip to content

Commit e7da852

Browse files
committed
Fixing review comments
1 parent 2374b0d commit e7da852

File tree

4 files changed

+20
-53
lines changed

4 files changed

+20
-53
lines changed

shared-bindings/busdevice/I2CDevice.c

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,23 @@ STATIC mp_obj_t busdevice_i2cdevice_make_new(const mp_obj_type_t *type, size_t n
7676

7777
busio_i2c_obj_t* i2c = args[ARG_i2c].u_obj;
7878

79-
common_hal_busdevice_i2cdevice_construct(self, i2c, args[ARG_device_address].u_int, args[ARG_probe].u_bool);
79+
common_hal_busdevice_i2cdevice_construct(MP_OBJ_TO_PTR(self), i2c, args[ARG_device_address].u_int);
8080
if (args[ARG_probe].u_bool == true) {
81-
common_hal_busdevice_i2cdevice___probe_for_device(self);
81+
common_hal_busdevice_i2cdevice_probe_for_device(self);
8282
}
8383

8484
return (mp_obj_t)self;
8585
}
8686

8787
STATIC mp_obj_t busdevice_i2cdevice_obj___enter__(mp_obj_t self_in) {
88-
common_hal_busdevice_i2cdevice_lock(self_in);
89-
return self_in;
88+
busdevice_i2cdevice_obj_t *self = MP_OBJ_TO_PTR(self_in);
89+
common_hal_busdevice_i2cdevice_lock(self);
90+
return self;
9091
}
9192
STATIC MP_DEFINE_CONST_FUN_OBJ_1(busdevice_i2cdevice___enter___obj, busdevice_i2cdevice_obj___enter__);
9293

9394
STATIC mp_obj_t busdevice_i2cdevice_obj___exit__(size_t n_args, const mp_obj_t *args) {
94-
common_hal_busdevice_i2cdevice_unlock(args[0]);
95+
common_hal_busdevice_i2cdevice_unlock(MP_OBJ_TO_PTR(args[0]));
9596
return mp_const_none;
9697
}
9798
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busdevice_i2cdevice___exit___obj, 4, 4, busdevice_i2cdevice_obj___exit__);
@@ -118,7 +119,7 @@ STATIC void readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t s
118119
mp_raise_ValueError(translate("Buffer must be at least length 1"));
119120
}
120121

121-
uint8_t status = common_hal_busdevice_i2cdevice_readinto(self, ((uint8_t*)bufinfo.buf) + start, length);
122+
uint8_t status = common_hal_busdevice_i2cdevice_readinto(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
122123
if (status != 0) {
123124
mp_raise_OSError(status);
124125
}
@@ -127,7 +128,7 @@ STATIC void readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t s
127128
STATIC mp_obj_t busdevice_i2cdevice_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
128129
enum { ARG_buffer, ARG_start, ARG_end };
129130
static const mp_arg_t allowed_args[] = {
130-
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
131+
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
131132
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
132133
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
133134
};
@@ -165,7 +166,7 @@ STATIC void write(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t star
165166
mp_raise_ValueError(translate("Buffer must be at least length 1"));
166167
}
167168

168-
uint8_t status = common_hal_busdevice_i2cdevice_write(self, ((uint8_t*)bufinfo.buf) + start, length);
169+
uint8_t status = common_hal_busdevice_i2cdevice_write(MP_OBJ_TO_PTR(self), ((uint8_t*)bufinfo.buf) + start, length);
169170
if (status != 0) {
170171
mp_raise_OSError(status);
171172
}
@@ -174,7 +175,7 @@ STATIC void write(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, int32_t star
174175
STATIC mp_obj_t busdevice_i2cdevice_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
175176
enum { ARG_buffer, ARG_start, ARG_end };
176177
static const mp_arg_t allowed_args[] = {
177-
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
178+
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
178179
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
179180
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
180181
};
@@ -214,8 +215,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busdevice_i2cdevice_write_obj, 2, busdevice_i2cdevice
214215
STATIC mp_obj_t busdevice_i2cdevice_write_then_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
215216
enum { ARG_out_buffer, ARG_in_buffer, ARG_out_start, ARG_out_end, ARG_in_start, ARG_in_end };
216217
static const mp_arg_t allowed_args[] = {
217-
{ MP_QSTR_out_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
218-
{ MP_QSTR_in_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
218+
{ MP_QSTR_out_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
219+
{ MP_QSTR_in_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
219220
{ MP_QSTR_out_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
220221
{ MP_QSTR_out_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
221222
{ MP_QSTR_in_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
@@ -234,31 +235,14 @@ STATIC mp_obj_t busdevice_i2cdevice_write_then_readinto(size_t n_args, const mp_
234235
}
235236
MP_DEFINE_CONST_FUN_OBJ_KW(busdevice_i2cdevice_write_then_readinto_obj, 3, busdevice_i2cdevice_write_then_readinto);
236237

237-
//| def __probe_for_device(self):
238-
//| """
239-
//| Try to read a byte from an address,
240-
//| if you get an OSError it means the device is not there
241-
//| or that the device does not support these means of probing
242-
//| """
243-
//| ...
244-
//|
245-
STATIC mp_obj_t busdevice_i2cdevice___probe_for_device(mp_obj_t self_in) {
246-
busdevice_i2cdevice_obj_t *self = self_in;
247-
common_hal_busdevice_i2cdevice___probe_for_device(self);
248-
return mp_const_none;
249-
}
250-
MP_DEFINE_CONST_FUN_OBJ_1(busdevice_i2cdevice___probe_for_device_obj, busdevice_i2cdevice___probe_for_device);
251-
252238
STATIC const mp_rom_map_elem_t busdevice_i2cdevice_locals_dict_table[] = {
253239
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&busdevice_i2cdevice___enter___obj) },
254240
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busdevice_i2cdevice___exit___obj) },
255241
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&busdevice_i2cdevice_readinto_obj) },
256242
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&busdevice_i2cdevice_write_obj) },
257243
{ MP_ROM_QSTR(MP_QSTR_write_then_readinto), MP_ROM_PTR(&busdevice_i2cdevice_write_then_readinto_obj) },
258-
{ MP_ROM_QSTR(MP_QSTR___probe_for_device), MP_ROM_PTR(&busdevice_i2cdevice___probe_for_device_obj) },
259244
};
260245

261-
262246
STATIC MP_DEFINE_CONST_DICT(busdevice_i2cdevice_locals_dict, busdevice_i2cdevice_locals_dict_table);
263247

264248
const mp_obj_type_t busdevice_i2cdevice_type = {

shared-bindings/busdevice/I2CDevice.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@
4343
extern const mp_obj_type_t busdevice_i2cdevice_type;
4444

4545
// Initializes the hardware peripheral.
46-
extern void common_hal_busdevice_i2cdevice_construct(busdevice_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address, bool probe);
46+
extern void common_hal_busdevice_i2cdevice_construct(busdevice_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address);
4747
extern uint8_t common_hal_busdevice_i2cdevice_readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
4848
extern uint8_t common_hal_busdevice_i2cdevice_write(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
49-
extern uint8_t common_hal_busdevice_i2cdevice_write_then_readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t out_buffer,
50-
mp_obj_t in_buffer, size_t out_length, size_t in_length);
5149
extern void common_hal_busdevice_i2cdevice_lock(busdevice_i2cdevice_obj_t *self);
5250
extern void common_hal_busdevice_i2cdevice_unlock(busdevice_i2cdevice_obj_t *self);
53-
extern void common_hal_busdevice_i2cdevice___probe_for_device(busdevice_i2cdevice_obj_t *self);
51+
extern void common_hal_busdevice_i2cdevice_probe_for_device(busdevice_i2cdevice_obj_t *self);
5452

5553
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_I2CDEVICE_H

shared-module/busdevice/I2CDevice.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@
3030
#include "py/nlr.h"
3131
#include "py/runtime.h"
3232

33-
void common_hal_busdevice_i2cdevice_construct(busdevice_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address, bool probe) {
33+
void common_hal_busdevice_i2cdevice_construct(busdevice_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address) {
3434
self->i2c = i2c;
3535
self->device_address = device_address;
36-
self->probe = probe;
3736
}
3837

3938
void common_hal_busdevice_i2cdevice_lock(busdevice_i2cdevice_obj_t *self) {
4039
bool success = false;
4140
while (!success) {
4241
success = common_hal_busio_i2c_try_lock(self->i2c);
42+
RUN_BACKGROUND_TASKS;
43+
mp_handle_pending();
4344
}
4445
}
4546

@@ -48,29 +49,14 @@ void common_hal_busdevice_i2cdevice_unlock(busdevice_i2cdevice_obj_t *self) {
4849
}
4950

5051
uint8_t common_hal_busdevice_i2cdevice_readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
51-
uint8_t status = common_hal_busio_i2c_read(self->i2c, self->device_address, buffer, length);
52-
53-
return status;
52+
return common_hal_busio_i2c_read(self->i2c, self->device_address, buffer, length);
5453
}
5554

5655
uint8_t common_hal_busdevice_i2cdevice_write(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
57-
uint8_t status = common_hal_busio_i2c_write(self->i2c, self->device_address, buffer, length, true);
58-
59-
return status;
60-
}
61-
62-
uint8_t common_hal_busdevice_i2cdevice_write_then_readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t out_buffer, mp_obj_t in_buffer,
63-
size_t out_length, size_t in_length) {
64-
uint8_t status = 0;
65-
66-
status = common_hal_busio_i2c_write(self->i2c, self->device_address, out_buffer, out_length, true);
67-
68-
status = common_hal_busio_i2c_read(self->i2c, self->device_address, in_buffer, in_length);
69-
70-
return status;
56+
return common_hal_busio_i2c_write(self->i2c, self->device_address, buffer, length, true);
7157
}
7258

73-
void common_hal_busdevice_i2cdevice___probe_for_device(busdevice_i2cdevice_obj_t *self) {
59+
void common_hal_busdevice_i2cdevice_probe_for_device(busdevice_i2cdevice_obj_t *self) {
7460
common_hal_busdevice_i2cdevice_lock(self);
7561

7662
mp_buffer_info_t bufinfo;

shared-module/busdevice/I2CDevice.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ typedef struct {
3434
mp_obj_base_t base;
3535
busio_i2c_obj_t *i2c;
3636
uint8_t device_address;
37-
bool probe;
3837
} busdevice_i2cdevice_obj_t;
3938

4039
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_I2CDEVICE_H

0 commit comments

Comments
 (0)