2
player.cpp@5:ce3a3ce60f30, 2017-12-12 (annotated)
- Committer:
- csggreen
- Date:
- Tue Dec 12 14:04:24 2017 +0000
- Revision:
- 5:ce3a3ce60f30
- Parent:
- 4:5b1bd3cca8dc
- Child:
- 6:a26a42923924
gs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cha45689 | 0:58524d569dfd | 1 | #include "player.h" |
cha45689 | 0:58524d569dfd | 2 | #include"SDFileSystem.h" |
cha45689 | 0:58524d569dfd | 3 | |
cha45689 | 0:58524d569dfd | 4 | SDFileSystem sd(D11, D12, D13, D9, "sd"); // the pinout on the mbed Cool |
cha45689 | 0:58524d569dfd | 5 | vs10xx vs1053(D11, D12, D13, D6, D7, D2, D8);//mosi,miso,sclk,xcs,xdcs,dreq,xreset |
csggreen | 4:5b1bd3cca8dc | 6 | Serial aa(USBTX, USBRX); |
csggreen | 4:5b1bd3cca8dc | 7 | Serial pc(D1, D0); |
cha45689 | 0:58524d569dfd | 8 | playerStatetype playerState; |
csggreen | 4:5b1bd3cca8dc | 9 | static unsigned char fileBuf[48000]; |
cha45689 | 0:58524d569dfd | 10 | unsigned char *bufptr; |
csggreen | 2:a59255e80c0d | 11 | char green; |
csggreen | 4:5b1bd3cca8dc | 12 | bool startplaysong; |
csggreen | 4:5b1bd3cca8dc | 13 | bool playsong; |
csggreen | 5:ce3a3ce60f30 | 14 | char datainput; |
cha45689 | 0:58524d569dfd | 15 | |
csggreen | 2:a59255e80c0d | 16 | char list[20][50]; //song list |
csggreen | 2:a59255e80c0d | 17 | |
csggreen | 1:17913cff05a2 | 18 | char data ; |
csggreen | 1:17913cff05a2 | 19 | DigitalIn sw_in(USER_BUTTON); |
cha45689 | 0:58524d569dfd | 20 | |
cha45689 | 0:58524d569dfd | 21 | void Player::begin(void) |
cha45689 | 0:58524d569dfd | 22 | { |
cha45689 | 0:58524d569dfd | 23 | DirHandle *dir; |
cha45689 | 0:58524d569dfd | 24 | struct dirent *ptr; |
cha45689 | 0:58524d569dfd | 25 | FileHandle *fp; |
cha45689 | 0:58524d569dfd | 26 | |
cha45689 | 0:58524d569dfd | 27 | vs1053.reset(); |
cha45689 | 0:58524d569dfd | 28 | dir = opendir("/sd"); |
csggreen | 4:5b1bd3cca8dc | 29 | //printf("\r\n**********playing list**********\r\n"); |
cha45689 | 0:58524d569dfd | 30 | unsigned char i = 0,j=0; |
cha45689 | 0:58524d569dfd | 31 | while(((ptr = dir->readdir()) != NULL)&&(i <20)) |
cha45689 | 0:58524d569dfd | 32 | { |
csggreen | 1:17913cff05a2 | 33 | if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3")) |
cha45689 | 0:58524d569dfd | 34 | { |
cha45689 | 0:58524d569dfd | 35 | fp =sd.open(ptr->d_name, O_RDONLY); |
cha45689 | 0:58524d569dfd | 36 | if(fp != NULL) |
cha45689 | 0:58524d569dfd | 37 | { |
cha45689 | 0:58524d569dfd | 38 | char *byte = ptr->d_name; |
csggreen | 1:17913cff05a2 | 39 | j=0; |
csggreen | 1:17913cff05a2 | 40 | while(*byte){ |
csggreen | 1:17913cff05a2 | 41 | list[i][j++] = *byte++; |
csggreen | 2:a59255e80c0d | 42 | green=i; |
cha45689 | 0:58524d569dfd | 43 | } |
csggreen | 4:5b1bd3cca8dc | 44 | aa.printf("%2d . %s\r\n", i,list[i++]); |
csggreen | 1:17913cff05a2 | 45 | //fp->close(); |
cha45689 | 0:58524d569dfd | 46 | } |
cha45689 | 0:58524d569dfd | 47 | } |
cha45689 | 0:58524d569dfd | 48 | } |
cha45689 | 0:58524d569dfd | 49 | dir->closedir(); |
cha45689 | 0:58524d569dfd | 50 | } |
cha45689 | 0:58524d569dfd | 51 | |
csggreen | 3:94b538ace77a | 52 | |
csggreen | 3:94b538ace77a | 53 | |
csggreen | 4:5b1bd3cca8dc | 54 | |
csggreen | 4:5b1bd3cca8dc | 55 | |
csggreen | 4:5b1bd3cca8dc | 56 | /* select file by num. */ |
csggreen | 4:5b1bd3cca8dc | 57 | void Player::selectfile(char n){ |
csggreen | 5:ce3a3ce60f30 | 58 | |
csggreen | 4:5b1bd3cca8dc | 59 | startplaysong=false; |
csggreen | 4:5b1bd3cca8dc | 60 | playerState =PS_STOP; |
csggreen | 4:5b1bd3cca8dc | 61 | playsong=false; |
csggreen | 4:5b1bd3cca8dc | 62 | green = n; |
csggreen | 4:5b1bd3cca8dc | 63 | } |
csggreen | 4:5b1bd3cca8dc | 64 | |
csggreen | 4:5b1bd3cca8dc | 65 | |
csggreen | 4:5b1bd3cca8dc | 66 | |
csggreen | 4:5b1bd3cca8dc | 67 | /* This function stop an audio file. */ |
csggreen | 1:17913cff05a2 | 68 | void Player::stop(void){ |
csggreen | 4:5b1bd3cca8dc | 69 | startplaysong=true; |
csggreen | 1:17913cff05a2 | 70 | playerState =PS_STOP; |
csggreen | 1:17913cff05a2 | 71 | } |
csggreen | 2:a59255e80c0d | 72 | |
csggreen | 5:ce3a3ce60f30 | 73 | |
csggreen | 5:ce3a3ce60f30 | 74 | void Player::run(void){ |
csggreen | 5:ce3a3ce60f30 | 75 | if(datainput=='0'){ |
csggreen | 5:ce3a3ce60f30 | 76 | selectfile(0); |
csggreen | 5:ce3a3ce60f30 | 77 | playFile(list[green]); |
csggreen | 5:ce3a3ce60f30 | 78 | //stop(); |
csggreen | 5:ce3a3ce60f30 | 79 | } |
csggreen | 4:5b1bd3cca8dc | 80 | if(datainput=='1'){ |
csggreen | 4:5b1bd3cca8dc | 81 | selectfile(1); |
csggreen | 5:ce3a3ce60f30 | 82 | playFile(list[green]); |
csggreen | 5:ce3a3ce60f30 | 83 | //stop(); |
csggreen | 5:ce3a3ce60f30 | 84 | |
csggreen | 4:5b1bd3cca8dc | 85 | } |
csggreen | 4:5b1bd3cca8dc | 86 | if(datainput=='2'){ |
csggreen | 4:5b1bd3cca8dc | 87 | selectfile(2); |
csggreen | 5:ce3a3ce60f30 | 88 | playFile(list[green]); |
csggreen | 5:ce3a3ce60f30 | 89 | //stop(); |
csggreen | 5:ce3a3ce60f30 | 90 | } |
csggreen | 5:ce3a3ce60f30 | 91 | if(datainput=='3'){ |
csggreen | 5:ce3a3ce60f30 | 92 | selectfile(3); |
csggreen | 5:ce3a3ce60f30 | 93 | playFile(list[green]); |
csggreen | 5:ce3a3ce60f30 | 94 | //stop(); |
csggreen | 4:5b1bd3cca8dc | 95 | } |
csggreen | 5:ce3a3ce60f30 | 96 | if(datainput=='4'){ |
csggreen | 5:ce3a3ce60f30 | 97 | selectfile(4); |
csggreen | 5:ce3a3ce60f30 | 98 | playFile(list[green]); |
csggreen | 5:ce3a3ce60f30 | 99 | //stop(); |
csggreen | 5:ce3a3ce60f30 | 100 | } |
csggreen | 5:ce3a3ce60f30 | 101 | if(datainput=='5'){ |
csggreen | 5:ce3a3ce60f30 | 102 | selectfile(5); |
csggreen | 5:ce3a3ce60f30 | 103 | playFile(list[green]); |
csggreen | 5:ce3a3ce60f30 | 104 | //stop(); |
csggreen | 5:ce3a3ce60f30 | 105 | } |
csggreen | 5:ce3a3ce60f30 | 106 | else{ |
csggreen | 5:ce3a3ce60f30 | 107 | stop(); |
csggreen | 5:ce3a3ce60f30 | 108 | } |
csggreen | 4:5b1bd3cca8dc | 109 | } |
csggreen | 1:17913cff05a2 | 110 | |
csggreen | 2:a59255e80c0d | 111 | |
csggreen | 4:5b1bd3cca8dc | 112 | |
csggreen | 4:5b1bd3cca8dc | 113 | void Player::read(void){ |
csggreen | 4:5b1bd3cca8dc | 114 | pc.readable(); |
csggreen | 5:ce3a3ce60f30 | 115 | datainput = pc.getc(); |
csggreen | 4:5b1bd3cca8dc | 116 | |
csggreen | 4:5b1bd3cca8dc | 117 | } |
csggreen | 4:5b1bd3cca8dc | 118 | |
csggreen | 1:17913cff05a2 | 119 | |
csggreen | 4:5b1bd3cca8dc | 120 | |
csggreen | 4:5b1bd3cca8dc | 121 | /* This function plays audio file. */ |
cha45689 | 0:58524d569dfd | 122 | void Player::playFile(char *file) { |
cha45689 | 0:58524d569dfd | 123 | int bytes; // How many bytes in buffer left |
cha45689 | 0:58524d569dfd | 124 | char n; |
cha45689 | 0:58524d569dfd | 125 | |
cha45689 | 0:58524d569dfd | 126 | playerState = PS_PLAY; |
cha45689 | 0:58524d569dfd | 127 | |
cha45689 | 0:58524d569dfd | 128 | vs1053.setFreq(24000000); //hight speed |
csggreen | 1:17913cff05a2 | 129 | |
csggreen | 2:a59255e80c0d | 130 | FileHandle *fp =sd.open(file, O_RDONLY); |
cha45689 | 0:58524d569dfd | 131 | |
cha45689 | 0:58524d569dfd | 132 | if(fp == NULL) { |
cha45689 | 0:58524d569dfd | 133 | printf("Could not open %s\r\n",file); |
cha45689 | 0:58524d569dfd | 134 | |
cha45689 | 0:58524d569dfd | 135 | } |
cha45689 | 0:58524d569dfd | 136 | else |
cha45689 | 0:58524d569dfd | 137 | { |
cha45689 | 0:58524d569dfd | 138 | printf("Playing %s ...\r\n",file); |
cha45689 | 0:58524d569dfd | 139 | |
cha45689 | 0:58524d569dfd | 140 | /* Main playback loop */ |
csggreen | 5:ce3a3ce60f30 | 141 | while((bytes = fp->read(fileBuf,48000)) > 0) { |
csggreen | 5:ce3a3ce60f30 | 142 | { |
csggreen | 5:ce3a3ce60f30 | 143 | pc.printf("green\n"); |
csggreen | 5:ce3a3ce60f30 | 144 | if(pc.readable()){ |
csggreen | 5:ce3a3ce60f30 | 145 | break; |
csggreen | 5:ce3a3ce60f30 | 146 | //stop(); |
csggreen | 5:ce3a3ce60f30 | 147 | } |
csggreen | 5:ce3a3ce60f30 | 148 | vs1053.setFreq(24000000); |
cha45689 | 0:58524d569dfd | 149 | bufptr = fileBuf; |
cha45689 | 0:58524d569dfd | 150 | // actual audio data gets sent to VS10xx. |
cha45689 | 0:58524d569dfd | 151 | while(bytes > 0) |
cha45689 | 0:58524d569dfd | 152 | { |
csggreen | 5:ce3a3ce60f30 | 153 | n = (bytes < 32)?bytes:32; //defalt 32 |
cha45689 | 0:58524d569dfd | 154 | vs1053.writeData(bufptr,n); |
cha45689 | 0:58524d569dfd | 155 | bytes -= n; |
cha45689 | 0:58524d569dfd | 156 | bufptr += n; |
csggreen | 5:ce3a3ce60f30 | 157 | if(pc.readable()){ |
csggreen | 5:ce3a3ce60f30 | 158 | break; |
csggreen | 5:ce3a3ce60f30 | 159 | //stop(); |
csggreen | 5:ce3a3ce60f30 | 160 | } |
csggreen | 1:17913cff05a2 | 161 | |
cha45689 | 0:58524d569dfd | 162 | } |
csggreen | 1:17913cff05a2 | 163 | uint8_t vol = 0x00;//set vlume |
csggreen | 1:17913cff05a2 | 164 | vs1053.setVolume(vol); //set vlume |
csggreen | 2:a59255e80c0d | 165 | |
cha45689 | 0:58524d569dfd | 166 | if(playerState != PS_PLAY) //stop |
cha45689 | 0:58524d569dfd | 167 | { |
cha45689 | 0:58524d569dfd | 168 | fp->close(); |
cha45689 | 0:58524d569dfd | 169 | vs1053.softReset(); |
cha45689 | 0:58524d569dfd | 170 | } |
csggreen | 1:17913cff05a2 | 171 | |
csggreen | 1:17913cff05a2 | 172 | } |
cha45689 | 0:58524d569dfd | 173 | } |
cha45689 | 0:58524d569dfd | 174 | } |
cha45689 | 0:58524d569dfd | 175 | |
cha45689 | 0:58524d569dfd | 176 | } |
cha45689 | 0:58524d569dfd | 177 |