A program to read out the FTSE100 in real time using a TLV320 CODEC and Ethernet connexion

Dependencies:   EthernetNetIf I2SSlave mbed TLV320

Committer:
d_worrall
Date:
Fri Aug 05 13:05:52 2011 +0000
Revision:
0:e1324d2fc338
version 2.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
d_worrall 0:e1324d2fc338 1 #include "mbed.h"
d_worrall 0:e1324d2fc338 2 #include "EthernetNetIf.h"
d_worrall 0:e1324d2fc338 3 #include "HTTPClient.h"
d_worrall 0:e1324d2fc338 4 #include "SDHCFileSystem.h"
d_worrall 0:e1324d2fc338 5 #include "TLV320.h"
d_worrall 0:e1324d2fc338 6
d_worrall 0:e1324d2fc338 7 EthernetNetIf eth; //Ethernet object
d_worrall 0:e1324d2fc338 8 HTTPClient http; //HTTP object
d_worrall 0:e1324d2fc338 9 TLV320 audio(p9, p10, 52, p5, p6, p7, p8, p29); //TLV320 object
d_worrall 0:e1324d2fc338 10 SDFileSystem sd(p11, p12, p13, p14, "sd"); //SD Card object
d_worrall 0:e1324d2fc338 11 InterruptIn volumeSet(p17); //Pin to set volume
d_worrall 0:e1324d2fc338 12 AnalogIn aIn(p19); //Volume control
d_worrall 0:e1324d2fc338 13 FILE *infp; //File pointer object
d_worrall 0:e1324d2fc338 14 /* Useful buffers and pointers*/
d_worrall 0:e1324d2fc338 15 int loadBufferA[512];
d_worrall 0:e1324d2fc338 16 volatile int readPointer = 0;
d_worrall 0:e1324d2fc338 17 volatile int theta = 0;
d_worrall 0:e1324d2fc338 18 /* Function to control volume */
d_worrall 0:e1324d2fc338 19 void setVolume(void){
d_worrall 0:e1324d2fc338 20 audio.outputVolume(aIn, aIn); //this way we can forget about ADC fluctuations
d_worrall 0:e1324d2fc338 21 }
d_worrall 0:e1324d2fc338 22 /* Function to send data to TLV320 */
d_worrall 0:e1324d2fc338 23 void play(void){
d_worrall 0:e1324d2fc338 24 audio.write(loadBufferA, readPointer, 8);
d_worrall 0:e1324d2fc338 25 /* Some pointers */
d_worrall 0:e1324d2fc338 26 readPointer += 8;
d_worrall 0:e1324d2fc338 27 readPointer &= 0x1ff;
d_worrall 0:e1324d2fc338 28 theta -= 8;
d_worrall 0:e1324d2fc338 29 }
d_worrall 0:e1324d2fc338 30 /* Function to load buffer from SD Card */
d_worrall 0:e1324d2fc338 31 void fillBuffer(FILE *file){
d_worrall 0:e1324d2fc338 32 fseek(file, 44, SEEK_SET); //skip the header file, to where the data starts
d_worrall 0:e1324d2fc338 33 while(!feof(file)){
d_worrall 0:e1324d2fc338 34 static volatile int writePointer = 0;
d_worrall 0:e1324d2fc338 35 if(theta < 512){
d_worrall 0:e1324d2fc338 36 loadBufferA[writePointer] = ((char)(fgetc(file))|((char)fgetc(file)<<8)|((char)fgetc(file)<<16)|((char)fgetc(file)<<24));
d_worrall 0:e1324d2fc338 37 /* More pointer fun */
d_worrall 0:e1324d2fc338 38 ++theta;
d_worrall 0:e1324d2fc338 39 ++writePointer;
d_worrall 0:e1324d2fc338 40 writePointer &= 0x1ff;
d_worrall 0:e1324d2fc338 41 }
d_worrall 0:e1324d2fc338 42 }
d_worrall 0:e1324d2fc338 43 }
d_worrall 0:e1324d2fc338 44 int main() {
d_worrall 0:e1324d2fc338 45 /* Set up audio */
d_worrall 0:e1324d2fc338 46 for(int j = 0; j < 512; ++j){
d_worrall 0:e1324d2fc338 47 loadBufferA[j] = 0;
d_worrall 0:e1324d2fc338 48 }
d_worrall 0:e1324d2fc338 49 volumeSet.rise(&setVolume);
d_worrall 0:e1324d2fc338 50 audio.power(0x07);
d_worrall 0:e1324d2fc338 51 audio.attach(&play);
d_worrall 0:e1324d2fc338 52 /* Setup ethernet connection */
d_worrall 0:e1324d2fc338 53 EthernetErr ethErr = eth.setup();
d_worrall 0:e1324d2fc338 54 if(ethErr) return -1;
d_worrall 0:e1324d2fc338 55 /* Instantiate HTTPStream object */
d_worrall 0:e1324d2fc338 56 HTTPText txt;
d_worrall 0:e1324d2fc338 57 while(1){
d_worrall 0:e1324d2fc338 58 char buf[10];
d_worrall 0:e1324d2fc338 59 HTTPResult r = http.get("http://www.srcf.ucam.org/~dew35/mbed/v1/dispBoBApp.php", &txt); //load page into buffer
d_worrall 0:e1324d2fc338 60 if(r==HTTP_OK){
d_worrall 0:e1324d2fc338 61 string str = txt.gets(); //load web page into string
d_worrall 0:e1324d2fc338 62 str.copy(buf,9,0); //load buffer with chunk of web page
d_worrall 0:e1324d2fc338 63 for(int k = 0; k < 10; ++k){ //transform data such that the number corresponds to the correctly labelled file
d_worrall 0:e1324d2fc338 64 if(buf[k] == 0x2E) buf[k] = 10;
d_worrall 0:e1324d2fc338 65 else buf[k] -= 0x30;
d_worrall 0:e1324d2fc338 66 }
d_worrall 0:e1324d2fc338 67 }
d_worrall 0:e1324d2fc338 68 /* Play intro */
d_worrall 0:e1324d2fc338 69 infp = fopen("/sd/11.wav", "r");
d_worrall 0:e1324d2fc338 70 audio.start(TRANSMIT);
d_worrall 0:e1324d2fc338 71 fillBuffer(infp);
d_worrall 0:e1324d2fc338 72 audio.stop();
d_worrall 0:e1324d2fc338 73 fclose(infp);
d_worrall 0:e1324d2fc338 74 /* Play a file for each number in FTSE100 index */
d_worrall 0:e1324d2fc338 75 for(int j = 0; j < 7; ++j){
d_worrall 0:e1324d2fc338 76 char strBuf[20];
d_worrall 0:e1324d2fc338 77 sprintf(strBuf, "/sd/%d.wav", buf[j]); //create filename based on index
d_worrall 0:e1324d2fc338 78 infp = fopen(strBuf, "r"); //open said file
d_worrall 0:e1324d2fc338 79 audio.start(TRANSMIT); //play said file
d_worrall 0:e1324d2fc338 80 fillBuffer(infp);
d_worrall 0:e1324d2fc338 81 audio.stop();
d_worrall 0:e1324d2fc338 82 fclose(infp);
d_worrall 0:e1324d2fc338 83
d_worrall 0:e1324d2fc338 84 }
d_worrall 0:e1324d2fc338 85 }
d_worrall 0:e1324d2fc338 86 }
d_worrall 0:e1324d2fc338 87