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

pybricks / pybricks-micropython / 5044552036

pending completion
5044552036

Pull #169

github

GitHub
Merge 98773a05d into edaf7e51e
Pull Request #169: Refactor src/uartdev for async and code size

69 of 283 new or added lines in 13 files covered. (24.38%)

8 existing lines in 7 files now uncovered.

3169 of 5993 relevant lines covered (52.88%)

32960120.55 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

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

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

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

NEW
33
    int8_t brightness_values[4];
×
34

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

55
    // Set the brightness values
NEW
56
    pb_pup_device_set_data(self->iodev, self->light_mode, (uint8_t *)brightness_values);
×
57

58
    return mp_const_none;
×
59
}
60
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(common_LightArray_on_obj, 1, common_LightArray_on);
61

62
// pybricks._common.LightArray.off
63
STATIC mp_obj_t common_LightArray_off(mp_obj_t self_in) {
×
NEW
64
    const mp_obj_t pos_args[] = {self_in, MP_OBJ_NEW_SMALL_INT(0) };
×
NEW
65
    common_LightArray_on(MP_ARRAY_SIZE(pos_args), pos_args, NULL);
×
UNCOV
66
    return mp_const_none;
×
67
}
68
STATIC MP_DEFINE_CONST_FUN_OBJ_1(common_LightArray_off_obj, common_LightArray_off);
69

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

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

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

92
#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