Skip to content

Commit ad3142e

Browse files
craigsammisutherlandddemidov
authored andcommitted
Added a function to list all the connected sensors. (#294)
* Added a function to list all the connected sensors.
1 parent bc16e37 commit ad3142e

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

ev3dev/core.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,18 +930,17 @@ def list_motors(name_pattern=Motor.SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
930930
931931
Parameters:
932932
name_pattern: pattern that device name should match.
933-
For example, 'sensor*' or 'motor*'. Default value: '*'.
933+
For example, 'motor*'. Default value: '*'.
934934
keyword arguments: used for matching the corresponding device
935935
attributes. For example, driver_name='lego-ev3-l-motor', or
936936
address=['outB', 'outC']. When argument value
937937
is a list, then a match against any entry of the list is
938938
enough.
939939
"""
940-
classpath = abspath(Device.DEVICE_ROOT_PATH + '/' + Motor.SYSTEM_CLASS_NAME)
940+
class_path = abspath(Device.DEVICE_ROOT_PATH + '/' + Motor.SYSTEM_CLASS_NAME)
941941

942942
return (Motor(name_pattern=name, name_exact=True)
943-
for name in list_device_names(classpath, name_pattern, **kwargs))
944-
943+
for name in list_device_names(class_path, name_pattern, **kwargs))
945944

946945
# ~autogen generic-class classes.largeMotor>currentClass
947946

@@ -1769,6 +1768,24 @@ def bin_data(self, fmt=None):
17691768

17701769
return unpack(fmt, raw)
17711770

1771+
def list_sensors(name_pattern=Sensor.SYSTEM_DEVICE_NAME_CONVENTION, **kwargs):
1772+
"""
1773+
This is a generator function that enumerates all sensors that match the
1774+
provided arguments.
1775+
1776+
Parameters:
1777+
name_pattern: pattern that device name should match.
1778+
For example, 'sensor*'. Default value: '*'.
1779+
keyword arguments: used for matching the corresponding device
1780+
attributes. For example, driver_name='lego-ev3-touch', or
1781+
address=['in1', 'in3']. When argument value is a list,
1782+
then a match against any entry of the list is enough.
1783+
"""
1784+
class_path = abspath(Device.DEVICE_ROOT_PATH + '/' + Sensor.SYSTEM_CLASS_NAME)
1785+
return (Sensor(name_pattern=name, name_exact=True)
1786+
for name in list_device_names(class_path, name_pattern, **kwargs))
1787+
1788+
17721789
# ~autogen generic-class classes.i2cSensor>currentClass
17731790

17741791
class I2cSensor(Sensor):

0 commit comments

Comments
 (0)