Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
DFPlayerMini.h
1 #pragma once
2 
3 #include <Arduino.h>
4 #include <stdint.h>
5 
6 struct DFPResponse; // defined below
7 
25 class DFPlayerMini {
26 public:
27  static const uint8_t BufferLength = 10;
28  static const uint8_t MinimumTimeMs = 20;
29  static const uint8_t ResponseTimeoutMs = 20;
30 
31  // Structure of the command packet
32  static const uint8_t PacketOffsetCmd = 3;
33  static const uint8_t PacketOffsetArg = 5;
34  static const uint8_t PacketOffsetCkSum = 7;
35 
36 public:
37 
44  enum Cmd {
45  Undefined = 0x00,
46  Next = 0x01,
47  Prev = 0x02,
48  PlayPhysical = 0x03,
49  SetVolume = 0x06,
50  SetEqalizer = 0x07,
51  SetDevice = 0x09,
52  Sleep = 0x0A,
53  Reset = 0x0C,
54  Play = 0x0D,
55  Pause = 0x0E,
56  PlayTf = 0x12,
57  Stop = 0x16,
58  SingleLoop = 0x19,
59  GetStatus = 0x42,
60  GetVolume = 0x43,
61  GetUSum = 0x47,
62  GetTfSum = 0x48,
63  GetFlashSum = 0x49,
64  GetUCurrent = 0x4B,
65  GetTfCurrent = 0x4C,
67  };
68 
77  DFPlayerMini(Stream& serial, const uint8_t busyPin=0);
78 
87  void begin(bool bootWait=true);
88 
94  void sendCmd(Cmd cmd, uint16_t arg=0);
95 
104  DFPResponse query(Cmd cmd, uint8_t tries=3);
105 
121  bool busy();
122 
127  bool check();
128 
129 private:
130  DFPResponse _query(Cmd cmd);
131 
132  void resetSendBuf();
133  void copyBigend(uint8_t *thebuf, uint16_t data);
134  void fillChecksum();
135  uint16_t calculateChecksum(uint8_t *thebuf);
136  void serialCmd();
137 
138 private:
139  Stream& _serial;
140  const uint8_t _busyPin;
141  uint8_t _sendBuf[BufferLength];
142  uint32_t _lastCmdSent;
143 
144 };
145 
154 struct DFPResponse {
155  enum Status {
157  Ok=1,
161  };
162 
165  DFPResponse() : status(Incomplete), messageType(0), arg(0) {;}
166 
167  // Data members.
168  Status status;
169  uint8_t messageType;
170  uint16_t arg;
171 };
172 
DFPlayerMini(Stream &serial, const uint8_t busyPin=0)
Definition: DFPlayerMini.cpp:6
Get volume setting.
Definition: DFPlayerMini.h:60
Play track arg from (SD card)
Definition: DFPlayerMini.h:56
Get file count Tf.
Definition: DFPlayerMini.h:62
Enter low power sleep mode.
Definition: DFPlayerMini.h:52
Play track arg.
Definition: DFPlayerMini.h:54
Get file count Flash.
Definition: DFPlayerMini.h:63
Indicates broken serial comms.
Definition: DFPlayerMini.h:159
DFPResponse query(Cmd cmd, uint8_t tries=3)
Get file current Flash [No response].
Definition: DFPlayerMini.h:66
Messaged received and is valid.
Definition: DFPlayerMini.h:157
void sendCmd(Cmd cmd, uint16_t arg=0)
Get file current Tf.
Definition: DFPlayerMini.h:65
Get some meaningless status code.
Definition: DFPlayerMini.h:59
Set when we start receiving, should not remain at end.
Definition: DFPlayerMini.h:156
Play next track.
Definition: DFPlayerMini.h:46
Pause playback.
Definition: DFPlayerMini.h:55
Set device, arg 1-5 (U, SD, AUX, SLEEP, FLASH)
Definition: DFPlayerMini.h:51
Set EQ, arg 0-5 (normal=0, pop, rock, jazz, classical, bass)
Definition: DFPlayerMini.h:50
Stop playback.
Definition: DFPlayerMini.h:57
Loop track arg = boolean !state.
Definition: DFPlayerMini.h:58
Play previous track.
Definition: DFPlayerMini.h:47
Response was not validated (bad cksum, header etc)
Definition: DFPlayerMini.h:160
Exit sleep mode.
Definition: DFPlayerMini.h:53
Get file count U.
Definition: DFPlayerMini.h:61
Message timed out (DFPlayerMini::ResponseTimeoutMs ms passed)
Definition: DFPlayerMini.h:158
void begin(bool bootWait=true)
Get file current U.
Definition: DFPlayerMini.h:64
Set volume to arg (0-30)
Definition: DFPlayerMini.h:49