class DFPlayer: (source)
Constructor: DFPlayer(uart_id, busy_pin_id, timeout, timeout_feedback, ...)
Controller for the DFPlayer module.
⚠️ All async methods may raise a DFPlayerError on communication failure or on internal errors reported by the module.
Method | __init__ |
Construct a new DFPlayer object to control a DFPlayer module using UART communication. |
Method | available |
Get if the player is booted and ready to be interfaced with. |
Async Method | dac |
Enable or disable the internal DAC (enabled on DFPlayer boot). |
Method | deinit |
Turn off the DFPlayer connection. |
Async Method | eq |
Query or set an equalizer preset. |
Async Method | gain |
Set DAC output gain. |
Method | init |
Initialize the DFPlayer. |
Async Method | mode |
Query or set a playback mode. |
Async Method | next |
Play the next track. |
Async Method | num |
Query the number of files on a given storage device. |
Async Method | num |
Query the number of files in a numeric folder. |
Async Method | num |
Query the number of available folders. |
Method | off |
Remove one or all event handler(s) for completion of file playback. |
Method | off |
Remove one or all event handler(s) for storage device ejection. |
Method | off |
Remove one or all event handler(s) for storage device insertion. |
Method | off |
Remove one or all event handler(s) for player / storage device readying. |
Method | on |
Register an event handler for completion of file playback. |
Method | on |
Register an event handler for storage device ejection. |
Method | on |
Register an event handler for storage device insertion. |
Method | on |
Register an event handler for player / storage device readying. |
Async Method | pause |
Pause playback (can be resumed via resume). |
Async Method | play |
Start playback of a track or advert. |
Async Method | play |
Alias of play(DFPlayer.FOLDER_ADVERT, ...). |
Async Method | play |
Start playback via track's id. |
Async Method | play |
Alias of play(DFPlayer.FOLDER_MP3, ...). |
Async Method | playing |
Fetch whether playback is in progress. |
Async Method | previous |
Play the previous track. |
Async Method | reset |
Resets (reboots) the player. |
Async Method | resume |
Resume playback. |
Async Method | send |
Send a raw command to the player. |
Async Method | send |
Send a raw query command to the player and return the responded value. |
Async Method | sleep |
Set the player into sleep mode. |
Async Method | source |
Set the playback source device. |
Async Method | standby |
Enable or disable the player's standby mode. |
Async Method | state |
Query playback state. |
Async Method | stop |
Stop playback. |
Async Method | stop |
Stop playback of running advert, continuing regular track. |
Async Method | track |
Query the currently playing/internally selected track's id on a given storage device. |
Async Method | version |
Query the player's software version. |
Async Method | volume |
Query or set the player's volume. |
Method | wait |
Await DFPlayer availability (see available) |
Async Method | wake |
Wake the player from sleep mode. |
Constant | EQ |
Bass-heavy equalizer mode. |
Constant | EQ |
Classic equalizer mode. |
Constant | EQ |
Flat (default/balanced) equalizer mode. |
Constant | EQ |
Jazz equalizer mode. |
Constant | EQ |
Pop equalizer mode. |
Constant | EQ |
Rock equalizer mode. |
Constant | FOLDER |
'ADVERT' folder on the storage device. |
Constant | FOLDER |
'MP3' folder on the storage device. |
Constant | MODE |
Randomize through all tracks (includes adverts). |
Constant | MODE |
Repeat all tracks sequentially (includes adverts). |
Constant | MODE |
Repeat a single file. |
Constant | MODE |
Repeat all tracks of a numeric folder. |
Constant | MODE |
Play files once (default). |
Constant | STATE |
Playback halted, track can be resumed. |
Constant | STATE |
Playback in progress. |
Constant | STATE |
Playback halted, no track progress retained. |
Instance Variable | log |
Print additional communication information (expects DFPlayer.LOG_... constant). |
Instance Variable | retries |
How often to re-send a command on communication failure. |
Instance Variable | skip |
Set of command bytes (see source) to not require/request command ACK for. |
Instance Variable | timeout |
Milliseconds allowed for the player to process and acknowledge (ACK) sent commands. |
Instance Variable | timeout |
Milliseconds allowed between command ACK and busy pin activating (relevant commands only). |
Instance Variable | timeout |
Milliseconds allowed between command ACK and feedback (error/query value). |
Method | _bytes |
Undocumented |
Async Method | _exec |
Undocumented |
Async Method | _fetch |
Undocumented |
Method | _get |
Undocumented |
Method | _handle |
Undocumented |
Method | _off |
Undocumented |
Method | _on |
Undocumented |
Async Method | _read |
Undocumented |
Async Method | _read |
Undocumented |
Async Method | _receive |
Undocumented |
Method | _require |
Undocumented |
Method | _uint16 |
Undocumented |
Method | _validate |
Undocumented |
Instance Variable | _buffer |
Undocumented |
Instance Variable | _buffer |
Undocumented |
Instance Variable | _busy |
Undocumented |
Instance Variable | _busy |
Undocumented |
Instance Variable | _error |
Undocumented |
Instance Variable | _events |
Undocumented |
Instance Variable | _init |
Undocumented |
Instance Variable | _last |
Undocumented |
Instance Variable | _last |
Undocumented |
Instance Variable | _lock |
Undocumented |
Instance Variable | _log |
Undocumented |
Instance Variable | _message |
Undocumented |
Instance Variable | _message |
Undocumented |
Instance Variable | _read |
Undocumented |
Instance Variable | _stream |
Undocumented |
Instance Variable | _uart |
Undocumented |
int
, busy_pin_id: int|None
= None, timeout=200, timeout_feedback=50, timeout_busy=300, retries=5, skip_ack: set[ int]
= _ALL_QUERIES, log_level=_LOG_NONE):
(source)
¶
Construct a new DFPlayer object to control a DFPlayer module using UART communication.
Parameters | |
uartint | Hardware UART id |
busyint|None | Optional pin id that is connected to the BUSY pin of the DFPlayer (uses up one interrupt slot) |
timeout | Milliseconds allowed for the player to process and acknowledge (ACK) sent commands |
timeout | Milliseconds allowed between command ACK and feedback (error/query value) |
timeout | Milliseconds allowed between command ACK and busy pin activating (relevant commands only) |
retries | How often to re-send a command on communication failure |
skipset[ | Set of command bytes (see source) to not require/request command ACK for (by default excludes all queries as original DFPlayer will never ACK query commands) |
log | Print additional communication information (expects DFPlayer.LOG_... constant) |
Get if the player is booted and ready to be interfaced with.
⚠️ The player may still not process commands if in standby or sleep mode.
Turn off the DFPlayer connection.
Turns off the underlying UART bus and frees an interrupt handler for the busy pin (if used).
Query or set an equalizer preset.
Parameters | |
eq:int|None | Set equalizer to one of the DFPlayer.EQ_... constants. |
Initialize the DFPlayer.
Needs to be called before any commands can be sent or events can be received. Additional keyword-only parameters will be passed to UART.init of the underlying UART bus used for communicating with the player.
Query or set a playback mode.
⚠️ While DFPlayer.MODE_SINGLE and DFPlayer.MODE_REPEAT_FILE passively change the playback mode, the other modes will actively start playback.
⚠️ DFPlayer.MODE_REPEAT_ALL and DFPlayer.MODE_RANDOM_ALL will also consider adverts.
Parameters | |
mode:int|None | Set mode to one of the DFPlayer.MODE_... constants. |
folder:int|None | Folder to repeat (only with DFPlayer.MODE_REPEAT_FOLDER). |
Query the number of files on a given storage device.
If no device is specified, the last active one known to this driver is queried.
Parameters | |
device:int|None | DFPlayer.DEVICE_... constant to query. |
Remove one or all event handler(s) for completion of file playback.
Parameters | |
handler:Callable[ | Handler to remove (all when None) |
Remove one or all event handler(s) for storage device ejection.
Parameters | |
handler:Callable[ | Handler to remove (all when None) |
Remove one or all event handler(s) for storage device insertion.
Parameters | |
handler:Callable[ | Handler to remove (all when None) |
Remove one or all event handler(s) for player / storage device readying.
Parameters | |
handler:Callable[ | Handler to remove (all when None) |
Register an event handler for completion of file playback.
⚠️ Some DFPlayer versions may not report this event for adverts or might report it multiple times in quick succession for any file type.
Parameters | |
handler:Callable[ | Takes integer parameters: (device, track_id) |
Register an event handler for storage device ejection.
Parameters | |
handler:Callable[ | Takes integer parameter: (device) |
Register an event handler for storage device insertion.
⚠️ Some DFPlayer versions may not (always) report this event for device insertion, but always or sometimes report the 'ready' event instead.
Parameters | |
handler:Callable[ | Takes integer parameter: (device) |
Register an event handler for player / storage device readying.
⚠️ Some DFPlayer versions may not (always) report this event for device readying, but always or sometimes report the 'insert' event instead.
Example:
df.on_ready(lambda d: print("Ready! SD-card available?", bool(d & df.DEVICE_SDCARD)))
Parameters | |
handler:Callable[ | Takes integer parameter (bit field of DFPlayer.DEVICE_... constants): (devices) |
Start playback of a track or advert.
Adverts will temporarily interrupt tracks.
⚠️ On some DFPlayer versions, adverts may only be played while a regular track is playing.
⚠️ Playback from numeric folders and files prefixed with 0 seems to work but may report an out-of-bounds error.
Examples:
done = await df.play(df.FOLDER_MP3, 24) await done print("Playback finished.") from asyncio import sleep from asyncio.funcs import gather done = [] done.append(await df.play(8, 64)) await sleep(4) done.append(await df.play(df.FOLDER_ADVERT, 12)) await gather(*done) print("Playback finished.")
Parameters | |
folder:int | Folder of the track to be played (either numeric 1-99 or DFPlayer.FOLDER_... constant). |
file:int | Numeric file prefix (1-3000 in folders 1-15; 1-256 in folders 16-99; 0-9999 in MP3; 0-9999 in ADVERT) |
await | If set, this coroutine only returns once the player starts playback (is busy). busy_pin_id must have been passed to constructor. |
Returns | |
An awaitable that will finish at the end of playback. When looping is enabled, the awaitable will still finish when the first iteration ends. |
Start playback via track's id.
The id refers to the index of the file on the filesystem (starting at 1). This usually resembles the order in which files were copied onto the storage device and can target files in any folder.
⚠️ Using this method is seldom the best approach. Other playback methods are more robust when playing specific files on the filesystem.
Fetch whether playback is in progress.
Will use the busy pin, if specified in the constructor. Otherwise is equivalent to (await self.state()) == DFPlayer.STATE_PLAYING.
Resets (reboots) the player.
The reboot can take up to 3 seconds and can be awaited using wait_available
Example:
await df.reset() await df.wait_available() # ...
async def send_cmd(self, cmd:
int
, param1=0, param2: int|None
= None, await_busy=False):
(source)
¶
Send a raw command to the player.
While usually unnecessary as the full feature-set of the DFPlayer is supported by the API, raw commands can be sent to directly interface with the DFPlayer.
Parameters | |
cmd:int | DFPlayer command byte |
param1 | uint8 when first of two parameters or single uint16 parameter |
param2:int|None | uint8 second parameter |
await | If set, an error is raised if the player does not report busy state in response |
Send a raw query command to the player and return the responded value.
While usually unnecessary as the full feature-set of the DFPlayer is supported by the API, raw query commands can be sent to directly interface with the DFPlayer.
Parameters | |
cmd:int | DFPlayer command byte |
param1 | uint8 when first of two parameters or single uint16 parameter |
param2:int|None | uint8 second parameter |
Set the playback source device.
Parameters | |
device:int | Set device to one of the DFPlayer.DEVICE_... constants. |
Enable or disable the player's standby mode.
This is supposed to be a power-saving mode but does not seem to have a power-saving effect on most or all DFPlayer versions. Due to the player not processing any commands in this mode, disabling standby mode via standby(False) does not actually seem to work.
Query the currently playing/internally selected track's id on a given storage device.
If no device is specified, the last active one known to this driver is queried.
Parameters | |
device:int|None | DFPlayer.DEVICE_... constant to query. |
Query or set the player's volume.
Parameters | |
volume:int|None | Set volume to a value between 0-30 inclusive. |
Await DFPlayer availability (see available)
Can be used at program start to ensure the player is booted up before taking commands.
Example:
df = DFPlayer(0) df.init() await df.wait_available() # ...
Returns | |
Awaitable that finishes once the player is available to be interfaced with. |
Wake the player from sleep mode.
This changes the playback source to the last active one known to this driver, defaulting to the SD card.