From 8d8264d4d3ddced632b57c4b95933c4535784272 Mon Sep 17 00:00:00 2001 From: Terry Greeniaus Date: Tue, 15 Feb 2022 01:48:55 -0800 Subject: [PATCH] stm_layout_tk: Factor platform-specific twiddle values into xplat parameters. --- .pylintrc | 3 ++ stm_layout/stm_layout_tk.py | 43 ++++++++++++++++-- stm_layout/tk/tk_bga.py | 7 ++- stm_layout/tk/tk_edgy.py | 66 +++++++++++++++++++++++++++ stm_layout/tk/tk_lqfp.py | 86 ++++++----------------------------- stm_layout/tk/tk_tssop.py | 47 ++++--------------- stm_layout/tk/tk_workspace.py | 16 +++---- stm_layout/tk/xplat.py | 15 +++--- 8 files changed, 150 insertions(+), 133 deletions(-) create mode 100644 stm_layout/tk/tk_edgy.py diff --git a/.pylintrc b/.pylintrc index bede79f..46dc10e 100644 --- a/.pylintrc +++ b/.pylintrc @@ -17,3 +17,6 @@ disable=bad-whitespace, useless-super-delegation, consider-using-f-string, R0801 + +[TYPECHECK] +generated-members=xplat.* diff --git a/stm_layout/stm_layout_tk.py b/stm_layout/stm_layout_tk.py index 639a478..4120716 100644 --- a/stm_layout/stm_layout_tk.py +++ b/stm_layout/stm_layout_tk.py @@ -6,9 +6,43 @@ import stm_layout.tk -FONT = ('Monaco', 10) -FONT_PIN_NUM = ('Monaco', 9) -FONT_INFO = ('Monaco', 10) +stm_layout.tk.xplat.register( + windows={ + 'LABEL_FONT' : ('Courier New', 10), + 'INFO_FONT' : ('Courier New', 10), + 'PIN_FONT' : ('Courier New', 9), + 'EDGE_PIN_H_KEY_DX' : 0, + 'EDGE_PIN_H_KEY_DY' : 1, + 'EDGE_PIN_V_KEY_DX' : 1, + 'BGA_PIN_NAME_DY' : 1, + 'BGA_PIN_KEY_DX' : 0, + 'BGA_PIN_KEY_DY' : 1, + }, + darwin={ + 'LABEL_FONT' : ('Monaco', 10), + 'INFO_FONT' : ('Monaco', 10), + 'PIN_FONT' : ('Monaco', 9), + 'EDGE_PIN_H_KEY_DX' : 1, + 'EDGE_PIN_H_KEY_DY' : 0, + 'EDGE_PIN_V_KEY_DX' : 0, + 'BGA_PIN_NAME_DY' : 0, + 'BGA_PIN_KEY_DX' : 1, + 'BGA_PIN_KEY_DY' : 0, + }, + linux={ + # TBD. + 'LABEL_FONT' : ('Courier', 10), + 'INFO_FONT' : ('Courier', 10), + 'PIN_FONT' : ('Courier', 9), + 'EDGE_PIN_H_KEY_DX' : 0, + 'EDGE_PIN_H_KEY_DY' : 1, + 'EDGE_PIN_V_KEY_DX' : 1, + 'BGA_PIN_NAME_DY' : 1, + 'BGA_PIN_KEY_DX' : 0, + 'BGA_PIN_KEY_DY' : 1, + }, +) + RECT_FILL = 'white' HILITE_FILL = 'lightblue' SELECT_FILL = 'yellow' @@ -25,8 +59,7 @@ def main(chip, regex): else: raise Exception('Unsupported chip package.') - ws = cls(chip, FONT, FONT_PIN_NUM, FONT_INFO, RECT_FILL, HILITE_FILL, - SELECT_FILL, RE_FILL) + ws = cls(chip, RECT_FILL, HILITE_FILL, SELECT_FILL, RE_FILL) if regex: ws.set_regex(regex) diff --git a/stm_layout/tk/tk_bga.py b/stm_layout/tk/tk_bga.py index 86831fa..0cdb8b1 100644 --- a/stm_layout/tk/tk_bga.py +++ b/stm_layout/tk/tk_bga.py @@ -1,4 +1,5 @@ from . import tk_workspace +from . import xplat PIN_DIAM = 30 @@ -36,9 +37,11 @@ def __init__(self, *args): fill=self.elem_fill) self.pin_elems.append(o) c.add_text( - o.x + o.width / 2, o.y + o.height, + o.x + o.width / 2, + o.y + o.height + xplat.BGA_PIN_NAME_DY, font=self.label_font, text=p.name, anchor='n') c.add_text( - o.x + o.width / 2 + 1, o.y + o.height / 2, + o.x + o.width / 2 + xplat.BGA_PIN_KEY_DX, + o.y + o.height / 2 + xplat.BGA_PIN_KEY_DY, font=self.pin_font, text=p.key, anchor='c') o.pin = p diff --git a/stm_layout/tk/tk_edgy.py b/stm_layout/tk/tk_edgy.py new file mode 100644 index 0000000..0591526 --- /dev/null +++ b/stm_layout/tk/tk_edgy.py @@ -0,0 +1,66 @@ +from . import tk_workspace +from . import xplat + + +PIN_WIDTH = 12 +PIN_DELTA = 2*PIN_WIDTH +PIN_LENGTH = 20 +PIN_LABEL_OFFSET = 2 + + +def pin_d(t0, t1, i): + if t0 < t1: + return t0 + PIN_WIDTH + i*PIN_DELTA + return t0 - (i + 1)*PIN_DELTA + + +class Workspace(tk_workspace.Workspace): + def __init__(self, *args): + super().__init__(*args) + self.pin_width = PIN_WIDTH + self.pin_length = PIN_LENGTH + self.pin_label_offset = PIN_LABEL_OFFSET + + def add_h_pin(self, p, x, y): + c = self.mcu_canvas + r = c.add_rectangle(x, y, PIN_LENGTH, PIN_WIDTH, fill=self.elem_fill) + c.add_text(x + PIN_LENGTH / 2 + xplat.EDGE_PIN_H_KEY_DX, + y + PIN_WIDTH / 2 + xplat.EDGE_PIN_H_KEY_DY, + font=self.pin_font, text=p.key, anchor='c') + return r + + def add_v_pin(self, p, x, y): + c = self.mcu_canvas + r = c.add_rectangle(x, y, PIN_WIDTH, PIN_LENGTH, fill=self.elem_fill) + c.add_text(x + PIN_WIDTH / 2 + xplat.EDGE_PIN_V_KEY_DX, + y + PIN_LENGTH / 2, font=self.pin_font, text=p.key, + anchor='c', angle=90) + return r + + def add_l_pin(self, p, x, y0, y1, i): + y = pin_d(y0, y1, i) + self.mcu_canvas.add_text(x - PIN_LABEL_OFFSET, y + PIN_WIDTH / 2, + font=self.label_font, text=p.name, anchor='e') + return self.add_h_pin(p, x, y) + + def add_r_pin(self, p, x, y0, y1, i): + y = pin_d(y0, y1, i) + self.mcu_canvas.add_text(x + PIN_LENGTH + 1 + PIN_LABEL_OFFSET, + y + PIN_WIDTH / 2, font=self.label_font, + text=p.name, anchor='w') + return self.add_h_pin(p, x, y) + + def add_t_pin(self, p, x0, x1, i, y): + x = pin_d(x0, x1, i) + self.mcu_canvas.add_text(x + PIN_WIDTH / 2, y - PIN_LABEL_OFFSET, + font=self.label_font, text=p.name, anchor='w', + angle=90) + return self.add_v_pin(p, x, y) + + def add_b_pin(self, p, x0, x1, i, y): + x = pin_d(x0, x1, i) + self.mcu_canvas.add_text(x + PIN_WIDTH / 2, + y + PIN_LENGTH + 1 + PIN_LABEL_OFFSET, + font=self.label_font, text=p.name, anchor='e', + angle=90) + return self.add_v_pin(p, x, y) diff --git a/stm_layout/tk/tk_lqfp.py b/stm_layout/tk/tk_lqfp.py index a09fc7b..81a5c2a 100644 --- a/stm_layout/tk/tk_lqfp.py +++ b/stm_layout/tk/tk_lqfp.py @@ -1,25 +1,20 @@ -from . import tk_workspace +from . import tk_edgy from .. import chip_db -PIN_WIDTH = 12 -PIN_LENGTH = 20 -PIN_LABEL_OFFSET = 2 - - -class LQFPWorkspace(tk_workspace.Workspace): +class LQFPWorkspace(tk_edgy.Workspace): def __init__(self, *args): super().__init__(*args) cw = self.chip.width - 2 ch = self.chip.height - 2 - w = 2*PIN_WIDTH*cw + PIN_WIDTH - h = 2*PIN_WIDTH*ch + PIN_WIDTH + w = 2*self.pin_width*cw + self.pin_width + h = 2*self.pin_width*ch + self.pin_width pad = 0 for _, p in self.chip.pins.items(): pad = max(pad, self.label_font.measure(p.name)) - pad += PIN_LENGTH + 5 + PIN_LABEL_OFFSET + pad += self.pin_length + 5 + self.pin_label_offset self.set_geometry(50, 50, w + 2*pad + self.info_width, max(h + 2*pad, self.info_height)) @@ -32,71 +27,18 @@ def __init__(self, *args): r0 = b0 + cw t0 = r0 + ch m = c.add_rectangle(pad, pad, w, h, fill=self.elem_fill) - for i, p in self.chip.pins.items(): - i = int(i) + for _, p in self.chip.pins.items(): + i = int(p.key) if l0 <= i < b0: - # Left row. - r = c.add_rectangle( - m.x - PIN_LENGTH, - m.y + PIN_WIDTH*(2*(i - l0) + 1), - PIN_LENGTH, PIN_WIDTH, fill=self.elem_fill) - c.add_text( - r.x - PIN_LABEL_OFFSET, r.y + r.height / 2, - font=self.label_font, text=p.name, anchor='e') - c.add_text( - r.x + r.width / 2 + 1, r.y + r.height / 2, - font=self.pin_font, text='%u' % i, anchor='c') - self.pin_elems.append(r) - + r = self.add_l_pin(p, m.x - self.pin_length, m.ty, m.by, i - l0) elif b0 <= i < r0: - # Bottom row. - r = c.add_rectangle( - m.x + PIN_WIDTH*(2*(i - b0) + 1), - m.y + m.height, - PIN_WIDTH, PIN_LENGTH, fill=self.elem_fill) - c.add_text( - r.x + r.width / 2, - r.y + r.height + 1 + PIN_LABEL_OFFSET, - font=self.label_font, text=p.name, anchor='e', - angle=90) - c.add_text( - r.x + r.width / 2, r.y + r.height / 2, - font=self.pin_font, text='%u' % i, - anchor='c', angle=90) - self.pin_elems.append(r) - + r = self.add_b_pin(p, m.lx, m.rx, i - b0, m.y + m.height) elif r0 <= i < t0: - # Right row. - r = c.add_rectangle( - m.x + m.width, - m.y + m.height - PIN_WIDTH*(2*(i - r0) + 2), - PIN_LENGTH, PIN_WIDTH, fill=self.elem_fill) - c.add_text( - r.x + r.width + 1 + PIN_LABEL_OFFSET, - r.y + r.height / 2, - font=self.label_font, text=p.name, anchor='w') - c.add_text( - r.x + r.width / 2, r.y + r.height / 2, - font=self.pin_font, text='%u' % i, - anchor='c') - self.pin_elems.append(r) - + r = self.add_r_pin(p, m.x + m.width, m.by, m.ty, i - r0) else: - # Top row. - r = c.add_rectangle( - m.x + m.width - PIN_WIDTH*(2*(i - t0) + 2), - m.y - PIN_LENGTH, - PIN_WIDTH, PIN_LENGTH, fill=self.elem_fill) - c.add_text( - r.x + r.width / 2, r.y - PIN_LABEL_OFFSET, - font=self.label_font, - text=p.name, anchor='w', angle=90) - c.add_text( - r.x + r.width / 2, r.y + r.height / 2, - font=self.pin_font, text='%u' % i, - anchor='c', angle=90) - self.pin_elems.append(r) + r = self.add_t_pin(p, m.rx, m.lx, i - t0, m.y - self.pin_length) + self.pin_elems.append(r) r.pin = p package_name = chip_db.package(self.chip.part) @@ -107,5 +49,5 @@ def __init__(self, *args): anchor='c') if 'LQFP' not in package_name: - m.resize(pad - PIN_LENGTH, pad - PIN_LENGTH, - w + 2*PIN_LENGTH, h + 2*PIN_LENGTH) + m.resize(pad - self.pin_length, pad - self.pin_length, + w + 2*self.pin_length, h + 2*self.pin_length) diff --git a/stm_layout/tk/tk_tssop.py b/stm_layout/tk/tk_tssop.py index b26ae37..5573118 100644 --- a/stm_layout/tk/tk_tssop.py +++ b/stm_layout/tk/tk_tssop.py @@ -1,25 +1,22 @@ -from . import tk_workspace +from . import tk_edgy from .. import chip_db -PKG_WIDTH = 100 -PIN_WIDTH = 12 -PIN_LENGTH = 20 -PIN_LABEL_OFFSET = 2 +PKG_WIDTH = 100 -class TSSOPWorkspace(tk_workspace.Workspace): +class TSSOPWorkspace(tk_edgy.Workspace): def __init__(self, *args): super().__init__(*args) ch = self.chip.height w = PKG_WIDTH - h = 2*PIN_WIDTH*ch + PIN_WIDTH + h = 2*self.pin_width*ch + self.pin_width pad = 0 for _, p in self.chip.pins.items(): pad = max(pad, self.label_font.measure(p.name)) - pad += PIN_LENGTH + 5 + PIN_LABEL_OFFSET + pad += self.pin_length + 5 + self.pin_label_offset self.set_geometry(50, 50, w + 2*pad + self.info_width, max(h + 2*pad, self.info_height)) @@ -30,38 +27,14 @@ def __init__(self, *args): l0 = 1 r0 = l0 + ch m = c.add_rectangle(pad, pad, w, h, fill=self.elem_fill) - for k, p in self.chip.pins.items(): - i = int(k) + for _, p in self.chip.pins.items(): + i = int(p.key) if l0 <= i < r0: - # Left row. - r = c.add_rectangle( - m.x - PIN_LENGTH, - m.y + PIN_WIDTH*(2*(i - l0) + 1), - PIN_LENGTH, PIN_WIDTH, fill=self.elem_fill) - c.add_text( - r.x - PIN_LABEL_OFFSET, r.y + r.height / 2, - font=self.label_font, text=p.name, anchor='e') - c.add_text( - r.x + r.width / 2 + 1, r.y + r.height / 2, - font=self.pin_font, text='%u' % i, anchor='c') - self.pin_elems.append(r) - + r = self.add_l_pin(p, m.x - self.pin_length, m.ty, m.by, i - l0) else: - # Right row. - r = c.add_rectangle( - m.x + m.width, - m.y + m.height - PIN_WIDTH*(2*(i - r0) + 2), - PIN_LENGTH, PIN_WIDTH, fill=self.elem_fill) - c.add_text( - r.x + r.width + 1 + PIN_LABEL_OFFSET, - r.y + r.height / 2, - font=self.label_font, text=p.name, anchor='w') - c.add_text( - r.x + r.width / 2, r.y + r.height / 2, - font=self.pin_font, text='%u' % i, - anchor='c') - self.pin_elems.append(r) + r = self.add_r_pin(p, m.x + m.width, m.by, m.ty, i - r0) + self.pin_elems.append(r) r.pin = p package_name = chip_db.package(self.chip.part) diff --git a/stm_layout/tk/tk_workspace.py b/stm_layout/tk/tk_workspace.py index d0f209b..244f3c2 100644 --- a/stm_layout/tk/tk_workspace.py +++ b/stm_layout/tk/tk_workspace.py @@ -2,6 +2,7 @@ import tkinter.font from .tk_elems import TKBase +from . import xplat class InfoText: @@ -22,8 +23,7 @@ def set_bg(self, bg_color): class Workspace(TKBase): - def __init__(self, chip, label_font, pin_font, info_font, elem_fill, - hilite_fill, select_fill, re_fill): + def __init__(self, chip, elem_fill, hilite_fill, select_fill, re_fill): super().__init__() self.chip = chip @@ -37,12 +37,12 @@ def __init__(self, chip, label_font, pin_font, info_font, elem_fill, self.pin_elems = [] self.regex = None - self.label_font = tkinter.font.Font(family=label_font[0], - size=label_font[1]) - self.pin_font = tkinter.font.Font(family=pin_font[0], - size=pin_font[1]) - self.info_font = tkinter.font.Font(family=info_font[0], - size=info_font[1]) + self.label_font = tkinter.font.Font(family=xplat.LABEL_FONT[0], + size=xplat.LABEL_FONT[1]) + self.pin_font = tkinter.font.Font(family=xplat.PIN_FONT[0], + size=xplat.PIN_FONT[1]) + self.info_font = tkinter.font.Font(family=xplat.INFO_FONT[0], + size=xplat.INFO_FONT[1]) dy = self.info_font.metrics('linespace') w = 300 diff --git a/stm_layout/tk/xplat.py b/stm_layout/tk/xplat.py index 39b0697..c2acab2 100644 --- a/stm_layout/tk/xplat.py +++ b/stm_layout/tk/xplat.py @@ -1,14 +1,11 @@ import platform - - -SYSTEM = platform.system().lower() -CONFIG = {} +import sys def register(**kwargs): - global CONFIG - CONFIG = kwargs[SYSTEM] - + module = sys.modules[__name__] + del module.register -def get(key): - return CONFIG[key] + config = kwargs[platform.system().lower()] + for k, v in config.items(): + setattr(module, k, v)