Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ev3dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import array
import mmap
import ctypes
import re
from os.path import abspath
from PIL import Image, ImageDraw
from struct import pack, unpack
Expand Down Expand Up @@ -108,6 +109,8 @@ class Device(object):

DEVICE_ROOT_PATH = '/sys/class'

_DEVICE_INDEX = re.compile(r'^.*(?P<idx>\d+)$')

def __init__(self, class_name, name='*', **kwargs ):
"""Spin through the Linux sysfs class for the device type and find
a device that matches the provided name and attributes (if any).
Expand Down Expand Up @@ -140,6 +143,13 @@ def __init__(self, class_name, name='*', **kwargs ):
# See if requested attributes match:
if all([self._matches(k, kwargs[k]) for k in kwargs]):
self.connected = True

match = Device._DEVICE_INDEX.match(file)
if match:
self._device_index = int(match.group('idx'))
else:
self._device_index = None

return

self._path = ''
Expand Down Expand Up @@ -189,6 +199,10 @@ def get_attr_from_set( self, attribute ):
return v
return ""

@property
def device_index(self):
return self._device_index

#~autogen generic-class classes.motor>currentClass

class Motor(Device):
Expand Down
8 changes: 7 additions & 1 deletion tests/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ def test_medium_motor(self):

self.assertTrue(m.connected);

self.assertEqual(m.device_index, 0)

# Check that reading twice works:
self.assertEqual(m.driver_name, 'lego-ev3-m-motor')
self.assertEqual(m.driver_name, 'lego-ev3-m-motor')

self.assertEqual(m.count_per_rot, 360)
self.assertEqual(m.commands, ['run-forever', 'run-to-abs-pos', 'run-to-rel-pos', 'run-timed', 'run-direct', 'stop', 'reset'])
self.assertEqual(m.driver_name, 'lego-ev3-m-motor')
self.assertEqual(m.duty_cycle, 0)
self.assertEqual(m.duty_cycle_sp, 42)
self.assertEqual(m.encoder_polarity, 'normal')
Expand All @@ -59,6 +64,7 @@ def test_infrared_sensor(self):

self.assertTrue(s.connected)

self.assertEqual(s.device_index, 0)
self.assertEqual(s.bin_data_format, 's8')
self.assertEqual(s.bin_data('<b'), (16,))
self.assertEqual(s.num_values, 1)
Expand Down