Fix(I2S example): make fix to the ESP32 I2S simple tone example (#10954)
* made some fix to the ESP32 I2S simple tone example * edit the I2S - simple tone example * edit the I2S - simple tone example * some edit * edit comment * edit * edit * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
parent
c21ef70a15
commit
d71135e2ca
1 changed files with 18 additions and 4 deletions
|
|
@ -24,10 +24,17 @@
|
|||
2nd September 2021
|
||||
Lucas Saavedra Vaz (lucasssvaz)
|
||||
22nd December 2023
|
||||
anon
|
||||
10nd February 2025
|
||||
*/
|
||||
|
||||
#include <ESP_I2S.h>
|
||||
|
||||
// The GPIO pins are not fixed, most other pins could be used for the I2S function.
|
||||
#define I2S_LRC 25
|
||||
#define I2S_BCLK 5
|
||||
#define I2S_DIN 26
|
||||
|
||||
const int frequency = 440; // frequency of square wave in Hz
|
||||
const int amplitude = 500; // amplitude of square wave
|
||||
const int sampleRate = 8000; // sample rate in Hz
|
||||
|
|
@ -36,10 +43,10 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT;
|
|||
i2s_mode_t mode = I2S_MODE_STD;
|
||||
i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO;
|
||||
|
||||
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
|
||||
const unsigned int halfWavelength = sampleRate / frequency / 2; // half wavelength of square wave
|
||||
|
||||
int32_t sample = amplitude; // current sample value
|
||||
int count = 0;
|
||||
unsigned int count = 0;
|
||||
|
||||
I2SClass i2s;
|
||||
|
||||
|
|
@ -47,6 +54,8 @@ void setup() {
|
|||
Serial.begin(115200);
|
||||
Serial.println("I2S simple tone");
|
||||
|
||||
i2s.setPins(I2S_BCLK, I2S_LRC, I2S_DIN);
|
||||
|
||||
// start I2S at the sample rate with 16-bits per sample
|
||||
if (!i2s.begin(mode, sampleRate, bps, slot)) {
|
||||
Serial.println("Failed to initialize I2S!");
|
||||
|
|
@ -60,8 +69,13 @@ void loop() {
|
|||
sample = -1 * sample;
|
||||
}
|
||||
|
||||
i2s.write(sample); // Right channel
|
||||
i2s.write(sample); // Left channel
|
||||
// Left channel, the low 8 bits then high 8 bits
|
||||
i2s.write(sample);
|
||||
i2s.write(sample >> 8);
|
||||
|
||||
// Right channel, the low 8 bits then high 8 bits
|
||||
i2s.write(sample);
|
||||
i2s.write(sample >> 8);
|
||||
|
||||
// increment the counter for the next sample
|
||||
count++;
|
||||
|
|
|
|||
Loading…
Reference in a new issue