2#include "../../Defines.h"
9#define READ_LEN (2 * 256)
11uint8_t _BUFFER[READ_LEN] = {0};
13int16_t *_adcBuffer = NULL;
16void _callbackFunction;
19void callCallback(
int volume)
21 if (_callbackFunction)
23 void (*callbackFunction)(int) = _callbackFunction;
24 (*callbackFunction)(volume);
30int _loudThreshhold = 4000;
33void showSignal_AudioModule()
37 for (
int n = 0; n < 160; n++) {
38 y = _adcBuffer[n] * GAIN_FACTOR;
42 if (max > _loudThreshhold)
44 SerialDebug.printf(
"** HIGH = %d\n", max);
47 SerialDebug.printf(
"low = %d\n", max);
52void checkMicSound_AudioModule()
55 i2s_read(I2S_NUM_0, (
char *)_BUFFER, READ_LEN, &bytesread,
56 (100 / portTICK_RATE_MS));
57 _adcBuffer = (int16_t *)_BUFFER;
58 showSignal_AudioModule();
63void i2sInit_AudioModule()
65 i2s_config_t i2s_config = {
66 .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
69 I2S_BITS_PER_SAMPLE_16BIT,
70 .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT,
71#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 1, 0)
72 .communication_format = I2S_COMM_FORMAT_STAND_I2S,
74 .communication_format = I2S_COMM_FORMAT_I2S,
76 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
81 i2s_pin_config_t pin_config;
83#if (ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 3, 0))
84 pin_config.mck_io_num = I2S_PIN_NO_CHANGE;
87 pin_config.bck_io_num = I2S_PIN_NO_CHANGE;
88 pin_config.ws_io_num = PIN_CLK;
89 pin_config.data_out_num = I2S_PIN_NO_CHANGE;
90 pin_config.data_in_num = PIN_DATA;
92 i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
93 i2s_set_pin(I2S_NUM_0, &pin_config);
94 i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
97int _delayStartMillis_AudioModule;
98int _delaySeconds_AudioModule;
100void startTimer_AudioModule()
102 _delayStartMillis_AudioModule = millis();
103 _delaySeconds_AudioModule = 1 * portTICK_RATE_MS;
107boolean timesUp_AudioModule()
109 boolean timesUp =
false;
110 int currentTimeMillis = millis();
112 int currentCounterSeconds = _delaySeconds_AudioModule - (currentTimeMillis - _delayStartMillis_AudioModule)/1000;
114 SerialLots.printf(
"delay = %d, counter = %d, delaySeconds = %d\n", _delayStartMillis_AudioModule, currentCounterSeconds, _delaySeconds_AudioModule);
115 if (currentCounterSeconds <= 0)
117 SerialLots.printf(
"delayFinished_AudioModule\n");
119 startTimer_AudioModule();
131 i2sInit_AudioModule();
134 startTimer_AudioModule();
140 if (timesUp_AudioModule())
142 checkMicSound_AudioModule();
void loop_AudioModule()
called for the loop() of this plugin