Crate dfplayer_async

Source
Expand description

A no_std async library for interfacing with DFPlayer Mini MP3 modules

This crate provides an async interface to control DFPlayer Mini MP3 modules using embedded-hal-async compatible serial interfaces. It handles the binary protocol, error checking, and timeout management required when communicating with these devices.

§Features

  • Async/await API for embedded systems
  • Full command support for DFPlayer Mini and compatible modules
  • Proper error handling and timeout management
  • no_std compatible
  • Robust initialization sequence with fallback mechanisms
  • Efficient non-blocking I/O patterns for embedded environments

§Example

use dfplayer_async::{DfPlayer, PlayBackSource, TimeSource};
use embassy_time::{Duration, Instant, Delay};

// Define a time source for the DFPlayer
struct MyTimeSource;
impl TimeSource for MyTimeSource {
    type Instant = Instant;
    fn now(&self) -> Self::Instant { Instant::now() }
    fn is_elapsed(&self, since: Self::Instant, timeout_ms: u64) -> bool {
        Instant::now().duration_since(since) >= Duration::from_millis(timeout_ms)
    }
}

// In your async function:
async fn example(mut uart: impl embedded_io_async::Read + embedded_io_async::Write + embedded_io_async::ReadReady) {
    let mut dfplayer = DfPlayer::try_new(
        &mut uart,      // UART port (9600 baud, 8N1)
        false,          // feedback_enable
        1000,           // timeout_ms
        MyTimeSource,   // time source
        Delay,          // delay provider
        None,           // reset_duration_override
    ).await.expect("Failed to initialize DFPlayer");

    // Play first track
    dfplayer.play(1).await.expect("Failed to play track");
}

This crate optionally supports logging via the defmt framework. Enable the “defmt” feature to activate logging.

Structs§

DfPlayer
Main driver for interfacing with DFPlayer Mini modules
MessageData
Data structure representing a message to/from the DFPlayer

Enums§

Command
Commands supported by the DFPlayer module
Equalizer
Equalizer settings available on the DFPlayer
Error
Errors that can occur when operating the DFPlayer
ModuleError
Error codes reported by the DFPlayer module
PlayBackMode
Playback modes supported by the DFPlayer
PlayBackSource
Media sources supported by the DFPlayer
Source
Represents available media sources on the DFPlayer

Traits§

TimeSource
Minimal time provider trait for timeout tracking. Implement this for your platform.

Functions§

checksum
Calculate the checksum for a DFPlayer message