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

Commit

Permalink
Update syntax to faebryk 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Sep 3, 2024
1 parent 222e92d commit 799b479
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 104 deletions.
27 changes: 11 additions & 16 deletions examples/signal_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,38 @@

import logging

import faebryk.library._F as F
import typer
from faebryk.core.core import Module

import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.core.util import specialize_module
from faebryk.libs.experiments.buildutil import (
tag_and_export_module_to_netlist,
)
from faebryk.libs.examples.buildutil import apply_design_to_pcb
from faebryk.libs.logging import setup_basic_logging
from faebryk.libs.units import P

logger = logging.getLogger(__name__)


class App(Module):
def __init__(self) -> None:
super().__init__()

class _NODES(Module.NODES()):
lowpass = F.Filter()

self.NODEs = _NODES(self)
lowpass: F.Filter

def __preinit__(self) -> None:
# TODO actually do something with the filter

# Parametrize
self.NODEs.lowpass.PARAMs.cutoff_frequency.merge(200)
self.NODEs.lowpass.PARAMs.response.merge(F.Filter.Response.LOWPASS)
self.lowpass.cutoff_frequency.merge(200 * P.hz)
self.lowpass.response.merge(F.Filter.Response.LOWPASS)

# Specialize
specialize_module(self.NODEs.lowpass, F.FilterElectricalLC())
specialize_module(self.lowpass, F.FilterElectricalLC())


def main():
logger.info("Building app")
app = App()

logger.info("Export")
tag_and_export_module_to_netlist(app)
apply_design_to_pcb(app)


if __name__ == "__main__":
Expand Down
26 changes: 6 additions & 20 deletions src/faebryk/library/Filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

from enum import Enum, auto

import faebryk.library._F as F
from faebryk.core.core import Module
from faebryk.library.Signal import Signal
from faebryk.library.TBD import TBD


class Filter(Module):
Expand All @@ -16,22 +15,9 @@ class Response(Enum):
BANDSTOP = auto()
OTHER = auto()

@classmethod
def PARAMS(cls):
class _PARAMs(super().PARAMS()):
cutoff_frequency = TBD[float]()
order = TBD[int]()
response = TBD[Filter.Response]()
cutoff_frequency: F.TBD[float]
order: F.TBD[int]
response: F.TBD[Response]

return _PARAMs

def __init__(self):
super().__init__()

self.PARAMs = self.PARAMS()(self)

class _IFs(super().IFS()):
in_ = Signal()
out = Signal()

self.IFs = _IFs(self)
in_: F.Signal
out: F.Signal
69 changes: 21 additions & 48 deletions src/faebryk/library/FilterElectricalLC.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,40 @@

import math

from faebryk.core.util import specialize_interface
from faebryk.library.Capacitor import Capacitor
from faebryk.library.Filter import Filter
from faebryk.library.has_parameter_construction_dependency import (
has_parameter_construction_dependency,
)
from faebryk.library.Inductor import Inductor
from faebryk.library.SignalElectrical import SignalElectrical
import faebryk.library._F as F
from faebryk.libs.library import L


class FilterElectricalLC(Filter):
@classmethod
def PARAMS(cls):
class _PARAMs(super().PARAMS()): ...
class FilterElectricalLC(F.Filter):
in_: F.SignalElectrical
out: F.SignalElectrical
capacitor: F.Capacitor
inductor: F.Inductor

return _PARAMs

def __init__(self):
super().__init__()

self.PARAMs = self.PARAMS()(self)

self.IFs_filter = self.IFs

class _IFs(super().IFS()):
in_ = SignalElectrical()
out = SignalElectrical()

self.IFs = _IFs(self)

specialize_interface(self.IFs_filter.in_, self.IFs.in_)
specialize_interface(self.IFs_filter.out, self.IFs.out)

class _NODES(super().NODES()):
capacitor = Capacitor()
inductor = Inductor()

self.NODEs = _NODES(self)
def __preinit__(self) -> None: ...

@L.rt_field
def has_parameter_construction_dependency(self):
class _has_parameter_construction_dependency(
has_parameter_construction_dependency.impl()
F.has_parameter_construction_dependency.impl()
):
def construct(_self):
if not self._construct():
return
_self._fullfill()

self.add_trait(_has_parameter_construction_dependency())
return _has_parameter_construction_dependency()

def _construct(self):
# TODO other responses
self.PARAMs.response.merge(Filter.Response.LOWPASS)
self.response.merge(F.Filter.Response.LOWPASS)

# TODO other orders
self.PARAMs.order.merge(2)
self.order.merge(2)

L = self.NODEs.inductor.PARAMs.inductance
C = self.NODEs.capacitor.PARAMs.capacitance
fc = self.PARAMs.cutoff_frequency
L = self.inductor.inductance
C = self.capacitor.capacitance
fc = self.cutoff_frequency

# TODO requires parameter constraint solving implemented
# fc.merge(1 / (2 * math.pi * math.sqrt(C * L)))
Expand All @@ -72,11 +47,9 @@ def _construct(self):
# TODO consider splitting C / L in a typical way

# low pass
self.IFs.in_.IFs.signal.connect_via(
(self.NODEs.inductor, self.NODEs.capacitor),
self.IFs.in_.IFs.reference.IFs.lv,
self.in_.signal.connect_via(
(self.inductor, self.capacitor),
self.in_.reference.lv,
)

self.IFs.in_.IFs.signal.connect_via(
self.NODEs.inductor, self.IFs.out.IFs.signal
)
self.in_.signal.connect_via(self.inductor, self.out.signal)
20 changes: 6 additions & 14 deletions src/faebryk/library/SignalElectrical.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
# This file is part of the faebryk project
# SPDX-License-Identifier: MIT

from faebryk.library.Electrical import Electrical
from faebryk.library.ElectricPower import ElectricPower
from faebryk.library.Signal import Signal
import faebryk.library._F as F


class SignalElectrical(Signal):
def __init__(self) -> None:
super().__init__()

class _IFs(Signal.IFS()):
# line is a better name, but for compatibility with Logic we use signal
# might change in future
signal = Electrical()
reference = ElectricPower()

self.IFs = _IFs(self)
class SignalElectrical(F.Signal):
# line is a better name, but for compatibility with Logic we use signal
# might change in future
signal: F.Electrical
reference: F.ElectricPower
7 changes: 3 additions & 4 deletions src/faebryk/library/has_construction_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

from abc import abstractmethod

from faebryk.core.core import NodeTrait
from faebryk.core.trait import Trait


class has_construction_dependency(NodeTrait):
def __init__(self) -> None:
super().__init__()
class has_construction_dependency(Trait):
def __preinit__(self) -> None:
self.executed = False

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions src/faebryk/library/has_parameter_construction_dependency.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is part of the faebryk project
# SPDX-License-Identifier: MIT

from faebryk.library.has_construction_dependency import has_construction_dependency
import faebryk.library._F as F


class has_parameter_construction_dependency(has_construction_dependency): ...
class has_parameter_construction_dependency(F.has_construction_dependency): ...

0 comments on commit 799b479

Please sign in to comment.