Skip to content

Commit 2d12db5

Browse files
committed
Merge pull request #34 from ddemidov/device-index
Implement Device.device_index
2 parents 90d0853 + 26e5d80 commit 2d12db5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
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):

tests/api_tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ def test_medium_motor(self):
3232

3333
self.assertTrue(m.connected);
3434

35+
self.assertEqual(m.device_index, 0)
36+
37+
# Check that reading twice works:
38+
self.assertEqual(m.driver_name, 'lego-ev3-m-motor')
39+
self.assertEqual(m.driver_name, 'lego-ev3-m-motor')
40+
3541
self.assertEqual(m.count_per_rot, 360)
3642
self.assertEqual(m.commands, ['run-forever', 'run-to-abs-pos', 'run-to-rel-pos', 'run-timed', 'run-direct', 'stop', 'reset'])
37-
self.assertEqual(m.driver_name, 'lego-ev3-m-motor')
3843
self.assertEqual(m.duty_cycle, 0)
3944
self.assertEqual(m.duty_cycle_sp, 42)
4045
self.assertEqual(m.encoder_polarity, 'normal')
@@ -59,6 +64,7 @@ def test_infrared_sensor(self):
5964

6065
self.assertTrue(s.connected)
6166

67+
self.assertEqual(s.device_index, 0)
6268
self.assertEqual(s.bin_data_format, 's8')
6369
self.assertEqual(s.bin_data('<b'), (16,))
6470
self.assertEqual(s.num_values, 1)

0 commit comments

Comments
 (0)