pass in Interface factory, to do GPIO differently
[pinmux.git] / src / ifacebase.py
1 import os.path
2
3 try:
4 from UserDict import UserDict
5 except ImportError:
6 from collections import UserDict
7
8
9 class InterfacesBase(UserDict):
10 """ contains a list of interface definitions
11 """
12
13 def __init__(self, ifacekls, pth=None, ifaceklsdict=None):
14 self.pth = pth
15 self.ifacecount = []
16 if ifaceklsdict is None:
17 ifaceklsdict = {}
18 UserDict.__init__(self, {})
19 if not pth:
20 return
21 ift = 'interfaces.txt'
22 if pth:
23 ift = os.path.join(pth, ift)
24 with open(ift, 'r') as ifile:
25 for ln in ifile.readlines():
26 ln = ln.strip()
27 ln = ln.split("\t")
28 name = ln[0] # will have uart
29 count = int(ln[1]) # will have count of uart
30 # spec looks like this:
31 """
32 [{'name': 'sda', 'outen': True},
33 {'name': 'scl', 'outen': True},
34 ]
35 """
36 ikls = ifacekls
37 for k, v in ifaceklsdict.items():
38 if name.startswith(k):
39 ikls = v
40 break
41 spec, ganged = self.read_spec(pth, name)
42 # XXX HORRIBLE hack!!!
43 if name == 'pwm' and count == 1 and len(spec) != 1:
44 #print "read", name, count, spec, ganged
45 #print "multi pwm", spec[:1], len(spec)
46 spec[0]['name'] = 'out'
47 iface = ikls(name, spec[:1], ganged, False)
48 self.ifaceadd(name, len(spec), iface)
49 else:
50 iface = ikls(name, spec, ganged, count == 1)
51 self.ifaceadd(name, count, iface)
52
53 def getifacetype(self, fname):
54 # finds the interface type, e.g sd_d0 returns "inout"
55 for iface in self.values():
56 typ = iface.getifacetype(fname)
57 # if fname.startswith('pwm'):
58 # print fname, iface.ifacename, typ
59 if typ:
60 return typ
61 return None
62
63 def ifaceadd(self, name, count, iface, at=None):
64 if at is None:
65 at = len(self.ifacecount) # ifacecount is a list
66 self.ifacecount.insert(at, (name, count)) # appends the list
67 # with (name,count) *at* times
68 self[name] = iface
69
70 """
71 will check specific files of kind peripheral.txt like spi.txt,
72 uart.txt in test directory
73 """
74
75 def read_spec(self, pth, name):
76 spec = []
77 ganged = {}
78 fname = '%s.txt' % name
79 if pth:
80 ift = os.path.join(pth, fname)
81 with open(ift, 'r') as sfile:
82 for ln in sfile.readlines():
83 ln = ln.strip()
84 ln = ln.split("\t")
85 name = ln[0]
86 d = {'name': name, # here we start to make the dictionary
87 'type': ln[1]}
88 if ln[1] == 'out':
89 d['action'] = True # adding element to the dict
90 elif ln[1] == 'inout':
91 d['outen'] = True
92 if len(ln) == 3:
93 bus = ln[2]
94 if bus not in ganged:
95 ganged[bus] = []
96 ganged[bus].append(name)
97 spec.append(d)
98 return spec, ganged