Skip to content

Commit 104deaa

Browse files
maass-hamburgdpgeorge
authored andcommitted
zephyr: Use nodelabel when printing device name.
This gives a more user-friendly name when the Python object (eg Pin) is printed. If the nodelabel is unavailable then it uses `dev->name` as a fallback. Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
1 parent 0dac591 commit 104deaa

7 files changed

Lines changed: 28 additions & 4 deletions

File tree

ports/zephyr/machine_i2c.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ typedef struct _machine_hard_i2c_obj_t {
4949

5050
static void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
5151
machine_hard_i2c_obj_t *self = self_in;
52-
mp_printf(print, "%s", self->dev->name);
52+
const char *name = zephyr_device_get_name(self->dev);
53+
mp_printf(print, "<I2C %s>", name);
5354
}
5455

5556
mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {

ports/zephyr/machine_pin.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ static void gpio_callback_handler(const struct device *port, struct gpio_callbac
6969

7070
static void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
7171
machine_pin_obj_t *self = self_in;
72-
mp_printf(print, "<Pin %p %d>", self->port, self->pin);
72+
const char *port_name = zephyr_device_get_name(self->port);
73+
mp_printf(print, "<Pin %s %d>", port_name, self->pin);
7374
}
7475

7576
// pin.init(mode, pull=None, *, value)

ports/zephyr/machine_pwm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ static void configure_pwm(machine_pwm_obj_t *self) {
6969
static void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in,
7070
mp_print_kind_t kind) {
7171
machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
72-
mp_printf(print, "<PWM Controller %p channel=%d ", self->pwm, self->channel);
72+
const char *name = zephyr_device_get_name(self->pwm);
73+
mp_printf(print, "<PWM Controller %s channel=%d ", name, self->channel);
7374

7475
if (self->duty_ns != VALUE_NOT_SET) {
7576
mp_printf(print, " duty_ns=%d", self->duty_ns);

ports/zephyr/machine_spi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ typedef struct _machine_hard_spi_obj_t {
5555

5656
static void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
5757
machine_hard_spi_obj_t *self = self_in;
58+
const char *name = zephyr_device_get_name(self->dev);
5859
mp_printf(print, "SPI(%s, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%s)",
59-
self->dev->name,
60+
name,
6061
self->config.frequency,
6162
(self->config.operation & 0x2) >> 1,
6263
(self->config.operation & 0x4) >> 2,

ports/zephyr/modzsensor.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ static mp_obj_t sensor_make_new(const mp_obj_type_t *type, size_t n_args, size_t
4646
return MP_OBJ_FROM_PTR(o);
4747
}
4848

49+
static void sensor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
50+
mp_obj_sensor_t *self = self_in;
51+
const char *name = zephyr_device_get_name(self->dev);
52+
mp_printf(print, "<Sensor %s>", name);
53+
}
54+
4955
static mp_obj_t sensor_measure(mp_obj_t self_in) {
5056
mp_obj_sensor_t *self = MP_OBJ_TO_PTR(self_in);
5157
int st = sensor_sample_fetch(self->dev);
@@ -173,6 +179,7 @@ static MP_DEFINE_CONST_OBJ_TYPE(
173179
MP_QSTR_Sensor,
174180
MP_TYPE_FLAG_NONE,
175181
make_new, sensor_make_new,
182+
print, sensor_print,
176183
locals_dict, &sensor_locals_dict
177184
);
178185

ports/zephyr/zephyr_device.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ const struct device *zephyr_device_find(mp_obj_t name) {
4747

4848
return dev;
4949
}
50+
51+
const char *zephyr_device_get_name(const struct device *dev) {
52+
#ifdef CONFIG_DEVICE_DT_METADATA
53+
const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
54+
55+
if (nl != NULL && nl->num_nodelabels > 0) {
56+
return nl->nodelabels[0];
57+
}
58+
#endif
59+
60+
return dev->name;
61+
}

ports/zephyr/zephyr_device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@
2828
#include "py/obj.h"
2929

3030
const struct device *zephyr_device_find(mp_obj_t name);
31+
const char *zephyr_device_get_name(const struct device *dev);

0 commit comments

Comments
 (0)