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

Commit

Permalink
Add refactor libTof & run it
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Aug 29, 2024
1 parent a6c268e commit f98968d
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 236 deletions.
33 changes: 16 additions & 17 deletions src/faebryk/exporters/bom/jlcpcb.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# This file is part of the faebryk project
# SPDX-License-Identifier: MIT

import csv
import logging
import os
import re
from dataclasses import dataclass
from pathlib import Path

import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.library.has_descriptive_properties import has_descriptive_properties
from faebryk.library.has_designator import has_designator
from faebryk.library.has_footprint import has_footprint
from faebryk.library.has_kicad_footprint import has_kicad_footprint
from faebryk.library.has_simple_value_representation import (
has_simple_value_representation,
)
from faebryk.libs.picker.picker import DescriptiveProperties

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -104,30 +101,30 @@ def _compact_bomlines(bomlines: list[BOMLine]) -> list[BOMLine]:


def _get_bomline(cmp: Module) -> BOMLine | None:
if not cmp.has_trait(has_footprint):
if not cmp.has_trait(F.has_footprint):
return

if not all(
cmp.has_trait(t)
for t in (
has_descriptive_properties,
has_designator,
F.has_descriptive_properties,
F.has_designator,
)
):
logger.warning(f"Missing fields on component {cmp}")
return

properties = cmp.get_trait(has_descriptive_properties).get_properties()
footprint = cmp.get_trait(has_footprint).get_footprint()
properties = cmp.get_trait(F.has_descriptive_properties).get_properties()
footprint = cmp.get_trait(F.has_footprint).get_footprint()

value = (
cmp.get_trait(has_simple_value_representation).get_value()
if cmp.has_trait(has_simple_value_representation)
cmp.get_trait(F.has_simple_value_representation).get_value()
if cmp.has_trait(F.has_simple_value_representation)
else ""
)
designator = cmp.get_trait(has_designator).get_designator()
designator = cmp.get_trait(F.has_designator).get_designator()

if not footprint.has_trait(has_kicad_footprint):
if not footprint.has_trait(F.has_kicad_footprint):
logger.warning(f"Missing kicad footprint on component {cmp}")
return

Expand All @@ -145,7 +142,9 @@ def _get_bomline(cmp: Module) -> BOMLine | None:
else ""
)

footprint_name = footprint.get_trait(has_kicad_footprint).get_kicad_footprint_name()
footprint_name = footprint.get_trait(
F.has_kicad_footprint
).get_kicad_footprint_name()

return BOMLine(
Designator=designator,
Expand Down
9 changes: 4 additions & 5 deletions src/faebryk/exporters/esphome/esphome.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import yaml

import faebryk.library._F as F
from faebryk.core.graphinterface import Graph
from faebryk.core.parameter import Parameter
from faebryk.library.Constant import Constant
from faebryk.library.has_esphome_config import has_esphome_config

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,17 +55,17 @@ def merge_dicts(*dicts: dict) -> dict:
def make_esphome_config(G: Graph) -> dict:
from faebryk.core.util import get_all_nodes_with_trait

esphome_components = get_all_nodes_with_trait(G, has_esphome_config)
esphome_components = get_all_nodes_with_trait(G, F.has_esphome_config)

esphome_config = merge_dicts(*[t.get_config() for _, t in esphome_components])

def instantiate_param(param: Parameter | Any):
if not isinstance(param, Parameter):
return param

if not isinstance(param, Constant):
if not isinstance(param, F.Constant):
raise Exception(
f"Parameter {param} is not a Constant, but {type(param)}"
f"Parameter {param} is not a F.Constant, but {type(param)}"
f"Config: {esphome_config}"
)
return param.value
Expand Down
54 changes: 20 additions & 34 deletions src/faebryk/exporters/netlist/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,15 @@

import networkx as nx

import faebryk.library._F as F
from faebryk.core.graphinterface import Graph
from faebryk.core.module import Module
from faebryk.exporters.netlist.netlist import T2Netlist
from faebryk.library.Electrical import Electrical
from faebryk.library.Footprint import Footprint
from faebryk.library.has_descriptive_properties import has_descriptive_properties
from faebryk.library.has_descriptive_properties_defined import (
has_descriptive_properties_defined,
)
from faebryk.library.has_footprint import has_footprint
from faebryk.library.has_kicad_footprint import has_kicad_footprint
from faebryk.library.has_overriden_name import has_overriden_name
from faebryk.library.has_overriden_name_defined import has_overriden_name_defined
from faebryk.library.has_simple_value_representation import (
has_simple_value_representation,
)
from faebryk.library.Net import Net
from faebryk.library.Pad import Pad

logger = logging.getLogger(__name__)


class can_represent_kicad_footprint(Footprint.TraitT):
class can_represent_kicad_footprint(F.Footprint.TraitT):
kicad_footprint = T2Netlist.Component

@abstractmethod
Expand All @@ -38,19 +24,19 @@ def get_name_and_value(self) -> tuple[str, str]: ...
def get_kicad_obj(self) -> kicad_footprint: ...

@abstractmethod
def get_pin_name(self, pin: Pad) -> str: ...
def get_pin_name(self, pin: F.Pad) -> str: ...


def get_or_set_name_and_value_of_node(c: Module):
value = (
c.get_trait(has_simple_value_representation).get_value()
if c.has_trait(has_simple_value_representation)
c.get_trait(F.has_simple_value_representation).get_value()
if c.has_trait(F.has_simple_value_representation)
else type(c).__name__
)

if not c.has_trait(has_overriden_name):
if not c.has_trait(F.has_overriden_name):
c.add_trait(
has_overriden_name_defined(
F.has_overriden_name_defined(
"{}[{}:{}]".format(
c.get_full_name(),
type(c).__name__,
Expand All @@ -59,11 +45,11 @@ def get_or_set_name_and_value_of_node(c: Module):
)
)

has_descriptive_properties_defined.add_properties_to(
F.has_descriptive_properties_defined.add_properties_to(
c, {"faebryk_name": c.get_full_name()}
)

return c.get_trait(has_overriden_name).get_name(), value
return c.get_trait(F.has_overriden_name).get_name(), value


class can_represent_kicad_footprint_via_attached_component(
Expand All @@ -81,20 +67,20 @@ def __init__(self, component: Module, graph: nx.Graph) -> None:
def get_name_and_value(self):
return get_or_set_name_and_value_of_node(self.component)

def get_pin_name(self, pin: Pad):
return self.obj.get_trait(has_kicad_footprint).get_pin_names()[pin]
def get_pin_name(self, pin: F.Pad):
return self.obj.get_trait(F.has_kicad_footprint).get_pin_names()[pin]

def get_kicad_obj(self):
fp = self.get_obj(Footprint)
fp = self.get_obj(F.Footprint)

properties = {
"footprint": fp.get_trait(has_kicad_footprint).get_kicad_footprint()
"footprint": fp.get_trait(F.has_kicad_footprint).get_kicad_footprint()
}

for c in [fp, self.component]:
if c.has_trait(has_descriptive_properties):
if c.has_trait(F.has_descriptive_properties):
properties.update(
c.get_trait(has_descriptive_properties).get_properties()
c.get_trait(F.has_descriptive_properties).get_properties()
)

name, value = self.get_name_and_value()
Expand All @@ -106,17 +92,17 @@ def get_kicad_obj(self):
)


def add_or_get_net(interface: Electrical):
def add_or_get_net(interface: F.Electrical):
from faebryk.core.util import get_connected_mifs

mifs = get_connected_mifs(interface.connected)
nets = {
p[0]
for mif in mifs
if (p := mif.get_parent()) is not None and isinstance(p[0], Net)
if (p := mif.get_parent()) is not None and isinstance(p[0], F.Net)
}
if not nets:
net = Net()
net = F.Net()
net.part_of.connect(interface)
return net
if len(nets) > 1:
Expand All @@ -135,7 +121,7 @@ def attach_nets_and_kicad_info(g: Graph):
n: t.get_footprint()
# TODO maybe nicer to just look for footprints
# and get their respective components instead
for n, t in get_all_nodes_with_trait(Gclosed, has_footprint)
for n, t in get_all_nodes_with_trait(Gclosed, F.has_footprint)
if isinstance(n, Module)
}

Expand All @@ -151,5 +137,5 @@ def attach_nets_and_kicad_info(g: Graph):

for fp in node_fps.values():
# TODO use graph
for mif in fp.get_children(direct_only=True, types=Pad):
for mif in fp.get_children(direct_only=True, types=F.Pad):
add_or_get_net(mif.net)
7 changes: 3 additions & 4 deletions src/faebryk/exporters/netlist/netlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import logging
from dataclasses import dataclass

import faebryk.library._F as F
from faebryk.core.graphinterface import Graph
from faebryk.library.has_footprint import has_footprint
from faebryk.library.has_overriden_name import has_overriden_name

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,7 +44,7 @@ def make_t2_netlist_from_graph(G: Graph) -> T2Netlist:

t2_nets = [
T2Netlist.Net(
properties={"name": net.get_trait(has_overriden_name).get_name()},
properties={"name": net.get_trait(F.has_overriden_name).get_name()},
vertices=sorted(
[
T2Netlist.Net.Vertex(
Expand All @@ -63,7 +62,7 @@ def make_t2_netlist_from_graph(G: Graph) -> T2Netlist:

comps = {
t.get_footprint().get_trait(can_represent_kicad_footprint).get_kicad_obj()
for _, t in get_all_nodes_with_trait(G, has_footprint)
for _, t in get_all_nodes_with_trait(G, F.has_footprint)
}

not_found = [
Expand Down
12 changes: 5 additions & 7 deletions src/faebryk/exporters/pcb/routing/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import networkx as nx

import faebryk.library._F as F
from faebryk.exporters.pcb.kicad.transformer import PCB_Transformer
from faebryk.exporters.pcb.routing.grid import (
Coord,
Expand All @@ -18,8 +19,6 @@
GridInvalidVertexException,
OutCoord,
)
from faebryk.library.has_overriden_name import has_overriden_name
from faebryk.library.Net import Net
from faebryk.libs.geometry.basic import Geometry
from faebryk.libs.kicad.pcb import Footprint, GR_Circle, GR_Line, GR_Rect, Pad

Expand Down Expand Up @@ -126,13 +125,12 @@ def draw_circle(self, coord: OutCoord, size=0.5, layer="User.9"):

def route_all(self):
from faebryk.core.util import get_all_nodes_of_type
from faebryk.library.Net import Net as FNet

nets = get_all_nodes_of_type(self.transformer.graph, FNet)
nets = get_all_nodes_of_type(self.transformer.graph, F.Net)

# TODO add net picking heuristic
for net in nets:
netname = net.get_trait(has_overriden_name).get_name()
netname = net.get_trait(F.has_overriden_name).get_name()
try:
self.route_net(net)
except GridInvalidVertexException as e:
Expand All @@ -158,12 +156,12 @@ def layer(p):

return [layer(p) for p in pad.pos]

def route_net(self, net: Net):
def route_net(self, net: F.Net):
transformer = self.transformer

assert net is not None
pcb_net = transformer.get_net(net)
net_name = net.get_trait(has_overriden_name).get_name()
net_name = net.get_trait(F.has_overriden_name).get_name()
mifs = net.get_connected_interfaces()

# get pads
Expand Down
24 changes: 11 additions & 13 deletions src/faebryk/exporters/pcb/routing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Iterable, Sequence

import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.core.moduleinterface import ModuleInterface
from faebryk.core.node import Node
from faebryk.library.Electrical import Electrical
from faebryk.library.Net import Net
from faebryk.library.Pad import Pad
from faebryk.libs.geometry.basic import Geometry

if TYPE_CHECKING:
Expand Down Expand Up @@ -89,12 +87,12 @@ def __add__(self, other: "Path"):


class Route(Module):
net_: Electrical
net_: F.Electrical
pcb: ModuleInterface

def __init__(
self,
pads: Iterable[Pad],
pads: Iterable[F.Pad],
path: Path | None = None,
):
super().__init__()
Expand Down Expand Up @@ -172,7 +170,7 @@ def apply_route_in_pcb(route: Route, transformer: "PCB_Transformer"):

def get_internal_nets_of_node(
node: Node,
) -> dict[Net | None, Iterable[ModuleInterface]]:
) -> dict[F.Net | None, Iterable[ModuleInterface]]:
"""
Returns all Nets occuring (at least partially) within Node
and returns for each of those the corresponding mifs
Expand All @@ -186,16 +184,16 @@ def get_internal_nets_of_node(
)
from faebryk.libs.util import groupby

if isinstance(node, Net):
if isinstance(node, F.Net):
return {node: get_connected_mifs(node.part_of.connected)}

mifs = {n for n in get_all_nodes(node) + [node] if isinstance(n, Electrical)}
mifs = {n for n in get_all_nodes(node) + [node] if isinstance(n, F.Electrical)}
nets = groupby(mifs, lambda mif: get_net(mif))

return nets


def get_pads_pos_of_mifs(mifs: Sequence[Electrical]):
def get_pads_pos_of_mifs(mifs: Sequence[F.Electrical]):
from faebryk.exporters.pcb.kicad.transformer import PCB_Transformer

return {
Expand All @@ -206,9 +204,9 @@ def get_pads_pos_of_mifs(mifs: Sequence[Electrical]):


def group_pads_that_are_connected_already(
pads: Iterable[Pad],
) -> list[set[Pad]]:
out: list[set[Pad]] = []
pads: Iterable[F.Pad],
) -> list[set[F.Pad]]:
out: list[set[F.Pad]] = []
for pad in pads:
for group in out:
# Only need to check first, because transitively connected
Expand All @@ -220,7 +218,7 @@ def group_pads_that_are_connected_already(
return out


def get_routes_of_pad(pad: Pad):
def get_routes_of_pad(pad: F.Pad):
from faebryk.core.util import get_parent_of_type

return {
Expand Down
Loading

0 comments on commit f98968d

Please sign in to comment.