Trait Beep

Source
pub trait Beep {
    // Required method
    fn beep(&mut self, hertz: u16) -> Result<()>;

    // Provided methods
    fn note(&mut self, note: &Note) -> Result<()> { ... }
    fn play<T>(&mut self, melody: T) -> Result<()>
       where T: AsRef<[Note]> { ... }
}
Expand description

Allows to beep the PC speaker.

Required Methods§

Source

fn beep(&mut self, hertz: u16) -> Result<()>

Beep the PC speaker at the given frequency.

§Errors

Returns an [Error] if beeping the PC speaker fails.

§Examples
use beep_evdev::{Beep, DEFAULT_FILE};
use std::{thread, time};
use evdev::Device;

let mut pcspkr = Device::open(DEFAULT_FILE).unwrap();
pcspkr.beep(440).expect("could not beep");
thread::sleep(time::Duration::from_millis(500));
pcspkr.beep(880).expect("could not beep");
thread::sleep(time::Duration::from_millis(500));
pcspkr.beep(0).expect("could not beep");

Provided Methods§

Source

fn note(&mut self, note: &Note) -> Result<()>

Play the given note on the PC speaker.

§Errors

Returns an [Error] if beeping the PC speaker fails.

§Examples
use evdev::Device;
use beep_evdev::{Note, Beep, DEFAULT_FILE};

let mut pcspkr = Device::open(DEFAULT_FILE)
    .unwrap()
    .note(&Note::default())
    .expect("could not play melody :-(");
Source

fn play<T>(&mut self, melody: T) -> Result<()>
where T: AsRef<[Note]>,

Play the given melody on the PC speaker.

§Errors

Returns an [Error] if beeping the PC speaker fails.

§Examples
use evdev::Device;
use beep_evdev::{Beep, Note, DEFAULT_FILE};

let melody = vec![
    (659, 120).into(),
    (622, 120).into(),
    (659, 120).into(),
    (622, 120).into(),
    (659, 120).into(),
    (94, 120).into(),
    (587, 120).into(),
    (523, 120).into(),
    (440, 120).into(),
    (262, 120).into(),
    (330, 120).into(),
    (440, 120).into(),
    (494, 120).into(),
    (330, 120).into(),
    (415, 120).into(),
    (494, 120).into(),
    (523, 120).into(),
    (330, 120).into(),
    (659, 120).into(),
    (622, 120).into(),
    (659, 120).into(),
    (622, 120).into(),
    (659, 120).into(),
    (494, 120).into(),
    (587, 120).into(),
    (523, 120).into(),
    (440, 120).into(),
    (262, 120).into(),
    (330, 120).into(),
    (440, 120).into(),
    (494, 120).into(),
    (330, 120).into(),
    (523, 120).into(),
    (494, 120).into(),
    (440, 120).into(),
];

Device::open(DEFAULT_FILE)
    .expect("could not open pcspkr device")
    .play(&melody)
    .expect("could not play melody :-(");
Examples found in repository?
examples/beep-evdev.rs (line 9)
6fn main() {
7    Device::open(DEFAULT_FILE)
8        .unwrap()
9        .play([Note::default()])
10        .unwrap();
11}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Beep for Device

Source§

fn beep(&mut self, hertz: u16) -> Result<()>

Implementors§