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

pybricks / pybricks-micropython / 18399229281

10 Oct 2025 07:09AM UTC coverage: 59.794%. Remained the same
18399229281

push

github

laurensvalk
pbio/sys/hmi_lcd: Show program name.

See https://github.com/pybricks/support/issues/2360

4301 of 7193 relevant lines covered (59.79%)

20154529.21 hits per line

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

95.65
/pybricks/pybricks.c
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2023 The Pybricks Authors
3

4
#include <string.h>
5

6
#include "py/mpconfig.h"
7
#include "py/obj.h"
8
#include "py/objmodule.h"
9
#include "py/objstr.h"
10
#include "py/objtuple.h"
11
#include "py/runtime.h"
12

13
#include <pbdrv/bluetooth.h>
14
#include <pbio/version.h>
15
#include <pbsys/host.h>
16
#include <pbsys/status.h>
17

18
#include <pybricks/common.h>
19
#include <pybricks/hubs.h>
20
#include <pybricks/parameters.h>
21
#include <pybricks/pupdevices.h>
22
#include <pybricks/common/pb_type_device.h>
23
#include <pybricks/tools.h>
24

25
#include "genhdr/mpversion.h"
26

27
static const MP_DEFINE_STR_OBJ(pybricks_info_hub_obj, PYBRICKS_HUB_NAME);
28
static const MP_DEFINE_STR_OBJ(pybricks_info_release_obj, PBIO_VERSION_STR);
29
static const MP_DEFINE_STR_OBJ(pybricks_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
30

31
static const mp_rom_obj_tuple_t pybricks_info_obj = {
32
    {&mp_type_tuple},
33
    3,
34
    {
35
        MP_ROM_PTR(&pybricks_info_hub_obj),
36
        MP_ROM_PTR(&pybricks_info_release_obj),
37
        MP_ROM_PTR(&pybricks_info_version_obj),
38
    }
39
};
40

41
#if MICROPY_MODULE_ATTR_DELEGATION
42
void pb_package_pybricks_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
28✔
43
    // This will get called when external imports tries to store the module
44
    // as an attribute to this package. This is not currently supported, but
45
    // it should not cause an exception, so indicate success.
46
    dest[0] = MP_OBJ_NULL;
28✔
47
}
28✔
48
#endif
49

50
#if MICROPY_MODULE_BUILTIN_SUBPACKAGES
51
#if PYBRICKS_PY_EXPERIMENTAL
52
extern const mp_obj_module_t pb_module_experimental;
53
#endif
54
#if PYBRICKS_PY_HUBS
55
extern const mp_obj_module_t pb_module_hubs;
56
#endif
57
#if PYBRICKS_PY_NXTDEVICES
58
extern const mp_obj_module_t pb_module_nxtdevices;
59
#endif
60
#if PYBRICKS_PY_EV3DEVICES
61
extern const mp_obj_module_t pb_module_ev3devices;
62
#endif
63
#if PYBRICKS_PY_PUPDEVICES
64
extern const mp_obj_module_t pb_module_pupdevices;
65
#endif
66
#if PYBRICKS_PY_IODEVICES
67
extern const mp_obj_module_t pb_module_iodevices;
68
#endif
69
#if PYBRICKS_PY_PARAMETERS
70
extern const mp_obj_module_t pb_module_parameters;
71
#endif
72
#if PYBRICKS_PY_TOOLS
73
extern const mp_obj_module_t pb_module_tools;
74
#endif
75
#if PYBRICKS_PY_ROBOTICS
76
extern const mp_obj_module_t pb_module_robotics;
77
#endif
78
#endif
79

80
static const mp_rom_map_elem_t pybricks_globals_table[] = {
81
    { MP_ROM_QSTR(MP_QSTR___name__),            MP_ROM_QSTR(MP_QSTR_pybricks) },
82
    { MP_ROM_QSTR(MP_QSTR_version),             MP_ROM_PTR(&pybricks_info_obj)},
83
    #if MICROPY_MODULE_BUILTIN_SUBPACKAGES
84
    #if PYBRICKS_PY_EXPERIMENTAL
85
    { MP_ROM_QSTR(MP_QSTR_experimental), MP_ROM_PTR(&pb_module_experimental) },
86
    #endif
87
    #if PYBRICKS_PY_HUBS
88
    { MP_ROM_QSTR(MP_QSTR_hubs), MP_ROM_PTR(&pb_module_hubs) },
89
    #endif
90
    #if PYBRICKS_PY_NXTDEVICES
91
    { MP_ROM_QSTR(MP_QSTR_nxtdevices), MP_ROM_PTR(&pb_module_nxtdevices) },
92
    #endif
93
    #if PYBRICKS_PY_EV3DEVICES
94
    { MP_ROM_QSTR(MP_QSTR_ev3devices), MP_ROM_PTR(&pb_module_ev3devices) },
95
    #endif
96
    #if PYBRICKS_PY_PUPDEVICES
97
    { MP_ROM_QSTR(MP_QSTR_pupdevices), MP_ROM_PTR(&pb_module_pupdevices) },
98
    #endif
99
    #if PYBRICKS_PY_IODEVICES
100
    { MP_ROM_QSTR(MP_QSTR_iodevices), MP_ROM_PTR(&pb_module_iodevices) },
101
    #endif
102
    #if PYBRICKS_PY_PARAMETERS
103
    { MP_ROM_QSTR(MP_QSTR_parameters), MP_ROM_PTR(&pb_module_parameters) },
104
    #endif
105
    #if PYBRICKS_PY_TOOLS
106
    { MP_ROM_QSTR(MP_QSTR_tools), MP_ROM_PTR(&pb_module_tools) },
107
    #endif
108
    #if PYBRICKS_PY_ROBOTICS
109
    { MP_ROM_QSTR(MP_QSTR_robotics), MP_ROM_PTR(&pb_module_robotics) },
110
    #endif
111
    #endif
112
};
113
static MP_DEFINE_CONST_DICT(pb_package_pybricks_globals, pybricks_globals_table);
114

115
const mp_obj_module_t pb_package_pybricks = {
116
    .base = { &mp_type_module },
117
    .globals = (mp_obj_dict_t *)&pb_package_pybricks_globals,
118
};
119

120
MP_REGISTER_MODULE(MP_QSTR_pybricks, pb_package_pybricks);
121
MP_REGISTER_MODULE_DELEGATION(pb_package_pybricks, pb_package_pybricks_attr);
122

123
#if PYBRICKS_OPT_COMPILER
124
/**
125
 * Import all MicroPython modules and import * from Pybricks modules.
126
 */
127
static void pb_package_import_all(void) {
30✔
128

129
    // Go through all modules in mp_builtin_module_map.
130
    for (size_t i = 0; i < mp_builtin_module_map.used; i++) {
360✔
131
        // This is a constant map of modules, so we can skip checks for
132
        // filled slots or confirming that we have module types.
133
        qstr module_name = MP_OBJ_QSTR_VALUE(mp_builtin_module_map.table[i].key);
330✔
134
        mp_obj_t module = mp_builtin_module_map.table[i].value;
330✔
135
        if (!strncmp("pybricks", qstr_str(module_name), 8)) {
330✔
136
            // Import everything from a Pybricks module.
137
            mp_import_all(module);
30✔
138
        } else {
139
            // Otherwise import just the module.
140
            mp_store_global(module_name, module);
300✔
141
        }
142
    }
143

144
    #if PYBRICKS_PY_HUBS
145
    // Initialize hub instance
146
    const mp_obj_t args;
30✔
147
    mp_store_name(MP_QSTR_hub, MP_OBJ_TYPE_GET_SLOT(&pb_type_ThisHub, make_new)(&pb_type_ThisHub, 0, 0, &args));
30✔
148
    #endif
149
}
30✔
150

151
/**
152
 * Prepares Pybricks MicroPython environment.
153
 *
154
 * @param [in]  import_all      Whether to import * from all pybricks.* modules.
155
 */
156

157
void pb_package_pybricks_init(bool import_all) {
30✔
158

159
    nlr_buf_t nlr;
30✔
160
    if (nlr_push(&nlr) == 0) {
30✔
161
        // Initialize the package.
162
        #if PYBRICKS_PY_PARAMETERS
163
        pb_type_Color_reset();
30✔
164
        #endif
165
        #if PYBRICKS_PY_TOOLS
166
        pb_module_tools_init();
30✔
167
        #endif
168
        // Import all if requested.
169
        if (import_all) {
30✔
170
            pb_package_import_all();
30✔
171
        }
172
        nlr_pop();
30✔
173
    } else {
174
        // Print initialization or import exception.
175
        mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
×
176
    }
177
}
30✔
178
#else
179
// Cheaper implementation of the above. This is sufficient on builds without
180
// the compiler, since the following deterministic action should not raise
181
// exceptions as it is only called before executing anything else.
182
void pb_package_pybricks_init(bool import_all) {
183
    pb_type_Color_reset();
184
    pb_module_tools_init();
185
}
186
#endif // PYBRICKS_OPT_COMPILER
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