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
28 extern "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
38 static inline int dfplayer_next(dfplayer_t *dev)
39 {
40  return dfplayer_step(dev, 1);
41 }
42 
43 static inline int dfplayer_prev(dfplayer_t *dev)
44 {
45  return dfplayer_step(dev, -1);
46 }
47 
48 static inline int dfplayer_set_volume(dfplayer_t *dev, uint8_t volume)
49 {
50  if (volume > DFPLAYER_MAX_VOLUME) {
51  volume = DFPLAYER_MAX_VOLUME;
52  }
53  return dfplayer_cmd_1param(dev, DFPLAYER_CMD_SET_VOLUME, volume);
54 }
55 
56 static 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 
67 static 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 
76 static inline int dfplayer_enter_standby(dfplayer_t *dev)
77 {
79 }
80 
81 static inline int dfplayer_exit_standby(dfplayer_t *dev)
82 {
84 }
85 
86 static inline int dfplayer_play(dfplayer_t *dev)
87 {
88  return dfplayer_cmd(dev, DFPLAYER_CMD_PLAY);
89 }
90 
91 static inline int dfplayer_pause(dfplayer_t *dev)
92 {
93  return dfplayer_cmd(dev, DFPLAYER_CMD_PAUSE);
94 }
95 
96 static inline int dfplayer_stop_advert(dfplayer_t *dev)
97 {
99 }
100 
101 static 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 
106 static inline int dfplayer_shuffle_all(dfplayer_t *dev)
107 {
108  return dfplayer_cmd(dev, DFPLAYER_CMD_RANDOM);
109 }
110 
111 static inline int dfplayer_repeat(dfplayer_t *dev, bool repeat)
112 {
113  return dfplayer_cmd_1param(dev, DFPLAYER_CMD_REPEAT, (uint8_t)repeat);
114 }
115 
116 static 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 
132 static 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 
149 static 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 
166 static 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 
175 static 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 
184 static 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 
193 static 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 
202 static 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 
211 static 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 
220 static 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 
238  dfplayer_source_t src)
239 {
240  return (set & (0x01 << src)) ? 1 : 0;
241 }
242 
243 static inline void dfplayer_source_set_add(dfplayer_source_set_t set,
244  dfplayer_source_t src)
245 {
246  set |= 0x01 << src;
247 }
248 
249 static inline void dfplayer_source_set_rm(dfplayer_source_set_t set,
250  dfplayer_source_t src)
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 */
261 
dfplayer_mode_t
dfplayer_mode_t
Enumeration of the playback modes supported by the DFPlayer.
Definition: dfplayer_types.h:61
dfplayer_set_volume
static int dfplayer_set_volume(dfplayer_t *dev, uint8_t volume)
Sets the volume to the given value.
dfplayer_step
int dfplayer_step(dfplayer_t *dev, int step)
Step forward/backward following the currently used naming scheme.
mutex_lock
void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
dfplayer_next
static int dfplayer_next(dfplayer_t *dev)
Start playing the next song in the currently used naming scheme.
dfplayer_source_set_add
static void dfplayer_source_set_add(dfplayer_source_set_t set, dfplayer_source_t src)
Add the given source to the given source set.
EINVAL
#define EINVAL
Invalid argument.
Definition: errno.h:97
DFPLAYER_CMD_SET_VOLUME
#define DFPLAYER_CMD_SET_VOLUME
Set the volume to the given level.
Definition: dfplayer_constants.h:57
DFPLAYER_CMD_FILENO_SDCARD
#define DFPLAYER_CMD_FILENO_SDCARD
Get the currently select file number on the SD-Card.
Definition: dfplayer_constants.h:81
dfplayer::file
dfplayer_file_t file
Currently played song.
Definition: dfplayer_types.h:189
dfplayer::mutex
mutex_t mutex
Used to mutual exclusive access.
Definition: dfplayer_types.h:192
dfplayer_get_played_file
static dfplayer_file_t dfplayer_get_played_file(dfplayer_t *dev)
Get the currently played file and the used naming scheme.
DFPLAYER_CMD_STANDBY_EXIT
#define DFPLAYER_CMD_STANDBY_EXIT
Exit low power mode, back to normal mode.
Definition: dfplayer_constants.h:61
dfplayer_set_source
static int dfplayer_set_source(dfplayer_t *dev, dfplayer_source_t src)
Apply the given source.
DFPLAYER_CMD_GET_MODE
#define DFPLAYER_CMD_GET_MODE
Retrieve the current playback mode.
Definition: dfplayer_constants.h:75
DFPLAYER_CMD_FILENO_USB
#define DFPLAYER_CMD_FILENO_USB
Get the currently select file number on the USB storage.
Definition: dfplayer_constants.h:80
dfplayer_stop_advert
static int dfplayer_stop_advert(dfplayer_t *dev)
Stop playing a file from the ADVERT folder and resume previous playback.
DFPLAYER_SOURCE_NUMOF
@ DFPLAYER_SOURCE_NUMOF
Number of supported playback modes.
Definition: dfplayer_types.h:79
dfplayer_get_fileno_flash
static int dfplayer_get_fileno_flash(dfplayer_t *dev, uint16_t *fileno)
Query the selected file on the NOR flash.
dfplayer_eq_t
dfplayer_eq_t
Enumeration of the equalizer settings supported by the DFPlayer.
Definition: dfplayer_types.h:48
dfplayer_shuffle_all
static int dfplayer_shuffle_all(dfplayer_t *dev)
Launch shuffle playback of all files.
dfplayer_get_fileno_usb
static int dfplayer_get_fileno_usb(dfplayer_t *dev, uint16_t *fileno)
Query the selected file on the USB storage device.
dfplayer_file_cmd
int dfplayer_file_cmd(dfplayer_t *dev, uint8_t cmd, uint8_t p1, uint8_t p2)
Send a command selecting a playback file.
dfplayer_get_equalizer
static int dfplayer_get_equalizer(dfplayer_t *dev, dfplayer_eq_t *equalizer)
Query the current equalizer setting of the DFPlayer Mini.
dfplayer_get_mode
static int dfplayer_get_mode(dfplayer_t *dev, dfplayer_mode_t *mode)
Query the current playback mode of the DFPlayer Mini.
dfplayer_play
static int dfplayer_play(dfplayer_t *dev)
Start/resume playing.
dfplayer_types.h
Types used in the DFPlayer Mini Device Driver.
dfplayer_get_fileno_sdcard
static int dfplayer_get_fileno_sdcard(dfplayer_t *dev, uint16_t *fileno)
Query the selected file on the SD card.
DFPLAYER_CMD_FILES_SDCARD
#define DFPLAYER_CMD_FILES_SDCARD
Get the total number of files on the SD card.
Definition: dfplayer_constants.h:78
dfplayer_set_equalizer
static int dfplayer_set_equalizer(dfplayer_t *dev, dfplayer_eq_t equalizer)
Apply the given equalizer setting.
dfplayer_enter_standby
static int dfplayer_enter_standby(dfplayer_t *dev)
Enter standby mode.
dfplayer_source_t
dfplayer_source_t
Enumeration of the different sources for playback supported.
Definition: dfplayer_types.h:73
DFPLAYER_EQ_NUMOF
@ DFPLAYER_EQ_NUMOF
Number of supported equalizer settings.
Definition: dfplayer_types.h:55
DFPLAYER_CMD_GET_VOLUME
#define DFPLAYER_CMD_GET_VOLUME
Retrieve the current volume.
Definition: dfplayer_constants.h:73
dfplayer_get_version
static int dfplayer_get_version(dfplayer_t *dev, uint16_t *version)
Query the software version running on the DFPlayer Mini.
dfplayer_files_sdcard
static int dfplayer_files_sdcard(dfplayer_t *dev, uint16_t *files)
Query the number of files on the SD card.
dfplayer_exit_standby
static int dfplayer_exit_standby(dfplayer_t *dev)
Exit standby mode.
dfplayer_cmd
static int dfplayer_cmd(dfplayer_t *dev, uint8_t cmd)
Send a command without parameters to the DFPlayer Mini.
Definition: dfplayer_internal.h:187
dfplayer.h
DFPlayer Mini Device Driver.
dfplayer_pause
static int dfplayer_pause(dfplayer_t *dev)
Pause the playback.
dfplayer_query
static int dfplayer_query(dfplayer_t *dev, uint16_t *resp, uint8_t cmd)
Send a query and receive the response.
Definition: dfplayer_internal.h:204
DFPLAYER_CMD_FILENO_FLASH
#define DFPLAYER_CMD_FILENO_FLASH
Get the currently select file number on the NOR flash.
Definition: dfplayer_constants.h:82
DFPLAYER_CMD_REPEAT_FOLDER
#define DFPLAYER_CMD_REPEAT_FOLDER
Start repeat-playing the given folder (1-99)
Definition: dfplayer_constants.h:69
dfplayer_repeat_folder
static int dfplayer_repeat_folder(dfplayer_t *dev, uint8_t folder)
Start playing and repeating the specified folder.
dfplayer_files_usb
static int dfplayer_files_usb(dfplayer_t *dev, uint16_t *files)
Query the number of files on the USB storage device.
dfplayer_source_set_rm
static void dfplayer_source_set_rm(dfplayer_source_set_t set, dfplayer_source_t src)
Remove the given source to the given source set.
DFPLAYER_MAX_VOLUME
#define DFPLAYER_MAX_VOLUME
Maximum supported volume.
Definition: dfplayer_constants.h:182
DFPLAYER_CMD_GET_VERSION
#define DFPLAYER_CMD_GET_VERSION
Retrieve the device's software version.
Definition: dfplayer_constants.h:76
DFPLAYER_CMD_SET_SOURCE
#define DFPLAYER_CMD_SET_SOURCE
Set the source to play files from.
Definition: dfplayer_constants.h:59
DFPLAYER_CMD_REPEAT
#define DFPLAYER_CMD_REPEAT
0 = repeat currently played file, 1 = stop repeating
Definition: dfplayer_constants.h:71
DFPLAYER_CMD_FILES_FLASH
#define DFPLAYER_CMD_FILES_FLASH
Get the total number of files on NOR flash.
Definition: dfplayer_constants.h:79
DFPLAYER_CMD_SET_EQUALIZER
#define DFPLAYER_CMD_SET_EQUALIZER
Set the equalizer to the given setting.
Definition: dfplayer_constants.h:58
dfplayer_prev
static int dfplayer_prev(dfplayer_t *dev)
Start playing the previous song in the currently used naming scheme.
dfplayer_source_set_contains
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.
DFPLAYER_CMD_RANDOM
#define DFPLAYER_CMD_RANDOM
Start playing all files in random order.
Definition: dfplayer_constants.h:70
dfplayer_get_volume
static int dfplayer_get_volume(dfplayer_t *dev, uint8_t *volume)
Query the current volume of the DFPlayer Mini.
dfplayer_repeat
static int dfplayer_repeat(dfplayer_t *dev, bool repeat)
Enable or disable repeat playback.
DFPLAYER_CMD_ABORT_ADVERT
#define DFPLAYER_CMD_ABORT_ADVERT
Play the given file (1-9999) from the folder "ADVERT", resume current playback afterwards.
Definition: dfplayer_constants.h:68
DFPLAYER_CMD_PLAY
#define DFPLAYER_CMD_PLAY
Start playing the selected file.
Definition: dfplayer_constants.h:63
DFPLAYER_CMD_GET_EQUALIZER
#define DFPLAYER_CMD_GET_EQUALIZER
Retrieve the current equalizer setting.
Definition: dfplayer_constants.h:74
dfplayer_constants.h
Constants used in the DFPlayer Mini Driver.
dfplayer_files_flash
static int dfplayer_files_flash(dfplayer_t *dev, uint16_t *files)
Query the number of files on the NOR flash.
DFPLAYER_CMD_STANDBY_ENTER
#define DFPLAYER_CMD_STANDBY_ENTER
Enter low power mode.
Definition: dfplayer_constants.h:60
dfplayer_cmd_1param
static int dfplayer_cmd_1param(dfplayer_t *dev, uint8_t cmd, uint8_t param)
Send a command with one parameter to the DFPlayer Mini.
Definition: dfplayer_internal.h:171
dfplayer_internal.h
Internal functions of DFPlayer Mini Device driver.
DFPLAYER_CMD_FILES_USB
#define DFPLAYER_CMD_FILES_USB
Get the total number of files on USB storage.
Definition: dfplayer_constants.h:77
dfplayer_file_t
Data structure representing a file on the DFPlayer.
Definition: dfplayer_types.h:170
dfplayer_source_set_t
uint8_t dfplayer_source_set_t
Set of DFPlayer playback sources.
Definition: dfplayer_types.h:104
mutex_unlock
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
errno.h
dfplayer
A DFPlayer Mini device descriptor.
Definition: dfplayer_types.h:184
DFPLAYER_CMD_PAUSE
#define DFPLAYER_CMD_PAUSE
Pause the playback.
Definition: dfplayer_constants.h:64