JQ6500 MP3 Player Arduino Library
A simple library to control a JQ6500 MP3 Player Module from an Arduino.
JQ6500_Serial.h
Go to the documentation of this file.
1 
29 // Please note, the Arduino IDE is a bit retarded, if the below define has an
30 // underscore other than _h, it goes mental. Wish it wouldn't mess
31 // wif ma files!
32 #ifndef JQ6500Serial_h
33 #define JQ6500Serial_h
34 
35 
36 #define MP3_EQ_NORMAL 0
37 #define MP3_EQ_POP 1
38 #define MP3_EQ_ROCK 2
39 #define MP3_EQ_JAZZ 3
40 #define MP3_EQ_CLASSIC 4
41 #define MP3_EQ_BASS 5
42 
43 #define MP3_SRC_SDCARD 1
44 #define MP3_SRC_BUILTIN 4
45 
46 // Looping options, ALL, FOLDER, ONE and ONE_STOP are the
47 // only ones that appear to do much interesting
48 // ALL plays all the tracks in a repeating loop
49 // FOLDER plays all the tracks in the same folder in a repeating loop
50 // ONE plays the same track repeating
51 // ONE_STOP does not loop, plays the track and stops
52 // RAM seems to play one track and someties disables the ability to
53 // move to next/previous track, really weird.
54 
55 #define MP3_LOOP_ALL 0
56 #define MP3_LOOP_FOLDER 1
57 #define MP3_LOOP_ONE 2
58 #define MP3_LOOP_RAM 3
59 #define MP3_LOOP_ONE_STOP 4
60 #define MP3_LOOP_NONE 4
61 
62 #define MP3_STATUS_STOPPED 0
63 #define MP3_STATUS_PLAYING 1
64 #define MP3_STATUS_PAUSED 2
65 
66 // The response from a status query we get is for some reason
67 // a bit... iffy, most of the time it is reliable, but sometimes
68 // instead of a playing (1) response, we get a paused (2) response
69 // even though it is playing. Stopped responses seem reliable.
70 // So to work around this when getStatus() is called we actually
71 // request the status this many times and only if one of them is STOPPED
72 // or they are all in agreement that it is playing or paused then
73 // we return that status. If some of them differ, we do another set
74 // of tests etc...
75 #define MP3_STATUS_CHECKS_IN_AGREEMENT 4
76 
77 #define MP3_DEBUG 0
78 
80 {
81 
82  public:
83 
111  JQ6500_Serial(Stream *dev) : _serial(dev) {}
112  JQ6500_Serial(Stream &dev) : _serial(&dev) {}
113 
117  void play();
118 
131  void restart();
132 
137  void pause();
138 
142  void next();
143 
147  void prev();
148 
152  void nextFolder();
153 
157  void prevFolder();
158 
167  void playFileByIndexNumber(unsigned int fileNumber);
168 
180  void playFileNumberInFolderNumber(unsigned int folderNumber, unsigned int fileNumber);
181 
184  void volumeUp();
185 
188  void volumeDn();
189 
195  void setVolume(byte volumeFrom0To30);
196 
210  void setEqualizer(byte equalizerMode); // EQ_NORMAL to EQ_BASS
211 
223  void setLoopMode(byte loopMode);
224 
233  void setSource(byte source); // SRC_BUILTIN or SRC_SDCARD
234 
243  void sleep();
244 
257  void reset();
258 
259  // Status querying commands
273  byte getStatus();
274 
280  byte getVolume();
281 
294  byte getEqualizer();
295 
307  byte getLoopMode();
308 
316  unsigned int countFiles(byte source);
317 
326  unsigned int countFolders(byte source);
327 
337  unsigned int currentFileIndexNumber(byte source);
338 
345  unsigned int currentFilePositionInSeconds();
346 
353  unsigned int currentFileLengthInSeconds();
354 
368  void currentFileName(char *buffer, unsigned int bufferLength);
369 
370 
371  protected:
372 
381  void sendCommand(byte command, byte arg1, byte arg2, char *responseBuffer, unsigned int bufferLength);
382 
383  // Just some different versions of that for ease of use
384  void sendCommand(byte command);
385  void sendCommand(byte command, byte arg1);
386  void sendCommand(byte command, byte arg1, byte arg2);
387 
397  unsigned int sendCommandWithUnsignedIntResponse(byte command);
398 
399  // This seems not that useful since there only seems to be a version 1 anway :/
400  unsigned int getVersion();
401 
402  int waitUntilAvailable(unsigned long maxWaitTime = 1000);
403 
404  Stream *_serial;
405 
406  static const uint8_t MP3_CMD_BEGIN = 0x7E;
407  static const uint8_t MP3_CMD_END = 0xEF;
408 
409  static const uint8_t MP3_CMD_PLAY = 0x0D;
410  static const uint8_t MP3_CMD_PAUSE = 0x0E;
411  static const uint8_t MP3_CMD_NEXT = 0x01;
412  static const uint8_t MP3_CMD_PREV = 0x02;
413  static const uint8_t MP3_CMD_PLAY_IDX = 0x03;
414 
415  static const uint8_t MP3_CMD_NEXT_FOLDER = 0x0F;
416  static const uint8_t MP3_CMD_PREV_FOLDER = 0x0F; // Note the same as next, the data byte indicates direction
417 
418  static const uint8_t MP3_CMD_PLAY_FILE_FOLDER = 0x12;
419 
420  static const uint8_t MP3_CMD_VOL_UP = 0x04;
421  static const uint8_t MP3_CMD_VOL_DN = 0x05;
422  static const uint8_t MP3_CMD_VOL_SET = 0x06;
423 
424  static const uint8_t MP3_CMD_EQ_SET = 0x07;
425  static const uint8_t MP3_CMD_LOOP_SET = 0x11;
426  static const uint8_t MP3_CMD_SOURCE_SET = 0x09;
427  static const uint8_t MP3_CMD_SLEEP = 0x0A;
428  static const uint8_t MP3_CMD_RESET = 0x0C;
429 
430  static const uint8_t MP3_CMD_STATUS = 0x42;
431  static const uint8_t MP3_CMD_VOL_GET = 0x43;
432  static const uint8_t MP3_CMD_EQ_GET = 0x44;
433  static const uint8_t MP3_CMD_LOOP_GET = 0x45;
434  static const uint8_t MP3_CMD_VER_GET = 0x46;
435 
436  static const uint8_t MP3_CMD_COUNT_SD = 0x47;
437  static const uint8_t MP3_CMD_COUNT_MEM = 0x49;
438  static const uint8_t MP3_CMD_COUNT_FOLDERS = 0x53;
439  static const uint8_t MP3_CMD_CURRENT_FILE_IDX_SD = 0x4B;
440  static const uint8_t MP3_CMD_CURRENT_FILE_IDX_MEM = 0x4D;
441 
442  static const uint8_t MP3_CMD_CURRENT_FILE_POS_SEC = 0x50;
443  static const uint8_t MP3_CMD_CURRENT_FILE_LEN_SEC = 0x51;
444  static const uint8_t MP3_CMD_CURRENT_FILE_NAME = 0x52;
445 
446 };
447 
448 #endif
Definition: JQ6500_Serial.h:80
void prev()
Play the previous file.
Definition: JQ6500_Serial.cpp:58
void nextFolder()
Play the next folder.
Definition: JQ6500_Serial.cpp:68
void next()
Play the next file.
Definition: JQ6500_Serial.cpp:53
void sleep()
Put the device to sleep.
Definition: JQ6500_Serial.cpp:113
byte getStatus()
Get the status from the device.
Definition: JQ6500_Serial.cpp:125
void playFileNumberInFolderNumber(unsigned int folderNumber, unsigned int fileNumber)
Play a specific file in a specific folder based on the name of those folder and file.
Definition: JQ6500_Serial.cpp:78
void currentFileName(char *buffer, unsigned int bufferLength)
Get the name of the "current" file on the SD Card.
Definition: JQ6500_Serial.cpp:190
byte getVolume()
Get the current volume level.
Definition: JQ6500_Serial.cpp:144
unsigned int countFolders(byte source)
Count the number of folders on the specified media.
Definition: JQ6500_Serial.cpp:163
void volumeDn()
Decrease the volume by 1 (volume ranges 0 to 30).
Definition: JQ6500_Serial.cpp:88
void setVolume(byte volumeFrom0To30)
Set the volume to a specific level (0 to 30).
Definition: JQ6500_Serial.cpp:93
void sendCommand(byte command, byte arg1, byte arg2, char *responseBuffer, unsigned int bufferLength)
Send a command to the JQ6500 module,.
Definition: JQ6500_Serial.cpp:219
void reset()
Reset the device (softly).
Definition: JQ6500_Serial.cpp:118
byte getLoopMode()
Get loop mode.
Definition: JQ6500_Serial.cpp:146
unsigned int countFiles(byte source)
Count the number of files on the specified media.
Definition: JQ6500_Serial.cpp:149
void volumeUp()
Increase the volume by 1 (volume ranges 0 to 30).
Definition: JQ6500_Serial.cpp:83
unsigned int currentFileIndexNumber(byte source)
For the currently playing (or paused, or file that would be played next if stopped) file,...
Definition: JQ6500_Serial.cpp:173
void restart()
Restart the current (possibly paused) track from the beginning.
Definition: JQ6500_Serial.cpp:38
byte getEqualizer()
Get the equalizer mode.
Definition: JQ6500_Serial.cpp:145
unsigned int sendCommandWithUnsignedIntResponse(byte command)
Send a command to the JQ6500 module, and get a response.
Definition: JQ6500_Serial.cpp:197
void setLoopMode(byte loopMode)
Set the looping mode.
Definition: JQ6500_Serial.cpp:103
JQ6500_Serial(Stream *dev)
Create JQ6500 object.
Definition: JQ6500_Serial.h:111
void playFileByIndexNumber(unsigned int fileNumber)
Play a specific file based on it's (FAT table) index number.
Definition: JQ6500_Serial.cpp:63
unsigned int currentFileLengthInSeconds()
For the currently playing or paused file, return the total length of the file in seconds.
Definition: JQ6500_Serial.cpp:188
void prevFolder()
Play the previous folder.
Definition: JQ6500_Serial.cpp:73
void setEqualizer(byte equalizerMode)
Set the equalizer to one of 6 preset modes.
Definition: JQ6500_Serial.cpp:98
void pause()
Pause the current file.
Definition: JQ6500_Serial.cpp:48
unsigned int currentFilePositionInSeconds()
For the currently playing or paused file, return the current position in seconds.
Definition: JQ6500_Serial.cpp:187
void play()
Start playing the current file.
Definition: JQ6500_Serial.cpp:33
void setSource(byte source)
Set the source to read mp3 data from.
Definition: JQ6500_Serial.cpp:108