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>
impl<P, C, D, I> InfraredEmitter<P, C, I>
Sourcepub fn new(pause_cycles: u8, pwm: P, channel: C) -> Self
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 emissionchannel- the channel to be used by the PWM
Sourcepub fn send_half_bit(&mut self)
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>
impl<P, C, D> InfraredEmitter<P, C, DatagramBigEndianIterator>
Sourcepub fn send_if_possible(&mut self, datagram: Datagram, sending_power: D) -> bool
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 sendsending_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>
impl<P, C, D> InfraredEmitter<P, C, DatagramLittleEndianIterator>
Sourcepub fn send_if_possible(&mut self, datagram: Datagram, sending_power: D) -> bool
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 sendsending_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