forked from pcduino/python-pcduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinmap.py
More file actions
34 lines (24 loc) · 941 Bytes
/
pinmap.py
File metadata and controls
34 lines (24 loc) · 941 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os.path
class InvalidChannelException(ValueError):
"""The channel sent was invalid."""
def __init__(self, pin):
super(InvalidChannelException, self).__init__("pin %s not found" % pin)
class PinMap(object):
def __init__(self, path, prefix, count):
self.pins = ['%s%s' % (prefix, i) for i in xrange(count)]
self.path = path
def get_path(self, pin, path=None):
"""Get path of pin fd.
pin can either be the pin basename (i.e. 'adc2') or pin number (i.e. 2)
if prefix is supplied, override the default path prefix.
"""
if not path:
path = self.path
if isinstance(pin, int):
try:
pin = self.pins[pin]
except IndexError:
raise InvalidChannelException(pin)
if not pin in self.pins:
raise InvalidChannelException(pin)
return os.path.join(path, pin)