• 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_lightarray.c
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2023 The Pybricks Authors
3

4
#include "py/mpconfig.h"
5

6
#if PYBRICKS_PY_COMMON_LIGHT_ARRAY
7

8
#include "py/obj.h"
9

10
#include <pybricks/common.h>
11

12
#include <pybricks/pupdevices.h>
13
#include <pybricks/common/pb_type_device.h>
14

15
#include <pybricks/util_pb/pb_error.h>
16
#include <pybricks/util_mp/pb_obj_helper.h>
17
#include <pybricks/util_mp/pb_kwarg_helper.h>
18
#include <pybricks/util_mp/pb_obj_helper.h>
19

20
// pybricks._common.Light class object
21
typedef struct _common_LightArray_obj_t {
22
    mp_obj_base_t base;
23
    pb_type_device_obj_base_t *sensor;
24
    uint8_t light_mode;
25
    uint8_t number_of_lights;
26
} common_LightArray_obj_t;
27

28
// pybricks._common.LightArray.on
29
STATIC mp_obj_t common_LightArray_on(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
×
30
    PB_PARSE_ARGS_METHOD(n_args, pos_args, kw_args,
×
31
        common_LightArray_obj_t, self,
32
        PB_ARG_DEFAULT_INT(brightness, 100));
×
33

34
    int8_t brightness_values[4];
×
35

36
    // Given an integer, make all lights the same brightness.
37
    if (mp_obj_is_int(brightness_in)) {
×
38
        int32_t b = pb_obj_get_pct(brightness_in);
×
39
        for (uint8_t i = 0; i < self->number_of_lights; i++) {
×
40
            brightness_values[i] = b;
×
41
        }
42
    }
43
    // Otherwise, get each brightness value from list or tuple.
44
    else {
45
        mp_obj_t *brightness_objects;
×
46
        size_t num_values;
×
47
        mp_obj_get_array(brightness_in, &num_values, &brightness_objects);
×
48
        if (num_values != self->number_of_lights) {
×
49
            pb_assert(PBIO_ERROR_INVALID_ARG);
×
50
        }
51
        for (uint8_t i = 0; i < self->number_of_lights; i++) {
×
52
            brightness_values[i] = pb_obj_get_pct(brightness_objects[i]);
×
53
        }
54
    }
55

56
    // Set the brightness values and wait or await it.
57
    return pb_type_device_set_data(self->sensor, self->light_mode, brightness_values, self->number_of_lights);
×
58
}
59
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_LightArray_on_obj, 1, common_LightArray_on);
60

61
// pybricks._common.LightArray.off
62
STATIC mp_obj_t common_LightArray_off(mp_obj_t self_in) {
×
63
    common_LightArray_obj_t *self = MP_OBJ_TO_PTR(self_in);
×
64
    int8_t brightness_values[4] = { };
×
65
    return pb_type_device_set_data(self->sensor, self->light_mode, brightness_values, self->number_of_lights);
×
66
}
67
STATIC MP_DEFINE_CONST_FUN_OBJ_1(common_LightArray_off_obj, common_LightArray_off);
68

69
// dir(pybricks.builtins.LightArray)
70
STATIC const mp_rom_map_elem_t common_LightArray_locals_dict_table[] = {
71
    { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&common_LightArray_on_obj) },
72
    { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&common_LightArray_off_obj) },
73
};
74
STATIC MP_DEFINE_CONST_DICT(common_LightArray_locals_dict, common_LightArray_locals_dict_table);
75

76
// type(pybricks.builtins.LightArray)
77
STATIC MP_DEFINE_CONST_OBJ_TYPE(pb_type_LightArray,
78
    MP_QSTR_LightArray,
79
    MP_TYPE_FLAG_NONE,
80
    locals_dict, &common_LightArray_locals_dict);
81

82
// pybricks._common.LightArray.__init__
83
mp_obj_t common_LightArray_obj_make_new(pb_type_device_obj_base_t *sensor, uint8_t light_mode, uint8_t number_of_lights) {
×
84
    common_LightArray_obj_t *light = mp_obj_malloc(common_LightArray_obj_t, &pb_type_LightArray);
×
85
    light->sensor = sensor;
×
86
    light->light_mode = light_mode;
×
87
    light->number_of_lights = number_of_lights;
×
88
    return MP_OBJ_FROM_PTR(light);
×
89
}
90

91
#endif // PYBRICKS_PY_COMMON_LIGHT_ARRAY
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