• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pybricks / pybricks-micropython / 6675885095

28 Oct 2023 08:38AM UTC coverage: 56.053% (+10.0%) from 46.074%
6675885095

push

github

laurensvalk
pybricks.hubs.MoveHub: Use standard hub init.

The Move Hub cannot have true vector axes, but we can still use this API to initialize the hub using numeric indices.

This ensures we can use the custom orientation not just in tilt, but also in acceleration like we do on other hubs.

3616 of 6451 relevant lines covered (56.05%)

20895680.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pybricks/common/pb_type_system.c
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2022 The Pybricks Authors
3

4
#include "py/mpconfig.h"
5

6
#if PYBRICKS_PY_COMMON && PYBRICKS_PY_COMMON_SYSTEM
7

8
#include <string.h>
9

10
#include <pbdrv/bluetooth.h>
11
#include <pbsys/program_load.h>
12

13
#include "py/obj.h"
14
#include "py/objstr.h"
15
#include "py/runtime.h"
16

17
#include <pybricks/common.h>
18
#include <pybricks/util_pb/pb_error.h>
19
#include <pybricks/util_mp/pb_kwarg_helper.h>
20
#include <pybricks/util_mp/pb_obj_helper.h>
21

22
STATIC mp_obj_t pb_type_System_name(void) {
×
23
    const char *hub_name = pbdrv_bluetooth_get_hub_name();
×
24
    return mp_obj_new_str(hub_name, strlen(hub_name));
×
25
}
26
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_name_obj, pb_type_System_name);
27

28
#if PBDRV_CONFIG_RESET
29

30
#include <pbdrv/reset.h>
31

32
STATIC mp_obj_t pb_type_System_reset_reason(void) {
33
    pbdrv_reset_reason_t reason = pbdrv_reset_get_reason();
34
    return MP_OBJ_NEW_SMALL_INT(reason);
35
}
36
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_reset_reason_obj, pb_type_System_reset_reason);
37

38
#endif // PBDRV_CONFIG_RESET
39

40
#if PBIO_CONFIG_ENABLE_SYS
41

42
#include <pbsys/status.h>
43
#include <pbsys/program_stop.h>
44

45
#include <pybricks/parameters.h>
46

47
STATIC mp_obj_t pb_type_System_set_stop_button(mp_obj_t buttons_in) {
×
48
    pbio_button_flags_t buttons = 0;
×
49

50
    if (mp_obj_is_true(buttons_in)) {
×
51
        #if PYBRICKS_PY_COMMON_KEYPAD_HUB_BUTTONS > 1
52
        nlr_buf_t nlr;
53
        if (nlr_push(&nlr) == 0) {
54
            // try an iterator first in case there are multiple buttons
55
            mp_obj_t iter = mp_getiter(buttons_in, NULL);
56
            mp_obj_t item;
57
            while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
58
                buttons |= pb_type_enum_get_value(item, &pb_enum_type_Button);
59
            }
60
            nlr_pop();
61
        } else {
62
            // mp_getiter() will raise an exception if it is not an iter, so we
63
            // will end up here in that case, where we are expecting a single
64
            // button enum value. Technically there could be other error that
65
            // get us here, but they should be rare and will likely be a ValueError
66
            // which is the same error that will be raised here.
67
            buttons = pb_type_enum_get_value(buttons_in, &pb_enum_type_Button);
68
        }
69
        #else // PYBRICKS_PY_COMMON_KEYPAD_HUB_BUTTONS > 1
70
        buttons = PBIO_BUTTON_CENTER;
71
        #endif // PYBRICKS_PY_COMMON_KEYPAD_HUB_BUTTONS > 1
72
    }
73

74
    pbsys_program_stop_set_buttons(buttons);
×
75

76
    return mp_const_none;
×
77
}
78
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pb_type_System_set_stop_button_obj, pb_type_System_set_stop_button);
79

80
STATIC mp_obj_t pb_type_System_shutdown(void) {
×
81

82
    // Start shutdown.
83
    pbsys_status_set(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST);
×
84

85
    // Keep running MicroPython until we are stopped.
86
    for (;;) {
×
87
        MICROPY_EVENT_POLL_HOOK;
×
88
    }
89

90
    return mp_const_none;
91
}
92
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_shutdown_obj, pb_type_System_shutdown);
93

94
STATIC mp_obj_t pb_type_System_storage(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
×
95
    PB_PARSE_ARGS_FUNCTION(n_args, pos_args, kw_args,
×
96
        PB_ARG_REQUIRED(offset),
97
        PB_ARG_DEFAULT_NONE(read),
98
        PB_ARG_DEFAULT_NONE(write));
×
99

100
    // Get offset and confirm integer type.
101
    mp_int_t offset = mp_obj_get_int(offset_in);
×
102

103
    // Handle read.
104
    if (read_in != mp_const_none && write_in == mp_const_none) {
×
105
        byte *data;
×
106
        mp_uint_t size = mp_obj_get_int(read_in);
×
107
        pb_assert(pbsys_program_load_get_user_data(offset, &data, size));
×
108
        return mp_obj_new_bytes(data, size);
×
109
    }
110

111
    // Handle write.
112
    if (write_in != mp_const_none && read_in == mp_const_none) {
×
113
        mp_buffer_info_t bufinfo;
×
114
        mp_get_buffer_raise(write_in, &bufinfo, MP_BUFFER_READ);
×
115

116
        pb_assert(pbsys_program_load_set_user_data(offset, bufinfo.buf, bufinfo.len));
×
117

118
        return mp_const_none;
×
119
    }
120

121
    mp_raise_TypeError(MP_ERROR_TEXT("Must set either read (int) or write (bytes)."));
×
122
}
123
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_System_storage_obj, 0, pb_type_System_storage);
124

125
#endif // PBIO_CONFIG_ENABLE_SYS
126

127
// dir(pybricks.common.System)
128
STATIC const mp_rom_map_elem_t common_System_locals_dict_table[] = {
129
    { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pb_type_System_name_obj) },
130
    #if PBDRV_CONFIG_RESET
131
    { MP_ROM_QSTR(MP_QSTR_reset_reason), MP_ROM_PTR(&pb_type_System_reset_reason_obj) },
132
    #endif // PBDRV_CONFIG_RESET
133
    #if PBIO_CONFIG_ENABLE_SYS
134
    { MP_ROM_QSTR(MP_QSTR_set_stop_button), MP_ROM_PTR(&pb_type_System_set_stop_button_obj) },
135
    { MP_ROM_QSTR(MP_QSTR_shutdown), MP_ROM_PTR(&pb_type_System_shutdown_obj) },
136
    { MP_ROM_QSTR(MP_QSTR_storage), MP_ROM_PTR(&pb_type_System_storage_obj) },
137
    #endif
138
};
139
STATIC MP_DEFINE_CONST_DICT(common_System_locals_dict, common_System_locals_dict_table);
140

141
// type(pybricks.common.System) but implemented as module for reduced build size.
142
// REVISIT: Make implementation consistent across modules/singletons: https://github.com/pybricks/support/issues/840
143
const mp_obj_module_t pb_type_System = {
144
    .base = { &mp_type_module },
145
    .globals = (mp_obj_dict_t *)&common_System_locals_dict,
146
};
147

148
#endif // PYBRICKS_PY_COMMON && PYBRICKS_PY_COMMON_SYSTEM
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc