Skip to content

Commit

Permalink
microbit: Add acceleromter set_range method.
Browse files Browse the repository at this point in the history
set_range is available in MakeCode and has been requested in MicroPython to
be able to create equivalent programmes and lessons.

get_range has not been requested, is not available in MakeCode, and there
is not a lot of cases where it would be useful, so it's not included here.
Getting/setting the sampling rate is similarly not exposed in MakeCode and
has never been requested yet in MakeCode nor MicroPython, so is also not
included.
  • Loading branch information
carlosperate authored and dpgeorge committed Oct 25, 2021
1 parent e9dc17f commit 3148254
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/accelerometer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ Functions
calls to some accelerometer method to do the gesture detection. Usually
gestures can be detected using a loop with a small :func:`microbit.sleep` delay.

.. py:function:: set_range(value)
Set the accelerometer sensitivity range, in g (standard gravity), to the
closest values supported by the hardware, so it rounds to either ``1``,
``2``, ``4``, or ``8`` g.

Examples
--------

Expand Down
1 change: 1 addition & 0 deletions inc/genhdr/qstrdefs.generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ 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")
QDEF(MP_QSTR_set, (const byte*)"\x27\x03" "set")
QDEF(MP_QSTR_set_range, (const byte*)"\x67\x09" "set_range")
QDEF(MP_QSTR_setattr, (const byte*)"\xd4\x07" "setattr")
QDEF(MP_QSTR_setdefault, (const byte*)"\x6c\x0a" "setdefault")
QDEF(MP_QSTR_sin, (const byte*)"\xb1\x03" "sin")
Expand Down
1 change: 1 addition & 0 deletions inc/microbit/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(microbit_panic_obj);
MP_DECLARE_CONST_FUN_OBJ_1(microbit_accelerometer_get_x_obj);
MP_DECLARE_CONST_FUN_OBJ_1(microbit_accelerometer_get_y_obj);
MP_DECLARE_CONST_FUN_OBJ_1(microbit_accelerometer_get_z_obj);
MP_DECLARE_CONST_FUN_OBJ_2(microbit_accelerometer_set_range_obj);
MP_DECLARE_CONST_FUN_OBJ_1(microbit_button_is_pressed_obj);
MP_DECLARE_CONST_FUN_OBJ_1(microbit_button_was_pressed_obj);
MP_DECLARE_CONST_FUN_OBJ_1(microbit_button_get_presses_obj);
Expand Down
1 change: 1 addition & 0 deletions inc/microbit/qstrdefsport.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Q(get_x)
Q(get_y)
Q(get_z)
Q(get_values)
Q(set_range)
Q(current_gesture)
Q(is_gesture)
Q(was_gesture)
Expand Down
8 changes: 8 additions & 0 deletions source/microbit/microbitaccelerometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ mp_obj_t microbit_accelerometer_get_strength(mp_obj_t self_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(microbit_accelerometer_get_strength_obj, microbit_accelerometer_get_strength);

mp_obj_t microbit_accelerometer_set_range(mp_obj_t self_in, mp_obj_t g) {
(void)self_in;
ubit_accelerometer->setRange(mp_obj_get_int(g));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(microbit_accelerometer_set_range_obj, microbit_accelerometer_set_range);

STATIC const qstr gesture_name_map[] = {
[MICROBIT_ACCELEROMETER_EVT_NONE] = MP_QSTR_NULL,
[MICROBIT_ACCELEROMETER_EVT_TILT_UP] = MP_QSTR_up,
Expand Down Expand Up @@ -191,6 +198,7 @@ STATIC const mp_map_elem_t microbit_accelerometer_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_is_gesture), (mp_obj_t)&microbit_accelerometer_is_gesture_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_was_gesture), (mp_obj_t)&microbit_accelerometer_was_gesture_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_gestures), (mp_obj_t)&microbit_accelerometer_get_gestures_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_range), (mp_obj_t)&microbit_accelerometer_set_range_obj },
};

STATIC MP_DEFINE_CONST_DICT(microbit_accelerometer_locals_dict, microbit_accelerometer_locals_dict_table);
Expand Down

0 comments on commit 3148254

Please sign in to comment.