From 05c370abd4b04d2e7d333443d5924a86d39daa30 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 16 Mar 2022 16:46:28 +0000 Subject: [PATCH] PAA5100/PMW3901 Fix default float argument bug for #228 --- .../breakout_paa5100/breakout_paa5100.cpp | 33 ++++++++++++++----- .../breakout_pmw3901/breakout_pmw3901.cpp | 33 ++++++++++++++----- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/micropython/modules/breakout_paa5100/breakout_paa5100.cpp b/micropython/modules/breakout_paa5100/breakout_paa5100.cpp index 31a698e7..e6780130 100644 --- a/micropython/modules/breakout_paa5100/breakout_paa5100.cpp +++ b/micropython/modules/breakout_paa5100/breakout_paa5100.cpp @@ -190,7 +190,7 @@ mp_obj_t BreakoutPAA5100_get_motion(size_t n_args, const mp_obj_t *pos_args, mp_ enum { ARG_self, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_obj_new_float((float)BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS / 1000)} }, + { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -198,8 +198,13 @@ mp_obj_t BreakoutPAA5100_get_motion(size_t n_args, const mp_obj_t *pos_args, mp_ breakout_paa5100_BreakoutPAA5100_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_paa5100_BreakoutPAA5100_obj_t); - float timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - uint16_t timeout_ms = (uint16_t)(timeout * 1000.0f); + float timeout = (float)BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS / 1000.0f; + uint16_t timeout_ms = BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS; + + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + timeout_ms = (uint16_t)(timeout * 1000.0f); + } int16_t x = 0; int16_t y = 0; if(self->breakout->get_motion(x, y, timeout_ms)) { @@ -215,7 +220,7 @@ mp_obj_t BreakoutPAA5100_get_motion_slow(size_t n_args, const mp_obj_t *pos_args enum { ARG_self, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_obj_new_float((float)BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS / 1000)} }, + { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -223,8 +228,13 @@ mp_obj_t BreakoutPAA5100_get_motion_slow(size_t n_args, const mp_obj_t *pos_args breakout_paa5100_BreakoutPAA5100_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_paa5100_BreakoutPAA5100_obj_t); - float timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - uint16_t timeout_ms = (uint16_t)(timeout * 1000.0f); + float timeout = (float)BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS / 1000.0f; + uint16_t timeout_ms = BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS; + + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + timeout_ms = (uint16_t)(timeout * 1000.0f); + } int16_t x = 0; int16_t y = 0; if(self->breakout->get_motion_slow(x, y, timeout_ms)) { @@ -241,7 +251,7 @@ mp_obj_t BreakoutPAA5100_frame_capture(size_t n_args, const mp_obj_t *pos_args, static const mp_arg_t allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_obj_new_float((float)BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS / 1000)} }, + { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -256,8 +266,13 @@ mp_obj_t BreakoutPAA5100_frame_capture(size_t n_args, const mp_obj_t *pos_args, mp_raise_ValueError("Supplied buffer is the wrong size for frame capture. Needs to be 1225."); } - float timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - uint16_t timeout_ms = (uint16_t)(timeout * 1000.0f); + float timeout = (float)BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS / 1000.0f; + uint16_t timeout_ms = BreakoutPAA5100::DEFAULT_MOTION_TIMEOUT_MS; + + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + timeout_ms = (uint16_t)(timeout * 1000.0f); + } uint16_t data_size = 0; uint8_t data[BreakoutPAA5100::FRAME_BYTES]; diff --git a/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp b/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp index 6b89e58f..a5fc2df8 100644 --- a/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp +++ b/micropython/modules/breakout_pmw3901/breakout_pmw3901.cpp @@ -190,7 +190,7 @@ mp_obj_t BreakoutPMW3901_get_motion(size_t n_args, const mp_obj_t *pos_args, mp_ enum { ARG_self, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_obj_new_float((float)BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS / 1000)} }, + { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -198,8 +198,13 @@ mp_obj_t BreakoutPMW3901_get_motion(size_t n_args, const mp_obj_t *pos_args, mp_ breakout_pmw3901_BreakoutPMW3901_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_pmw3901_BreakoutPMW3901_obj_t); - float timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - uint16_t timeout_ms = (uint16_t)(timeout * 1000.0f); + float timeout = (float)BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS / 1000.0f; + uint16_t timeout_ms = BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS; + + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + timeout_ms = (uint16_t)(timeout * 1000.0f); + } int16_t x = 0; int16_t y = 0; if(self->breakout->get_motion(x, y, timeout_ms)) { @@ -215,7 +220,7 @@ mp_obj_t BreakoutPMW3901_get_motion_slow(size_t n_args, const mp_obj_t *pos_args enum { ARG_self, ARG_timeout }; static const mp_arg_t allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_obj_new_float((float)BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS / 1000)} }, + { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -223,8 +228,13 @@ mp_obj_t BreakoutPMW3901_get_motion_slow(size_t n_args, const mp_obj_t *pos_args breakout_pmw3901_BreakoutPMW3901_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, breakout_pmw3901_BreakoutPMW3901_obj_t); - float timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - uint16_t timeout_ms = (uint16_t)(timeout * 1000.0f); + float timeout = (float)BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS / 1000.0f; + uint16_t timeout_ms = BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS; + + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + timeout_ms = (uint16_t)(timeout * 1000.0f); + } int16_t x = 0; int16_t y = 0; if(self->breakout->get_motion_slow(x, y, timeout_ms)) { @@ -241,7 +251,7 @@ mp_obj_t BreakoutPMW3901_frame_capture(size_t n_args, const mp_obj_t *pos_args, static const mp_arg_t allowed_args[] = { { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_obj_new_float((float)BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS / 1000)} }, + { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -256,8 +266,13 @@ mp_obj_t BreakoutPMW3901_frame_capture(size_t n_args, const mp_obj_t *pos_args, mp_raise_ValueError("Supplied buffer is the wrong size for frame capture. Needs to be 1225."); } - float timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - uint16_t timeout_ms = (uint16_t)(timeout * 1000.0f); + float timeout = (float)BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS / 1000.0f; + uint16_t timeout_ms = BreakoutPMW3901::DEFAULT_MOTION_TIMEOUT_MS; + + if (args[ARG_timeout].u_obj != mp_const_none) { + timeout = mp_obj_get_float(args[ARG_timeout].u_obj); + timeout_ms = (uint16_t)(timeout * 1000.0f); + } uint16_t data_size = 0; uint8_t data[BreakoutPMW3901::FRAME_BYTES];