JQ8400 MP3 Player Arduino Library
A simple library to control a JQ8400 MP3 Player Module from an Arduino.
JQ8400_Serial.h
Go to the documentation of this file.
1 
29 #ifndef JQ8400Serial_h
30 #define JQ8400Serial_h
31 
32 #define MP3_EQ_NORMAL 0
33 #define MP3_EQ_POP 1
34 #define MP3_EQ_ROCK 2
35 #define MP3_EQ_JAZZ 3
36 #define MP3_EQ_CLASSIC 4
37 
38 #define MP3_SRC_USB 0
39 #define MP3_SRC_SDCARD 1
40 #define MP3_SRC_FLASH 2
41 #define MP3_SRC_BUILTIN MP3_SRC_FLASH
42 
43 // Loop Options
44 // ALL = all tracks loop, ALL_STOP all tracks then stop, ALL_RANDOM all tracks randomly
45 // ONE = one track loop, ONE_STOP (default) one track then stop
46 // FOLDER = tracks within folder loop, FOLDER_STOP tracks within folder then stop, FOLDER_RANDOM random in folder
47 
48 #define MP3_LOOP_ALL 0
49 #define MP3_LOOP_ALL_STOP 7
50 #define MP3_LOOP_ALL_RANDOM 3
51 
52 #define MP3_LOOP_ONE 1
53 #define MP3_LOOP_ONE_STOP 2
54 
55 #define MP3_LOOP_FOLDER 4
56 #define MP3_LOOP_FOLDER_RANDOM 5
57 #define MP3_LOOP_FOLDER_STOP 6
58 
59 #define MP3_LOOP_NONE 2
60 
61 #define MP3_STATUS_STOPPED 0
62 #define MP3_STATUS_PLAYING 1
63 #define MP3_STATUS_PAUSED 2
64 
65 // The response from a status query could be unreliable
66 // we can increase this to check multiple times.
67 #define MP3_STATUS_CHECKS_IN_AGREEMENT 1
68 
69 #define MP3_DEBUG 0
70 
71 #define HEX_PRINT(a) if(a < 16) Serial.print(0); Serial.print(a, HEX);
72 
74 {
75  protected:
76  Stream *_Serial;
77 
78  public:
79 
145  JQ8400_Serial(Stream &_Stream) { _Serial = &_Stream; };
146 
153  void play();
154 
159  void restart();
160 
165  void pause();
166 
170  void stop();
171 
175  void fastForward(uint16_t seconds = 5);
176 
177 
181  void rewind(uint16_t seconds = 5);
182 
186  void next();
187 
191  void prev();
192 
196  void nextFolder();
197 
201  void prevFolder();
202 
223  void playFileByIndexNumber(uint16_t fileNumber);
224 
248  void interjectFileByIndexNumber(uint16_t fileNumber);
249 
250 
272  void playFileNumberInFolderNumber(uint16_t folderNumber, uint16_t fileNumber);
273 
286  void playInFolderNumber(uint16_t folderNumber);
287 
288 
317  void seekFileByIndexNumber(uint16_t fileNumber);
318 
346  void abLoopPlay(uint16_t secondsStart, uint16_t secondsEnd);
347 
356  void abLoopClear();
357 
360  void volumeUp();
361 
364  void volumeDn();
365 
371  void setVolume(byte volumeFrom0To30);
372 
385  void setEqualizer(byte equalizerMode); // EQ_NORMAL to EQ_BASS
386 
410  void setLoopMode(byte loopMode);
411 
421  void setSource(byte source);
422 
432  uint8_t getSource();
433 
444  uint8_t sourceAvailable(uint8_t source)
445  {
446  return getAvailableSources() & 1<<source;
447  }
448 
459  void sleep();
460 
472  void reset();
473 
479  byte getStatus();
480 
488  uint8_t busy() { return getStatus() == MP3_STATUS_PLAYING; }
489 
495  byte getVolume();
496 
509  byte getEqualizer();
510 
527  byte getLoopMode();
528 
529 
536  uint16_t countFiles();
537 
561  uint16_t currentFileIndexNumber();
562 
570  uint16_t currentFilePositionInSeconds();
571 
579  uint16_t currentFileLengthInSeconds();
580 
600  void currentFileName(char *buffer, uint16_t bufferLength);
601 
626  void playSequenceByFileNumber(uint8_t playList[], uint8_t listLength);
627 
650  void playSequenceByFileName(const char *playList[], uint8_t listLength);
651 
652 
653 
654  protected:
655 
666  void sendCommandData(uint8_t command, uint8_t *requestBuffer, uint8_t requestLength, uint8_t *responseBuffer, uint8_t bufferLength);
667 
673  inline void sendCommand(uint8_t command, uint8_t *responseBuffer = 0, uint8_t bufferLength = 0)
674  {
675  sendCommandData(command, NULL, 0, responseBuffer, bufferLength);
676  }
677 
684  inline void sendCommand(uint8_t command, uint8_t arg, uint8_t *responseBuffer = 0, uint8_t bufferLength = 0)
685  {
686  sendCommandData(command, &arg, 1, responseBuffer, bufferLength);
687  }
688 
695  inline void sendCommand(uint8_t command, uint16_t arg, uint8_t *responseBuffer = 0, uint8_t bufferLength = 0)
696  {
697  #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
698  sendCommandData(command, ((uint8_t *)(&arg)), 2, responseBuffer, bufferLength);
699  #else
700  uint8_t buf[] = { *(((uint8_t *)(&arg))+1), *((uint8_t *)(&arg)) };
701  sendCommandData(command, buf, 2, responseBuffer, bufferLength);
702  #endif
703  }
704 
711  uint16_t sendCommandWithUnsignedIntResponse(byte command);
712 
719  uint8_t sendCommandWithByteResponse(uint8_t command);
720 
721 
731  uint8_t getAvailableSources();
732 
739  int waitUntilAvailable(uint16_t maxWaitTime = 1000);
740 
741 
742  uint8_t currentVolume = 20;
743  uint8_t currentEq = 0;
744  uint8_t currentLoop = 2;
745 
749  static const uint8_t MP3_CMD_BEGIN = 0xAA;
751 
752  static const uint8_t MP3_CMD_PLAY = 0x02;
753  static const uint8_t MP3_CMD_PAUSE = 0x03;
754 
755  static const uint8_t MP3_CMD_FFWD = 0x23;
756  static const uint8_t MP3_CMD_RWND = 0x22;
757 
758  static const uint8_t MP3_CMD_STOP = 0x10; // Not sure, maybe 0x04?
759 
760  static const uint8_t MP3_CMD_NEXT = 0x06;
761  static const uint8_t MP3_CMD_PREV = 0x05;
762  static const uint8_t MP3_CMD_PLAY_IDX = 0x07;
763  static const uint8_t MP3_CMD_SEEK_IDX = 0x1F;
764  static const uint8_t MP3_CMD_INSERT_IDX = 0x16;
765 
766  static const uint8_t MP3_CMD_AB_PLAY = 0x20;
767  static const uint8_t MP3_CMD_AB_PLAY_STOP = 0x21;
768 
769  static const uint8_t MP3_CMD_NEXT_FOLDER = 0x0F;
770  static const uint8_t MP3_CMD_PREV_FOLDER = 0x0E;
771 
772  static const uint8_t MP3_CMD_PLAY_FILE_FOLDER = 0x08;
773 
774  static const uint8_t MP3_CMD_VOL_UP = 0x14;
775  static const uint8_t MP3_CMD_VOL_DN = 0x15;
776  static const uint8_t MP3_CMD_VOL_SET = 0x13;
777 
778  static const uint8_t MP3_CMD_EQ_SET = 0x1A;
779  static const uint8_t MP3_CMD_LOOP_SET = 0x18;
780  static const uint8_t MP3_CMD_SOURCE_SET = 0x0B;
781 
782  static const uint8_t MP3_CMD_SLEEP = 0x04; // I am not sure about these, see implmentation of sleep() and reset()
783  static const uint8_t MP3_CMD_RESET = 0x04; // what I have done seems to work maybe, maybe.
784 
785  static const uint8_t MP3_CMD_STATUS = 0x01;
786 
787  static const uint8_t MP3_CMD_GET_SOURCES = 0x09;
788  static const uint8_t MP3_CMD_GET_SOURCE = 0x0A;
789 
790  static const uint8_t MP3_CMD_COUNT_FILES = 0x0C;
791  static const uint8_t MP3_CMD_COUNT_IN_FOLDER = 0x12;
792 
793  static const uint8_t MP3_CMD_CURRENT_FILE_IDX = 0x0D;
794  static const uint8_t MP3_CMD_FIRST_FILE_IN_FOLDER_IDX = 0x11;
795 
796  static const uint8_t MP3_CMD_CURRENT_FILE_LEN = 0x24;
797  static const uint8_t MP3_CMD_CURRENT_FILE_POS = 0x25; // This turns on continuous reporting of position
798  static const uint8_t MP3_CMD_CURRENT_FILE_POS_STOP = 0x26; // This stops that
799  static const uint8_t MP3_CMD_CURRENT_FILE_NAME = 0x1E;
800 
801  static const uint8_t MP3_CMD_PLAYLIST = 0x1B;
803 };
804 
805 #endif
int waitUntilAvailable(uint16_t maxWaitTime=1000)
Blocking wait with a timeout for serial input.
Definition: JQ8400_Serial.cpp:533
void abLoopPlay(uint16_t secondsStart, uint16_t secondsEnd)
A-B Loop for the file currently playing.
Definition: JQ8400_Serial.cpp:81
void seekFileByIndexNumber(uint16_t fileNumber)
Seek to a specific file based on it's FAT index number.
Definition: JQ8400_Serial.cpp:75
void setSource(byte source)
Set the source to read mp3 data from.
Definition: JQ8400_Serial.cpp:245
void prevFolder()
Play the previous folder.
Definition: JQ8400_Serial.cpp:109
uint8_t currentLoop
Record of current loop mode (JQ8400 has no way to query)
Definition: JQ8400_Serial.h:744
void setLoopMode(byte loopMode)
Set the looping mode.
Definition: JQ8400_Serial.cpp:233
Definition: JQ8400_Serial.h:73
void pause()
Pause the current file.
Definition: JQ8400_Serial.cpp:43
uint8_t currentEq
Record of current equalizer (JQ8400 has no way to query)
Definition: JQ8400_Serial.h:743
uint16_t countFiles()
Count the number of files on the current media.
Definition: JQ8400_Serial.cpp:336
void sendCommandData(uint8_t command, uint8_t *requestBuffer, uint8_t requestLength, uint8_t *responseBuffer, uint8_t bufferLength)
Send a command to the JQ8400 module,.
Definition: JQ8400_Serial.cpp:393
Stream * _Serial
Set in the constructor, the stream (eg HardwareSerial or SoftwareSerial object) that connects us to t...
Definition: JQ8400_Serial.h:76
void volumeDn()
Decrease the volume by 1 (volume ranges 0 to 30).
Definition: JQ8400_Serial.cpp:215
void fastForward(uint16_t seconds=5)
Fast Forward by a number of seconds (default 5).
Definition: JQ8400_Serial.cpp:92
void sleep()
Put the device to sleep.
Definition: JQ8400_Serial.cpp:256
void stop()
Stop the current playing (if any).
Definition: JQ8400_Serial.cpp:48
byte getLoopMode()
Get loop mode.
Definition: JQ8400_Serial.cpp:333
uint8_t busy()
Return if the device is busy (playing) or not.
Definition: JQ8400_Serial.h:488
uint8_t getSource()
Return the currently selected source.
Definition: JQ8400_Serial.cpp:250
uint16_t sendCommandWithUnsignedIntResponse(byte command)
Send a command to the JQ8400 module, and get a 16 bit integer response.
Definition: JQ8400_Serial.cpp:379
void playFileByIndexNumber(uint16_t fileNumber)
Play a specific file based on it's FAT index number.
Definition: JQ8400_Serial.cpp:63
void setEqualizer(byte equalizerMode)
Set the equalizer to one of 6 preset modes.
Definition: JQ8400_Serial.cpp:227
JQ8400_Serial(Stream &_Stream)
Create JQ8400 object with a given serial object to communicate to the JQ8400.
Definition: JQ8400_Serial.h:145
uint8_t sourceAvailable(uint8_t source)
Return boolean indicating if the given source is available (can be selected using setSource()) ...
Definition: JQ8400_Serial.h:444
uint8_t getAvailableSources()
Return a bitmask of the available sources.
Definition: JQ8400_Serial.cpp:240
byte getEqualizer()
Get the equalizer mode.
Definition: JQ8400_Serial.cpp:332
uint8_t currentVolume
Record of current volume level (JQ8400 has no way to query)
Definition: JQ8400_Serial.h:742
void next()
Play the next file.
Definition: JQ8400_Serial.cpp:53
void sendCommand(uint8_t command, uint8_t arg, uint8_t *responseBuffer=0, uint8_t bufferLength=0)
Send a command with a single 8 bit argument and no response.
Definition: JQ8400_Serial.h:684
void rewind(uint16_t seconds=5)
Rewind by a number of seconds (default 5).
Definition: JQ8400_Serial.cpp:98
void nextFolder()
Play the next folder.
Definition: JQ8400_Serial.cpp:104
byte getVolume()
Get the current volume level.
Definition: JQ8400_Serial.cpp:331
void setVolume(byte volumeFrom0To30)
Set the volume to a specific level (0 to 30).
Definition: JQ8400_Serial.cpp:221
void playSequenceByFileNumber(uint8_t playList[], uint8_t listLength)
Play a sequence of files, which must all exist in a folder called "ZH" and be named 00...
Definition: JQ8400_Serial.cpp:178
void interjectFileByIndexNumber(uint16_t fileNumber)
Interject the currently playing file (if any) with the given FAT index number file.
Definition: JQ8400_Serial.cpp:69
uint8_t sendCommandWithByteResponse(uint8_t command)
Send a command to the JQ8400 module, and get an 8 bit integer response.
Definition: JQ8400_Serial.cpp:386
void reset()
Reset the device (softly).
Definition: JQ8400_Serial.cpp:270
uint16_t currentFileLengthInSeconds()
For the currently playing or paused file, return the total length of the file in seconds.
Definition: JQ8400_Serial.cpp:359
uint16_t currentFilePositionInSeconds()
For the currently playing or paused file, return the current position in seconds. ...
Definition: JQ8400_Serial.cpp:346
void play()
Start playing the current file, if paused the playing is resumed.
Definition: JQ8400_Serial.cpp:32
void playSequenceByFileName(const char *playList[], uint8_t listLength)
Play a sequence of files, which must all exist in a folder called "ZH" and have 2 character names...
Definition: JQ8400_Serial.cpp:195
void restart()
Restart the current track from the beginning.
Definition: JQ8400_Serial.cpp:37
void abLoopClear()
Use this to break a currently running A-B loop.
Definition: JQ8400_Serial.cpp:87
void sendCommand(uint8_t command, uint8_t *responseBuffer=0, uint8_t bufferLength=0)
Send a command with no arguments and no response.
Definition: JQ8400_Serial.h:673
byte getStatus()
Get the status from the device.
Definition: JQ8400_Serial.cpp:307
void currentFileName(char *buffer, uint16_t bufferLength)
Get the name of the "current" file.
Definition: JQ8400_Serial.cpp:370
void playFileNumberInFolderNumber(uint16_t folderNumber, uint16_t fileNumber)
Play a specific file in a specific folder based on the name of those folder and file.
Definition: JQ8400_Serial.cpp:114
void sendCommand(uint8_t command, uint16_t arg, uint8_t *responseBuffer=0, uint8_t bufferLength=0)
Send a command with a 16 bit integer argument.
Definition: JQ8400_Serial.h:695
void volumeUp()
Increase the volume by 1 (volume ranges 0 to 30).
Definition: JQ8400_Serial.cpp:209
void playInFolderNumber(uint16_t folderNumber)
Play the first (?) file in a specific folder 00 to 99.
Definition: JQ8400_Serial.cpp:158
uint16_t currentFileIndexNumber()
For the currently playing (or paused, or file that would be played next if stopped) file...
Definition: JQ8400_Serial.cpp:341
void prev()
Play the previous file.
Definition: JQ8400_Serial.cpp:58