Skip to content

Commit

Permalink
Updated API
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed May 13, 2021
1 parent 238f56a commit 3b626bc
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 93 deletions.
37 changes: 23 additions & 14 deletions examples/hello_pdm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
#include "tusb.h"

struct pdm_microphone_config config = {
.gpio_clk = 2,
.gpio_data = 3,
.pio = pio0,
.pio_sm = 0,
.sample_rate = 8000,
.gpio_clk = 2,
.gpio_data = 3,
.sample_buffer_size = 256,
};

int16_t sample_buffer[256];
volatile int samples_read = 0;

void on_pdm_data(int16_t* samples, uint num_samples)
void on_pdm_samples_ready()
{
memcpy(sample_buffer, samples, num_samples * sizeof(samples[0]));

samples_read = num_samples;
samples_read = pdm_microphone_read(sample_buffer, 256);
}

int main( void )
Expand All @@ -38,21 +37,31 @@ int main( void )
tight_loop_contents();
}

// printf("hello PDM microphone\n");

pdm_microphone_init(&config);
pdm_microphone_start(on_pdm_data);
printf("hello PDM microphone\n");

while (1) {
while (samples_read == 0) {
if (pdm_microphone_init(&config) < 0) {
printf("PDM microphone initialization failed!\n");
while (1) {
tight_loop_contents();
}
}

for (int i = 0; i < samples_read; i++) {
printf("%d\n", sample_buffer[i]);
pdm_microphone_set_samples_ready_handler(on_pdm_samples_ready);

if (pdm_microphone_start() < 0) {
printf("PDM microphone start failed!\n");
while (1) {
tight_loop_contents();
}
}

while (1) {
int sample_count = samples_read;
samples_read = 0;

for (int i = 0; i < sample_count; i++) {
printf("%d\n", sample_buffer[i]);
}
}

return 0;
Expand Down
19 changes: 10 additions & 9 deletions examples/usb_microphone/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ void led_blinking_task(void);
void audio_task(void);

struct pdm_microphone_config config = {
.pio = pio0,
.pio_sm = 0,
.sample_rate = 16000,
.gpio_clk = 2,
.gpio_data = 3,
.gpio_clk = 2,
.gpio_data = 3,
.pio = pio0,
.pio_sm = 0,
.sample_rate = 16000,
.sample_buffer_size = CFG_TUD_AUDIO_TX_FIFO_SIZE/2,
};

void on_pdm_data(int16_t* samples, uint num_samples)
void on_pdm_samples_ready()
{
memcpy(test_buffer_audio, samples, num_samples * sizeof(samples[0]));
pdm_microphone_read(test_buffer_audio, CFG_TUD_AUDIO_TX_FIFO_SIZE/2);
}


/*------------- MAIN -------------*/
int main(void)
{
Expand All @@ -97,7 +97,8 @@ int main(void)
sampleFreqRng.subrange[0].bRes = 0;

pdm_microphone_init(&config);
pdm_microphone_start(on_pdm_data);
pdm_microphone_set_samples_ready_handler(on_pdm_samples_ready);
pdm_microphone_start();

while (1)
{
Expand Down
20 changes: 15 additions & 5 deletions src/include/pico/microphone/pdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@

#include "hardware/pio.h"

typedef void (*pdm_data_handler_t)(int16_t*, uint);
typedef void (*pdm_samples_ready_handler_t)(void);

struct pdm_microphone_config {
uint gpio_clk;
uint gpio_data;
PIO pio;
uint pio_sm;
uint sample_rate;
uint gpio_clk;
uint gpio_data;
// TODO: pass in buffer???
uint sample_buffer_size;
};

int pdm_microphone_init(const struct pdm_microphone_config* config);
int pdm_microphone_start(pdm_data_handler_t handler);
void pdm_microphone_deinit();

int pdm_microphone_start();
void pdm_microphone_stop();

void pdm_microphone_set_samples_ready_handler(pdm_samples_ready_handler_t handler);
void pdm_microphone_set_filter_max_volume(uint8_t max_volume);
void pdm_microphone_set_filter_gain(uint8_t gain);
void pdm_microphone_set_filter_volume(uint16_t volume);

int pdm_microphone_read(int16_t* buffer, size_t samples);

#endif
Loading

0 comments on commit 3b626bc

Please sign in to comment.