Reading txt file while playing music under RTOS

Introduction

This is a small project trying to utilize RTOS to enable reading .txt files line by line while playing music. All .txt and .wav files are stored in the micro sd card.

main.cpp

#include "mbed.h"
#include "rtos.h"
#include "uLCD_4DGL.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#include <fstream>
#include <string>
#include <iostream>

Serial pc(USBTX, USBRX);
Mutex stdio_mutex;
SDFileSystem sd(p5, p6, p7, p8, "sd"); 
uLCD_4DGL uLCD(p9, p10, p11);
AnalogOut DACout(p18);
wave_player waver(&DACout);
std::string line ;
std::ifstream infile;
FILE *wave_file;
bool txt_finished = false;
bool wave_finished = false;

void txt_show(void const *argument) {
    if ( infile ) {
        while ( getline( infile , line ) ) {
            stdio_mutex.lock();
            uLCD.cls();
            uLCD.printf("%s \n", line);   //print out one line from txt file to LCD
            stdio_mutex.unlock();
            wait(2);
        }
    }
    stdio_mutex.lock();
    infile.close( ) ;     
    uLCD.printf("End of the file!\n");    
    stdio_mutex.unlock();
    txt_finished = true;
}

void wave_file_player(void const *argument) {
    waver.play(wave_file);
    fclose(wave_file);   
    wave_finished = true;
}    

int main()  {
    uLCD.baudrate(3000000);
    stdio_mutex.lock();
    infile.open( "/sd/sample.txt", std::ifstream::in) ;     //open files should be mutex protected since it takes some time
    wave_file=fopen("/sd/sample.wav","r");
    stdio_mutex.unlock();
    
    Thread t2(txt_show);   
    Thread t3(wave_file_player);

    while (!wave_finished || !txt_finished){}   //wait untill both of the thread finished
}

Pin connections

mbeduLCDSD cardSpeaker
VOUTVCC+
GNDGNDGND
VU+5V
P5DI
P6DO
P7SCK
P8CS
P9RX
P10TX
P11Reset
P18-

uLCD and Speaker connections

/media/uploads/yzhang612/ulcdpins.jpg /media/uploads/yzhang612/_scaled_speakerdriverschem.png


1 comment on Reading txt file while playing music under RTOS:

21 Oct 2014

Pin connections

Please log in to post comments.