upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
Public Types | Public Member Functions | Protected Member Functions
WT5001 Class Reference

API for the WT5001 Serial MP3 Module. More...

Detailed Description

UPM support for the WT5001 Serial MP3 module. This was tested specifically with the Grove Serial MP3 module.

wt5001.jpg
// Instantiate a WT5001 serial MP3 player on uart 0.
// This example was tested on the Grove Serial MP3 module.
upm::WT5001* mp3 = new upm::WT5001(0);
int cmd = -1;
if (argc > 1)
cmd = atoi(argv[1]);
// make sure port is initialized properly. 9600 baud is the default.
if (!mp3->setupTty(B9600))
{
cerr << "Failed to setup tty port parameters" << endl;
return 1;
}
switch (cmd)
{
case 0:
mp3->stop();
break;
case 1:
mp3->play(upm::WT5001::SD, 1);
break;
case 2:
mp3->pause();
break;
case 3:
mp3->next();
break;
case 4:
mp3->previous();
break;
default:
// nothing, just output usage, and info below
printUsage(argv[0]);
break;
}
// Example: set the date
// mp3->setDate(2015, 1, 1);
// Example: set the time
// mp3->setTime(12, 30, 30);
// print out some information
uint8_t vol = 0;
if (mp3->getVolume(&vol))
cout << "The current volume is: " << int(vol) << endl;
uint8_t ps = 0;
if (mp3->getPlayState(&ps))
cout << "The current play state is: " << int(ps) << endl;
uint16_t numf = 0;
if (mp3->getNumFiles(upm::WT5001::SD, &numf))
cout << "The number of files on the SD card is: " << int(numf) << endl;
uint16_t curf = 0;
if (mp3->getCurrentFile(&curf))
cout << "The current file is: " << int(curf) << endl;
uint16_t year = 0;
uint8_t month = 0, day = 0;
if (mp3->getDate(&year, &month, &day))
cout << "The device date is: " << int(month) << "/" << int(day)
<< "/" << int(year) << endl;
uint8_t hour = 0, minute = 0, second = 0;
if (mp3->getTime(&hour, &minute, &second))
cout << "The device time is: " << int(hour) << ":" << int(minute)
<< ":" << int(second) << endl;

Public Types

enum  WT5001_OPCODE_T {
  NONE = 0x00, PLAY_SD = 0xa0, PLAY_SPI = 0xa1, PLAY_UDISK = 0xa2,
  PAUSE = 0xa3, STOP = 0xa4, NEXT = 0xa5, PREVIOUS = 0xa6,
  SET_VOLUME = 0xa7, QUEUE = 0xa8, PLAY_MODE = 0xa9, COPY_SD2FLASH = 0xaa,
  COPY_UDISK2FLASH = 0xab, INSERT_SONG = 0xac, SET_DATE = 0xb1, SET_TIME = 0xb2,
  SET_ALARM = 0xb3, SET_ALARM_DUR = 0xb4, CLEAR_ALARM = 0xb5, CLEAR_ALARM_DUR = 0xb6,
  READ_VOLUME = 0xc1, READ_PLAY_STATE = 0xc2, READ_SPI_NUMF = 0xc3, READ_SD_NUMF = 0xc4,
  READ_UDISK_NUMF = 0xc5, READ_CUR_FNAME = 0xc6, READ_CF_CHAR = 0xc7, READ_DATE = 0xd1,
  READ_TIME = 0xd2
}
 
enum  WT5001_PLAYMODE_T { NORMAL = 0x00, SINGLE_REPEAT = 0x01, ALL_REPEAT = 0x02, RANDOM = 0x03 }
 
enum  WT5001_PLAYSOURCE_T { SD, SPI, UDISK }
 

Public Member Functions

 WT5001 (int uart)
 
 ~WT5001 ()
 
bool dataAvailable (unsigned int millis)
 
int readData (char *buffer, int len)
 
int writeData (char *buffer, int len)
 
bool setupTty (speed_t baud=B9600)
 
bool checkResponse (WT5001_OPCODE_T opcode)
 
bool play (WT5001_PLAYSOURCE_T psrc, uint16_t index)
 
bool stop ()
 
bool pause ()
 
bool next ()
 
bool previous ()
 
bool setVolume (uint8_t vol)
 
bool queue (uint16_t index)
 
bool setPlayMode (WT5001_PLAYMODE_T pm)
 
bool insert (uint16_t index)
 
bool setDate (uint16_t year, uint8_t month, uint8_t day)
 
bool setTime (uint8_t hour, uint8_t minute, uint8_t second)
 
bool setAlarm (uint8_t hour, uint8_t minute, uint8_t second)
 
bool clearAlarm ()
 
bool getVolume (uint8_t *vol)
 
uint8_t getVolume ()
 
bool getPlayState (uint8_t *ps)
 
uint8_t getPlayState ()
 
bool getNumFiles (WT5001_PLAYSOURCE_T psrc, uint16_t *numf)
 
uint16_t getNumFiles (WT5001_PLAYSOURCE_T psrc)
 
bool getCurrentFile (uint16_t *curf)
 
uint16_t getCurrentFile ()
 
bool getDate (uint16_t *year, uint8_t *month, uint8_t *day)
 
bool getTime (uint8_t *hour, uint8_t *minute, uint8_t *second)
 

Protected Member Functions

int ttyFd ()
 

Constructor & Destructor Documentation

WT5001 ( int  uart)

WT5001 constructor

Parameters
uartDefault UART to use (0 or 1)
~WT5001 ( )

WT5001 destructor

Member Function Documentation

bool dataAvailable ( unsigned int  millis)

Checks to see if there is data available for reading

Parameters
millisNumber of milliseconds to wait; 0 means no waiting
Returns
True if there is data available for reading
int readData ( char *  buffer,
int  len 
)

Reads any available data in a user-supplied buffer. Note: the call blocks until data is available to be read. Use dataAvailable() to determine whether there is data available beforehand, to avoid blocking.

Parameters
bufferBuffer to hold the data read
lenLength of the buffer
Returns
Number of bytes read
int writeData ( char *  buffer,
int  len 
)

Writes the data in the buffer to the device

Parameters
bufferBuffer to hold the data read
lenLength of the buffer
Returns
Number of bytes written
bool setupTty ( speed_t  baud = B9600)

Sets up proper tty I/O modes and the baud rate. The default baud rate is 9,600 (B9600).

Parameters
baudDesired baud rate.
Returns
True if successful
bool checkResponse ( WT5001_OPCODE_T  opcode)

Gets a command response and returns its validity

Parameters
indexOpcode to verify
Returns
True if successful
bool play ( WT5001_PLAYSOURCE_T  psrc,
uint16_t  index 
)

Plays a file from a source

Parameters
psrcPlay source (SD, UDISK, SPI)
indexFile number to play
Returns
True if successful
bool stop ( )

Stops playing

Returns
True if successful
bool pause ( )

Pauses the playback or resumes it if already paused

Returns
True if successful
bool next ( )

Moves to the next track

Returns
True if successful
bool previous ( )

Moves to the previous track

Returns
True if successful
bool setVolume ( uint8_t  vol)

Sets the volume. Valid range is 0-31. 0 means mute.

Returns
True if successful
bool queue ( uint16_t  index)

Queues a track to play next, when the current song is finished

Parameters
indexFile number to queue
Returns
True if successful
bool setPlayMode ( WT5001_PLAYMODE_T  pm)

Sets the playback mode

Parameters
pmPlay mode to enable
Returns
True if successful
bool insert ( uint16_t  index)

Inserts a track to play immediately, interrupting the current track. When the inserted track is finished, the interrupted track resumes where it was interrupted.

Parameters
indexFile number to insert
Returns
True if successful
bool setDate ( uint16_t  year,
uint8_t  month,
uint8_t  day 
)

Sets the date of the internal clock

Parameters
year4-digit year
monthMonth
dayDay
Returns
True if successful
bool setTime ( uint8_t  hour,
uint8_t  minute,
uint8_t  second 
)

Sets the time of the internal clock

Parameters
hourHour
minuteMinute
secondSecond
Returns
True if successful
bool setAlarm ( uint8_t  hour,
uint8_t  minute,
uint8_t  second 
)

Sets the alarm

Parameters
hourHour
minuteMinute
secondSecond
Returns
True if successful
bool clearAlarm ( )

Clears any alarm that has been set

Returns
True if successful
bool getVolume ( uint8_t *  vol)

Gets the current volume

Parameters
volVolume
Returns
True if successful
uint8_t getVolume ( )

Gets the current volume

Returns
Volume
Exceptions
std::runtime_errorif reading from the sensor failed
bool getPlayState ( uint8_t *  ps)

Gets the current play state: 1 = playing, 2 = stopped, 3 = paused

Parameters
psPlay state
Returns
True if successful
uint8_t getPlayState ( )

Gets the current play state: 1 = playing, 2 = stopped, 3 = paused

Returns
Play state
Exceptions
std::runtime_errorif reading from the sensor failed
bool getNumFiles ( WT5001_PLAYSOURCE_T  psrc,
uint16_t *  numf 
)

Gets the number of files present on the source device

Parameters
psrcStorage source
numfNumber of files
Returns
True if successful
uint16_t getNumFiles ( WT5001_PLAYSOURCE_T  psrc)

Gets the number of files present on the source device

Parameters
psrcStorage source
Returns
Number of files
Exceptions
std::runtime_errorif reading from the sensor failed
bool getCurrentFile ( uint16_t *  curf)

Gets the index of the current file

Parameters
curfIndex of the current file
Returns
True if successful
uint16_t getCurrentFile ( )

Gets the index of the current file

Returns
Index of the curretn file
Exceptions
std::runtime_errorif reading from the sensor failed
bool getDate ( uint16_t *  year,
uint8_t *  month,
uint8_t *  day 
)

Gets the device date

Parameters
year4-digit year
monthMonth
dayDay
Returns
True if successful
bool getTime ( uint8_t *  hour,
uint8_t *  minute,
uint8_t *  second 
)

Gets the device time

Parameters
hourHour
minuteMinute
secondSecond
Returns
True if successful

The documentation for this class was generated from the following files: