Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Library: Remove direct library imports (#34)
Browse files Browse the repository at this point in the history
Use import F pattern everywhere
  • Loading branch information
iopapamanoglou authored Sep 3, 2024
1 parent 152d4b7 commit 3a38f09
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 213 deletions.
22 changes: 7 additions & 15 deletions src/faebryk/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,10 @@ def get_direct_connected_nodes[T: Node](


def get_net(mif: F.Electrical):
from faebryk.library.Net import Net

nets = {
net
for mif in get_connected_mifs(mif.connected)
if (net := get_parent_of_type(mif, Net)) is not None
if (net := get_parent_of_type(mif, F.Net)) is not None
}

if not nets:
Expand Down Expand Up @@ -323,12 +321,10 @@ def get_param_tree(param: Parameter) -> list[tuple[Parameter, list]]:
def connect_interfaces_via_chain(
start: ModuleInterface, bridges: Iterable[Node], end: ModuleInterface, linkcls=None
):
from faebryk.library.can_bridge import can_bridge

last = start
for bridge in bridges:
last.connect(bridge.get_trait(can_bridge).get_in(), linkcls=linkcls)
last = bridge.get_trait(can_bridge).get_out()
last.connect(bridge.get_trait(F.can_bridge).get_in(), linkcls=linkcls)
last = bridge.get_trait(F.can_bridge).get_out()
last.connect(end, linkcls=linkcls)


Expand Down Expand Up @@ -375,13 +371,11 @@ def connect_module_mifs_by_name(


def reversed_bridge(bridge: Node):
from faebryk.library.can_bridge import can_bridge

class _reversed_bridge(Node):
def __init__(self) -> None:
super().__init__()

bridge_trait = bridge.get_trait(can_bridge)
bridge_trait = bridge.get_trait(F.can_bridge)
if_in = bridge_trait.get_in()
if_out = bridge_trait.get_out()

Expand Down Expand Up @@ -565,8 +559,6 @@ def pretty_param_tree_top(param: Parameter) -> str:


def use_interface_names_as_net_names(node: Node, name: str | None = None):
from faebryk.library.Net import Net

if not name:
p = node.get_parent()
assert p
Expand Down Expand Up @@ -597,7 +589,7 @@ def use_interface_names_as_net_names(node: Node, name: str | None = None):
n
for c in connections
if (p := c.get_parent())
and isinstance(n := p[0], Net)
and isinstance(n := p[0], F.Net)
and n.part_of in connections
}:
# logger.warning(f"Skipped, attached to Net: {el_if}: {matched_nets!r}")
Expand All @@ -617,7 +609,7 @@ def use_interface_names_as_net_names(node: Node, name: str | None = None):
# performance
resolved.update(group)

nets: dict[str, tuple[Net, F.Electrical]] = {}
nets: dict[str, tuple[F.Net, F.Electrical]] = {}
for el_if in to_use:
net_name = f"{name}{el_if.get_full_name().removeprefix(name_prefix)}"

Expand All @@ -635,7 +627,7 @@ def use_interface_names_as_net_names(node: Node, name: str | None = None):
+ "\n\t".join(map(str, net.part_of.get_direct_connections()))
)

net = Net()
net = F.Net()
net.add_trait(F.has_overriden_name_defined(net_name))
net.part_of.connect(el_if)
logger.debug(f"Created {net_name} for {el_if}")
Expand Down
3 changes: 1 addition & 2 deletions src/faebryk/exporters/netlist/netlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class Vertex:
def make_t2_netlist_from_graph(G: Graph) -> T2Netlist:
from faebryk.core.util import get_all_nodes_of_type, get_all_nodes_with_trait
from faebryk.exporters.netlist.graph import can_represent_kicad_footprint
from faebryk.library.Net import Net as FNet

nets = get_all_nodes_of_type(G, FNet)
nets = get_all_nodes_of_type(G, F.Net)

t2_nets = [
T2Netlist.Net(
Expand Down
6 changes: 2 additions & 4 deletions src/faebryk/exporters/pcb/layout/heuristic_pulls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def __init__(self, params: Params | None = None):

def apply(self, *node: Node):
from faebryk.core.util import get_parent_of_type
from faebryk.library.ElectricLogic import ElectricLogic
from faebryk.library.Resistor import Resistor

# Remove nodes that have a position defined
node = tuple(
Expand All @@ -32,8 +30,8 @@ def apply(self, *node: Node):
)

for n in node:
assert isinstance(n, Resistor)
logic = NotNone(get_parent_of_type(n, ElectricLogic))
assert isinstance(n, F.Resistor)
logic = NotNone(get_parent_of_type(n, F.ElectricLogic))

place_next_to(logic.signal, n, route=True, params=self._params)

Expand Down
16 changes: 5 additions & 11 deletions src/faebryk/libs/app/erc.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,23 @@ def simple_erc(G: Graph):
Returns:
"""
from faebryk.library.Capacitor import Capacitor
from faebryk.library.ElectricPower import ElectricPower
from faebryk.library.Fuse import Fuse
from faebryk.library.Net import Net
from faebryk.library.Resistor import Resistor

logger.info("Checking graph for ERC violations")

# power short
electricpower = get_all_nodes_of_type(G, ElectricPower)
electricpower = get_all_nodes_of_type(G, F.ElectricPower)
logger.info(f"Checking {len(electricpower)} Power")
for ep in electricpower:
if ep.lv.is_connected_to(ep.hv):
raise ERCFaultShort([ep], "shorted power")

# shorted nets
nets = get_all_nodes_of_type(G, Net)
nets = get_all_nodes_of_type(G, F.Net)
logger.info(f"Checking {len(nets)} nets")
for net in nets:
collisions = {
p[0]
for mif in net.part_of.get_direct_connections()
if (p := mif.get_parent()) and isinstance(p[0], Net)
if (p := mif.get_parent()) and isinstance(p[0], F.Net)
}

if collisions:
Expand Down Expand Up @@ -113,9 +107,9 @@ def simple_erc(G: Graph):
# checked.add(mif)
# if any(mif.is_connected_to(other) for other in (mifs - checked)):
# raise ERCFault([mif], "shorted symmetric footprint")
comps = get_all_nodes_of_types(G, (Resistor, Capacitor, Fuse))
comps = get_all_nodes_of_types(G, (F.Resistor, F.Capacitor, F.Fuse))
for comp in comps:
assert isinstance(comp, (Resistor, Capacitor, Fuse))
assert isinstance(comp, (F.Resistor, F.Capacitor, F.Fuse))
# TODO make prettier
if (
comp.has_trait(has_part_picked)
Expand Down
93 changes: 46 additions & 47 deletions src/faebryk/libs/examples/pickers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.core.util import specialize_module
from faebryk.library._F import Constant, Range
from faebryk.libs.app.parameters import replace_tbd_with_any
from faebryk.libs.picker.lcsc import LCSC_Part
from faebryk.libs.picker.picker import PickerOption, pick_module_by_params
Expand All @@ -30,17 +29,17 @@ def pick_fuse(module: F.Fuse):
PickerOption(
part=LCSC_Part(partno="C914087"),
params={
"fuse_type": Constant(F.Fuse.FuseType.RESETTABLE),
"response_type": Constant(F.Fuse.ResponseType.SLOW),
"trip_current": Constant(1 * P.A),
"fuse_type": F.Constant(F.Fuse.FuseType.RESETTABLE),
"response_type": F.Constant(F.Fuse.ResponseType.SLOW),
"trip_current": F.Constant(1 * P.A),
},
),
PickerOption(
part=LCSC_Part(partno="C914085"),
params={
"fuse_type": Constant(F.Fuse.FuseType.RESETTABLE),
"response_type": Constant(F.Fuse.ResponseType.SLOW),
"trip_current": Constant(0.5 * P.A),
"fuse_type": F.Constant(F.Fuse.FuseType.RESETTABLE),
"response_type": F.Constant(F.Fuse.ResponseType.SLOW),
"trip_current": F.Constant(0.5 * P.A),
},
),
],
Expand All @@ -59,14 +58,14 @@ def pick_mosfet(module: F.MOSFET):
PickerOption(
part=LCSC_Part(partno="C20917"),
params={
"channel_type": Constant(F.MOSFET.ChannelType.N_CHANNEL),
"channel_type": F.Constant(F.MOSFET.ChannelType.N_CHANNEL),
},
pinmap=standard_pinmap,
),
PickerOption(
part=LCSC_Part(partno="C15127"),
params={
"channel_type": Constant(F.MOSFET.ChannelType.P_CHANNEL),
"channel_type": F.Constant(F.MOSFET.ChannelType.P_CHANNEL),
},
pinmap=standard_pinmap,
),
Expand All @@ -87,23 +86,23 @@ def pick_capacitor(module: F.Capacitor):
PickerOption(
part=LCSC_Part(partno="C1525"),
params={
"temperature_coefficient": Range(
"temperature_coefficient": F.Range(
F.Capacitor.TemperatureCoefficient.Y5V,
F.Capacitor.TemperatureCoefficient.X7R,
),
"capacitance": Constant(100 * P.nF),
"rated_voltage": Range(0 * P.V, 16 * P.V),
"capacitance": F.Constant(100 * P.nF),
"rated_voltage": F.Range(0 * P.V, 16 * P.V),
},
),
PickerOption(
part=LCSC_Part(partno="C19702"),
params={
"temperature_coefficient": Range(
"temperature_coefficient": F.Range(
F.Capacitor.TemperatureCoefficient.Y5V,
F.Capacitor.TemperatureCoefficient.X7R,
),
"capacitance": Constant(10 * P.uF),
"rated_voltage": Range(0 * P.V, 10 * P.V),
"capacitance": F.Constant(10 * P.uF),
"rated_voltage": F.Range(0 * P.V, 10 * P.V),
},
),
],
Expand All @@ -122,59 +121,59 @@ def pick_resistor(resistor: F.Resistor):
[
PickerOption(
part=LCSC_Part(partno="C25111"),
params={"resistance": Constant(40.2 * P.kohm)},
params={"resistance": F.Constant(40.2 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25076"),
params={"resistance": Constant(100 * P.kohm)},
params={"resistance": F.Constant(100 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25087"),
params={"resistance": Constant(200 * P.kohm)},
params={"resistance": F.Constant(200 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C11702"),
params={"resistance": Constant(1 * P.kohm)},
params={"resistance": F.Constant(1 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25879"),
params={"resistance": Constant(2.2 * P.kohm)},
params={"resistance": F.Constant(2.2 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25900"),
params={"resistance": Constant(4.7 * P.kohm)},
params={"resistance": F.Constant(4.7 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25905"),
params={"resistance": Constant(5.1 * P.kohm)},
params={"resistance": F.Constant(5.1 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25917"),
params={"resistance": Constant(6.8 * P.kohm)},
params={"resistance": F.Constant(6.8 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25744"),
params={"resistance": Constant(10 * P.kohm)},
params={"resistance": F.Constant(10 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25752"),
params={"resistance": Constant(12 * P.kohm)},
params={"resistance": F.Constant(12 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25771"),
params={"resistance": Constant(27 * P.kohm)},
params={"resistance": F.Constant(27 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25741"),
params={"resistance": Constant(100 * P.kohm)},
params={"resistance": F.Constant(100 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25782"),
params={"resistance": Constant(390 * P.kohm)},
params={"resistance": F.Constant(390 * P.kohm)},
),
PickerOption(
part=LCSC_Part(partno="C25790"),
params={"resistance": Constant(470 * P.kohm)},
params={"resistance": F.Constant(470 * P.kohm)},
),
],
)
Expand All @@ -187,30 +186,30 @@ def pick_led(module: F.LED):
PickerOption(
part=LCSC_Part(partno="C72043"),
params={
"color": Constant(F.LED.Color.EMERALD),
"max_brightness": Constant(285 * P.mcandela),
"forward_voltage": Constant(3.7 * P.volt),
"max_current": Constant(100 * P.mA),
"color": F.Constant(F.LED.Color.EMERALD),
"max_brightness": F.Constant(285 * P.mcandela),
"forward_voltage": F.Constant(3.7 * P.volt),
"max_current": F.Constant(100 * P.mA),
},
pinmap={"1": module.cathode, "2": module.anode},
),
PickerOption(
part=LCSC_Part(partno="C72041"),
params={
"color": Constant(F.LED.Color.BLUE),
"max_brightness": Constant(28.5 * P.mcandela),
"forward_voltage": Constant(3.1 * P.volt),
"max_current": Constant(100 * P.mA),
"color": F.Constant(F.LED.Color.BLUE),
"max_brightness": F.Constant(28.5 * P.mcandela),
"forward_voltage": F.Constant(3.1 * P.volt),
"max_current": F.Constant(100 * P.mA),
},
pinmap={"1": module.cathode, "2": module.anode},
),
PickerOption(
part=LCSC_Part(partno="C72038"),
params={
"color": Constant(F.LED.Color.YELLOW),
"max_brightness": Constant(180 * P.mcandela),
"forward_voltage": Constant(2.3 * P.volt),
"max_current": Constant(60 * P.mA),
"color": F.Constant(F.LED.Color.YELLOW),
"max_brightness": F.Constant(180 * P.mcandela),
"forward_voltage": F.Constant(2.3 * P.volt),
"max_current": F.Constant(60 * P.mA),
},
pinmap={"1": module.cathode, "2": module.anode},
),
Expand All @@ -225,7 +224,7 @@ def pick_tvs(module: F.TVS):
PickerOption(
part=LCSC_Part(partno="C85402"),
params={
"reverse_working_voltage": Constant(5 * P.V),
"reverse_working_voltage": F.Constant(5 * P.V),
},
pinmap={
"1": module.cathode,
Expand All @@ -252,11 +251,11 @@ def pick_battery(module: F.Battery):
PickerOption(
part=LCSC_Part(partno="C5239862"),
params={
"voltage": Constant(3 * P.V),
"capacity": Range.from_center(225 * P.mAh, 50 * P.mAh),
"material": Constant(F.ButtonCell.Material.Lithium),
"size": Constant(F.ButtonCell.Size.N_2032),
"shape": Constant(F.ButtonCell.Shape.Round),
"voltage": F.Constant(3 * P.V),
"capacity": F.Range.from_center(225 * P.mAh, 50 * P.mAh),
"material": F.Constant(F.ButtonCell.Material.Lithium),
"size": F.Constant(F.ButtonCell.Size.N_2032),
"shape": F.Constant(F.ButtonCell.Shape.Round),
},
pinmap={
"1": module.power.lv,
Expand Down
Loading

0 comments on commit 3a38f09

Please sign in to comment.