use crate::{Ngspice, Primitive};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use substrate::block::Block;
use substrate::io::schematic::HardwareType;
use substrate::io::TwoTerminalIo;
use substrate::schematic::primitives::DcVsource;
use substrate::schematic::{CellBuilder, ExportsNestedData, PrimitiveBinding, Schematic};
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Pulse {
pub val0: Decimal,
pub val1: Decimal,
pub period: Option<Decimal>,
pub rise: Option<Decimal>,
pub fall: Option<Decimal>,
pub width: Option<Decimal>,
pub delay: Option<Decimal>,
pub num_pulses: Option<Decimal>,
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, PartialEq, Eq, Block)]
#[substrate(io = "TwoTerminalIo")]
pub enum Vsource {
Dc(Decimal),
Pulse(Pulse),
}
impl Vsource {
pub fn dc(value: Decimal) -> Self {
Self::Dc(value)
}
pub fn pulse(value: Pulse) -> Self {
Self::Pulse(value)
}
}
impl ExportsNestedData for Vsource {
type NestedData = ();
}
impl Schematic<Ngspice> for Vsource {
fn schematic(
&self,
io: &<<Self as Block>::Io as HardwareType>::Bundle,
cell: &mut CellBuilder<Ngspice>,
) -> substrate::error::Result<Self::NestedData> {
let mut prim = PrimitiveBinding::new(Primitive::Vsource(*self));
prim.connect("P", io.p);
prim.connect("N", io.n);
cell.set_primitive(prim);
Ok(())
}
}
impl Schematic<Ngspice> for DcVsource {
fn schematic(
&self,
io: &<<Self as Block>::Io as HardwareType>::Bundle,
cell: &mut CellBuilder<Ngspice>,
) -> substrate::error::Result<Self::NestedData> {
cell.flatten();
cell.instantiate_connected(Vsource::dc(self.value()), io);
Ok(())
}
}