Skip to content

Commit ba7540b

Browse files
committed
Implement Device.device_index
ref #15
1 parent 90d0853 commit ba7540b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

ev3dev.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import array
3636
import mmap
3737
import ctypes
38+
import re
3839
from os.path import abspath
3940
from PIL import Image, ImageDraw
4041
from struct import pack, unpack
@@ -108,6 +109,8 @@ class Device(object):
108109

109110
DEVICE_ROOT_PATH = '/sys/class'
110111

112+
_DEVICE_INDEX = re.compile(r'^.*(?P<idx>\d+)$')
113+
111114
def __init__(self, class_name, name='*', **kwargs ):
112115
"""Spin through the Linux sysfs class for the device type and find
113116
a device that matches the provided name and attributes (if any).
@@ -140,6 +143,13 @@ def __init__(self, class_name, name='*', **kwargs ):
140143
# See if requested attributes match:
141144
if all([self._matches(k, kwargs[k]) for k in kwargs]):
142145
self.connected = True
146+
147+
match = Device._DEVICE_INDEX.match(file)
148+
if match:
149+
self._device_index = int(match.group('idx'))
150+
else:
151+
self._device_index = None
152+
143153
return
144154

145155
self._path = ''
@@ -189,6 +199,10 @@ def get_attr_from_set( self, attribute ):
189199
return v
190200
return ""
191201

202+
@property
203+
def device_index(self):
204+
return self._device_index
205+
192206
#~autogen generic-class classes.motor>currentClass
193207

194208
class Motor(Device):

0 commit comments

Comments
 (0)