Vasanth Kumar / Mbed 2 deprecated RapidMP3_check

Dependencies:   RAPIDMP3 mbed

Fork of RapidMP3_check by Vasanth Kumar

RAPIDMP3.cpp

Committer:
vasanth
Date:
2014-11-21
Revision:
0:a540b792fbf0
Child:
1:dbb63bd6c828

File content as of revision 0:a540b792fbf0:

/****************************************************************************
 * file     RAPIDMP3.cpp
 * author   Vasanth Kumar.D (www.chipthinglabs.com)
 * date     July 2014
 * brief    Rapid MP3 module driver code
 *
 * Project:
 * Rapid MP3 v1.0 driver code
 * The driver code is compatible with any mbed enabled microcontroller platform
 *
 * Description:
 * This is a sample code that shows how to control a Rapid MP3 module
 * using the UART peripheral of MBED micro. The code provides a set of API functions
 * to access and control the module using UART peripheral. 
 *
 * Copyright (C) 2014, Chipthing Labs, all right reserved.
 * The Rapid MP3 code is a free software and there is NO WARRANTY.
 * No restriction on use. You can use, modify and redistribute it for
 * personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
 * Redistributions of source code must retain the above copyright notice.
 ****************************************************************************/

#include "mbed.h"
#include "RAPIDMP3.h"

/**************************************************************************/

RAPIDMP3::RAPIDMP3(PinName Tx, PinName Rx) : serialRMP3(Tx, Rx) {
    serialRMP3.baud(9600);
}
/**************************************************************************/

void RAPIDMP3::transmitEndOfFrame() {
    serialRMP3.puts("\r\n");
}
/**************************************************************************/

void RAPIDMP3::getResponseStr() {
    uint8_t i, temp;
    i = 0;
    do{
        temp = serialRMP3.getc();
        responseStr[i] = temp;
        i++;
    }while (temp != '\n');
    responseStr[i] = '\0';
}
/**************************************************************************/

uint8_t RAPIDMP3::getResponse() {
    uint8_t response = RMP3_ER;
    
    getResponseStr();
    
    if (strcmp((const char *) responseStr, "OK\r\n") == 0) {
        response = RMP3_OK;
    }
    return response;
}

/**************************************************************************/
/*
    @brief          Checks whether the module is responding
                    to serial communication requests

    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER
    
 */
/**************************************************************************/

uint8_t RAPIDMP3::checkDevice() {
    uint8_t response = RMP3_ER;

    serialRMP3.puts("AT\r\n");
    getResponseStr();

    if (strcmp((const char *) responseStr, "OK\r\n") == 0) {
        response = RMP3_OK;
    }
    return response;
}
/**************************************************************************/
/*!
    @brief  Plays a MP3 or WAV file from an SD card
            
    @param[in]  fileName
                Pointer to the filename of stored MP3 or WAV file
                only 8.3 file name format is supported

    @param[out] RMP3_OK or RMP3_ER

    @section Example

    @code

    uint8_t response;
    response = rmp3.playFile("my_file.mp3");
    if (response == RMP3_OK) {
        rmp3.waitEndOfPlay();
    }

    @endcode
 */
/**************************************************************************/

uint8_t RAPIDMP3::playFile(const char *fileName) {
    serialRMP3.puts("AT+PLAY=");
    serialRMP3.putc('"');
    serialRMP3.puts(fileName);
    serialRMP3.putc('"');
    transmitEndOfFrame();
    
    return getResponse();
}

/**************************************************************************/
/*
    @brief          Waits until the current playback
                    gets completely finished
                    
    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER

 */
/**************************************************************************/

uint8_t RAPIDMP3::waitEndOfPlay() {
    uint8_t response = RMP3_ER;
    getResponseStr();

    if (strcmp((const char *) responseStr, "FREE\r\n") == 0) {
        response = RMP3_OK;
    }
    return response;
}

/**************************************************************************/
/*
    @brief          Waits for the device to indicate 
                    the initialization is complete
                    
    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER

 */
/**************************************************************************/

uint8_t RAPIDMP3::waitDeviceFree() {
    return waitEndOfPlay();
}

/**************************************************************************/
/*
    @brief          Stops the current playback

    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER

 */
/**************************************************************************/

uint8_t RAPIDMP3::stop() {
    serialRMP3.puts("AT+STOP\r\n");
    
    uint8_t response;
    response = getResponse();
    if (response == RMP3_OK) {
        return waitEndOfPlay();
    }
    return RMP3_ER;
}
/**************************************************************************/
/*
    @brief          Pause the current playback

    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER

 */
/**************************************************************************/

uint8_t RAPIDMP3::playPause() {
    serialRMP3.puts("AT+PAUSE\r\n");    
    return getResponse();
}
/**************************************************************************/
/*
    @brief          Resume the current playback

    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER

 */
/**************************************************************************/

uint8_t RAPIDMP3::playResume() {
    serialRMP3.puts("AT+RESUME\r\n");
    return getResponse();
}
/**************************************************************************/
/*
    @brief          Sets the volume level

    @param[in]      vol
                    volume level to be set
                    values it can take: 0 to 9
                    where 0 is maximum volume and 9 is mute

    @param[out]     RMP3_OK or RMP3_ER
 */
/**************************************************************************/

uint8_t RAPIDMP3::volumeSet(uint8_t vol) {
    if (vol > 9) {
        return RMP3_ER;
    }
    serialRMP3.puts("AT+VOL=");
    serialRMP3.putc(vol + '0');
    transmitEndOfFrame();
    
    return getResponse();
}
/**************************************************************************/
/*
    @brief          Gets the volume level

    @param[in]      none                    

    @param[out]     current volume level
 */
/**************************************************************************/

uint8_t RAPIDMP3::volumeGet() {
    uint8_t volumeLevel = 0;

    serialRMP3.puts("AT+VOL=?\r\n");
    getResponseStr();

    volumeLevel = responseStr[4];
    volumeLevel -= '0';   // ASCII to decimal

    return volumeLevel;
}
/**************************************************************************/
/*
    @brief          Increases the volume level

    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER
 */
/**************************************************************************/

uint8_t RAPIDMP3::volumeInc() {
    serialRMP3.puts("AT+INC\r\n");    
    return getResponse();
}
/**************************************************************************/
/*
    @brief          Decreases the volume level

    @param[in]      none

    @param[out]     RMP3_OK or RMP3_ER
 */
/**************************************************************************/

uint8_t RAPIDMP3::volumeDec() {
    serialRMP3.puts("AT+DEC\r\n");
    return getResponse();
}
/**************************************************************************/
/*
    @brief          Gets the system state, either BUSY or FREE

    @param[in]      none

    @param[out]     RMP3_FREE or RMP3_BUSY

    Description     When either playback or recording is going on
                    system state is BUSY
                    When the module is not doing any of the two process,
                    system state is FREE
 */
/**************************************************************************/

uint8_t RAPIDMP3::getPlaybackStatus() {
    uint8_t systemState = RMP3_NONE;

    serialRMP3.puts("AT+STATUS=?\r\n");
    getResponseStr();

    if (strcmp((const char *) responseStr, "BUSY\r\n") == 0) {
        systemState = RMP3_BUSY;
    } else if (strcmp((const char *) responseStr, "FREE\r\n") == 0) {
        systemState = RMP3_FREE;
    }
    return systemState;
}
/**************************************************************************/
/*!
    @brief  Records audio as WAV file and saves it in an SD card

    @param[in]  fileName
                Pointer to the filename of the WAV file where the recording should be stored                
                Only 8.3 file name format is supported

    @param[out] RMP3_OK or RMP3_ER

    Note        Calling this function creates a new .wav file in the specified name
                into the microSD memory card

    @section Example

    @code

    uint8_t response;
    response = rmp3.recordFile("rec_1.wav");
  
    if (response == RMP3_OK) {
        delayMs(10000);
        rmp3.stop();
 
        rmp3.playFile("rec_1.wav");
        rmp3.waitEndOfPlay();
    }
 
    @endcode
 */
/**************************************************************************/

uint8_t RAPIDMP3::recordFile(const char *fileName) {
    serialRMP3.puts("AT+RECD=");
    serialRMP3.putc('"');
    serialRMP3.puts(fileName);
    serialRMP3.putc('"');
    transmitEndOfFrame();

    return getResponse();
}

/**************************************************************************/
                                /* END */
/**************************************************************************/