Skip to main content

litz_board/
lib.rs

1//! Turning a diagram into something that can be run.
2//!
3//! Two passes:
4//!
5//! * [`flatten`] expands development boards and rewrites names, producing one
6//!   flat part list and one set of nets. No hierarchy survives.
7//! * [`runtime::Board`] owns the part instances, resolves each net's level
8//!   from the drives on it, and reports what the chip should see on its input
9//!   pads.
10//!
11//! The crate knows nothing about ESP32 or any other backend. An adapter feeds
12//! it `set_chip_output` and forwards `take_chip_inputs`.
13//!
14//! ```
15//! use litz_catalog::Registry;
16//! use litz_diagram::Circuit;
17//! use litz_board::{Board, FlatBoard};
18//!
19//! let registry = Registry::builtin();
20//! let circuit = Circuit::from_json(r#"{
21//!   "parts": [
22//!     {"id": "esp", "type": "board-esp32-c3-devkitm-1", "attrs": {}},
23//!     {"id": "r1", "type": "litz-resistor", "attrs": {"value": "220"}},
24//!     {"id": "led1", "type": "litz-led", "attrs": {"color": "green"}}
25//!   ],
26//!   "connections": [
27//!     ["esp:GPIO8", "r1:1", "", []],
28//!     ["r1:2", "led1:A", "", []],
29//!     ["led1:C", "esp:GND.1", "", []]
30//!   ]
31//! }"#).unwrap();
32//! let mut board = Board::new(FlatBoard::build(&circuit, &registry).unwrap()).unwrap();
33//! board.set_chip_output(8, litz_core::Drive::strong(litz_core::Level::High), 0);
34//! assert_eq!(board.state("led1"), Some(litz_core::PartState::Led { lit: true }));
35//! ```
36// The host bridge to JavaScript components declares `extern "C"` imports on
37// wasm32, which edition 2024 counts as unsafe code. It is the one module that
38// needs it; everything else stays safe.
39#![deny(unsafe_code)]
40
41pub mod analog;
42pub mod ble_parts;
43pub mod bus;
44pub mod bus_parts;
45pub mod chip;
46pub mod devices;
47pub mod displays;
48pub mod flatten;
49/// The JavaScript host bridge for custom parts and chips (wasm32 builds).
50#[cfg(target_arch = "wasm32")]
51pub mod host;
52pub mod ieee802154_parts;
53pub mod inputs;
54pub mod logic;
55pub mod module_parts;
56pub mod network_parts;
57pub mod outputs;
58pub mod parts;
59pub mod pin_parts;
60pub mod pixels;
61pub mod potentiometer;
62pub mod probe;
63pub mod protocol_displays;
64pub mod radio_parts;
65pub mod runtime;
66pub mod segments;
67pub mod sensor_ics;
68pub mod sensors;
69pub mod signal_gen;
70pub mod storage;
71pub mod switch;
72pub mod timing;
73pub mod touch;
74pub mod waveform;
75
76pub use analog::{AnalogConfig, AnalogSolution};
77pub use bus::{Buses, I2c, I2cDevice, Spi, SpiDevice, Uart, UartPeer};
78pub use bus_parts::{DateTime, Ds3231, Mcp3008, Mpu6050, Pcf8574, Ssd1306};
79pub use chip::{Chip, ChipDebug, ChipInfo, FlashLayout, ImageKind, Input, RunResult, Stop};
80pub use devices::Bmp280;
81pub use displays::{Epd, EpdSize, Hd44780, Ili9341, Lcd1602, Lcd2004};
82pub use flatten::{
83    Device, Endpoint, Engine, FlatBoard, FlatBuses, FlatPart, I2cBus, Rail, SpiBus, UartBus,
84    WasmModule,
85};
86pub use inputs::{AnalogJoystick, DipSwitch, MembraneKeypad, RotaryEncoder, TiltSwitch};
87pub use litz_ble::{Beacon, BleConfig};
88pub use litz_radio::{Packet, RadioConfig, RadioHub, RadioHubHandle, RadioId, RadioStats};
89pub use logic::{Cd4017, Cd4051, Cd4094, Gate, GateFn, Hc164, Hc573};
90pub use outputs::{NeoPixel, Stepper};
91pub use pin_parts::{
92    Bjt, Capacitor, Dht22, DhtReading, Hc138, Hc165, HcSr04, Inductor, L293d, LedBarGraph, Mosfet,
93    NtcSensor, Opamp, Photoresistor, PirMotionSensor, Relay, RgbLed,
94};
95pub use pixels::{Diff, Framebuffer, PixelFormat, compare_framebuffers};
96pub use probe::{Channel, Measurements, Probe, Series};
97pub use radio_parts::{Nrf24l01, Sx1276};
98pub use runtime::{
99    ANALOG_DEFAULT_DT_NS, Board, CONNECTION_DELAY_NS, CONNECTION_EVENT_NS, CONNECTION_INTERVAL_NS,
100    Conflict,
101};
102pub use sensors::{
103    FlameSensor, GasSensor, GpsNeo6m, HeartBeatSensor, Hx711, IrReceiver, NecFrame, Pms5003,
104};
105pub use signal_gen::{SignalGenerator, Wave};
106pub use storage::MicroSd;
107pub use touch::Xpt2046;
108
109#[derive(Debug, thiserror::Error)]
110pub enum BuildError {
111    #[error("the diagram has errors:\n{}", flatten::render(.0))]
112    Diagnostics(Vec<litz_diagram::Diagnostic>),
113    #[error("{0}")]
114    Message(String),
115    #[error("{0}")]
116    Diagram(#[from] litz_diagram::DiagramError),
117}