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

Music.c

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

#include "Music.h"               

 

/*-------------------------- Module Constants -----------------------------*/

const static char msWait = 100;

 

/*------------------------- Module Definitions ----------------------------*/

 

/*-------------------------- Module Variables -----------------------------*/

static char isPlaying;

 

/*-------------------------- Module Functions -----------------------------*/

static void wait(unsigned int);

static void pressPlay(void);

static void pressNext(void);

 

/*---------------------------- Module Code ------------------------------*/

 

//Initialize

void initMusic(void) {

  pressPlay();

  wait(1500);

  pressPlay();            //The next time PLAY is pressed, music will start automatically

  isPlaying = 0;

}

 

void playMusic(void) {

  if (isPlaying == 0){    //if MP3 player is currently not playing, press NEXT to restart the track  

    isPlaying = 1;

    pressNext();

  }

  else {

    //do nothing if the MP3 player is already playing music

  }

}

 

void stopMusic(void) {

  if(isPlaying) {         //if MP3 is currently playing, press PLAY to PAUSE

    isPlaying = 0;

    pressPlay();

  }

}

 

void restartMusic(void) {

  pressNext();           //press NEXT to restart music

  isPlaying = 1;

}

 

//PLAY is M4 output

static void pressPlay(void) {

  PTM = PTM | BIT4HI;             //set M4 hi

  wait(msWait);

  PTM = PTM & BIT4LO;             //set M4 lo

}

 

//NEXT is M5 output

static void pressNext(void) {

  PTM = PTM | BIT5HI;

  wait(msWait);

  PTM = PTM & BIT5LO;

}

 

//delay for a specified number of ms

static void wait(unsigned int ms) {

  unsigned int myTime = 0;

  myTime = TMRS12_GetTime();

  //wait until the current time minus the start time is greater than the wait time

  while ((TMRS12_GetTime() - myTime) < ms) {};

}