-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_led_blinker.py
More file actions
269 lines (231 loc) Β· 7.52 KB
/
Copy pathexample_led_blinker.py
File metadata and controls
269 lines (231 loc) Β· 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
"""
Real-world PCB example: LED blinker circuit with ESP32
This demonstrates:
1. Creating a complete circuit in Circuit IR
2. Validating power domains
3. Validating power budget
4. Generating validation report
5. Saving to JSON
"""
import sys
sys.path.insert(0, 'src')
from pathlib import Path
from agilev.pcb.circuit_ir import CircuitIR, Component, Net, NetType, Pin, PinType, PowerDomain
from agilev.pcb.validators import generate_validation_report, validate_circuit
def create_led_blinker_circuit():
"""Create an ESP32-based LED blinker circuit."""
# Initialize circuit
circuit = CircuitIR(
name="led_blinker",
description="ESP32-C3 LED Blinker with USB-C Power",
version="1.0"
)
print("π§ Creating LED Blinker Circuit...")
# Define power domain (3.3V from USB)
vcc_3v3 = PowerDomain(
name="VCC_3V3",
voltage_nominal=3.3,
voltage_min=3.0,
voltage_max=3.6,
current_max=0.5, # 500mA from USB
nets=["VCC_3V3"],
source_component="U2" # LDO regulator
)
circuit.add_power_domain(vcc_3v3)
print(" β
Added 3.3V power domain")
# Add ESP32-C3 module
esp32 = Component(
id="U1",
type="mcu",
value="ESP32-C3-MINI-1-N4",
package="SMD-53",
manufacturer="Espressif",
part_number="ESP32-C3-MINI-1-N4",
power_domain="VCC_3V3",
power_consumption=0.35, # 350mA max
pins=[
Pin(number="1", name="GND", type=PinType.GROUND, electrical_type="ground"),
Pin(number="2", name="3V3", type=PinType.POWER, electrical_type="power_input"),
Pin(number="8", name="GPIO0", type=PinType.BIDIRECTIONAL, electrical_type="bidirectional"),
Pin(number="9", name="GPIO1", type=PinType.BIDIRECTIONAL, electrical_type="bidirectional"),
]
)
circuit.add_component(esp32)
print(" β
Added ESP32-C3 module")
# Add LDO regulator (5V USB β 3.3V)
ldo = Component(
id="U2",
type="regulator",
value="AP2112K-3.3",
package="SOT-23-5",
manufacturer="Diodes Inc",
part_number="AP2112K-3.3TRG1",
current_rating=0.6, # 600mA output
pins=[
Pin(number="1", name="VIN", type=PinType.POWER, electrical_type="power_input"),
Pin(number="2", name="GND", type=PinType.GROUND, electrical_type="ground"),
Pin(number="3", name="EN", type=PinType.INPUT, electrical_type="input"),
Pin(number="5", name="VOUT", type=PinType.POWER, electrical_type="power_output"),
]
)
circuit.add_component(ldo)
print(" β
Added 3.3V LDO regulator")
# Add status LED
led1 = Component(
id="D1",
type="led",
value="Red",
package="0603",
manufacturer="Wurth",
part_number="150060RS75000",
pins=[
Pin(number="1", name="A", type=PinType.PASSIVE, electrical_type="passive"),
Pin(number="2", name="K", type=PinType.PASSIVE, electrical_type="passive"),
]
)
circuit.add_component(led1)
print(" β
Added status LED")
# Add current limiting resistor for LED
r1 = Component(
id="R1",
type="resistor",
value="330R",
package="0603",
tolerance="1%",
power_rating=0.1,
pins=[
Pin(number="1", name="1", type=PinType.PASSIVE, electrical_type="passive"),
Pin(number="2", name="2", type=PinType.PASSIVE, electrical_type="passive"),
]
)
circuit.add_component(r1)
print(" β
Added LED resistor")
# Add decoupling capacitors
c1 = Component(
id="C1",
type="capacitor",
value="100nF",
package="0603",
voltage_rating=50,
pins=[
Pin(number="1", name="1", type=PinType.PASSIVE, electrical_type="passive"),
Pin(number="2", name="2", type=PinType.PASSIVE, electrical_type="passive"),
]
)
circuit.add_component(c1)
c2 = Component(
id="C2",
type="capacitor",
value="10uF",
package="0805",
voltage_rating=16,
pins=[
Pin(number="1", name="1", type=PinType.PASSIVE, electrical_type="passive"),
Pin(number="2", name="2", type=PinType.PASSIVE, electrical_type="passive"),
]
)
circuit.add_component(c2)
print(" β
Added decoupling capacitors")
# Create nets
# Power nets
vcc_net = Net(
name="VCC_3V3",
type=NetType.POWER,
voltage=3.3,
connections=[
("U1", "2"), # ESP32 VCC
("U2", "5"), # LDO output
("C1", "1"), # Cap 1
("C2", "1"), # Cap 2
]
)
circuit.add_net(vcc_net)
gnd_net = Net(
name="GND",
type=NetType.GROUND,
voltage=0.0,
connections=[
("U1", "1"), # ESP32 GND
("U2", "2"), # LDO GND
("C1", "2"), # Cap 1
("C2", "2"), # Cap 2
("D1", "2"), # LED cathode
]
)
circuit.add_net(gnd_net)
# GPIO signal to LED
gpio0_net = Net(
name="GPIO0_LED",
type=NetType.SIGNAL,
connections=[
("U1", "8"), # ESP32 GPIO0
("R1", "1"), # Resistor
]
)
circuit.add_net(gpio0_net)
led_anode_net = Net(
name="LED_ANODE",
type=NetType.SIGNAL,
connections=[
("R1", "2"), # Resistor
("D1", "1"), # LED anode
]
)
circuit.add_net(led_anode_net)
print(" β
Added all nets (4 nets, 13 connections)")
return circuit
def main():
"""Create and validate the circuit."""
print("=" * 60)
print("LED Blinker Circuit Example")
print("=" * 60)
print()
# Create circuit
circuit = create_led_blinker_circuit()
print()
print("π Circuit Statistics:")
stats = circuit.get_stats()
for key, value in stats.items():
print(f" {key}: {value}")
# Validate connections
print()
print("π Validating Connections...")
errors = circuit.validate_connections()
if errors:
print(" β Validation errors found:")
for error in errors:
print(f" - {error}")
return 1
else:
print(" β
All connections valid")
# Run semantic validators
print()
print("π Running Semantic Validators...")
results = validate_circuit(circuit)
# Generate report
report = generate_validation_report(results)
print()
print(report)
# Save circuit
output_file = Path("examples/pcb/led_blinker_circuit.json")
output_file.parent.mkdir(parents=True, exist_ok=True)
circuit.save(output_file)
print()
print(f"πΎ Circuit saved to: {output_file}")
# Verify we can load it back
loaded = CircuitIR.load(output_file)
print(f"β
Verified: Loaded circuit '{loaded.name}' with {len(loaded.components)} components")
print()
print("=" * 60)
print("β
SUCCESS: Complete LED blinker circuit created and validated!")
print("=" * 60)
print()
print("Next steps:")
print(" 1. Review circuit JSON: examples/pcb/led_blinker_circuit.json")
print(" 2. Create KiCad schematic using this as reference")
print(" 3. Run: agilev pcb validate --task <task-id>")
print(" 4. Generate evidence bundle for manufacturing approval")
return 0
if __name__ == "__main__":
sys.exit(main())