Skip to content

Commit

Permalink
Merge pull request #150 from FHeilmann/pr_pdm_mic
Browse files Browse the repository at this point in the history
Add PDM microphones and clean up old comments
  • Loading branch information
atuline authored Dec 16, 2021
2 parents e62e879 + 2af8429 commit 648c5b2
Show file tree
Hide file tree
Showing 7 changed files with 871 additions and 855 deletions.
2 changes: 0 additions & 2 deletions wled00/audio_reactive.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ double fftAdd( int from, int to) {
// FFT main code
void FFTcode( void * parameter) {
DEBUG_PRINT("FFT running on core: "); DEBUG_PRINTLN(xPortGetCoreID());
//double beatSample = 0; // COMMENTED OUT - UNUSED VARIABLE COMPILER WARNINGS
//double envelope = 0; // COMMENTED OUT - UNUSED VARIABLE COMPILER WARNINGS

for(;;) {
delay(1); // DO NOT DELETE THIS LINE! It is needed to give the IDLE(0) task enough time and to keep the watchdog happy.
Expand Down
53 changes: 48 additions & 5 deletions wled00/audio_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,16 @@ class I2SSource : public AudioSource {

virtual void initialize() {

if (!pinManager.allocatePin(i2sckPin, true, PinOwner::DigitalMic) ||
!pinManager.allocatePin(i2swsPin, true, PinOwner::DigitalMic) ||
if (!pinManager.allocatePin(i2swsPin, true, PinOwner::DigitalMic) ||
!pinManager.allocatePin(i2ssdPin, true, PinOwner::DigitalMic)) {
return;
}
}

// i2ssckPin needs special treatment, since it might be unused on PDM mics
if (i2sckPin != -1) {
if (!pinManager.allocatePin(i2sckPin, true, PinOwner::DigitalMic))
return;
}

esp_err_t err = i2s_driver_install(I2S_NUM_0, &_config, 0, nullptr);
if (err != ESP_OK) {
Expand All @@ -138,6 +143,12 @@ class I2SSource : public AudioSource {
Serial.printf("Failed to uninstall i2s driver: %d\n", err);
return;
}
pinManager.deallocatePin(i2swsPin, PinOwner::DigitalMic);
pinManager.deallocatePin(i2ssdPin, PinOwner::DigitalMic);
// i2ssckPin needs special treatment, since it might be unused on PDM mics
if (i2sckPin != -1) {
pinManager.deallocatePin(i2sckPin, PinOwner::DigitalMic);
}
}

virtual void getSamples(double *buffer, uint16_t num_samples) {
Expand Down Expand Up @@ -357,10 +368,19 @@ class I2SAdcSource : public I2SSource {

void deinitialize() {
pinManager.deallocatePin(audioPin, PinOwner::AnalogMic);
I2SSource::deinitialize();
_initialized = false;
esp_err_t err = i2s_driver_uninstall(I2S_NUM_0);
if (err != ESP_OK) {
Serial.printf("Failed to uninstall i2s driver: %d\n", err);
return;
}
}
};

/* SPH0645 Microphone
This is an I2S microphone with some timing quirks that need
special consideration.
*/
class SPH0654 : public I2SSource {

public:
Expand All @@ -372,4 +392,27 @@ class SPH0654 : public I2SSource {
REG_SET_BIT(I2S_TIMING_REG(I2S_NUM_0), BIT(9));
REG_SET_BIT(I2S_CONF_REG(I2S_NUM_0), I2S_RX_MSB_SHIFT);
}
};
};

/* I2S PDM Microphone
This is an I2S PDM microphone, these microphones only use a clock and
data line, to make it simpler to debug, use the WS pin as CLK and SD
pin as DATA
*/

class I2SPdmSource : public I2SSource {

public:
I2SPdmSource(int sampleRate, int blockSize, uint16_t lshift, uint32_t mask) :
I2SSource(sampleRate, blockSize, lshift, mask) {

_config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM); // Change mode to pdm

_pinConfig = {
.bck_io_num = I2S_PIN_NO_CHANGE, // bck is unused in PDM mics
.ws_io_num = i2swsPin, // clk pin for PDM mic
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = i2ssdPin
};
}
};
3 changes: 2 additions & 1 deletion wled00/data/settings_sound.htm
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ <h2>Sound Input Pin Manager</h2>
<option value="1">Generic I2S</option>
<option value="2">ES7243</option>
<option value="3">SPH0654</option>
<option value="4">Generic I2S with Mclk</option>
<option value="5">Generic I2S PDM</option>
</select><br>
<!--<input type="checkbox" name="DMM" onchange="enDM()" id="dme"><br> -->
<div id="dm">
I2S SD pin: <input type="number" min="-1" max="40" name="DI"><br>
I2S WS pin: <input type="number" min="-1" max="40" name="LR"><br>
Expand Down
14 changes: 8 additions & 6 deletions wled00/html_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ type="checkbox" name="AGC"><br><h2>Sound Input Pin Manager</h2>Microphone type:
<select id="dme" name="DMM" onchange="enDM()"><option value="0"
selected="selected">Generic Analog</option><option value="1">Generic I2S
</option><option value="2">ES7243</option><option value="3">SPH0654</option>
</select><br><div id="dm">I2S SD pin: <input type="number" min="-1" max="40"
name="DI"><br>I2S WS pin: <input type="number" min="-1" max="40" name="LR"><br>
I2S SCK pin: <input type="number" min="-1" max="40" name="CK"><br></div><div
id="am">Analog Input pin: <input type="number" min="-1" max="40" name="SI"><br>
</div><button type="button" onclick="B()">Back</button><button type="submit">
Save</button></form></body></html>)=====";
<option value="4">Generic I2S with Mclk</option><option value="5">
Generic I2S PDM</option></select><br><div id="dm">I2S SD pin: <input
type="number" min="-1" max="40" name="DI"><br>I2S WS pin: <input type="number"
min="-1" max="40" name="LR"><br>I2S SCK pin: <input type="number" min="-1"
max="40" name="CK"><br></div><div id="am">Analog Input pin: <input
type="number" min="-1" max="40" name="SI"><br></div><button type="button"
onclick="B()">Back</button><button type="submit">Save</button></form></body>
</html>)=====";


#ifdef WLED_ENABLE_DMX
Expand Down
Loading

0 comments on commit 648c5b2

Please sign in to comment.