rbd_dimmer

Crates.iorbd_dimmer
lib.rsrbd_dimmer
version0.2.0
sourcesrc
created_at2023-12-31 16:15:24.308905
updated_at2024-07-04 10:35:19.404601
descriptionRewrite RBDDimmer for Rust. Work only on ESP32
homepagehttps://github.com/emeric-martineau/rbd_dimmer
repositoryhttps://github.com/emeric-martineau/rbd_dimmer
max_upload_size
id1084892
size539,664
Emeric MARTINEAU (emeric-martineau)

documentation

README

Rust crate for RdbDimmer

What is RdbDimmer?

RdbDimmer is device allow dim power of load. Device use a MOC3021 (no zero-crossing detection).

You can visit RobotDyn Official Store or Official Web site.

Rdb Dimmer Rdb Dimmer schema

Compatibility hardware

This crate works only on ESP32-WROOM-32 (2016) microcontroler.

ESP SKK config

You need add in your sdkconfig.defaults file (at root of your rust project):

CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD=y
CONFIG_ESP_TIMER_INTERRUPT_LEVEL=1

Example

unsafe {
    let zero_crossing_pin: PinDriver<'static, AnyInputPin, Input> = PinDriver::input(AnyInputPin::new(2)).unwrap();
    let d0_pin: PinDriver<'static, AnyOutputPin, Output> = PinDriver::output(AnyOutputPin::new(4)).unwrap();
    let id: u8 = 0;
    let d = DimmerDevice::new(id, d0_pin);

    // Create Power management
    let ddm = DevicesDimmerManager::init(DevicesDimmerManagerConfig::default_50_hz(zero_crossing_pin, vec![d])).unwrap();

    rbd_dimmer::set_power(id, 100).unwrap();

    loop {
        rbd_dimmer::wait_zero_crossing().unwrap();
    }
}

That's all!

Example for zero-crossing sub-module

// PsmPin abstract to manage Dimmer
pub struct PsmPin<'a, T>
where
    T: OutputPin,
{
    // The pin to turn on/off
    psm: PinDriver<'a, T, Output>,
}

impl<'a, T> PsmPin<'a, T>
where
    T: OutputPin,
{
    pub fn new(psm: PinDriver<'a, T, Output>) -> Self {
        Self { psm }
    }
}

impl<'a, T> rbd_dimmer::OutputPin for PsmPin<'a, T>
where
    T: OutputPin,
{
    fn set_high(&mut self) -> Result<(), RbdDimmerError> {
        match self.psm.set_high() {
            Ok(_) => Ok(()),
            Err(_) => Err(RbdDimmerError::from(RbdDimmerErrorKind::SetHigh)),
        }
    }

    fn set_low(&mut self) -> Result<(), RbdDimmerError> {
        match self.psm.set_low() {
            Ok(_) => Ok(()),
            Err(_) => Err(RbdDimmerError::from(RbdDimmerErrorKind::SetLow)),
        }
    }
}

// Zero cross pin
pub struct ZeroCrossPin<'a, T>
where
    T: InputPin,
{
    // The pin to turn on/off
    zc: PinDriver<'a, T, Input>,
}

impl<'a, T> ZeroCrossingPin for ZeroCrossPin<'a, T>
where
    T: InputPin,
{
    fn wait_for_rising_edge(&mut self) -> Result<(), RbdDimmerError> {
        let a = block_on(self.zc.wait_for_rising_edge());
        match a {
            Ok(_) => Ok(()),
            Err(_) => Err(RbdDimmerError::other(String::from(
                "Fail to wait signal on Zero Cross pin",
            ))),
        }
    }
}

impl<'a, T> ZeroCrossPin<'a, T>
where
    T: InputPin,
{
    pub fn new(zc: PinDriver<'a, T, Input>) -> Self {
        Self { zc }
    }
}

// This function create all you need.
pub fn new<'a>(
    zc_pin: impl Peripheral<P = impl InputPin> + 'a,
    dimmer_pin: impl Peripheral<P = impl OutputPin> + 'a,
) -> DevicesDimmerManager<PsmPin<'a, impl OutputPin>, ZeroCrossPin<'a, impl InputPin>> {
    let psm_dimmer1 = PsmPin::new(PinDriver::output(dimmer_pin).unwrap());
    let zc = ZeroCrossPin::new(PinDriver::input(zc_pin).unwrap());

    let dim_device = DimmerDevice::new(0, psm_dimmer1);

    let mut devices_dimmer_manager = DevicesDimmerManager::new(zc);

    // Add the device
    devices_dimmer_manager.add(dim_device);

    devices_dimmer_manager
}

More information

Read How it works? for more information.

License

The code is released under MIT License to allow every body to use it in all conditions. If you love open-source software and this crate, please give some money to HaikuOS or ReactOS.

Commit count: 21

cargo fmt