Skip to content

Commit

Permalink
Important change: initial channel volume value from 64 to 255. ( #33 )
Browse files Browse the repository at this point in the history
Change I2S port, I2S0 is used when DAC and ADC are used, I2S1 is used otherwise
  • Loading branch information
lovyan03 committed Aug 27, 2022
1 parent 03c812c commit 47919fc
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ void gfxLoop(LGFX_Device* gfx)
if (!gfx->displayBusy())
{ // draw volume bar
static int px;
uint8_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
uint8_t v = M5.Speaker.getVolume();
int x = v * (gfx->width()) >> 8;
if (px != x)
{
Expand Down Expand Up @@ -634,7 +634,7 @@ void loop(void)
}
if (M5.BtnA.isHolding() || M5.BtnB.isPressed() || M5.BtnC.isPressed())
{
size_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
size_t v = M5.Speaker.getVolume();
int add = (M5.BtnB.isPressed()) ? -1 : 1;
if (M5.BtnA.isHolding())
{
Expand All @@ -643,7 +643,7 @@ void loop(void)
v += add;
if (v <= 255)
{
M5.Speaker.setChannelVolume(m5spk_virtual_channel, v);
M5.Speaker.setVolume(v);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void gfxLoop(LGFX_Device* gfx)
if (!gfx->displayBusy())
{ // draw volume bar
static int px;
uint8_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
uint8_t v = M5.Speaker.getVolume();
int x = v * (gfx->width()) >> 8;
if (px != x)
{
Expand Down Expand Up @@ -490,11 +490,11 @@ void loop(void)
else
if (M5.BtnA.isHolding() || M5.BtnB.isPressed() || M5.BtnC.isPressed())
{
size_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
size_t v = M5.Speaker.getVolume();
if (M5.BtnB.isPressed()) { --v; } else { ++v; }
if (v <= 255 || M5.BtnA.isHolding())
{
M5.Speaker.setChannelVolume(m5spk_virtual_channel, v);
M5.Speaker.setVolume(v);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ void gfxLoop(LGFX_Device* gfx)
if (!gfx->displayBusy())
{ // draw volume bar
static int px;
uint8_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
uint8_t v = M5.Speaker.getVolume();
int x = v * (gfx->width()) >> 8;
if (px != x)
{
Expand Down Expand Up @@ -654,7 +654,7 @@ void loop(void)
}
if (M5.BtnA.isHolding() || M5.BtnB.isPressed() || M5.BtnC.isPressed())
{
size_t v = M5.Speaker.getChannelVolume(m5spk_virtual_channel);
size_t v = M5.Speaker.getVolume();
int add = (M5.BtnB.isPressed()) ? -1 : 1;
if (M5.BtnA.isHolding())
{
Expand All @@ -663,7 +663,7 @@ void loop(void)
v += add;
if (v <= 255)
{
M5.Speaker.setChannelVolume(m5spk_virtual_channel, v);
M5.Speaker.setVolume(v);
}
}
}
16 changes: 8 additions & 8 deletions examples/Basic/Speaker/Speaker.ino
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ void setup(void)
M5.Display.fillScreen(TFT_DARKGRAY);
M5.Display.print("SOUND TEST");

/// The setVolume function can be set the master volume in the range of 0-255.
/// The setVolume function can be set the master volume in the range of 0-255. (default : 64)
M5.Speaker.setVolume(64);

/// The setAllChannelVolume function can be set the all virtual channel volume in the range of 0-255.
M5.Speaker.setAllChannelVolume(64);
/// The setAllChannelVolume function can be set the all virtual channel volume in the range of 0-255. (default : 255)
M5.Speaker.setAllChannelVolume(255);

/// The setChannelVolume function can be set the specified virtual channel volume in the range of 0-255.
M5.Speaker.setChannelVolume(0, 64);
/// The setChannelVolume function can be set the specified virtual channel volume in the range of 0-255. (default : 255)
M5.Speaker.setChannelVolume(0, 255);

/// play 2000Hz tone sound, 100 msec.
M5.Speaker.tone(2000, 100);
Expand Down Expand Up @@ -236,7 +236,7 @@ void setup(void)
delay(500);

M5.Speaker.setVolume(0);
M5.Speaker.tone(220); // tone 220Hz sound output. (Keeps output until it stops.)
M5.Speaker.tone(880); // tone 880Hz sound output. (Keeps output until it stops.)
for (int i = 0; i <= 64; i++)
{
M5.Speaker.setVolume(i); // Volume can be changed during sound output.
Expand Down Expand Up @@ -296,8 +296,8 @@ void loop(void)
{
if (!M5.Display.displayBusy())
{
static uint8_t prev_channelvolume;
static uint8_t prev_mastervolume;
static int32_t prev_channelvolume;
static int32_t prev_mastervolume;
int32_t m_vol = (M5.Speaker.getVolume() * (M5.Display.height() - menu_y)) >> 8;
int32_t c_vol = (M5.Speaker.getChannelVolume(0) * (M5.Display.height() - menu_y)) >> 8;
if (prev_mastervolume != m_vol
Expand Down
25 changes: 21 additions & 4 deletions src/M5Unified.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@ namespace m5
#if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32)
case board_t::board_M5StackCore2:
case board_t::board_M5Tough:
self->Power.Axp192.setGPIO2(enabled);
{
auto cfg = self->Speaker.config();
if (cfg.pin_bck == GPIO_NUM_12)
{
self->Power.Axp192.setGPIO2(false);
if (enabled)
{ // To prevent I2S mis-synchronization, turn off the setting once and set the BCLK pin low.
m5gfx::pinMode(GPIO_NUM_12, m5gfx::pin_mode_t::output);
m5gfx::gpio_lo(GPIO_NUM_12);
self->Power.Axp192.setGPIO2(true);
}
}
}
break;

case board_t::board_M5StickC:
Expand Down Expand Up @@ -314,13 +326,15 @@ namespace m5
auto mic_cfg = Mic.config();

mic_cfg.over_sampling = 2;
mic_cfg.i2s_port = I2S_NUM_1;
switch (_board)
{
#if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32)
case board_t::board_M5Stack:
if (_cfg.internal_mic)
{
mic_cfg.pin_data_in = GPIO_NUM_34; // M5GO bottom MIC
mic_cfg.i2s_port = I2S_NUM_0;
mic_cfg.use_adc = true; // use ADC analog input
mic_cfg.input_offset = 192;
mic_cfg.over_sampling = 4;
Expand Down Expand Up @@ -368,6 +382,7 @@ namespace m5
auto spk_cfg = Speaker.config();
// set default speaker gain.
spk_cfg.magnification = 16;
spk_cfg.i2s_port = I2S_NUM_1;
switch (_board)
{
#if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32)
Expand All @@ -376,6 +391,7 @@ namespace m5
{
m5gfx::gpio_lo(GPIO_NUM_25);
m5gfx::pinMode(GPIO_NUM_25, m5gfx::pin_mode_t::output);
spk_cfg.i2s_port = I2S_NUM_0;
spk_cfg.use_dac = true;
spk_cfg.pin_data_out = GPIO_NUM_25;
spk_cfg.magnification = 8;
Expand All @@ -388,7 +404,7 @@ namespace m5
{
spk_cfg.buzzer = true;
spk_cfg.pin_data_out = GPIO_NUM_2;
spk_cfg.magnification = 32;
spk_cfg.magnification = 48;
}
NON_BREAK;
case board_t::board_M5StickC:
Expand All @@ -400,15 +416,16 @@ namespace m5
m5gfx::gpio_lo(GPIO_NUM_26);
m5gfx::pinMode(GPIO_NUM_26, m5gfx::pin_mode_t::output);
spk_cfg.pin_data_out = GPIO_NUM_26;
spk_cfg.i2s_port = I2S_NUM_0;
spk_cfg.use_dac = true;
spk_cfg.buzzer = false;
spk_cfg.magnification = 32;
}
break;

case board_t::board_M5Tough:
// The gain is set higher than Core2 here because the waterproof case reduces the sound.;
spk_cfg.magnification = 32;
// The magnification is set higher than Core2 here because the waterproof case reduces the sound.;
spk_cfg.magnification = 24;
NON_BREAK;
case board_t::board_M5StackCore2:
if (_cfg.internal_spk)
Expand Down
4 changes: 2 additions & 2 deletions src/utility/MPU6886_Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "MPU6886_Class.hpp"

#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

namespace m5
{
Expand Down
37 changes: 25 additions & 12 deletions src/utility/Mic_Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace m5
{
#if defined ( ESP_PLATFORM )
#if defined (ESP_IDF_VERSION_VAL)
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
#define COMM_FORMAT_I2S (I2S_COMM_FORMAT_STAND_I2S)
Expand All @@ -44,14 +45,11 @@ namespace m5
uint32_t Mic_Class::_calc_rec_rate(void) const
{
int rate = (_cfg.sample_rate * _cfg.over_sampling);
if (_cfg.use_adc) { rate *= 1.004f; }
return rate;
}

esp_err_t Mic_Class::_setup_i2s(void)
{
i2s_driver_uninstall(_cfg.i2s_port);

if (_cfg.pin_data_in < 0) { return ESP_FAIL; }

SAMPLE_RATE_TYPE sample_rate = _calc_rec_rate();
Expand All @@ -61,27 +59,39 @@ namespace m5
・I2S_MODE_ADC_BUILT_INを使用せずに初期化を行う
・最後にI2S0のレジスタを操作してADCモードを有効にする。
*/
if (_cfg.use_adc) { sample_rate >>= 4; }

bool use_pdm = (_cfg.pin_bck < 0);
if (_cfg.use_adc) { sample_rate >>= 4; use_pdm = false;}
ESP_LOGV("Mic","sampling rate:%d", sample_rate);
i2s_config_t i2s_config;
memset(&i2s_config, 0, sizeof(i2s_config_t));
i2s_config.mode = _cfg.use_adc
? (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_RX )
: (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_RX | (0x1 << 6) ); // 0x1<<6 is I2S_MODE_PDM
// : (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM );
i2s_config.mode = use_pdm
// ? (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM );
? (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_RX | (0x1 << 6) ) // 0x1<<6 is I2S_MODE_PDM
: (i2s_mode_t)( I2S_MODE_MASTER | I2S_MODE_RX );
i2s_config.sample_rate = sample_rate;
i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
i2s_config.channel_format = _cfg.stereo ? I2S_CHANNEL_FMT_RIGHT_LEFT : I2S_CHANNEL_FMT_ONLY_RIGHT;
i2s_config.communication_format = (i2s_comm_format_t)( COMM_FORMAT_I2S );
i2s_config.dma_buf_count = _cfg.dma_buf_count;
i2s_config.dma_buf_len = _cfg.dma_buf_len;

i2s_pin_config_t pin_config;
memset(&pin_config, ~0u, sizeof(i2s_pin_config_t)); /// all pin set to I2S_PIN_NO_CHANGE
#if defined (ESP_IDF_VERSION_VAL)
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 1)
pin_config.mck_io_num = _cfg.pin_mck;
#endif
#endif
pin_config.bck_io_num = _cfg.pin_bck;
pin_config.ws_io_num = _cfg.pin_ws;
pin_config.data_in_num = _cfg.pin_data_in;

esp_err_t err = i2s_driver_install(_cfg.i2s_port, &i2s_config, 0, nullptr);
esp_err_t err;
if (ESP_OK != (err = i2s_driver_install(_cfg.i2s_port, &i2s_config, 0, nullptr)))
{
i2s_driver_uninstall(_cfg.i2s_port);
err = i2s_driver_install(_cfg.i2s_port, &i2s_config, 0, nullptr);
}
if (err != ESP_OK) { return err; }

#if !defined (CONFIG_IDF_TARGET) || defined (CONFIG_IDF_TARGET_ESP32)
Expand Down Expand Up @@ -151,6 +161,7 @@ namespace m5
void Mic_Class::mic_task(void* args)
{
auto self = (Mic_Class*)args;
i2s_start(self->_cfg.i2s_port);

int oversampling = self->_cfg.over_sampling;
if ( oversampling < 1) { oversampling = 1; }
Expand Down Expand Up @@ -308,6 +319,7 @@ namespace m5
}

if (_cb_set_enabled) { _cb_set_enabled(_cb_set_enabled_args, false); }
i2s_driver_uninstall(_cfg.i2s_port);
}

bool Mic_Class::_rec_raw(void* recdata, size_t array_len, bool flg_16bit, uint32_t sample_rate)
Expand All @@ -329,4 +341,5 @@ namespace m5
}
return true;
}
}
#endif
}
13 changes: 11 additions & 2 deletions src/utility/Mic_Class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ namespace m5
/// i2s_data_in (for mic)
int pin_data_in = -1;

/// i2s_ws
int pin_ws = -1;
/// i2s_bclk
int pin_bck = I2S_PIN_NO_CHANGE;

/// i2s_mclk
int pin_mck = I2S_PIN_NO_CHANGE;

/// i2s_ws (lrck)
int pin_ws = I2S_PIN_NO_CHANGE;

/// input sampling rate (Hz)
uint32_t sample_rate = 16000;

/// use stereo output
bool stereo = false;

/// offset correction value of ADC input value
int input_offset = 0;

Expand Down
2 changes: 2 additions & 0 deletions src/utility/SH200Q_Class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include "SH200Q_Class.hpp"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

namespace m5
{
Expand Down
Loading

0 comments on commit 47919fc

Please sign in to comment.