HCMP3 Arduino library Audio playback modules

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 743
Joined: Sun Aug 05, 2012 4:02 pm

HCMP3 Arduino library Audio playback modules

Post by admin » Thu May 09, 2019 10:18 am

[IMAGE TBA]




This Arduino library adds a set of commands for controlling an audio playback device. Currently this library supports the following module:

Serial MP3 playback module with 1W speaker (SKU: HCARDU0138)


This library is provided free to support the open source community. We spend most of our time creating free content like this because we genuinely want to support the open source community. PLEASE SUPPORT US so that we can continue to create free content by purchasing products from our store - HOBBYCOMPONENTS.COM



You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/




Using the HCMP3 library


To use the library just include the HCMP3.h header file and then create an instance of the library. E.g:
  1. #include "HCMP3.h"
  2. HCMP3 HCMP3(txPin, rxPin);

Where:

txPin is the digital Tx pin used transmit commands to the module.
rxPin is the digital Rx pin used receive commands from the module.




size=150]HCMP3 library functions:[/size]


  1. void HCMP3.play();

Plays the next file, or resumes a currently paused file



  1. void HCMP3.playAll();

Plays all the files on the flash card in sequence



  1. void HCMP3.play(index);

Plays a file with a specified index where:

index is an 8 bit value containing the index number of the file to be played
valid values for index are 0x01 to 0xFF



  1. void HCMP3.play(dir, index);

Plays a file with a specified folder and index where:

dir is an 8 bit value containing the index number of the folder holding the file to play
valid values for dir are 0x01 to 0xFF

index is an 8 bit value containing the index number of the file to play within the specified folder
valid values for index are 0x01 to 0xFF



  1. void HCMP3.pause();

Pauses a currently playing file



  1. void HCMP3.stop();

Stops a currently playing file



  1. void HCMP3.playNext();

Plays the file proceeding the last file played



  1. void HCMP3.playPrevious();

Plays the file preceding the last file played



  1. void HCMP3.volumeUp();

Increases the playback volume by one step



  1. void HCMP3.volume(level);

Sets the playback volume to the specified level where:

level is an 8 bit value containing the level to set the volume to in percent
valid values for level are 0 to 100



  1. void HCMP3.sleep(state);

Sets the sleep state of the module where:

state is a boolean value which specifies the sleep state. Valid values for state are:
false = wakes the module from sleep
true = puts the module to sleep



  1. void HCMP3.reset();

Resets the module



  1. void HCMP3.getPlayState();

Gets the current playback state of the module

Returns an 8 bit value containing the current state of the module where:
STATUS_STOP no files are currently playing
STATUS_PLAY module is currently playing a file
STATUS_PAUSED module is currently paused
STATUS_ERROR There was an error reading the status.



  1. void HCMP3.getVolumeState();

Gets the current volume level

Returns an 8 bit value representing the current volume level in % where:
0 is the minimum volume level
100 is the maximum volume level



  1. void HCMP3.getCardState();

Gets the current status of the flash card

Returns an 8 bit value containing the current state of the card where:
CARD_EJECTED A flash card in not currently connected to the module
CARD_INSERTED A flash card is connected to the module.
CARD_UNKOWN The state of the flash card is unknown.
STATUS_ERROR There was an error reading the status.




Example Sketch:

  1. /* FILE:    HCMP3_Example.cpp
  2.    DATE:    09/05/19
  3.    VERSION: 1.0
  4.    AUTHOR:  Andrew Davies
  5.    
  6. 09/05/19 version 1.0: Original version
  7.  
  8. This example sketch demonstrates how to use the HCMP3 library to control
  9. an audio playback device.
  10.  
  11. Currently this library supports the following module:
  12.  
  13. Serial MP3 playback module with 1W speaker (SKU: HCARDU0138)
  14.  
  15. Connect the above module as follows:
  16.  
  17. Arduino.........MP3 Module
  18. RX..............3
  19. TX..............2
  20. VCC.............5V
  21. GND.............GND
  22.  
  23. Within the HCMP3 library folder you will find a folder called 'MP3_Files'
  24. Copy the contents of this folder to the root directory of your flash card.
  25.  
  26. This library is provided free to support the open source community.
  27. We spend most of our time creating free content like this because we genuinely
  28. want to support the open source community. PLEASE SUPPORT US so that we can
  29. continue to create free content by purchasing products from our store -
  30. HOBBYCOMPONENTS.COM
  31.  
  32. You may copy, alter and reuse this code in any way you like, but please leave
  33. reference to HobbyComponents.com in your comments if you redistribute this code.
  34. This software may not be used directly for the purpose of selling products that
  35. directly compete with Hobby Components Ltd's own range of products.
  36. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  37. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  38. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  39. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  40. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  41. REASON WHATSOEVER.
  42. */
  43.  
  44.  
  45. /* Include the library */
  46. #include "HCMP3.h"
  47.  
  48. /* Digital pins used for serial communication */
  49. #define RX_PIN 2
  50. #define TX_PIN 3
  51.  
  52.  
  53. /* Create an instance of the library */
  54. HCMP3 HCMP3(TX_PIN, RX_PIN);
  55.  
  56. void setup()
  57. {
  58.   Serial.begin(9600);
  59.   HCMP3.reset();
  60.  
  61.   /* Get the current status of the module */
  62.   Serial.println("MODULE STATUS");
  63.   Serial.print("Card state....");
  64.   PrintCardState();
  65.   Serial.print("Playback state.....");
  66.   PrintPlaybackState();
  67.   Serial.print("Volume level....");
  68.   Serial.println(HCMP3.getVolumeState());
  69.   Serial.println("");
  70.   delay(2000);
  71.  
  72.   /* Change the volume level */
  73.   Serial.print("Setting volume level to....");
  74.   HCMP3.volume(10);
  75.   Serial.println(HCMP3.getVolumeState());
  76.   delay(2000);
  77. }
  78.  
  79. void loop()
  80. {
  81.   /* Play the first file */
  82.   Serial.println("Playing first file on card");
  83.   HCMP3.play();
  84.   delay(2000);
  85.  
  86.   /* Play the next file */
  87.   Serial.println("Playing next file");
  88.   HCMP3.playNext();
  89.   delay(2000);
  90.  
  91.   /* Play the previous file */
  92.   Serial.println("Playing previous file");
  93.   HCMP3.playPrevious();
  94.   delay(2000);
  95.  
  96.   /* Play the first file in folder 1 */
  97.   Serial.println("Playing file 1 in folder 1");
  98.   HCMP3.play(1,1);
  99.   delay(5000);
  100.  
  101.   /* Pause playback */
  102.   Serial.print("Pausing....");
  103.   HCMP3.pause();
  104.   PrintPlaybackState();
  105.   delay(2000);
  106.  
  107.   /* Resume playback */
  108.   Serial.print("Resuming....");
  109.   HCMP3.play();
  110.   PrintPlaybackState();
  111.   delay(2000);
  112.  
  113.   /* Turn the volume down in increments */
  114.   Serial.print("Volume down");
  115.   for(byte i = 0; i < 20; i++)
  116.   {
  117.     Serial.print(".");
  118.     HCMP3.volumeDown();
  119.     delay(100);
  120.   }
  121.   Serial.println("");
  122.  
  123.  
  124.   /* Turn the volume up in increments */
  125.   Serial.print("Volume up");
  126.   for(byte i = 0; i < 20; i++)
  127.   {
  128.     Serial.print(".");
  129.     HCMP3.volumeUp();
  130.     delay(100);
  131.   }
  132.   Serial.println("");
  133.  
  134.   delay(5000);
  135.  
  136.   /* Stop the playback */
  137.   Serial.print("Playback...");
  138.   HCMP3.stop();
  139.   PrintPlaybackState();
  140.  
  141.   while(1);
  142.  
  143. }
  144.  
  145.  
  146. /* Prints out the current playback state */
  147. void PrintPlaybackState()
  148. {
  149.   switch(HCMP3.getPlayState())
  150.   {
  151.     case(STATUS_STOP):
  152.       Serial.println("Stopped");
  153.       break;
  154.  
  155.     case(STATUS_PLAY):
  156.       Serial.println("Playing");
  157.       break;
  158.  
  159.     case(STATUS_PAUSED):
  160.       Serial.println("Paused");
  161.       break;      
  162.   }
  163. }
  164.  
  165.  
  166. /* Prints out the current flash card state */
  167. void PrintCardState()
  168. {
  169.   switch(HCMP3.getCardState())
  170.   {
  171.     case(CARD_EJECTED):
  172.       Serial.println("Ejected");
  173.       break;
  174.  
  175.     case(CARD_INSERTED):
  176.       Serial.println("Inserted");
  177.       break;
  178.  
  179.     case(CARD_UNKOWN):
  180.       Serial.println("Unknown");
  181.       break;      
  182.   }
  183. }



Image

HCMP3.zip


Disclaimer: Libraries, example code, and diagrams within this forum thread are provided as an additional free service by Hobby Components and are not sold as part of any product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

Post Reply