pub struct DfPlayer<'a, S, T, D>{ /* private fields */ }
Expand description
Main driver for interfacing with DFPlayer Mini modules
Implementations§
Source§impl<'a, S, T, D> DfPlayer<'a, S, T, D>
Structure for interacting with the device
impl<'a, S, T, D> DfPlayer<'a, S, T, D>
Structure for interacting with the device
Sourcepub async fn try_new(
port: &'a mut S,
feedback_enable: bool,
timeout_ms: u64,
time_source: T,
delay: D,
reset_duration_override: Option<u64>,
) -> Result<Self, Error<S::Error>>
pub async fn try_new( port: &'a mut S, feedback_enable: bool, timeout_ms: u64, time_source: T, delay: D, reset_duration_override: Option<u64>, ) -> Result<Self, Error<S::Error>>
Create a new DFPlayer interface
This initializes the driver and performs a robust startup sequence for the DFPlayer module. The serial port must be configured with 9600 baud, 8N1 format before calling this function.
The initialization sequence:
- Clears any pending data in the receive buffer
- Sends a reset command and waits for the device to restart
- Configures SD card as the default media source
- Sets the volume to a moderate level (15 out of 30)
The function will attempt to continue even if certain initialization steps fail, making it more resilient to communication issues common with these modules.
§Arguments
port
- Serial port connected to the DFPlayer modulefeedback_enable
- Whether to enable command acknowledgement (set to false if you’re having reliability issues)timeout_ms
- Timeout for operations in millisecondstime_source
- Source of time for timeout trackingdelay
- Delay provider for timing operationsreset_duration_override
- Optional override for reset delay duration (ms)
Sourcepub async fn read_last_message(&mut self) -> Result<(), Error<S::Error>>
pub async fn read_last_message(&mut self) -> Result<(), Error<S::Error>>
Read and process a message from the DFPlayer module
This function handles the binary protocol parsing, message validation, and stores the last response. It uses a robust state machine to assemble complete messages from potentially fragmented reads, with proper timeout handling and error recovery.
Special handling is provided for reset commands and 8-byte response formats that sometimes occur in feedback mode.
Returns Ok(())
if a valid message was received and processed, or an error.
If a module error response was received, returns that specific error.
Sourcepub async fn send_command(
&mut self,
command_data: MessageData,
) -> Result<(), Error<S::Error>>
pub async fn send_command( &mut self, command_data: MessageData, ) -> Result<(), Error<S::Error>>
Send a command to the DFPlayer module
This constructs a properly formatted message, sends it to the device, and then waits for a response or acknowledgement if feedback is enabled. For query commands in non-feedback mode, attempts to read and process responses with multiple retries if needed.
The method calculates the appropriate checksum and handles all aspects of the binary communication protocol.
§Arguments
command_data
- The command and parameters to send
Sourcepub async fn next(&mut self) -> Result<(), Error<S::Error>>
pub async fn next(&mut self) -> Result<(), Error<S::Error>>
Play the next track
Sends the command to play the next track in sequence.
Sourcepub async fn reset(
&mut self,
reset_duration_override: Option<u64>,
) -> Result<(), Error<S::Error>>
pub async fn reset( &mut self, reset_duration_override: Option<u64>, ) -> Result<(), Error<S::Error>>
Reset the DFPlayer module
Sends a reset command to the module and waits for it to restart. The reset command typically causes the module to restart its processor and reinitialize its state.
Special handling is provided for the reset command, as it often won’t receive a response from the module.
§Arguments
reset_duration_override
- Optional override for reset delay duration (ms)
Sourcepub async fn set_playback_source(
&mut self,
playback_source: PlayBackSource,
) -> Result<(), Error<S::Error>>
pub async fn set_playback_source( &mut self, playback_source: PlayBackSource, ) -> Result<(), Error<S::Error>>
Set the media source for playback
Configures which media source the DFPlayer should use for audio files. This method includes an additional delay after sending the command to allow the module time to switch sources and initialize the file system.
§Arguments
playback_source
- The media source to use (SD card, USB, etc.)
Sourcepub async fn set_equalizer(
&mut self,
equalizer: Equalizer,
) -> Result<(), Error<S::Error>>
pub async fn set_equalizer( &mut self, equalizer: Equalizer, ) -> Result<(), Error<S::Error>>
Set the equalizer mode
Configures the audio equalizer preset on the DFPlayer.
§Arguments
equalizer
- Equalizer preset to use
Sourcepub async fn set_loop_all(
&mut self,
enable: bool,
) -> Result<(), Error<S::Error>>
pub async fn set_loop_all( &mut self, enable: bool, ) -> Result<(), Error<S::Error>>
Set whether to loop all tracks
Enables or disables looping through all tracks.
§Arguments
enable
- Whether to enable looping of all tracks
Sourcepub async fn pause(&mut self) -> Result<(), Error<S::Error>>
pub async fn pause(&mut self) -> Result<(), Error<S::Error>>
Pause the current playback
Pauses any currently playing track.
Sourcepub async fn resume(&mut self) -> Result<(), Error<S::Error>>
pub async fn resume(&mut self) -> Result<(), Error<S::Error>>
Resume playback
Resumes playback of a paused track.
Sourcepub async fn previous(&mut self) -> Result<(), Error<S::Error>>
pub async fn previous(&mut self) -> Result<(), Error<S::Error>>
Play the previous track
Plays the track before the current one in sequence.
Sourcepub async fn stop(&mut self) -> Result<(), Error<S::Error>>
pub async fn stop(&mut self) -> Result<(), Error<S::Error>>
Stop all playback
Stops any current playback, including advertisements.
Sourcepub async fn play_from_folder(
&mut self,
folder: u8,
track: u8,
) -> Result<(), Error<S::Error>>
pub async fn play_from_folder( &mut self, folder: u8, track: u8, ) -> Result<(), Error<S::Error>>
Play a track from a specific folder
The DFPlayer supports organizing tracks in folders. This command plays a specific track from a specific folder.
§Arguments
folder
- Folder number (1-99)track
- Track number within the folder (1-255)
§Errors
Returns Error::BadParameter
if parameters are out of range.
Sourcepub async fn play_random(&mut self) -> Result<(), Error<S::Error>>
pub async fn play_random(&mut self) -> Result<(), Error<S::Error>>
Play tracks in random order
Starts playback in random order from the current source.
Sourcepub async fn set_loop_current_track(
&mut self,
enable: bool,
) -> Result<(), Error<S::Error>>
pub async fn set_loop_current_track( &mut self, enable: bool, ) -> Result<(), Error<S::Error>>
Set whether to loop the current track
Enables or disables looping of the currently playing track.
§Arguments
enable
- Whether to enable looping of the current track
Sourcepub async fn query_tracks_sd(&mut self) -> Result<u16, Error<S::Error>>
pub async fn query_tracks_sd(&mut self) -> Result<u16, Error<S::Error>>
Query the total number of tracks on the SD card
Returns the number of tracks on the SD card.
This method implements a robust strategy for obtaining track counts:
- Sends the query command and processes immediate responses
- For non-feedback mode, waits for delayed responses with multiple attempts
- Returns 0 if no tracks are found or the count couldn’t be retrieved
The track count retrieval is one of the most timing-sensitive operations of the DFPlayer module and may require multiple attempts.