Browse Source

freertos tasks

main
Hendrik Langer 8 years ago
parent
commit
49d93a8fca
  1. 70
      src/main.cpp
  2. 1
      src/ringbuf.cpp
  3. 1
      src/ringbuf.h
  4. 2
      src/sound.cpp
  5. 1
      src/sound.h

70
src/main.cpp

@ -24,8 +24,42 @@ SDCard sdcard;
Sound sound;
Ringbuf ringbuf = Ringbuf((size_t) BUF_LENGTH);
void sd_read_task(void *pvParameter) {
sdcard.open("/T1.wav");
TaskHandle_t xTaskRead = NULL;
TaskHandle_t xTaskWrite = NULL;
bool playing = false;
static const char *soundFile[3][3] = {{"/T0.wav", "/T1.wav", "/T2.wav"},
{"/T3.wav", "/T4.wav", "/T5.wav"},
{"/T6.wav", "/T7.wav", "/T8.wav"}};
void i2s_write_task(void *pvParameter) {
sound.play();
for ( ;; ) {
//TickType_t delay = 10 / portTICK_PERIOD_MS; // max delay: 10ms instead of portMAX_DELAY
uint16_t *start_ptr = ringbuf.getRead();
size_t size_avail = ringbuf.getReadAvail();
int num_samples = 0;
for (int i=0; i<size_avail; i++) {
unsigned int sample = ((unsigned short) start_ptr[i] << 16 & 0xffff0000) | ((unsigned short) start_ptr[i]);
int num_pushed_bytes = i2s_push_sample(sound.i2s_num, (char *)&sample, 0);
num_samples += num_pushed_bytes/4;
}
ringbuf.setRead(num_samples);
// Serial.print(num_samples,DEC);
// Serial.println(" pushed samples to i2s");
if (!ringbuf.active && ringbuf.getReadAvail() == 0) break; // End
vTaskDelay(20 / portTICK_PERIOD_MS);
}
sound.stop();
vTaskDelete( NULL );
}
void sd_read_task(void *pvParameters) {
char *path = (char *) pvParameters;
Serial.println("sdcard: opening File");
sdcard.open(path);
ringbuf.active = true;
xTaskCreate(&i2s_write_task, "write_task", 1024, NULL, 5, &xTaskWrite);
for ( ;; ) {
uint16_t *start_ptr = ringbuf.getWrite();
size_t size_avail = ringbuf.getWriteAvail();
@ -40,29 +74,13 @@ void sd_read_task(void *pvParameter) {
if (!sdcard.available()) break; // EOF
vTaskDelay(20 / portTICK_PERIOD_MS); // 20ms
}
ringbuf.active = false;
Serial.println("sdcard: EOF");
sdcard.close();
vTaskDelete( NULL );
}
void i2s_write_task(void *pvParameter) {
for ( ;; ) {
//TickType_t delay = 10 / portTICK_PERIOD_MS; // max delay: 10ms instead of portMAX_DELAY
uint16_t *start_ptr = ringbuf.getRead();
size_t size_avail = ringbuf.getReadAvail();
int num_samples = 0;
for (int i=0; i<size_avail; i++) {
unsigned int sample = ((unsigned short) start_ptr[i] << 16 & 0xffff0000) | ((unsigned short) start_ptr[i]);
int num_pushed_bytes = i2s_push_sample(sound.i2s_num, (char *)&sample, 0);
num_samples += num_pushed_bytes/4;
}
ringbuf.setRead(num_samples);
Serial.print(num_samples,DEC);
Serial.println(" pushed samples to i2s");
vTaskDelay(20 / portTICK_PERIOD_MS);
}
vTaskDelete( NULL );
}
void setup()
{
@ -79,10 +97,9 @@ void setup()
sdcard.mount();
sound.init();
xTaskCreate(&sd_read_task, "read_task", 2048, NULL, 5, NULL);
xTaskCreate(&i2s_write_task, "write_task", 1024,NULL,5,NULL );
static const char *bootSound = "/T1.wav";
delay(1000);
xTaskCreate(&sd_read_task, "read_task", 2048, (void*)bootSound, 5, &xTaskRead);
}
void loop()
@ -91,17 +108,20 @@ void loop()
for(int i=0; i<=9; i++) {
if (keyboard.getTouchDetected(i) == true) {
touched = true;
Serial.println("touch");
if (sound.playing == false) {
Serial.println("new task");
xTaskCreate(&sd_read_task, "read_task", 2048, (void*)soundFile[i/3][i%3], 5, &xTaskRead);
}
}
}
if (touched == true) {
// turn the LED on (HIGH is the voltage level)
digitalWrite(LED_PIN, HIGH);
sound.play();
} else {
// turn the LED off by making the voltage LOW
digitalWrite(LED_PIN, LOW);
// sound.stop();
}

1
src/ringbuf.cpp

@ -12,6 +12,7 @@ Ringbuf::Ringbuf(size_t buf_length) {
this->read_pos = 0;
this->write_pos = 0;
this->full = false;
this->active = false;
}
Ringbuf::~Ringbuf() {

1
src/ringbuf.h

@ -17,6 +17,7 @@ class Ringbuf {
void setWrite(size_t bytes);
void setRead(size_t bytes);
bool full;
bool active = false;
private:
};

2
src/sound.cpp

@ -76,10 +76,12 @@ void Sound::init() {
void Sound::play() {
i2s_start(i2s_num);
playing = true;
}
void Sound::stop() {
i2s_stop(i2s_num);
playing = false;
}
void Sound::end() {

1
src/sound.h

@ -27,6 +27,7 @@ class Sound {
void stop();
int render_sample_block(uint16_t *sample_buf_left, uint16_t *sample_buf_right, int num_samples);
const i2s_port_t i2s_num = (i2s_port_t)I2S_NUM;
bool playing;
private:
i2sbuffer_t buffer;

Loading…
Cancel
Save