Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add acceleromter range and rate acccessors (in progress). #266

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably add more info about what changing the range actually does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, include the possible values to set.

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 @@ -187,6 +194,7 @@ STATIC const mp_map_elem_t microbit_accelerometer_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_z), (mp_obj_t)&microbit_accelerometer_get_z_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_values), (mp_obj_t)&microbit_accelerometer_get_values_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_strength), (mp_obj_t)&microbit_accelerometer_get_strength_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_range), (mp_obj_t)&microbit_accelerometer_set_range_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_current_gesture), (mp_obj_t)&microbit_accelerometer_current_gesture_obj },
{ 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 },
Expand Down