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

Commit

Permalink
RecursionGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Sep 12, 2024
1 parent cbe8a3c commit ebfedb2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
12 changes: 11 additions & 1 deletion examples/minimal_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@
class App(Module):
led: F.PoweredLED
battery: F.Battery
mcu: F.RP2040

def __preinit__(self) -> None:
self.led.power.connect(self.battery.power)
# self.led.power.connect(self.battery.power)

# Parametrize
self.led.led.color.merge(F.LED.Color.YELLOW)
self.led.led.brightness.merge(
TypicalLuminousIntensity.APPLICATION_LED_INDICATOR_INSIDE.value.value
)

F.ElectricLogic.connect_all_node_references(
[self.led, self.battery, self.mcu], gnd_only=True
)
self.mcu.power_io.connect(self.battery.power)

self.mcu.i2c[0].sda.signal.connect(self.led.power.hv)
self.mcu.i2c[0].sda.reference.connect_shallow(self.led.power)
self.mcu.pinmux.enable(self.mcu.i2c[0].sda, pins=list(range(10, 20)))


# Boilerplate -----------------------------------------------------------------

Expand Down
20 changes: 9 additions & 11 deletions src/faebryk/library/ElectricLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from enum import Enum, auto
from typing import Iterable, Self

from deprecated import deprecated

Check failure on line 9 in src/faebryk/library/ElectricLogic.py

View workflow job for this annotation

GitHub Actions / test

Ruff (F401)

src/faebryk/library/ElectricLogic.py:9:24: F401 `deprecated.deprecated` imported but unused
import faebryk.library._F as F
from faebryk.core.graphinterface import GraphInterface
from faebryk.core.link import LinkFilteredException, _TLinkDirectShallow
from faebryk.core.module import Module
from faebryk.core.moduleinterface import ModuleInterface
from faebryk.core.node import Node
from faebryk.libs.library import L
from faebryk.libs.util import RecursionGuard


class ElectricLogic(F.SignalElectrical, F.Logic):

Check failure on line 20 in src/faebryk/library/ElectricLogic.py

View workflow job for this annotation

GitHub Actions / test

Ruff (I001)

src/faebryk/library/ElectricLogic.py:4:1: I001 Import block is un-sorted or un-formatted
Expand Down Expand Up @@ -171,14 +173,15 @@ def connect_all_node_references(
F.Electrical.connect(*{r.lv for r in refs})
return next(iter(refs))

# TODO remove this workaround when we have lazy mifs
recursion_depth = sys.getrecursionlimit()
sys.setrecursionlimit(10000)
F.ElectricPower.connect(*refs)
sys.setrecursionlimit(recursion_depth)
with RecursionGuard():
F.ElectricPower.connect(*refs)

return next(iter(refs))

def connect(self: Self, *other: Self, linkcls=None) -> Self:
with RecursionGuard():
return super().connect(*other, linkcls=linkcls)

@classmethod
def connect_all_module_references(
cls, node: Module | ModuleInterface, gnd_only=False
Expand All @@ -188,12 +191,6 @@ def connect_all_module_references(
gnd_only=gnd_only,
)

# def connect_shallow(self, other: "ElectricLogic"):
# self.connect(
# other,
# linkcls=self.LinkDirectShallowLogic,
# )

def connect_via_bridge(
self, bridge: Module, up: bool, bridge_ref_to_signal: bool = False
):
Expand All @@ -209,6 +206,7 @@ def connect_shallow(
reference: bool = False,
lv: bool = False,
) -> Self:
# TODO this should actually use shallow links
assert not (signal and reference)
assert not (lv and reference)

Expand Down
2 changes: 1 addition & 1 deletion src/faebryk/library/RP2040_Reference_Design.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RP2040_Reference_Design(Module):

rp2040: F.RP2040
flash: F.SPIFlash
led: F.PoweredLED
led = L.f_field(F.LEDIndicator)(use_mosfet=False)
usb_current_limit_resistor = L.list_field(2, F.Resistor)
reset_button: F.Button
boot_button: F.Button
Expand Down
2 changes: 1 addition & 1 deletion src/faebryk/library/_F.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@
from faebryk.library.CBM9002A_56ILG_Reference_Design import CBM9002A_56ILG_Reference_Design
from faebryk.library.USB_RS485 import USB_RS485
from faebryk.library.ESP32_C3_MINI_1 import ESP32_C3_MINI_1
from faebryk.library.RP2040_Reference_Design import RP2040_Reference_Design
from faebryk.library.USB_C_PSU_Vertical import USB_C_PSU_Vertical
from faebryk.library.USB3_connector import USB3_connector
from faebryk.library.USB_C import USB_C
Expand All @@ -218,3 +217,4 @@
from faebryk.library.USB_C_PowerOnly import USB_C_PowerOnly
from faebryk.library.Powered_Relay import Powered_Relay
from faebryk.library.LEDIndicator import LEDIndicator
from faebryk.library.RP2040_Reference_Design import RP2040_Reference_Design
11 changes: 11 additions & 0 deletions src/faebryk/libs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dataclasses import dataclass, fields
from enum import StrEnum
from functools import cache
import sys
from textwrap import indent
from typing import (
Any,
Expand Down Expand Up @@ -858,3 +859,13 @@ def join_if_non_empty(sep: str, *args):

def dataclass_as_kwargs(obj: Any) -> dict[str, Any]:
return {f.name: getattr(obj, f.name) for f in fields(obj)}


class RecursionGuard:
# TODO remove this workaround when we have lazy mifs
def __enter__(self):
self.recursion_depth = sys.getrecursionlimit()
sys.setrecursionlimit(10000)

def __exit__(self, exc_type, exc_value, traceback):
sys.setrecursionlimit(self.recursion_depth)

0 comments on commit ebfedb2

Please sign in to comment.