Add embedded MP3 playback test sketch
This commit is contained in:
@@ -1,82 +1,79 @@
|
||||
#include <Arduino.h>
|
||||
#include "driver/twai.h"
|
||||
#include "MeyCan.h"
|
||||
|
||||
// https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/TWAI/TWAIreceive/TWAIreceive.ino
|
||||
|
||||
#define RX_PIN 2
|
||||
#define TX_PIN 3
|
||||
|
||||
void DebugBlink(int d) {
|
||||
pinMode(20, OUTPUT);
|
||||
while (true) {
|
||||
|
||||
digitalWrite(20, HIGH);
|
||||
delay(d);
|
||||
digitalWrite(20, LOW);
|
||||
delay(d);
|
||||
}
|
||||
}
|
||||
|
||||
bool driver_installed = false;
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
SPI.begin();
|
||||
|
||||
// Explicit GND for LED and Input
|
||||
pinMode(21, OUTPUT);
|
||||
digitalWrite(21, LOW);
|
||||
|
||||
// Initialize configuration structures using macro initializers
|
||||
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
|
||||
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_1MBITS(); //Look in the api-reference for other speed sets.
|
||||
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
|
||||
|
||||
|
||||
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
|
||||
Serial.println("Driver installed");
|
||||
} else {
|
||||
DebugBlink(100);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
esp_err_t e = twai_start();
|
||||
// Start TWAI driver
|
||||
if (e == ESP_OK) {
|
||||
driver_installed = true;
|
||||
} else {
|
||||
DebugBlink(500);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
SetDevicedId(0x05, 0x1F);
|
||||
|
||||
SetMeyPin(1, 5);
|
||||
SetMeyPin(2, 6);
|
||||
SetMeyPin(3, 7);
|
||||
SetMeyPin(4, 8);
|
||||
SetMeyPin(5, 9);
|
||||
SetMeyPin(6, 10);
|
||||
SetMeyPin(7, 20);
|
||||
|
||||
SetupMeyCan(8, 1, 3);
|
||||
}
|
||||
|
||||
twai_message_t frame;
|
||||
void loop() {
|
||||
|
||||
if (!driver_installed) {
|
||||
// Driver not installed
|
||||
DebugBlink(2000);
|
||||
return;
|
||||
}
|
||||
|
||||
CheckMeyPinsTriggered();
|
||||
if (twai_receive(&frame, 0) == ESP_OK) {
|
||||
|
||||
HandleFrame(&frame);
|
||||
}
|
||||
}
|
||||
#include <Arduino.h>
|
||||
#include <driver/i2s.h>
|
||||
#include <MP3DecoderHelix.h>
|
||||
#include "positive_sound_mp3.h"
|
||||
|
||||
using namespace libhelix;
|
||||
|
||||
constexpr i2s_port_t I2S_PORT = I2S_NUM_0;
|
||||
constexpr int I2S_BCLK = 6;
|
||||
constexpr int I2S_LRC = 7;
|
||||
constexpr int I2S_DIN = 5;
|
||||
constexpr float OUTPUT_GAIN = 0.35f;
|
||||
|
||||
MP3DecoderHelix mp3;
|
||||
|
||||
bool i2s_initialized = false;
|
||||
int current_sample_rate = 0;
|
||||
int current_channels = 0;
|
||||
|
||||
void configureI2s(int sample_rate, int channels) {
|
||||
if (!i2s_initialized) {
|
||||
i2s_config_t i2s_config = {};
|
||||
i2s_config.mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX);
|
||||
i2s_config.sample_rate = sample_rate;
|
||||
i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
|
||||
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
|
||||
i2s_config.communication_format = I2S_COMM_FORMAT_STAND_I2S;
|
||||
i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
|
||||
i2s_config.dma_buf_count = 8;
|
||||
i2s_config.dma_buf_len = 256;
|
||||
i2s_config.use_apll = false;
|
||||
i2s_config.tx_desc_auto_clear = true;
|
||||
i2s_config.fixed_mclk = 0;
|
||||
|
||||
i2s_pin_config_t pin_config = {};
|
||||
pin_config.bck_io_num = I2S_BCLK;
|
||||
pin_config.ws_io_num = I2S_LRC;
|
||||
pin_config.data_out_num = I2S_DIN;
|
||||
pin_config.data_in_num = I2S_PIN_NO_CHANGE;
|
||||
|
||||
i2s_driver_install(I2S_PORT, &i2s_config, 0, nullptr);
|
||||
i2s_set_pin(I2S_PORT, &pin_config);
|
||||
i2s_zero_dma_buffer(I2S_PORT);
|
||||
i2s_initialized = true;
|
||||
}
|
||||
|
||||
if (sample_rate != current_sample_rate || channels != current_channels) {
|
||||
i2s_channel_t channel_mode =
|
||||
channels == 1 ? I2S_CHANNEL_MONO : I2S_CHANNEL_STEREO;
|
||||
i2s_set_clk(I2S_PORT, sample_rate, I2S_BITS_PER_SAMPLE_16BIT, channel_mode);
|
||||
current_sample_rate = sample_rate;
|
||||
current_channels = channels;
|
||||
}
|
||||
}
|
||||
|
||||
void audioDataCallback(MP3FrameInfo &info, int16_t *pcm_buffer, size_t len, void *) {
|
||||
configureI2s(info.samprate, info.nChans);
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
pcm_buffer[i] = static_cast<int16_t>(pcm_buffer[i] * OUTPUT_GAIN);
|
||||
}
|
||||
|
||||
size_t bytes_written = 0;
|
||||
i2s_write(I2S_PORT, pcm_buffer, len * sizeof(int16_t), &bytes_written,
|
||||
portMAX_DELAY);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
mp3.setDataCallback(audioDataCallback);
|
||||
mp3.begin();
|
||||
Serial.println("MP3 playback started");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
mp3.write(positive_sound_short_mp3, positive_sound_short_mp3_len);
|
||||
mp3.begin();
|
||||
delay(20);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user