Browse Source

attempt to fix double task creation

main
Hendrik Langer 8 years ago
parent
commit
137c30be6e
  1. 9
      src/main.cpp
  2. 33
      src/sdcard.cpp
  3. 14
      src/sound.cpp
  4. 2
      src/sound.h

9
src/main.cpp

@ -45,7 +45,7 @@ void setup()
static const char *bootSound = "/boot.wav";
xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 4096, (void*)bootSound, 5, &xTaskSound);
//xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 3072, (void*)bootSound, 5, &xTaskSound);
}
void loop()
@ -56,9 +56,10 @@ void loop()
touched = true;
Serial.print("touch ");
Serial.println(i, DEC);
if (sound.playing == false) {
Serial.println("new task");
xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 4096, (void*)soundFile[i/3][i%3], 5, &xTaskSound);
if (Sound::playing == false) {
Sound::playing == true; // ToDo: fix double task init
Serial.println("main loop touched==true: new task");
xTaskCreate(&(Sound::buffer_refill_task), "buffer_refill_task", 3072, (void*)soundFile[i/3][i%3], 5, &xTaskSound);
}
}
}

33
src/sdcard.cpp

@ -9,6 +9,7 @@
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "freertos/semphr.h"
#include "hardware.h"
#include "sdcard.h"
@ -20,10 +21,12 @@ using namespace std;
static const char* TAG = "SDCard";
unsigned char *DataSource::buffer = NULL;
SemaphoreHandle_t xSemaphore = NULL;
unsigned int DataSource::buf_pos = 0;
File SDCard::file;
SDCard::SDCard() {
xSemaphore = xSemaphoreCreateMutex();
}
void SDCard::mount() {
@ -62,17 +65,33 @@ void SDCard::umount() {
}
void SDCard::open(const char *path) {
DataSource::buffer = (unsigned char *)malloc(BUF_LENGTH);
Serial.print("Allocated buffer at: "); Serial.println((int)(DataSource::buffer),HEX);
SDCard::file = (SDCard::fs).open(path);
if(!SDCard::file){
Serial.println("Failed to open file for reading");
return;
if( xSemaphore != NULL ) {
if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE ) {
if (DataSource::buffer != NULL) Serial.println("double malloc for buffer?");
DataSource::buffer = (unsigned char *)malloc(BUF_LENGTH);
Serial.print("Allocated buffer at: "); Serial.println((int)(DataSource::buffer),HEX);
SDCard::file = (SDCard::fs).open(path);
if(!SDCard::file){
Serial.println("Failed to open file for reading");
return;
}
} else {
/* xSemaphoreTake() != pdTRUE */
/* could not obtain the semaphore */
Serial.println("sdcard: open(): could not obtain semaphore");
}
}
}
void SDCard::close() {
free(DataSource::buffer);
if (DataSource::buffer != NULL) { // ToDo: really fix this
Serial.print("free(buffer) at: "); Serial.println((int)(DataSource::buffer),HEX);
free(DataSource::buffer);
DataSource::buffer = NULL;
xSemaphoreGive( xSemaphore );
}
SDCard::file.close();
}

14
src/sound.cpp

@ -28,7 +28,7 @@
using namespace std;
bool Sound::playing = false;
volatile bool Sound::playing = false;
Sound::Sound() {
}
@ -82,13 +82,15 @@ void Sound::init() {
}
void Sound::play() {
Serial.println("Sound::play()");
i2s_start(i2s_num);
playing = true;
Sound::playing = true;
}
void Sound::stop() {
Serial.println("Sound::stop()");
i2s_stop(i2s_num);
playing = false;
Sound::playing = false;
}
void Sound::end() {
@ -96,22 +98,25 @@ void Sound::end() {
}
void Sound::buffer_refill_task(void *pvParameter) {
// Serial.print("initial free stack: "); Serial.println(uxTaskGetStackHighWaterMark( NULL ), DEC); // DEBUG
char *path = (char *) pvParameter;
Serial.println("sdcard: opening File");
SDCard::open(path);
Sound::play();
while (SDCard::available()) {
// Serial.print("chunk free stack: "); Serial.println(uxTaskGetStackHighWaterMark( NULL ), DEC); // DEBUG
// get chunk of data
SDCard::read(); // overwrite buffer
DataSource::buf_pos = 0;
while (DataSource::buf_pos < BUF_LENGTH) {
// Serial.print("i2s_push free stack: "); Serial.println(uxTaskGetStackHighWaterMark( NULL ), DEC); // DEBUG
uint16_t *buf = (uint16_t *) DataSource::buffer;
int buf_pos = (DataSource::buf_pos)/4;
// convert data
//unsigned int sample = ((unsigned short) DataSource::buffer[DataSource::buf_pos] << 16 & 0xffff0000) | ((unsigned short) DataSource::buffer[DataSource::buf_pos]);
unsigned int sample = ((unsigned short) buf[buf_pos] << 16 & 0xffff0000) | ((unsigned short) DataSource::buffer[buf_pos]);
unsigned int sample = ((unsigned short) buf[buf_pos] << 16 & 0xffff0000) | ((unsigned short) buf[buf_pos]);
// push samples
int num_pushed_bytes = i2s_push_sample(Sound::i2s_num, (char *)&sample, 0);
if (num_pushed_bytes == 0) {
@ -127,6 +132,7 @@ void Sound::buffer_refill_task(void *pvParameter) {
Serial.println("sdcard: EOF");
SDCard::close();
Sound::stop();
Serial.println("task exiting");
vTaskDelete( NULL );
}

2
src/sound.h

@ -18,7 +18,7 @@ class Sound {
static void stop();
int render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples);
static const i2s_port_t i2s_num = (i2s_port_t)I2S_NUM;
static bool playing;
static volatile bool playing;
static void buffer_refill_task(void *pvParameter);
private:
TaskHandle_t xTaskRefillBuffer;

Loading…
Cancel
Save