• 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

96.15
/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 <pbio/version.h>
14

15
#include <pybricks/common.h>
16
#include <pybricks/hubs.h>
17
#include <pybricks/parameters.h>
18
#include <pybricks/pupdevices.h>
19
#include <pybricks/common/pb_type_device.h>
20
#include <pybricks/tools.h>
21

22
#include "genhdr/mpversion.h"
23

24
STATIC const MP_DEFINE_STR_OBJ(pybricks_info_hub_obj, PYBRICKS_HUB_NAME);
25
STATIC const MP_DEFINE_STR_OBJ(pybricks_info_release_obj, PBIO_VERSION_STR);
26
STATIC const MP_DEFINE_STR_OBJ(pybricks_info_version_obj, MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE);
27

28
STATIC const mp_rom_obj_tuple_t pybricks_info_obj = {
29
    {&mp_type_tuple},
30
    3,
31
    {
32
        MP_ROM_PTR(&pybricks_info_hub_obj),
33
        MP_ROM_PTR(&pybricks_info_release_obj),
34
        MP_ROM_PTR(&pybricks_info_version_obj),
35
    }
36
};
37

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

47
STATIC const mp_rom_map_elem_t pybricks_globals_table[] = {
48
    { MP_ROM_QSTR(MP_QSTR___name__),            MP_ROM_QSTR(MP_QSTR_pybricks) },
49
    { MP_ROM_QSTR(MP_QSTR_version),             MP_ROM_PTR(&pybricks_info_obj)},
50
    #if MICROPY_MODULE_ATTR_DELEGATION
51
    MP_MODULE_ATTR_DELEGATION_ENTRY(&pb_package_pybricks_attr),
52
    #endif
53
};
54
STATIC MP_DEFINE_CONST_DICT(pb_package_pybricks_globals, pybricks_globals_table);
55

56
const mp_obj_module_t pb_package_pybricks = {
57
    .base = { &mp_type_module },
58
    .globals = (mp_obj_dict_t *)&pb_package_pybricks_globals,
59
};
60

61
#if PYBRICKS_RUNS_ON_EV3DEV
62
// ev3dev extends the C module in Python
63
MP_REGISTER_MODULE(MP_QSTR_pybricks_c, pb_package_pybricks);
64
#else
65
MP_REGISTER_MODULE(MP_QSTR_pybricks, pb_package_pybricks);
66
#endif
67

68
#if PYBRICKS_OPT_COMPILER
69
/**
70
 * Import all MicroPython modules and import * from Pybricks modules.
71
 */
72
static void pb_package_import_all(void) {
21✔
73

74
    // Go through all modules in mp_builtin_module_map.
75
    for (size_t i = 0; i < mp_builtin_module_map.used; i++) {
588✔
76
        // This is a constant map of modules, so we can skip checks for
77
        // filled slots or confirming that we have module types.
78
        qstr module_name = MP_OBJ_QSTR_VALUE(mp_builtin_module_map.table[i].key);
567✔
79
        mp_obj_t module = mp_builtin_module_map.table[i].value;
567✔
80
        if (!strncmp("pybricks", qstr_str(module_name), 8)) {
567✔
81
            // Import everything from a Pybricks module.
82
            mp_import_all(module);
210✔
83
        } else {
84
            // Otherwise import just the module.
85
            mp_store_global(module_name, module);
357✔
86
        }
87
    }
88

89
    #if PYBRICKS_PY_HUBS
90
    // Initialize hub instance
91
    const mp_obj_t args;
21✔
92
    mp_store_name(MP_QSTR_hub, MP_OBJ_TYPE_GET_SLOT(&pb_type_ThisHub, make_new)(&pb_type_ThisHub, 0, 0, &args));
21✔
93
    #endif
94
}
21✔
95

96
/**
97
 * Prepares Pybricks MicroPython environment.
98
 *
99
 * @param [in]  import_all      Whether to import * from all pybricks.* modules.
100
 */
101

102
void pb_package_pybricks_init(bool import_all) {
21✔
103

104
    nlr_buf_t nlr;
21✔
105
    if (nlr_push(&nlr) == 0) {
21✔
106
        // Initialize the package.
107
        pb_type_Color_reset();
21✔
108
        pb_module_tools_init();
21✔
109
        // Import all if requested.
110
        if (import_all) {
21✔
111
            pb_package_import_all();
21✔
112
        }
113
        nlr_pop();
21✔
114
    } else {
115
        // Print initialization or import exception.
116
        mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
×
117
    }
118
}
21✔
119
#else
120
// Cheaper implementation of the above. This is sufficient on builds without
121
// the compiler, since the following deterministic action should not raise
122
// exceptions as it is only called before executing anything else.
123
void pb_package_pybricks_init(bool import_all) {
124
    pb_type_Color_reset();
125
    pb_module_tools_init();
126
}
127
#endif // PYBRICKS_OPT_COMPILER
128

129
// REVISIT: move these to object finalizers if we enable finalizers in the GC
130
void pb_package_pybricks_deinit(void) {
21✔
131
    #if PYBRICKS_PY_COMMON_BLE
132
    pb_type_BLE_cleanup();
133
    #endif
134
    // Disconnect from remote.
135
    #if PYBRICKS_PY_PUPDEVICES
136
    pb_type_Remote_cleanup();
21✔
137
    #endif // PYBRICKS_PY_PUPDEVICES
138
}
21✔
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

© 2025 Coveralls, Inc