Skip to content

Commit

Permalink
Added support for INMP441
Browse files Browse the repository at this point in the history
  • Loading branch information
RoSchmi committed Nov 7, 2021
1 parent d8b2b0d commit b168f00
Show file tree
Hide file tree
Showing 6 changed files with 530 additions and 101 deletions.
6 changes: 2 additions & 4 deletions lib/RoSchmi/SensorData/AnalogSensorMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ void AnalogSensorMgr::SetReadTimeAndValues(int pSensorIndex, DateTime now, float


AnalogSensor AnalogSensorMgr::GetSensorDates(int pSensorIndex)
{

return readValues[pSensorIndex];

{
return readValues[pSensorIndex];
}

137 changes: 66 additions & 71 deletions lib/RoSchmi/SensorData/SoundSwitcher.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include "SoundSwitcher.h"



//DateTime lastSwitchTime = DateTime();
//TimeSpan switchInterval = TimeSpan(60);

// SoundSwitcher
// How it works: See file SoundSwitcher.h

#include "SoundSwitcher.h"

static const i2s_port_t i2s_num = I2S_NUM_0; // i2s port number
static const i2s_config_t i2s_config = {

static const i2s_config_t i2s_config_SPH0645LM4H = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 22050,
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
Expand All @@ -19,6 +16,20 @@ static const i2s_config_t i2s_config = {
.dma_buf_len = 64,
.use_apll = false
};
// https://esp32.com/viewtopic.php?t=15185
i2s_config_t i2s_config_INMP441 = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 11025, // or 44100 if you like
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // Ground the L/R pin on the INMP441.
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 8, //changed from .dma_buf_count = 4,
.dma_buf_len = 64, // changed from .dma_buf_len = ESP_NOW_MAX_DATA_LEN * 4,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0,
};

// For Adafruit Huzzah Esp32
/*
Expand All @@ -40,52 +51,51 @@ static const i2s_pin_config_t pin_config = {
};
*/

SoundSwitcher::SoundSwitcher(i2s_pin_config_t config)
SoundSwitcher::SoundSwitcher(i2s_pin_config_t config, MicType micType)
{
pin_config = config;
}

void SoundSwitcher::begin(uint16_t switchThreshold, Hysteresis pHysteresis, uint32_t updateIntervalMs, uint32_t delayTimeMs)
{
i2s_driver_install(i2s_num, &i2s_config, 0, NULL); //install and start i2s driver
REG_SET_BIT( I2S_TIMING_REG(i2s_num),BIT(9)); /* #include "soc/i2s_reg.h" I2S_NUM -> 0 or 1*/
REG_SET_BIT( I2S_CONF_REG(i2s_num), I2S_RX_MSB_SHIFT);
i2s_set_pin(i2s_num, &pin_config);

lastFeedTimeMillis = millis();
lastBorderNarrowTimeMillis = millis();
feedIntervalMs = updateIntervalMs;
threshold = (float)switchThreshold;
hysteresis = pHysteresis;
readDelayTimeMs = delayTimeMs;
/*
if (pHysteresis == Hysteresis::Percent_20)
pin_config = config;
if (micType == MicType::SPH0645LM4H)
{
hysteresis = 20;
// https://www.esp32.com/viewtopic.php?t=4997
//install and start i2s driver
if (ESP_OK != i2s_driver_install(i2s_num, &i2s_config_SPH0645LM4H, 0, NULL))
{
Serial.println("SPH0645LM4H_i2s_driver_install: error");
}
REG_SET_BIT( I2S_TIMING_REG(i2s_num),BIT(9)); /* #include "soc/i2s_reg.h" I2S_NUM -> 0 or 1*/
REG_SET_BIT( I2S_CONF_REG(i2s_num), I2S_RX_MSB_SHIFT);
}
else
{
if (pHysteresis == Hysteresis::Percent_10)
if (micType == MicType::INMP441)
{
hysteresis = 10;
//https://esp32.com/viewtopic.php?t=15185
//install and start i2s driver
if (ESP_OK != i2s_driver_install(i2s_num, &i2s_config_SPH0645LM4H, 0, NULL))
{
Serial.println("INMP441_i2s_driver_install: error");
}
}
else
{
if (pHysteresis == Hysteresis::Percent_5)
{
hysteresis = 5;
}
else
{
if (pHysteresis == Hysteresis::Percent_2)
{
hysteresis = 2;
}
}
Serial.println("Not allowed microphone");
}
}
*/

if (ESP_OK != i2s_set_pin(I2S_NUM_0, &pin_config))
{
Serial.println("i2s_set_pin: error");
}

}

void SoundSwitcher::begin(uint16_t switchThreshold, Hysteresis pHysteresis, uint32_t updateIntervalMs, uint32_t delayTimeMs)
{
lastFeedTimeMillis = millis();
lastBorderNarrowTimeMillis = millis();
feedIntervalMs = updateIntervalMs;
threshold = (float)switchThreshold;
hysteresis = pHysteresis;
readDelayTimeMs = delayTimeMs;
}

bool SoundSwitcher::hasToggled()
Expand Down Expand Up @@ -183,14 +193,13 @@ FeedResponse SoundSwitcher::feed()
feedResponse.avValue = average;
feedResponse.lowAvValue = lowAvBorder;
feedResponse.highAvValue = highAvBorder;

}
}
}
hasSwitched = false;
return feedResponse;

}

AverageValue SoundSwitcher::getAverage()
{
AverageValue averageValue;
Expand All @@ -199,34 +208,17 @@ AverageValue SoundSwitcher::getAverage()
return averageValue;
}

/*
bool SoundSwitcher::hasToggled(DateTime actUtcTime)
{
if (isActive)
{
if (actUtcTime.operator>=(lastSwitchTime.operator+(switchInterval)))
{
lastSwitchTime = actUtcTime;
state = !state;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
*/

bool SoundSwitcher::GetState()
{
return state;
}

void SoundSwitcher::SetCalibrationParams(float pCalibOffset, float pCalibFactor)
{
calibOffset = pCalibOffset;
calibFactor = pCalibFactor;
}

void SoundSwitcher::SetInactive()
{
isActive = false;
Expand Down Expand Up @@ -277,7 +269,10 @@ float SoundSwitcher::getSoundFromMicro()
minsample = _min(minsample, cleanBuf[i]);
maxsample = _max(maxsample, cleanBuf[i]);
}
Serial.print("Volume: ");
Serial.println(maxsample - minsample);
return maxsample - minsample;

float retValue = (maxsample - minsample) * calibFactor + calibOffset;
retValue = retValue <= 0.0 ? 0.0 : retValue;
//Serial.print("Volume: ");
//Serial.println(retValue);
return retValue;
}
Loading

0 comments on commit b168f00

Please sign in to comment.