Skip to content

Commit

Permalink
pybricks/util_mp: Use args None test.
Browse files Browse the repository at this point in the history
Since this is done in quite a few functions, this should make the build size smaller and the code easier to follow.
  • Loading branch information
laurensvalk committed Oct 21, 2024
1 parent 4fa2174 commit 15c84e7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
9 changes: 4 additions & 5 deletions pybricks/common/pb_type_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static mp_obj_t pb_type_Control_limits(size_t n_args, const mp_obj_t *pos_args,
torque = pbio_control_settings_get_actuation_limit(&self->control->settings);

// If all given values are none, return current values
if (speed_in == mp_const_none && acceleration_in == mp_const_none && torque_in == mp_const_none) {
if (PB_PARSE_ARGS_METHOD_ALL_NONE()) {
mp_obj_t ret[] = {
mp_obj_new_int(speed),
make_acceleration_return_value(acceleration, deceleration),
Expand Down Expand Up @@ -134,8 +134,7 @@ static mp_obj_t pb_type_Control_pid(size_t n_args, const mp_obj_t *pos_args, mp_
pbio_control_settings_get_pid(&self->control->settings, &kp, &ki, &kd, &integral_deadzone, &integral_change_max);

// If all given values are none, return current values
if (kp_in == mp_const_none && ki_in == mp_const_none && kd_in == mp_const_none &&
integral_rate_in == mp_const_none) {
if (PB_PARSE_ARGS_METHOD_ALL_NONE()) {
mp_obj_t ret[5];
ret[0] = mp_obj_new_int(kp);
ret[1] = mp_obj_new_int(ki);
Expand Down Expand Up @@ -171,7 +170,7 @@ static mp_obj_t pb_type_Control_target_tolerances(size_t n_args, const mp_obj_t
pbio_control_settings_get_target_tolerances(&self->control->settings, &speed, &position);

// If all given values are none, return current values
if (speed_in == mp_const_none && position_in == mp_const_none) {
if (PB_PARSE_ARGS_METHOD_ALL_NONE()) {
mp_obj_t ret[2];
ret[0] = mp_obj_new_int(speed);
ret[1] = mp_obj_new_int(position);
Expand Down Expand Up @@ -202,7 +201,7 @@ static mp_obj_t pb_type_Control_stall_tolerances(size_t n_args, const mp_obj_t *
pbio_control_settings_get_stall_tolerances(&self->control->settings, &speed, &time);

// If all given values are none, return current values
if (speed_in == mp_const_none && time_in == mp_const_none) {
if (PB_PARSE_ARGS_METHOD_ALL_NONE()) {
mp_obj_t ret[2];
ret[0] = mp_obj_new_int(speed);
ret[1] = mp_obj_new_int_from_uint(time);
Expand Down
6 changes: 1 addition & 5 deletions pybricks/common/pb_type_imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,7 @@ static mp_obj_t pb_type_imu_settings(size_t n_args, const mp_obj_t *pos_args, mp
(void)self;

// Return current values if no arguments are given.
if (angular_velocity_threshold_in == mp_const_none &&
acceleration_threshold_in == mp_const_none &&
heading_correction_in == mp_const_none &&
acceleration_correction_in == mp_const_none) {

if (PB_PARSE_ARGS_METHOD_ALL_NONE()) {
// Raises if not set, so can safely dereference.
pbio_imu_persistent_settings_t *get_settings;
pb_assert(pbio_imu_get_settings(&get_settings));
Expand Down
6 changes: 1 addition & 5 deletions pybricks/robotics/pb_type_drivebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,7 @@ static mp_obj_t pb_type_DriveBase_settings(size_t n_args, const mp_obj_t *pos_ar
&turn_rate, &turn_acceleration, &turn_deceleration);

// If all given values are none, return current values
if (straight_speed_in == mp_const_none &&
straight_acceleration_in == mp_const_none &&
turn_rate_in == mp_const_none &&
turn_acceleration_in == mp_const_none
) {
if (PB_PARSE_ARGS_METHOD_ALL_NONE()) {
mp_obj_t ret[] = {
mp_obj_new_int(straight_speed),
make_acceleration_return_value(straight_acceleration, straight_deceleration),
Expand Down

0 comments on commit 15c84e7

Please sign in to comment.