Skip to content

Commit 12d770b

Browse files
committed
Added __probe_for_device
1 parent b637d39 commit 12d770b

File tree

3 files changed

+31
-38
lines changed

3 files changed

+31
-38
lines changed

shared-bindings/busdevice/I2CDevice.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "py/runtime.h"
3838
#include "supervisor/shared/translate.h"
3939

40+
4041
//| class I2CDevice:
4142
//| """Two wire serial protocol"""
4243
//|
@@ -59,6 +60,10 @@ STATIC mp_obj_t busdevice_i2cdevice_make_new(const mp_obj_type_t *type, size_t n
5960
busio_i2c_obj_t* i2c = args[ARG_i2c].u_obj;
6061

6162
common_hal_busdevice_i2cdevice_construct(self, i2c, args[ARG_device_address].u_int, args[ARG_probe].u_bool);
63+
if (args[ARG_probe].u_bool == true) {
64+
common_hal_busdevice_i2cdevice___probe_for_device(self);
65+
}
66+
6267
return (mp_obj_t)self;
6368
}
6469

@@ -204,28 +209,31 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busdevice_i2cdevice_write_then_readinto_obj, 3, busde
204209

205210

206211
STATIC mp_obj_t busdevice_i2cdevice___probe_for_device(mp_obj_t self_in) {
207-
//busdevice_i2cdevice_obj_t *self = self_in;
212+
busdevice_i2cdevice_obj_t *self = self_in;
213+
common_hal_busdevice_i2cdevice___probe_for_device(self);
208214

209-
//common_hal_busdevice_i2cdevice_lock(self_in);
215+
/* common_hal_busdevice_i2cdevice_lock(self);
210216
211-
/*
212-
uint8_t buffer;
217+
218+
//uint8_t buffer;
213219
mp_buffer_info_t bufinfo;
214-
mp_get_buffer_raise(&buffer, &bufinfo, MP_BUFFER_WRITE);
220+
//mp_obj_t bufobj = MP_OBJ_FROM_PTR(&buffer)
221+
mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
222+
223+
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
215224
216225
uint8_t status = common_hal_busdevice_i2cdevice_readinto(self_in, (uint8_t*)bufinfo.buf, 1);
217226
if (status != 0) {
218227
common_hal_busdevice_i2cdevice_unlock(self_in);
219228
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
220229
}
230+
231+
common_hal_busdevice_i2cdevice_unlock(self);
221232
*/
222-
//common_hal_busdevice_i2cdevice_unlock(self_in);
223-
224233
return mp_const_none;
225234
}
226235
MP_DEFINE_CONST_FUN_OBJ_1(busdevice_i2cdevice___probe_for_device_obj, busdevice_i2cdevice___probe_for_device);
227236

228-
229237
STATIC const mp_rom_map_elem_t busdevice_i2cdevice_locals_dict_table[] = {
230238
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&busdevice_i2cdevice___enter___obj) },
231239
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busdevice_i2cdevice___exit___obj) },

shared-bindings/busdevice/I2CDevice.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ extern const mp_obj_type_t busdevice_i2cdevice_type;
4545

4646
// Initializes the hardware peripheral.
4747
extern void common_hal_busdevice_i2cdevice_construct(busdevice_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address, bool probe);
48-
extern void common_hal_busdevice_i2cdevice___enter__(busdevice_i2cdevice_obj_t *self);
49-
extern void common_hal_busdevice_i2cdevice___exit__(busdevice_i2cdevice_obj_t *self);
5048
extern uint8_t common_hal_busdevice_i2cdevice_readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
5149
extern uint8_t common_hal_busdevice_i2cdevice_write(busdevice_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length);
5250
extern uint8_t common_hal_busdevice_i2cdevice_write_then_readinto(busdevice_i2cdevice_obj_t *self, mp_obj_t out_buffer,
5351
mp_obj_t in_buffer, size_t out_length, size_t in_length);
54-
extern uint8_t common_hal_busdevice_i2cdevice___probe_for_device(busdevice_i2cdevice_obj_t *self);
5552
extern void common_hal_busdevice_i2cdevice_lock(busdevice_i2cdevice_obj_t *self);
5653
extern void common_hal_busdevice_i2cdevice_unlock(busdevice_i2cdevice_obj_t *self);
54+
extern void common_hal_busdevice_i2cdevice___probe_for_device(busdevice_i2cdevice_obj_t *self);
5755

5856
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_I2CDEVICE_H

shared-module/busdevice/I2CDevice.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@
2828
#include "shared-bindings/busio/I2C.h"
2929
#include "py/mperrno.h"
3030
#include "py/nlr.h"
31+
#include "py/runtime.h"
3132

3233
void common_hal_busdevice_i2cdevice_construct(busdevice_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address, bool probe) {
3334
self->i2c = i2c;
3435
self->device_address = device_address;
3536
self->probe = probe;
36-
37-
if (self->probe == true) {
38-
common_hal_busdevice_i2cdevice___probe_for_device(self);
39-
}
4037
}
4138

4239
void common_hal_busdevice_i2cdevice_lock(busdevice_i2cdevice_obj_t *self) {
@@ -73,29 +70,19 @@ uint8_t common_hal_busdevice_i2cdevice_write_then_readinto(busdevice_i2cdevice_o
7370
return status;
7471
}
7572

76-
uint8_t common_hal_busdevice_i2cdevice___probe_for_device(busdevice_i2cdevice_obj_t *self) {
77-
78-
73+
void common_hal_busdevice_i2cdevice___probe_for_device(busdevice_i2cdevice_obj_t *self) {
74+
common_hal_busdevice_i2cdevice_lock(self);
7975

76+
mp_buffer_info_t bufinfo;
77+
mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
8078

81-
// write ""
79+
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
8280

83-
84-
/*
85-
while not self.i2c.try_lock():
86-
pass
87-
try:
88-
self.i2c.writeto(self.device_address, b"")
89-
except OSError:
90-
# some OS's dont like writing an empty bytesting...
91-
# Retry by reading a byte
92-
try:
93-
result = bytearray(1)
94-
self.i2c.readfrom_into(self.device_address, result)
95-
except OSError:
96-
raise ValueError("No I2C device at address: %x" % self.device_address)
97-
finally:
98-
self.i2c.unlock()
99-
*/
100-
return 0;
101-
}
81+
uint8_t status = common_hal_busdevice_i2cdevice_readinto(self, (uint8_t*)bufinfo.buf, 1);
82+
if (status != 0) {
83+
common_hal_busdevice_i2cdevice_unlock(self);
84+
mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
85+
}
86+
87+
common_hal_busdevice_i2cdevice_unlock(self);
88+
}

0 commit comments

Comments
 (0)