Skip to content

Commit e06101c

Browse files
committed
pybricks/tools/awaitable: Refactor cancel_all.
This does not need the object itself. Also fix enum names. It will also be used for operations other than cancel, so rename it.
1 parent ffeda63 commit e06101c

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

pybricks/common/pb_type_motor.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ STATIC mp_obj_t await_or_wait(common_Motor_obj_t *self) {
157157
common_Motor_test_completion,
158158
pb_type_awaitable_return_none,
159159
common_Motor_cancel,
160-
PB_TYPE_AWAITABLE_CANCEL_LINKED);
160+
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
161161
}
162162

163163
// pybricks._common.Motor.angle
@@ -184,7 +184,7 @@ STATIC mp_obj_t common_Motor_reset_angle(size_t n_args, const mp_obj_t *pos_args
184184

185185
// Set the new angle
186186
pb_assert(pbio_servo_reset_angle(self->srv, reset_angle, reset_to_abs));
187-
pb_type_awaitable_cancel_all(MP_OBJ_FROM_PTR(self), self->awaitables, PB_TYPE_AWAITABLE_CANCEL_LINKED);
187+
pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
188188
return mp_const_none;
189189
}
190190
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_Motor_reset_angle_obj, 1, common_Motor_reset_angle);
@@ -209,7 +209,7 @@ STATIC mp_obj_t common_Motor_run(size_t n_args, const mp_obj_t *pos_args, mp_map
209209

210210
mp_int_t speed = pb_obj_get_int(speed_in);
211211
pb_assert(pbio_servo_run_forever(self->srv, speed));
212-
pb_type_awaitable_cancel_all(MP_OBJ_FROM_PTR(self), self->awaitables, PB_TYPE_AWAITABLE_CANCEL_LINKED);
212+
pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
213213
return mp_const_none;
214214
}
215215
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_Motor_run_obj, 1, common_Motor_run);
@@ -218,7 +218,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_Motor_run_obj, 1, common_Motor_run);
218218
STATIC mp_obj_t common_Motor_hold(mp_obj_t self_in) {
219219
common_Motor_obj_t *self = MP_OBJ_TO_PTR(self_in);
220220
pb_assert(pbio_servo_stop(self->srv, PBIO_CONTROL_ON_COMPLETION_HOLD));
221-
pb_type_awaitable_cancel_all(self_in, self->awaitables, PB_TYPE_AWAITABLE_CANCEL_LINKED);
221+
pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
222222
return mp_const_none;
223223
}
224224
MP_DEFINE_CONST_FUN_OBJ_1(common_Motor_hold_obj, common_Motor_hold);
@@ -291,7 +291,7 @@ STATIC mp_obj_t common_Motor_run_until_stalled(size_t n_args, const mp_obj_t *po
291291
common_Motor_test_completion,
292292
common_Motor_stall_return_value,
293293
common_Motor_cancel,
294-
PB_TYPE_AWAITABLE_CANCEL_LINKED);
294+
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
295295
}
296296
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_Motor_run_until_stalled_obj, 1, common_Motor_run_until_stalled);
297297

@@ -353,7 +353,7 @@ STATIC mp_obj_t common_Motor_track_target(size_t n_args, const mp_obj_t *pos_arg
353353

354354
mp_int_t target_angle = pb_obj_get_int(target_angle_in);
355355
pb_assert(pbio_servo_track_target(self->srv, target_angle));
356-
pb_type_awaitable_cancel_all(MP_OBJ_FROM_PTR(self), self->awaitables, PB_TYPE_AWAITABLE_CANCEL_LINKED);
356+
pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
357357
return mp_const_none;
358358
}
359359
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_Motor_track_target_obj, 1, common_Motor_track_target);

pybricks/common/pb_type_speaker.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ STATIC mp_obj_t pb_type_Speaker_beep(size_t n_args, const mp_obj_t *pos_args, mp
172172
pb_type_Speaker_beep_test_completion,
173173
pb_type_awaitable_return_none,
174174
pb_type_Speaker_cancel,
175-
PB_TYPE_AWAITABLE_CANCEL_LINKED | PB_TYPE_AWAITABLE_CANCEL_LINKED_CALLBACK);
175+
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL | PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE);
176176
}
177177
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_Speaker_beep_obj, 1, pb_type_Speaker_beep);
178178

@@ -388,7 +388,7 @@ STATIC mp_obj_t pb_type_Speaker_play_notes(size_t n_args, const mp_obj_t *pos_ar
388388
pb_type_Speaker_notes_test_completion,
389389
pb_type_awaitable_return_none,
390390
pb_type_Speaker_cancel,
391-
PB_TYPE_AWAITABLE_CANCEL_LINKED | PB_TYPE_AWAITABLE_CANCEL_LINKED_CALLBACK);
391+
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL | PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE);
392392
}
393393
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_Speaker_play_notes_obj, 1, pb_type_Speaker_play_notes);
394394

pybricks/robotics/pb_type_drivebase.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ STATIC mp_obj_t await_or_wait(pb_type_DriveBase_obj_t *self) {
119119
pb_type_DriveBase_test_completion,
120120
pb_type_awaitable_return_none,
121121
pb_type_DriveBase_cancel,
122-
PB_TYPE_AWAITABLE_CANCEL_LINKED);
122+
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
123123
}
124124

125125
// pybricks.robotics.DriveBase.straight
@@ -203,7 +203,7 @@ STATIC mp_obj_t pb_type_DriveBase_drive(size_t n_args, const mp_obj_t *pos_args,
203203
mp_int_t turn_rate = pb_obj_get_int(turn_rate_in);
204204

205205
// Cancel awaitables but not hardware. Drive forever will handle this.
206-
pb_type_awaitable_cancel_all(MP_OBJ_FROM_PTR(self), self->awaitables, PB_TYPE_AWAITABLE_CANCEL_LINKED);
206+
pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
207207

208208
pb_assert(pbio_drivebase_drive_forever(self->db, speed, turn_rate));
209209
return mp_const_none;
@@ -215,7 +215,7 @@ STATIC mp_obj_t pb_type_DriveBase_stop(mp_obj_t self_in) {
215215

216216
// Cancel awaitables.
217217
pb_type_DriveBase_obj_t *self = MP_OBJ_TO_PTR(self_in);
218-
pb_type_awaitable_cancel_all(self_in, self->awaitables, PB_TYPE_AWAITABLE_CANCEL_LINKED);
218+
pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
219219

220220
// Stop hardware.
221221
pb_type_DriveBase_cancel(self_in);

pybricks/tools/pb_type_awaitable.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,12 @@ STATIC bool pb_type_awaitable_completed(mp_obj_t self_in, uint32_t start_time) {
132132
}
133133

134134
/**
135-
* Cancels all awaitables associated with an object.
135+
* Checks and updates all awaitables associated with an object.
136136
*
137-
* This is normally used by the function that makes a new awaitable, but it can
138-
* also be called independently to cancel without starting a new awaitable.
139-
*
140-
* @param [in] obj The object whose method we want to wait for completion.
141137
* @param [in] awaitables_in List of awaitables associated with @p obj.
142-
* @param [in] options Controls awaitable behavior.
138+
* @param [in] options Controls update behavior.
143139
*/
144-
void pb_type_awaitable_cancel_all(mp_obj_t obj, mp_obj_t awaitables_in, pb_type_awaitable_opt_t options) {
140+
void pb_type_awaitable_update_all(mp_obj_t awaitables_in, pb_type_awaitable_opt_t options) {
145141

146142
// Exit if nothing to do.
147143
if (!pb_module_tools_run_loop_is_active() || options == PB_TYPE_AWAITABLE_OPT_NONE) {
@@ -155,12 +151,12 @@ void pb_type_awaitable_cancel_all(mp_obj_t obj, mp_obj_t awaitables_in, pb_type_
155151
// Only cancel awaitables that are in use.
156152
if (awaitable->test_completion) {
157153
// Cancel hardware operation if requested and available.
158-
if (options & PB_TYPE_AWAITABLE_CANCEL_LINKED_CALLBACK && awaitable->cancel) {
154+
if (options & PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE && awaitable->cancel) {
159155
awaitable->cancel(awaitable->obj);
160156
}
161157
// Set awaitable to done so it gets cancelled it gracefully on the
162158
// next iteration.
163-
if (options & PB_TYPE_AWAITABLE_CANCEL_LINKED) {
159+
if (options & PB_TYPE_AWAITABLE_OPT_CANCEL_ALL) {
164160
awaitable->test_completion = pb_type_awaitable_completed;
165161
}
166162
}
@@ -199,7 +195,7 @@ mp_obj_t pb_type_awaitable_await_or_wait(
199195
}
200196

201197
// First cancel linked awaitables if requested.
202-
pb_type_awaitable_cancel_all(obj, awaitables_in, options);
198+
pb_type_awaitable_update_all(awaitables_in, options);
203199

204200
// Gets free existing awaitable or creates a new one.
205201
pb_type_awaitable_obj_t *awaitable = pb_type_awaitable_get(awaitables_in);

pybricks/tools/pb_type_awaitable.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ typedef enum _pb_type_awaitable_opt_t {
2727
* running in parallel are using the same resources. This way, the newly
2828
* started operation "wins" and everything else is cancelled.
2929
*/
30-
PB_TYPE_AWAITABLE_CANCEL_LINKED = 1 << 2,
30+
PB_TYPE_AWAITABLE_OPT_CANCEL_ALL = 1 << 2,
3131
/**
3232
* On cancelling the linked awaitables, also call their cancel function
3333
* to stop hardware. Only used to close hardware resources that aren't
3434
* already cleaned up by lower level drivers (so not needed for motors).
3535
*/
36-
PB_TYPE_AWAITABLE_CANCEL_LINKED_CALLBACK = 1 << 3,
36+
PB_TYPE_AWAITABLE_OPT_CANCEL_HARDWARE = 1 << 3,
3737
} pb_type_awaitable_opt_t;
3838

3939
/**
@@ -52,7 +52,7 @@ typedef struct _pb_type_awaitable_obj_t pb_type_awaitable_obj_t;
5252
* @param [in] start_time The time when the awaitable was created.
5353
* @return True if operation is complete, False otherwise.
5454
*/
55-
typedef bool (*pb_type_awaitable_test_completion_t)(mp_obj_t objt, uint32_t end_time);
55+
typedef bool (*pb_type_awaitable_test_completion_t)(mp_obj_t obj, uint32_t end_time);
5656

5757
/**
5858
* Gets the return value of the awaitable. If it always returns None, providing
@@ -77,7 +77,7 @@ typedef void (*pb_type_awaitable_cancel_t)(mp_obj_t obj);
7777

7878
#define pb_type_awaitable_cancel_none (NULL)
7979

80-
void pb_type_awaitable_cancel_all(mp_obj_t obj, mp_obj_t awaitables_in, pb_type_awaitable_opt_t options);
80+
void pb_type_awaitable_update_all(mp_obj_t awaitables_in, pb_type_awaitable_opt_t options);
8181

8282
mp_obj_t pb_type_awaitable_await_or_wait(
8383
mp_obj_t obj,

0 commit comments

Comments
 (0)