Skip to content

Commit 8c7b35c

Browse files
committed
pybricks.pupdevices: Use new awaitable.
1 parent 32d846f commit 8c7b35c

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

pybricks/common/pb_type_device.c

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,11 @@ void *pb_type_device_get_data_blocking(mp_obj_t self_in, uint8_t mode) {
6565
* data has been written to the device, including the neccessary delays for
6666
* discarding stale data or the time needed to externally process written data.
6767
*
68-
* @param [in] self_in The sensor object instance.
69-
* @param [in] end_time Not used.
70-
* @return True if operation is complete (device ready),
71-
* false otherwise.
68+
* See ::pbio_port_lump_is_ready for details.
7269
*/
73-
static bool pb_pup_device_test_completion(mp_obj_t self_in, uint32_t end_time) {
70+
static pbio_error_t pb_pup_device_iter_once(pbio_os_state_t *state, mp_obj_t self_in) {
7471
pb_type_device_obj_base_t *sensor = MP_OBJ_TO_PTR(self_in);
75-
pbio_error_t err = pbio_port_lump_is_ready(sensor->lump_dev);
76-
if (err == PBIO_ERROR_AGAIN) {
77-
return false;
78-
}
79-
pb_assert(err);
80-
return true;
72+
return pbio_port_lump_is_ready(sensor->lump_dev);
8173
}
8274

8375
/**
@@ -100,14 +92,12 @@ mp_obj_t pb_type_device_method_call(mp_obj_t self_in, size_t n_args, size_t n_kw
10092
pb_type_device_obj_base_t *sensor = MP_OBJ_TO_PTR(sensor_in);
10193
pb_assert(pbio_port_lump_set_mode(sensor->lump_dev, method->mode));
10294

103-
return pb_type_awaitable_await_or_wait(
104-
sensor_in,
105-
sensor->awaitables,
106-
pb_type_awaitable_end_time_none,
107-
pb_pup_device_test_completion,
108-
method->get_values,
109-
pb_type_awaitable_cancel_none,
110-
PB_TYPE_AWAITABLE_OPT_NONE);
95+
pb_type_async_t config = {
96+
.iter_once = pb_pup_device_iter_once,
97+
.parent_obj = sensor_in,
98+
.return_map = method->get_values,
99+
};
100+
return pb_type_async_wait_or_await(&config, &sensor->last_awaitable);
111101
}
112102

113103
/**
@@ -132,14 +122,11 @@ MP_DEFINE_CONST_OBJ_TYPE(
132122
*/
133123
mp_obj_t pb_type_device_set_data(pb_type_device_obj_base_t *sensor, uint8_t mode, const void *data, uint8_t size) {
134124
pb_assert(pbio_port_lump_set_mode_with_data(sensor->lump_dev, mode, data, size));
135-
return pb_type_awaitable_await_or_wait(
136-
MP_OBJ_FROM_PTR(sensor),
137-
sensor->awaitables,
138-
pb_type_awaitable_end_time_none,
139-
pb_pup_device_test_completion,
140-
pb_type_awaitable_return_none,
141-
pb_type_awaitable_cancel_none,
142-
PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY);
125+
pb_type_async_t config = {
126+
.iter_once = pb_pup_device_iter_once,
127+
.parent_obj = MP_OBJ_FROM_PTR(sensor),
128+
};
129+
return pb_type_async_wait_or_await(&config, &sensor->last_awaitable);
143130
}
144131

145132
void pb_device_set_lego_mode(pbio_port_t *port) {
@@ -174,7 +161,7 @@ lego_device_type_id_t pb_type_device_init_class(pb_type_device_obj_base_t *self,
174161
mp_hal_delay_ms(50);
175162
}
176163
pb_assert(err);
177-
self->awaitables = mp_obj_new_list(0, NULL);
164+
self->last_awaitable = NULL;
178165
return actual_id;
179166
}
180167

pybricks/common/pb_type_device.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
#include <pbio/port_lump.h>
13-
#include <pybricks/tools/pb_type_awaitable.h>
13+
#include <pybricks/tools/pb_type_async.h>
1414

1515
/**
1616
* Used in place of mp_obj_base_t in all pupdevices. This lets us share
@@ -19,7 +19,7 @@
1919
typedef struct _pb_type_device_obj_base_t {
2020
mp_obj_base_t base;
2121
pbio_port_lump_dev_t *lump_dev;
22-
mp_obj_t awaitables;
22+
pb_type_async_t *last_awaitable;
2323
} pb_type_device_obj_base_t;
2424

2525
#if PYBRICKS_PY_DEVICES
@@ -33,7 +33,7 @@ typedef struct _pb_type_device_obj_base_t {
3333
*/
3434
typedef struct {
3535
mp_obj_base_t base;
36-
pb_type_awaitable_return_t get_values;
36+
pb_type_async_return_map_t get_values;
3737
uint8_t mode;
3838
} pb_type_device_method_obj_t;
3939

0 commit comments

Comments
 (0)