crossbar.html


001| '''Crossbar Switch Simulation - Crossbar.
001| 
001| Data:
001|     XBarRows            ~int: number of crossbar rows (default: 10)
001|     XBarCols            ~int: number of crossbar columns (default: 10)
001| 
001| Classes:
001|     XBarPoint           ~single crosspoint
001|     XBarColumn          ~column of crossbar points
001|     XBarRow             ~row of crossbar points
001|     XBarSwitch          ~single MxN crossbar switch (with rows & colums)
001| 
001| '''

002| ####################################################################################################
003| from logger import logger
004| from goodies import typename, KWARG, makefilename
005| ####################################################################################################
006| BasePath = r'C:\CJS\prj\Python\ws\xbar'
007| Log = logger('XBar')
008| 
009| ##------------------------------------------------------------------------------------------------##
010| XBarRows = 10
011| XBarCols = 10
012| 
013| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
014| class XBarPoint:
015|     '''\
015| Implements a single crosspoint.
015| 
015| Properties:
015|     row_index           -which row this xpoint is in
015|     col_index           -which column this xpoint is in
015|     points              -True if the points are closed
015|     selected            -True if the row is selected
015|     col_mag             -True if the column is held
015|     colx                -alias for col_index
015|     rowx                -alias for row_index
015|     status              -"X" if points closed else "-"
015| 
015| Methods:
015|     select_on           -row select magnet energized
015|     select_off          -row select magnet released
015|     magnet_on           -column hold magnet energized
015|     magnet_off          -column hold magnet released
015| 
015|     bool(obj)           -True if xpoints closed
015|     repr(obj)           -repr string
015|     str(obj)            -nice string
015| '''

016|     def __init__ (self, rx:int, cx:int):
017|         self.row_index = rx
018|         self.col_index = cx
019|         self.points = False
020|         self.selected = False
021|         self.col_mag = False
022| 
023|     @property
024|     def colx (self): return self.col_index
025| 
026|     @property
027|     def rowx (self): return self.row_index
028| 
029|     @property
030|     def status (self): return 'X' if self else '-'
031| 
032|     def select_on (self):
033|         '''Row Select Magnet on.'''
034|         # Ignore if this column already in use...
035|         if self.col_mag:
036|             return
037|         # Crosspoint selected...
038|         self.selected = True
039| 
040|     def select_off (self):
041|         '''Row Select Magnet off.'''
042|         # Crosspoint no longer selected...
043|         self.selected = False
044| 
045|     def magnet_on (self):
046|         '''Column Hold Magnet on.'''
047|         self.col_mag = True
048|         # Activate crosspoint if selected...
049|         if self.selected:
050|             self.points = True
051| 
052|     def magnet_off (self):
053|         '''Column Hold Magnet off.'''
054|         self.col_mag = False
055|         # Release crosspoint...
056|         self.points = False
057| 
058|     def __bool__ (self):
059|         return self.points
060| 
061|     def __repr__ (self):
062|         return f'<{typename(self)} C{self.colx}R{self.rowx}{self.status} @{id(self):012x}>'
063| 
064|     def __str__ (self):
065|         return f'C{self.colx}R{self.rowx}[{self.status}]'
066| 
067| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
068| class XBarColumn:
069|     '''\
069| Implements a column of crossbar points.
069| 
069| Properties:
069|     col_index           -which column this column is (0-9)
069|     xpoints             -list of xpoints in this column
069|     magnet              -whether the hold magnet is energized
069|     colx                -alias for col_index
069|     active              -returns active xpoint in this column (or None)
069| 
069| Methods:
069|     magnet_on           -energize the hold magnet for this column
069|     magnet_off          -release the hold magnet for this column
069| 
069|     obj[ix]             -return a given xpoint by its row index
069|     bool(obj)           -True if column is in use
069|     len(obj)            -number of xpoints in column
069|     iter(obj)           -iterator over the xpoints in this column
069|     repr(obj)           -repr string
069|     str(obj)            -nice string
069| '''

070|     def __init__ (self, cx:int, n_rows:int=XBarCols):
071|         self.col_index = cx
072|         self.xpoints = [None]*n_rows
073|         self.magnet = False
074| 
075|     @property
076|     def colx (self): return self.col_index
077| 
078|     def magnet_on (self):
079|         '''Activate Hold Magnet.'''
080|         if self.magnet:
081|             raise RuntimeError(f'Col{self.colx} already in use.')
082|         self.magnet = True
083|         for xp in self.xpoints:
084|             xp.magnet_on()
085| 
086|     def magnet_off (self):
087|         '''Release Hold Magnet.'''
088|         self.magnet = False
089|         for xp in self.xpoints:
090|             xp.magnet_off()
091| 
092|     @property
093|     def active (self) -> XBarPoint|None:
094|         '''Returns active XBarPoint or None.'''
095|         buf = [xp for xp in self if xp]
096|         if 1 < len(buf):
097|             raise RuntimeError(f'More than one active xpoint in column {self.colx}')
098|         return buf[0] if len(buf) else None
099| 
100|     def __bool__ (self):
101|         return self.magnet
102| 
103|     def __iter__ (self): return iter(self.xpoints)
104|     def __len__ (self): return len(self.xpoints)
105|     def __getitem__ (self, ix:int): return self.xpoints[ix]
106| 
107|     def __setitem__ (self, ix:int, value):
108|         self.xpoints[ix] = value
109| 
110|     def __delitem__ (self, ix:int):
111|         raise RuntimeError(f'Col{self.colx}: xbar points cannot be deleted!')
112| 
113|     def __repr__ (self):
114|         return f'<{typename(self)} C{self.colx} @{id(self):012x}>'
115| 
116|     def __str__ (self):
117|         return f'Col{self.colx} <{id(self):012x}>'
118| 
119| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
120| class XBarRow:
121|     '''\
121| Implements a row of crossbar points.
121| 
121| Properties:
121|     row_index           -which row this row is (0-9)
121|     upper_lower         -whether row is upper (+1) or lower (-1)
121|     xpoints             -list of xpoints in this row
121|     magnet              -whether the select magnet is energized
121|     rowx                -alias for row_index
121|     active              -returns active xpoint in this row (or None)
121| 
121| Methods:
121|     select_on           -energize the select magnet for this row
121|     select_off          -release the select magnet for this row
121| 
121|     obj[ix]             -return a given xpoint by its column index
121|     bool(obj)           -True if row is in use
121|     len(obj)            -number of xpoints in row
121|     iter(obj)           -iterator over the xpoints in this row
121|     repr(obj)           -repr string
121|     str(obj)            -nice string
121| '''

122|     def __init__ (self, rx:int, n_cols:int=XBarRows):
123|         self.row_index = rx
124|         self.upper_lower = +1 if rx % 2 else -1
125|         self.xpoints = [None]*n_cols
126|         self.magnet = False
127| 
128|     def __bool__ (self):
129|         return 0 < len([xp for xp in self.xpoints if xp])
130| 
131|     @property
132|     def rowx (self): return self.row_index
133| 
134|     def select_on (self):
135|         '''Activate Select Magnet.'''
136|         if self.magnet:
137|             raise RuntimeError(f'Row{self.rowx} already in use.')
138|         self.magnet = True
139|         for xp in self.xpoints:
140|             xp.select_on()
141| 
142|     def select_off (self):
143|         '''Release Select Magnet.'''
144|         self.magnet = False
145|         for xp in self.xpoints:
146|             xp.select_off()
147| 
148|     @property
149|     def active (self):
150|         '''Returns active XBarPoint or None.'''
151|         buf = [xp for xp in self if xp]
152|         if 1 < len(buf):
153|             raise RuntimeError(f'More than one active xpoint in row {self.rowx}')
154|         return buf[0] if len(buf) else None
155| 
156|     def __iter__ (self): return iter(self.xpoints)
157|     def __len__ (self): return len(self.xpoints)
158|     def __getitem__ (self, ix:int): return self.xpoints[ix]
159| 
160|     def __setitem__ (self, ix:int, value):
161|         self.xpoints[ix] = value
162| 
163|     def __delitem__ (self, ix:int):
164|         raise RuntimeError(f'Row{self.rowx}: xbar row points cannot be deleted!')
165| 
166|     def __repr__ (self):
167|         return f'<{typename(self)} R{self.rowx} @{id(self):012x}>'
168| 
169|     def __str__ (self):
170|         return f'Row{self.rowx} <{id(self):012x}>'
171| 
172| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
173| class XBarSwitch:
174|     '''\
174| Implements a MxN crossbar switch.
174| 
174| Methods:
174|     reset               -reset the switch (release all magnets)
174|     display             -display the switch grid
174|     free_rows           -return list of free rows
174|     free_columns        -return list of free columns
174|     select_xpoint       -select an xpoint by row & column
174|     release_xpoint      -release xpoint (by column)
174| 
174| '''

175|     def __init__ (self, name:str, n_rows:int=XBarRows, n_cols:int=XBarCols):
176|         '''New X-Bar-Switch instance.'''
177|         self.name = name
178| 
179|         # Create columns and rows...
180|         self.cols = [XBarColumn(cx, n_rows) for cx in range(n_cols)]
181|         self.rows = [XBarRow(rx, n_cols) for rx in range(n_rows)]
182| 
183|         # Create xpoints and populate rows & columns...
184|         for rx,row in enumerate(self.rows):
185|             for cx,col in enumerate(self.cols):
186|                 xpt = XBarPoint(rx, cx)
187|                 row[cx] = xpt
188|                 col[rx] = xpt
189| 
190|     def reset (self):
191|         '''Reset the switch. Release all xpoints.'''
192|         for row in self.rows:
193|             row.select_off()
194|         for col in self.cols:
195|             col.magnet_off()
196| 
197|     def select_xpoint (self, cx:int, rx:int):
198|         '''Select a given xpoint.'''
199|         col = self.cols[cx]
200|         row = self.rows[rx]
201|         if col: raise RuntimeError(f'Col{cx} already in use.')
202|         if row: raise RuntimeError(f'Row{rx} already in use.')
203|         row.select_on()
204|         col.magnet_on()
205|         row.select_off()
206| 
207|     def release_xpoint (self, cx:int):
208|         '''Release xpoint in a given vertical.'''
209|         col = self.cols[cx]
210|         if not col: raise RuntimeError(f'Col{cx} not in use.')
211|         col.magnet_off()
212| 
213|     def free_rows (self, group:tuple=()):
214|         '''Return available rows, optionally limited by group.'''
215|         rows = [row.rowx for row in self.rows if not row]
216|         if 0 < len(group):
217|             return [rx for rx in rows if rx in group]
218|         return rows
219| 
220|     def free_columns (self, group:tuple=()):
221|         '''Return available columns, optionally limited by group.'''
222|         cols = [col.colx for col in self.cols if not col]
223|         if 0 < len(group):
224|             return [cx for cx in cols if cx in group]
225|         return cols
226| 
227|     def display (self, sout=None, mode=0):
228|         '''Display the switch xpoints and magnets.'''
229|         def prn (txt=''):
230|             if sout: print(txt, file=sout)
231|             match mode:
232|                 case 0:
233|                     Log.trace(txt)
234|                 case 1:
235|                     Log.debug(txt)
236|                 case 2:
237|                     Log.info(txt)
238|                 case _:
239|                     raise RuntimeError(f'Unexpected log mode: {mode}')
240| 
241|         for row in self.rows:
242|             xpnts = ' '.join(f'{xp}' for xp in row)
243|             mag = '[ ]' if row.active is None else f'[{row.active.colx}]'
244|             prn(f'{xpnts} _SM{row.rowx}{"[X]" if row else mag}')
245| 
246|         mags0 = [(col.colx, col.active) for col in self.cols]
247|         mags1 = [(cx, ' ') if xpt is None else (cx, xpt.rowx) for cx,xpt in mags0]
248|         mags2 = [f'_HM{cx}[{rx}]' for cx,rx in mags1]
249|         prn(' '.join(mags2))
250|         prn()
251| 
252|     def __repr__ (self):
253|         return f'<{typename(self)} {self.name} {len(self.rows)}x{len(self.cols)} @{id(self):012x}>'
254| 
255|     def __str__ (self):
256|         buf = []
257|         for col in self.cols:
258|             mag = '[X]' if col else ' '
259|             buf.append(f'C{col.colx}--{mag}' if col.active is None else str(col.active))
260|         return '|'.join(buf)+'|'
261| 
262| 
263| ####################################################################################################
264| '''eof'''
265|