InfraredEmitter

Struct InfraredEmitter 

Source
pub struct InfraredEmitter<P, C, I> { /* private fields */ }
Expand description

Control sending of datagrams, manage infrared radiation pollution

The InfraredEmitter behaves socially by enforcing a pause time between subsequent Required resources:

  • A configured PWM - typically at a frequency of 36..38 kHz (RC5 protocol)
  • A facility that periodically runs half bit sending, e.g. a timer ISR typically at a period of 889 µs (half bit time, RC5 protocol)

§Example

#[cortex_m_rt::entry]

fn main() -> ! {
    let dp = stm32::Peripherals::take().expect("cannot take peripherals");

   // Set up the system clock
    //// let mut flash = dp.FLASH.constrain();
    let mut rcc = dp.RCC.constrain();

    // Setup PWM we use Arduino PIN D5 -> is PB4 / TIM3_CH1 on stm32g071
    let gpiob = dp.GPIOB.split(&mut rcc);

    // let clocks = rcc.cfgr.sysclk(16.MHz()).freeze(&mut flash.acr);

    let pwm_pin = gpiob.pb4;
    let pwm = dp.TIM3.pwm(36_u32.khz(), &mut rcc);
    let mut pwm_send_ir = pwm.bind_pin(pwm_pin);

    pwm_send_ir.set_duty(pwm_send_ir.get_max_duty() / 4); // 25% duty cyle

    // Set up the interrupt timer
    let mut timer = dp.TIM2.timer(&mut rcc);
    timer.start(889.us());

    const PAUSE_HALF_BITS_BETWEEN_DATAGRAMS: u8 = 3;
    let mut infrared_emitter = InfraredEmitter::new(PAUSE_HALF_BITS_BETWEEN_DATAGRAMS, pwm_send_ir, ());

    defmt::println!("Init done");

    let datagram = Datagram::new("0101_0011_0111_0001");
    defmt::println!("Send new datagram {}", datagram);
    infrared_emitter.send_if_possible(datagram, 25);

    loop {
        infrared_emitter.send_half_bit();
        block!(timer.wait()).unwrap();
    }
}

Implementations§

Source§

impl<P, C, D, I> InfraredEmitter<P, C, I>
where P: Pwm + Pwm<Channel = C> + Pwm<Duty = D>, C: Copy, D: Mul<Output = D> + Div<Output = D>, I: Iterator<Item = bool>,

Source

pub fn new(pause_cycles: u8, pwm: P, channel: C) -> Self

Create a new infrared Emitter

§Arguments
  • pause_cycles - configures the time between subsequent datagram emissions. The total duration is half-bit-time (889 µs) times number of pause bit cycles. In the pause time no infrared radiation is emitted and other participants can occupy the radiation space.
  • pwm - the PWM to be used for ir pulse emission
  • channel - the channel to be used by the PWM
Source

pub fn send_half_bit(&mut self)

Progress on sending a datagram by emitting a half bit

This function needs to be called every half-bit period, i.e. each 889 µs. The periodically required call is most likely delegated to a timer ISR.

half-bit emitting happens by enabling/disabling a a properly configured PWM.

Source§

impl<P, C, D> InfraredEmitter<P, C, DatagramBigEndianIterator>
where P: Pwm + Pwm<Channel = C> + Pwm<Duty = D>, C: Copy, D: Mul<Output = D> + Div<Output = D>,

Source

pub fn send_if_possible(&mut self, datagram: Datagram, sending_power: D) -> bool

Immediately start sending a datagram if possible

Sending is possible iff there is no sending procedure in progress. A call to this function is not blocking

§Arguments
  • datagram - The datagram to be send
  • sending_power - The duty cycle of the pwm in percent should be less than or equal 25 (percent) Is reduced to 25 if a higher value is given. Lower sending power is appropriate for pairing datagrams.
§Returns
  • true - if sending was initiated
  • false - if sending was not possible to initiate
Source§

impl<P, C, D> InfraredEmitter<P, C, DatagramLittleEndianIterator>
where P: Pwm + Pwm<Channel = C> + Pwm<Duty = D>, C: Copy, D: Mul<Output = D> + Div<Output = D>,

Source

pub fn send_if_possible(&mut self, datagram: Datagram, sending_power: D) -> bool

Immediately start sending a datagram if possible

Sending is possible iff there is no sending procedure in progress. A call to this function is not blocking

§Arguments
  • datagram - The datagram to be send
  • sending_power - The duty cycle of the pwm in percent should be less than or equal 25 (percent) Is reduced to 25 if a higher value is given. Lower sending power is appropriate for pairing datagrams.
§Returns
  • true - if sending was initiated
  • false - if sending was not possible to initiate

Trait Implementations§

Source§

impl<P: Debug, C: Debug, I: Debug> Debug for InfraredEmitter<P, C, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, C, I> Freeze for InfraredEmitter<P, C, I>
where P: Freeze, C: Freeze, I: Freeze,

§

impl<P, C, I> RefUnwindSafe for InfraredEmitter<P, C, I>

§

impl<P, C, I> Send for InfraredEmitter<P, C, I>
where P: Send, C: Send, I: Send,

§

impl<P, C, I> Sync for InfraredEmitter<P, C, I>
where P: Sync, C: Sync, I: Sync,

§

impl<P, C, I> Unpin for InfraredEmitter<P, C, I>
where P: Unpin, C: Unpin, I: Unpin,

§

impl<P, C, I> UnwindSafe for InfraredEmitter<P, C, I>
where P: UnwindSafe, C: UnwindSafe, I: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.