|
|
@ -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();
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|