Loading...
Searching...
No Matches
dfplayer_implementation.h
Go to the documentation of this file.
1/*
2 * Copyright 2019 Marian Buschsieweke
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
17#ifndef DFPLAYER_IMPLEMENTATION_H
18#define DFPLAYER_IMPLEMENTATION_H
19
20#include <errno.h>
21
22#include "dfplayer.h"
23#include "dfplayer_constants.h"
24#include "dfplayer_internal.h"
25#include "dfplayer_types.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/* This file contains the implementations of the functions whose signature and
32 * Doxygen documentation is given in `drivers/include/dfplayer.h`. Doxygen
33 * sadly doesn't detect this and creates two entries for every function; with
34 * one entry being undocumented. We just hide the implementations here from
35 * Doxygen to prevent the duplicate entries
36 */
37#ifndef DOXYGEN
38static inline int dfplayer_next(dfplayer_t *dev)
39{
40 return dfplayer_step(dev, 1);
41}
42
43static inline int dfplayer_prev(dfplayer_t *dev)
44{
45 return dfplayer_step(dev, -1);
46}
47
48static inline int dfplayer_set_volume(dfplayer_t *dev, uint8_t volume)
49{
50 if (volume > DFPLAYER_MAX_VOLUME) {
51 volume = DFPLAYER_MAX_VOLUME;
52 }
54}
55
56static inline int dfplayer_set_equalizer(dfplayer_t *dev,
57 dfplayer_eq_t equalizer)
58{
59 if ((unsigned)equalizer >= (unsigned)DFPLAYER_EQ_NUMOF) {
60 return -EINVAL;
61 }
62
64 (uint8_t)equalizer);
65}
66
67static inline int dfplayer_set_source(dfplayer_t *dev, dfplayer_source_t src)
68{
69 if ((unsigned)src >= (unsigned)DFPLAYER_SOURCE_NUMOF) {
70 return -EINVAL;
71 }
72
73 return dfplayer_cmd_1param(dev, DFPLAYER_CMD_SET_SOURCE, (uint8_t)src);
74}
75
76static inline int dfplayer_enter_standby(dfplayer_t *dev)
77{
79}
80
81static inline int dfplayer_exit_standby(dfplayer_t *dev)
82{
84}
85
86static inline int dfplayer_play(dfplayer_t *dev)
87{
89}
90
91static inline int dfplayer_pause(dfplayer_t *dev)
92{
94}
95
96static inline int dfplayer_stop_advert(dfplayer_t *dev)
97{
99}
100
101static inline int dfplayer_repeat_folder(dfplayer_t *dev, uint8_t folder)
102{
103 return dfplayer_file_cmd(dev, DFPLAYER_CMD_REPEAT_FOLDER, 0, folder);
104}
105
106static inline int dfplayer_shuffle_all(dfplayer_t *dev)
107{
109}
110
111static inline int dfplayer_repeat(dfplayer_t *dev, bool repeat)
112{
113 return dfplayer_cmd_1param(dev, DFPLAYER_CMD_REPEAT, (uint8_t)repeat);
114}
115
116static inline int dfplayer_get_volume(dfplayer_t *dev, uint8_t *volume)
117{
118 if (!volume) {
119 return -EINVAL;
120 }
121
122 uint16_t tmp;
123 int retval = dfplayer_query(dev, &tmp, DFPLAYER_CMD_GET_VOLUME);
124 if (retval) {
125 return retval;
126 }
127
128 *volume = (uint8_t)tmp;
129 return 0;
130}
131
132static inline int dfplayer_get_equalizer(dfplayer_t *dev,
133 dfplayer_eq_t *equalizer)
134{
135 if (!equalizer) {
136 return -EINVAL;
137 }
138
139 uint16_t tmp;
140 int retval = dfplayer_query(dev, &tmp, DFPLAYER_CMD_GET_EQUALIZER);
141 if (retval) {
142 return retval;
143 }
144
145 *equalizer = (dfplayer_eq_t)tmp;
146 return 0;
147}
148
149static inline int dfplayer_get_mode(dfplayer_t *dev,
150 dfplayer_mode_t *mode)
151{
152 if (!mode) {
153 return -EINVAL;
154 }
155
156 uint16_t tmp;
157 int retval = dfplayer_query(dev, &tmp, DFPLAYER_CMD_GET_MODE);
158 if (retval) {
159 return retval;
160 }
161
162 *mode = (dfplayer_mode_t)tmp;
163 return 0;
164}
165
166static inline int dfplayer_get_version(dfplayer_t *dev, uint16_t *version)
167{
168 if (!version) {
169 return -EINVAL;
170 }
171
172 return dfplayer_query(dev, version, DFPLAYER_CMD_GET_VERSION);
173}
174
175static inline int dfplayer_files_usb(dfplayer_t *dev, uint16_t *files)
176{
177 if (!files) {
178 return -EINVAL;
179 }
180
181 return dfplayer_query(dev, files, DFPLAYER_CMD_FILES_USB);
182}
183
184static inline int dfplayer_files_sdcard(dfplayer_t *dev, uint16_t *files)
185{
186 if (!files) {
187 return -EINVAL;
188 }
189
190 return dfplayer_query(dev, files, DFPLAYER_CMD_FILES_SDCARD);
191}
192
193static inline int dfplayer_files_flash(dfplayer_t *dev, uint16_t *files)
194{
195 if (!files) {
196 return -EINVAL;
197 }
198
199 return dfplayer_query(dev, files, DFPLAYER_CMD_FILES_FLASH);
200}
201
202static inline int dfplayer_get_fileno_usb(dfplayer_t *dev, uint16_t *fileno)
203{
204 if (!fileno) {
205 return -EINVAL;
206 }
207
208 return dfplayer_query(dev, fileno, DFPLAYER_CMD_FILENO_USB);
209}
210
211static inline int dfplayer_get_fileno_sdcard(dfplayer_t *dev, uint16_t *fileno)
212{
213 if (!fileno) {
214 return -EINVAL;
215 }
216
217 return dfplayer_query(dev, fileno, DFPLAYER_CMD_FILENO_SDCARD);
218}
219
220static inline int dfplayer_get_fileno_flash(dfplayer_t *dev, uint16_t *fileno)
221{
222 if (!fileno) {
223 return -EINVAL;
224 }
225
226 return dfplayer_query(dev, fileno, DFPLAYER_CMD_FILENO_FLASH);
227}
228
230{
231 mutex_lock(&dev->mutex);
232 dfplayer_file_t res = dev->file;
233 mutex_unlock(&dev->mutex);
234 return res;
235}
236
239{
240 return (set & (0x01 << src)) ? 1 : 0;
241}
242
243static inline void dfplayer_source_set_add(dfplayer_source_set_t *set,
245{
246 *set |= 0x01 << src;
247}
248
249static inline void dfplayer_source_set_rm(dfplayer_source_set_t *set,
251{
252 *set &= ~((dfplayer_source_set_t)(0x01 << src));
253}
254#endif /* !DOXYGEN */
255
256#ifdef __cplusplus
257}
258#endif
259
260#endif /* DFPLAYER_IMPLEMENTATION_H */
DFPlayer Mini Device Driver.
Constants used in the DFPlayer Mini Driver.
#define DFPLAYER_CMD_RANDOM
Start playing all files in random order.
#define DFPLAYER_CMD_SET_VOLUME
Set the volume to the given level.
#define DFPLAYER_MAX_VOLUME
Maximum supported volume.
#define DFPLAYER_CMD_GET_EQUALIZER
Retrieve the current equalizer setting.
#define DFPLAYER_CMD_SET_SOURCE
Set the source to play files from.
#define DFPLAYER_CMD_STANDBY_ENTER
Enter low power mode.
#define DFPLAYER_CMD_FILES_SDCARD
Get the total number of files on the SD card.
#define DFPLAYER_CMD_PLAY
Start playing the selected file.
#define DFPLAYER_CMD_REPEAT_FOLDER
Start repeat-playing the given folder (1-99)
#define DFPLAYER_CMD_FILENO_USB
Get the currently select file number on the USB storage.
#define DFPLAYER_CMD_GET_MODE
Retrieve the current playback mode.
#define DFPLAYER_CMD_GET_VOLUME
Retrieve the current volume.
#define DFPLAYER_CMD_PAUSE
Pause the playback.
#define DFPLAYER_CMD_ABORT_ADVERT
Play the given file (1-9999) from the folder "ADVERT", resume current playback afterwards.
#define DFPLAYER_CMD_FILENO_SDCARD
Get the currently select file number on the SD-Card.
#define DFPLAYER_CMD_REPEAT
0 = repeat currently played file, 1 = stop repeating
#define DFPLAYER_CMD_FILENO_FLASH
Get the currently select file number on the NOR flash.
#define DFPLAYER_CMD_FILES_FLASH
Get the total number of files on NOR flash.
#define DFPLAYER_CMD_STANDBY_EXIT
Exit low power mode, back to normal mode.
#define DFPLAYER_CMD_GET_VERSION
Retrieve the device's software version.
#define DFPLAYER_CMD_SET_EQUALIZER
Set the equalizer to the given setting.
#define DFPLAYER_CMD_FILES_USB
Get the total number of files on USB storage.
Internal functions of DFPlayer Mini Device driver.
static int dfplayer_cmd(dfplayer_t *dev, uint8_t cmd)
Send a command without parameters to the DFPlayer Mini.
static int dfplayer_query(dfplayer_t *dev, uint16_t *resp, uint8_t cmd)
Send a query and receive the response.
static int dfplayer_cmd_1param(dfplayer_t *dev, uint8_t cmd, uint8_t param)
Send a command with one parameter to the DFPlayer Mini.
int dfplayer_file_cmd(dfplayer_t *dev, uint8_t cmd, uint8_t p1, uint8_t p2)
Send a command selecting a playback file.
Types used in the DFPlayer Mini Device Driver.
dfplayer_mode_t
Enumeration of the playback modes supported by the DFPlayer.
dfplayer_source_t
Enumeration of the different sources for playback supported.
@ DFPLAYER_SOURCE_NUMOF
Number of supported playback modes.
uint8_t dfplayer_source_set_t
Set of DFPlayer playback sources.
dfplayer_eq_t
Enumeration of the equalizer settings supported by the DFPlayer.
@ DFPLAYER_EQ_NUMOF
Number of supported equalizer settings.
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition mutex.h:312
#define EINVAL
Invalid argument.
Definition errno.h:97
static int dfplayer_shuffle_all(dfplayer_t *dev)
Launch shuffle playback of all files.
static int dfplayer_enter_standby(dfplayer_t *dev)
Enter standby mode.
static int dfplayer_files_flash(dfplayer_t *dev, uint16_t *files)
Query the number of files on the NOR flash.
static int dfplayer_files_usb(dfplayer_t *dev, uint16_t *files)
Query the number of files on the USB storage device.
static int dfplayer_get_equalizer(dfplayer_t *dev, dfplayer_eq_t *equalizer)
Query the current equalizer setting of the DFPlayer Mini.
static int dfplayer_stop_advert(dfplayer_t *dev)
Stop playing a file from the ADVERT folder and resume previous playback.
int dfplayer_step(dfplayer_t *dev, int step)
Step forward/backward following the currently used naming scheme.
static int dfplayer_get_mode(dfplayer_t *dev, dfplayer_mode_t *mode)
Query the current playback mode of the DFPlayer Mini.
static void dfplayer_source_set_rm(dfplayer_source_set_t *set, dfplayer_source_t src)
Remove the given source to the given source set.
static int dfplayer_get_fileno_sdcard(dfplayer_t *dev, uint16_t *fileno)
Query the selected file on the SD card.
static int dfplayer_repeat_folder(dfplayer_t *dev, uint8_t folder)
Start playing and repeating the specified folder.
static int dfplayer_get_volume(dfplayer_t *dev, uint8_t *volume)
Query the current volume of the DFPlayer Mini.
static int dfplayer_next(dfplayer_t *dev)
Start playing the next song in the currently used naming scheme.
static int dfplayer_set_equalizer(dfplayer_t *dev, dfplayer_eq_t equalizer)
Apply the given equalizer setting.
static int dfplayer_pause(dfplayer_t *dev)
Pause the playback.
static int dfplayer_get_version(dfplayer_t *dev, uint16_t *version)
Query the software version running on the DFPlayer Mini.
static int dfplayer_get_fileno_usb(dfplayer_t *dev, uint16_t *fileno)
Query the selected file on the USB storage device.
static int dfplayer_set_source(dfplayer_t *dev, dfplayer_source_t src)
Apply the given source.
static int dfplayer_get_fileno_flash(dfplayer_t *dev, uint16_t *fileno)
Query the selected file on the NOR flash.
static dfplayer_file_t dfplayer_get_played_file(dfplayer_t *dev)
Get the currently played file and the used naming scheme.
static int dfplayer_play(dfplayer_t *dev)
Start/resume playing.
static int dfplayer_repeat(dfplayer_t *dev, bool repeat)
Enable or disable repeat playback.
static int dfplayer_exit_standby(dfplayer_t *dev)
Exit standby mode.
static int dfplayer_prev(dfplayer_t *dev)
Start playing the previous song in the currently used naming scheme.
static int dfplayer_source_set_contains(dfplayer_source_set_t set, dfplayer_source_t src)
Check if the given source set contains the given source.
static int dfplayer_files_sdcard(dfplayer_t *dev, uint16_t *files)
Query the number of files on the SD card.
static int dfplayer_set_volume(dfplayer_t *dev, uint8_t volume)
Sets the volume to the given value.
static void dfplayer_source_set_add(dfplayer_source_set_t *set, dfplayer_source_t src)
Add the given source to the given source set.
Data structure representing a file on the DFPlayer.
A DFPlayer Mini device descriptor.
mutex_t mutex
Used to mutual exclusive access.
dfplayer_file_t file
Currently played song.