Skip to content

Commit 0f9e704

Browse files
committed
extmod/modiodevices/LUMPDevices: add ID
This adds a readonly attribute for the device ID. Combined with read and write, this gives access to all runtime information required to build device-specific sensor classes.
1 parent 2bff13d commit 0f9e704

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

extmod/modiodevices.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
typedef struct _iodevices_LUMPDevice_obj_t {
2626
mp_obj_base_t base;
2727
pbdevice_t *pbdev;
28+
mp_obj_t id;
2829
} iodevices_LUMPDevice_obj_t;
2930

3031
// pybricks.iodevices.LUMPDevice.__init__
@@ -39,14 +40,16 @@ STATIC mp_obj_t iodevices_LUMPDevice_make_new(const mp_obj_type_t *type, size_t
3940

4041
self->pbdev = pbdevice_get_device(port_num, PBIO_IODEV_TYPE_ID_LUMP_UART);
4142

42-
#if PYBRICKS_PY_PUPDEVICES
43-
// FIXME: Read sensor capability flag to see which sensor uses power. As
44-
// a precaution, only enable power for selected known sensors for now.
4543
pbio_port_t _port;
4644
pbio_iodev_type_id_t id;
4745
uint8_t curr_mode;
4846
uint8_t num_values;
4947
pbdevice_get_info(self->pbdev, &_port, &id, &curr_mode, &num_values);
48+
self->id = mp_obj_new_int(id);
49+
50+
#if PYBRICKS_PY_PUPDEVICES
51+
// FIXME: Read sensor capability flag to see which sensor uses power. As
52+
// a precaution, only enable power for selected known sensors for now.
5053
bool power = (id == PBIO_IODEV_TYPE_ID_SPIKE_COLOR_SENSOR || id == PBIO_IODEV_TYPE_ID_SPIKE_ULTRASONIC_SENSOR);
5154
pbdevice_set_power_supply(self->pbdev, power);
5255
#endif // PYBRICKS_PY_PUPDEVICES
@@ -113,6 +116,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(iodevices_LUMPDevice_write_obj, 1, iodevices_LUMPDevi
113116
STATIC const mp_rom_map_elem_t iodevices_LUMPDevice_locals_dict_table[] = {
114117
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&iodevices_LUMPDevice_read_obj) },
115118
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&iodevices_LUMPDevice_write_obj)},
119+
{ MP_ROM_QSTR(MP_QSTR_ID), MP_ROM_ATTRIBUTE_OFFSET(iodevices_LUMPDevice_obj_t, id) },
116120
};
117121
STATIC MP_DEFINE_CONST_DICT(iodevices_LUMPDevice_locals_dict, iodevices_LUMPDevice_locals_dict_table);
118122

0 commit comments

Comments
 (0)