Skip to content

Commit

Permalink
microbit: Add microbit.scale function.
Browse files Browse the repository at this point in the history
See issue #761.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Aug 23, 2022
1 parent 8d21387 commit 256520f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions inc/genhdr/qstrdefs.generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ QDEF(MP_QSTR_floor, (const byte*)"\x7d\x05" "floor")
QDEF(MP_QSTR_fmod, (const byte*)"\xe5\x04" "fmod")
QDEF(MP_QSTR_format, (const byte*)"\x26\x06" "format")
QDEF(MP_QSTR_frexp, (const byte*)"\x1c\x05" "frexp")
QDEF(MP_QSTR_from_, (const byte*)"\x4c\x05" "from_")
QDEF(MP_QSTR_from_bytes, (const byte*)"\x35\x0a" "from_bytes")
QDEF(MP_QSTR_fromkeys, (const byte*)"\x37\x08" "fromkeys")
QDEF(MP_QSTR_frozenset, (const byte*)"\xed\x09" "frozenset")
Expand Down Expand Up @@ -690,6 +691,7 @@ QDEF(MP_QSTR_rindex, (const byte*)"\xe9\x06" "rindex")
QDEF(MP_QSTR_round, (const byte*)"\xe7\x05" "round")
QDEF(MP_QSTR_rsplit, (const byte*)"\xa5\x06" "rsplit")
QDEF(MP_QSTR_rstrip, (const byte*)"\x3b\x06" "rstrip")
QDEF(MP_QSTR_scale, (const byte*)"\x7d\x05" "scale")
QDEF(MP_QSTR_scan, (const byte*)"\x1a\x04" "scan")
QDEF(MP_QSTR_sdiv, (const byte*)"\xcd\x04" "sdiv")
QDEF(MP_QSTR_sep, (const byte*)"\x23\x03" "sep")
Expand Down Expand Up @@ -728,6 +730,7 @@ QDEF(MP_QSTR_ticks_ms, (const byte*)"\x42\x08" "ticks_ms")
QDEF(MP_QSTR_ticks_us, (const byte*)"\x5a\x08" "ticks_us")
QDEF(MP_QSTR_time, (const byte*)"\xf0\x04" "time")
QDEF(MP_QSTR_time_pulse_us, (const byte*)"\x89\x0d" "time_pulse_us")
QDEF(MP_QSTR_to, (const byte*)"\x9e\x02" "to")
QDEF(MP_QSTR_to_bytes, (const byte*)"\xd8\x08" "to_bytes")
QDEF(MP_QSTR_trunc, (const byte*)"\x5b\x05" "trunc")
QDEF(MP_QSTR_tuple, (const byte*)"\xfd\x05" "tuple")
Expand Down
34 changes: 33 additions & 1 deletion source/microbit/modmicrobit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
extern "C" {

#include "py/nlr.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "microbit/modmicrobit.h"

Expand Down Expand Up @@ -81,6 +81,36 @@ STATIC mp_obj_t microbit_temperature(void) {
}
MP_DEFINE_CONST_FUN_OBJ_0(microbit_temperature_obj, microbit_temperature);

STATIC mp_obj_t microbit_scale(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_value, ARG_from_, ARG_to };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_from_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_to, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};

mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

// Extract from/to min/max arrays.
mp_obj_t *from_items, *to_items;
mp_obj_get_array_fixed_n(args[ARG_from_].u_obj, 2, &from_items);
mp_obj_get_array_fixed_n(args[ARG_to].u_obj, 2, &to_items);

// Extract all float values.
mp_float_t from_value = mp_obj_get_float(args[ARG_value].u_obj);
mp_float_t from_min = mp_obj_get_float(from_items[0]);
mp_float_t from_max = mp_obj_get_float(from_items[1]);
mp_float_t to_min = mp_obj_get_float(to_items[0]);
mp_float_t to_max = mp_obj_get_float(to_items[1]);

// Compute scaled value.
mp_float_t to_value = (from_value - from_min) / (from_max - from_min) * (to_max - to_min) + to_min;

return mp_obj_new_float(to_value);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(microbit_scale_obj, 0, microbit_scale);

STATIC const mp_map_elem_t microbit_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_microbit) },

Expand All @@ -103,6 +133,8 @@ STATIC const mp_map_elem_t microbit_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_panic), (mp_obj_t)&microbit_panic_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_temperature), (mp_obj_t)&microbit_temperature_obj },

{ MP_OBJ_NEW_QSTR(MP_QSTR_scale), (mp_obj_t)&microbit_scale_obj },

{ MP_OBJ_NEW_QSTR(MP_QSTR_pin0), (mp_obj_t)&microbit_p0_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_pin1), (mp_obj_t)&microbit_p1_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_pin2), (mp_obj_t)&microbit_p2_obj },
Expand Down

0 comments on commit 256520f

Please sign in to comment.