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 block;
44pub mod bus;
45pub mod bus_parts;
46pub mod chip;
47pub mod devices;
48pub mod displays;
49pub mod fat;
50pub mod flatten;
51/// The JavaScript host bridge for custom parts and chips (wasm32 builds).
52#[cfg(target_arch = "wasm32")]
53pub mod host;
54pub mod ieee802154_parts;
55pub mod inputs;
56pub mod logic;
57pub mod module_parts;
58pub mod network_parts;
59pub mod outputs;
60pub mod parts;
61pub mod pin_parts;
62pub mod pixels;
63pub mod potentiometer;
64pub mod probe;
65pub mod protocol_displays;
66pub mod radio_parts;
67pub mod runtime;
68pub mod segments;
69pub mod sensor_ics;
70pub mod sensors;
71pub mod signal_gen;
72pub mod sigrok;
73pub mod storage;
74pub mod switch;
75pub mod timing;
76pub mod touch;
77pub mod waveform;
78
79pub use analog::{AnalogConfig, AnalogSolution};
80pub use block::Image;
81pub use bus::{Buses, I2c, I2cDevice, Spi, SpiDevice, Uart, UartPeer};
82pub use bus_parts::{DateTime, Ds3231, Mcp3008, Mpu6050, Pcf8574, Ssd1306};
83pub use chip::{Chip, ChipDebug, ChipInfo, FlashLayout, ImageKind, Input, RunResult, Stop};
84pub use devices::Bmp280;
85pub use displays::{Epd, EpdSize, Hd44780, Ili9341, Lcd1602, Lcd2004};
86pub use flatten::{
87    Device, Endpoint, Engine, FlatBoard, FlatBuses, FlatPart, I2cBus, Rail, SpiBus, UartBus,
88    WasmModule,
89};
90pub use inputs::{AnalogJoystick, DipSwitch, MembraneKeypad, RotaryEncoder, TiltSwitch};
91pub use litz_ble::{Beacon, BleConfig};
92pub use litz_radio::{Packet, RadioConfig, RadioHub, RadioHubHandle, RadioId, RadioStats};
93pub use logic::{Cd4017, Cd4051, Cd4094, Gate, GateFn, Hc164, Hc573};
94pub use outputs::{NeoPixel, Stepper};
95pub use pin_parts::{
96    Bjt, Capacitor, Dht22, DhtReading, Hc138, Hc165, HcSr04, Inductor, L293d, LedBarGraph, Mosfet,
97    NtcSensor, Opamp, Photoresistor, PirMotionSensor, Relay, RgbLed,
98};
99pub use pixels::{Diff, Framebuffer, PixelFormat, compare_framebuffers};
100pub use probe::{Channel, Measurements, Probe, Series};
101pub use radio_parts::{Nrf24l01, Sx1276};
102pub use runtime::{
103    ANALOG_DEFAULT_DT_NS, Board, CONNECTION_DELAY_NS, CONNECTION_EVENT_NS, CONNECTION_INTERVAL_NS,
104    Conflict,
105};
106pub use sensors::{
107    FlameSensor, GasSensor, GpsNeo6m, HeartBeatSensor, Hx711, IrReceiver, NecFrame, Pms5003,
108};
109pub use signal_gen::{SignalGenerator, Wave};
110pub use storage::MicroSd;
111pub use touch::Xpt2046;
112
113#[derive(Debug, thiserror::Error)]
114pub enum BuildError {
115    #[error("the diagram has errors:\n{}", flatten::render(.0))]
116    Diagnostics(Vec<litz_diagram::Diagnostic>),
117    #[error("{0}")]
118    Message(String),
119    #[error("{0}")]
120    Diagram(#[from] litz_diagram::DiagramError),
121}