parent
efd5392f6c
commit
4fc5f931ac
@ -1,5 +1,15 @@ |
||||
idf_component_register( |
||||
SRCS "gay-ipod-fw.cpp" "dac.cpp" "gpio-expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" |
||||
INCLUDE_DIRS "." |
||||
REQUIRES "esp_adc_cal" "fatfs" "audio_pipeline" "audio_stream" "result" |
||||
) |
||||
idf_component_register(SRCS |
||||
"gay-ipod-fw.cpp" |
||||
"dac.cpp" |
||||
"gpio-expander.cpp" |
||||
"battery.cpp" |
||||
"storage.cpp" |
||||
"i2c.cpp" |
||||
"playback.cpp" INCLUDE_DIRS "." REQUIRES |
||||
"esp_adc_cal" |
||||
"fatfs" |
||||
"audio_pipeline" |
||||
"audio_stream" |
||||
"result") |
||||
target_compile_options(${COMPONENT_LIB} PRIVATE - |
||||
DRESULT_DISABLE_EXCEPTIONS) |
||||
|
@ -1,72 +1,73 @@ |
||||
#pragma once |
||||
|
||||
#include "esp_err.h" |
||||
#include "gpio-expander.h" |
||||
#include <stdint.h> |
||||
|
||||
#include <functional> |
||||
|
||||
namespace gay_ipod { |
||||
#include "esp_err.h" |
||||
#include "gpio-expander.h" |
||||
#include "result.hpp" |
||||
|
||||
static const uint8_t kPCM5122Address = 0x4C; |
||||
static const uint8_t kPCM5122Timeout = 100 / portTICK_RATE_MS; |
||||
namespace gay_ipod { |
||||
|
||||
/**
|
||||
* Interface for a PCM5122PWR DAC, configured over I2C. |
||||
*/ |
||||
class AudioDac { |
||||
public: |
||||
AudioDac(GpioExpander *gpio); |
||||
~AudioDac(); |
||||
public: |
||||
enum Error { |
||||
FAILED_TO_BOOT, |
||||
FAILED_TO_CONFIGURE, |
||||
}; |
||||
static auto create(GpioExpander* expander) |
||||
-> cpp::result<std::unique_ptr<AudioDac>, Error>; |
||||
|
||||
/**
|
||||
* Performs initial configuration of the DAC and sets it to begin expecting |
||||
* I2S audio data. |
||||
*/ |
||||
esp_err_t Start(); |
||||
AudioDac(GpioExpander* gpio); |
||||
~AudioDac(); |
||||
|
||||
/**
|
||||
* Sets the volume on a scale from 0 (loudest) to 254 (quietest). A value of |
||||
* 255 engages the soft mute function. |
||||
*/ |
||||
void WriteVolume(uint8_t volume); |
||||
/**
|
||||
* Sets the volume on a scale from 0 (loudest) to 254 (quietest). A value of |
||||
* 255 engages the soft mute function. |
||||
*/ |
||||
void WriteVolume(uint8_t volume); |
||||
|
||||
enum PowerState { |
||||
POWERDOWN = 0b0, |
||||
WAIT_FOR_CP = 0b1, |
||||
CALIBRATION_1 = 0b10, |
||||
CALIBRATION_2 = 0b11, |
||||
RAMP_UP = 0b100, |
||||
RUN = 0b101, |
||||
SHORT = 0b110, |
||||
RAMP_DOWN = 0b111, |
||||
STANDBY = 0b1000, |
||||
}; |
||||
enum PowerState { |
||||
POWERDOWN = 0b0, |
||||
WAIT_FOR_CP = 0b1, |
||||
CALIBRATION_1 = 0b10, |
||||
CALIBRATION_2 = 0b11, |
||||
RAMP_UP = 0b100, |
||||
RUN = 0b101, |
||||
SHORT = 0b110, |
||||
RAMP_DOWN = 0b111, |
||||
STANDBY = 0b1000, |
||||
}; |
||||
|
||||
/* Returns the current boot-up status and internal state of the DAC */ |
||||
std::pair<bool,PowerState> ReadPowerState(); |
||||
/* Returns the current boot-up status and internal state of the DAC */ |
||||
std::pair<bool, PowerState> ReadPowerState(); |
||||
|
||||
// Not copyable or movable.
|
||||
AudioDac(const AudioDac&) = delete; |
||||
AudioDac& operator=(const AudioDac&) = delete; |
||||
// Not copyable or movable.
|
||||
AudioDac(const AudioDac&) = delete; |
||||
AudioDac& operator=(const AudioDac&) = delete; |
||||
|
||||
private: |
||||
GpioExpander *gpio_; |
||||
private: |
||||
GpioExpander* gpio_; |
||||
|
||||
/*
|
||||
* Pools the power state for up to 10ms, waiting for the given predicate to |
||||
* be true. |
||||
*/ |
||||
bool WaitForPowerState(std::function<bool(bool,PowerState)> predicate); |
||||
/*
|
||||
* Pools the power state for up to 10ms, waiting for the given predicate to |
||||
* be true. |
||||
*/ |
||||
bool WaitForPowerState(std::function<bool(bool, PowerState)> predicate); |
||||
|
||||
enum Register { |
||||
PAGE_SELECT = 0,
|
||||
DE_EMPHASIS = 7, |
||||
DIGITAL_VOLUME_L = 61, |
||||
DIGITAL_VOLUME_R = 62, |
||||
DSP_BOOT_POWER_STATE = 118, |
||||
}; |
||||
enum Register { |
||||
PAGE_SELECT = 0, |
||||
DE_EMPHASIS = 7, |
||||
DIGITAL_VOLUME_L = 61, |
||||
DIGITAL_VOLUME_R = 62, |
||||
DSP_BOOT_POWER_STATE = 118, |
||||
}; |
||||
|
||||
void WriteRegister(Register reg, uint8_t val); |
||||
void WriteRegister(Register reg, uint8_t val); |
||||
}; |
||||
|
||||
} // namespace gay_ipod
|
||||
} // namespace gay_ipod
|
||||
|
@ -0,0 +1,224 @@ |
||||
#include "playback.h" |
||||
|
||||
#include <cstdint> |
||||
|
||||
#include "audio_element.h" |
||||
#include "audio_event_iface.h" |
||||
#include "audio_pipeline.h" |
||||
#include "dac.h" |
||||
#include "driver/i2s.h" |
||||
#include "esp_err.h" |
||||
#include "mp3_decoder.h" |
||||
|
||||
static const char* kTag = "PLAYBACK"; |
||||
static const i2s_port_t kI2SPort = I2S_NUM_0; |
||||
|
||||
namespace gay_ipod { |
||||
|
||||
// Static functions for interrop with the ESP IDF API, which requires a
|
||||
// function pointer.
|
||||
namespace callback { |
||||
static DacAudioPlayback* instance = nullptr; |
||||
|
||||
// Fits the required function pointer signature, but just delegates to the
|
||||
// wrapper function. Does that make this the wrapper? Who knows.
|
||||
__attribute__((unused)) // (gcc incorrectly thinks this is unused)
|
||||
static esp_err_t |
||||
on_event(audio_event_iface_msg_t* event, void* data) { |
||||
if (instance == nullptr) { |
||||
ESP_LOGW(kTag, "uncaught ADF event, type %x", event->cmd); |
||||
return ESP_OK; |
||||
} |
||||
return instance->HandleEvent(event, data); |
||||
} |
||||
} // namespace callback
|
||||
|
||||
auto DacAudioPlayback::create(AudioDac* dac) |
||||
-> cpp::result<std::unique_ptr<DacAudioPlayback>, Error> { |
||||
assert(callback::instance == nullptr); |
||||
|
||||
// Ensure we're soft-muted before initialising, in order to reduce protential
|
||||
// clicks and pops.
|
||||
dac->WriteVolume(255); |
||||
|
||||
audio_pipeline_handle_t pipeline; |
||||
audio_element_handle_t fatfs_stream_reader; |
||||
audio_element_handle_t i2s_stream_writer; |
||||
audio_event_iface_handle_t event_interface; |
||||
|
||||
audio_pipeline_cfg_t pipeline_config = |
||||
audio_pipeline_cfg_t(DEFAULT_AUDIO_PIPELINE_CONFIG()); |
||||
pipeline = audio_pipeline_init(&pipeline_config); |
||||
if (pipeline == NULL) { |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
fatfs_stream_cfg_t fatfs_stream_config = |
||||
fatfs_stream_cfg_t(FATFS_STREAM_CFG_DEFAULT()); |
||||
fatfs_stream_config.type = AUDIO_STREAM_READER; |
||||
fatfs_stream_reader = fatfs_stream_init(&fatfs_stream_config); |
||||
if (fatfs_stream_reader == NULL) { |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
i2s_stream_cfg_t i2s_stream_config = i2s_stream_cfg_t{ |
||||
.type = AUDIO_STREAM_WRITER, |
||||
.i2s_config = |
||||
{ |
||||
// static_cast bc esp-adf uses enums incorrectly
|
||||
.mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX), |
||||
.sample_rate = 44100, |
||||
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, |
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, |
||||
.communication_format = I2S_COMM_FORMAT_STAND_I2S, |
||||
.intr_alloc_flags = ESP_INTR_FLAG_LOWMED, |
||||
.dma_buf_count = 8, |
||||
.dma_buf_len = 64, |
||||
.use_apll = false, |
||||
.tx_desc_auto_clear = false, |
||||
.fixed_mclk = 0, |
||||
.mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, |
||||
.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT, |
||||
}, |
||||
.i2s_port = kI2SPort, |
||||
.use_alc = false, |
||||
.volume = 0, // Does nothing; use AudioDac to change this.
|
||||
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, |
||||
.task_stack = I2S_STREAM_TASK_STACK, |
||||
.task_core = I2S_STREAM_TASK_CORE, |
||||
.task_prio = I2S_STREAM_TASK_PRIO, |
||||
.stack_in_ext = false, |
||||
.multi_out_num = 0, |
||||
.uninstall_drv = true, |
||||
.need_expand = false, |
||||
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, |
||||
}; |
||||
i2s_stream_writer = i2s_stream_init(&i2s_stream_config); |
||||
if (i2s_stream_writer == NULL) { |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
// NOTE: i2s_stream_init does some additional setup that hardcodes MCK as
|
||||
// GPIO0. This happens to work fine for us, but be careful if changing.
|
||||
i2s_pin_config_t pin_config = {.mck_io_num = GPIO_NUM_0, |
||||
.bck_io_num = GPIO_NUM_26, |
||||
.ws_io_num = GPIO_NUM_27, |
||||
.data_out_num = GPIO_NUM_5, |
||||
.data_in_num = I2S_PIN_NO_CHANGE}; |
||||
if (esp_err_t err = i2s_set_pin(kI2SPort, &pin_config) != ESP_OK) { |
||||
ESP_LOGE(kTag, "failed to configure i2s pins %x", err); |
||||
return cpp::fail(Error::PIPELINE_INIT); |
||||
} |
||||
|
||||
// TODO: Create encoders dynamically when we need them.
|
||||
audio_element_handle_t mp3_decoder; |
||||
mp3_decoder_cfg_t mp3_config = |
||||
mp3_decoder_cfg_t(DEFAULT_MP3_DECODER_CONFIG()); |
||||
mp3_decoder = mp3_decoder_init(&mp3_config); |
||||
assert(mp3_decoder != NULL); |
||||
|
||||
audio_event_iface_cfg_t event_config = AUDIO_EVENT_IFACE_DEFAULT_CFG(); |
||||
event_config.on_cmd = &callback::on_event; |
||||
event_interface = audio_event_iface_init(&event_config); |
||||
audio_pipeline_set_listener(pipeline, event_interface); |
||||
|
||||
// TODO: most of this is likely post-init, since it involves a decoder.
|
||||
// All the elements of our pipeline have been initialised. Now switch them
|
||||
// together.
|
||||
audio_pipeline_register(pipeline, fatfs_stream_reader, "file"); |
||||
audio_pipeline_register(pipeline, mp3_decoder, "dec"); |
||||
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s"); |
||||
|
||||
const char* link_tag[3] = {"file", "dec", "i2s"}; |
||||
audio_pipeline_link(pipeline, &link_tag[0], 3); |
||||
|
||||
return std::make_unique<DacAudioPlayback>(dac, pipeline, fatfs_stream_reader, |
||||
i2s_stream_writer, event_interface, |
||||
mp3_decoder); |
||||
} |
||||
|
||||
DacAudioPlayback::DacAudioPlayback(AudioDac* dac, |
||||
audio_pipeline_handle_t pipeline, |
||||
audio_element_handle_t fatfs_stream_reader, |
||||
audio_element_handle_t i2s_stream_writer, |
||||
audio_event_iface_handle_t event_interface, |
||||
audio_element_handle_t mp3_decoder) |
||||
: dac_(dac), |
||||
pipeline_(pipeline), |
||||
fatfs_stream_reader_(fatfs_stream_reader), |
||||
i2s_stream_writer_(i2s_stream_writer), |
||||
event_interface_(event_interface), |
||||
mp3_decoder_(mp3_decoder) { |
||||
callback::instance = this; |
||||
} |
||||
|
||||
DacAudioPlayback::~DacAudioPlayback() { |
||||
dac_->WriteVolume(255); |
||||
|
||||
audio_pipeline_remove_listener(pipeline_); |
||||
callback::instance = nullptr; |
||||
|
||||
audio_pipeline_stop(pipeline_); |
||||
audio_pipeline_wait_for_stop(pipeline_); |
||||
audio_pipeline_terminate(pipeline_); |
||||
|
||||
audio_pipeline_unregister(pipeline_, fatfs_stream_reader_); |
||||
audio_pipeline_unregister(pipeline_, mp3_decoder_); |
||||
audio_pipeline_unregister(pipeline_, i2s_stream_writer_); |
||||
|
||||
audio_event_iface_destroy(event_interface_); |
||||
|
||||
audio_pipeline_deinit(pipeline_); |
||||
audio_element_deinit(fatfs_stream_reader_); |
||||
audio_element_deinit(i2s_stream_writer_); |
||||
audio_element_deinit(mp3_decoder_); |
||||
} |
||||
|
||||
void DacAudioPlayback::Play(const std::string& filename) { |
||||
dac_->WriteVolume(255); |
||||
// TODO: handle reconfiguring the pipeline if needed.
|
||||
audio_element_set_uri(fatfs_stream_reader_, filename.c_str()); |
||||
audio_pipeline_run(pipeline_); |
||||
dac_->WriteVolume(volume_); |
||||
} |
||||
|
||||
void DacAudioPlayback::Resume() { |
||||
// TODO.
|
||||
} |
||||
void DacAudioPlayback::Pause() { |
||||
// TODO.
|
||||
} |
||||
|
||||
void DacAudioPlayback::WaitForSongEnd() { |
||||
while (1) { |
||||
audio_element_state_t state = audio_element_get_state(i2s_stream_writer_); |
||||
if (state == AEL_STATE_FINISHED) { |
||||
return; |
||||
} |
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(1000)); |
||||
} |
||||
} |
||||
|
||||
/* for gapless */ |
||||
void DacAudioPlayback::set_next_file(const std::string& filename) { |
||||
next_filename_ = filename; |
||||
} |
||||
|
||||
void DacAudioPlayback::set_volume(uint8_t volume) { |
||||
volume_ = volume; |
||||
// TODO: don't write immediately if we're muting to change track or similar.
|
||||
dac_->WriteVolume(volume); |
||||
} |
||||
|
||||
auto DacAudioPlayback::volume() -> uint8_t { |
||||
return volume_; |
||||
} |
||||
|
||||
auto DacAudioPlayback::HandleEvent(audio_event_iface_msg_t* event, void* data) |
||||
-> esp_err_t { |
||||
ESP_LOGI(kTag, "got event!"); |
||||
return ESP_OK; |
||||
} |
||||
|
||||
} // namespace gay_ipod
|
@ -0,0 +1,68 @@ |
||||
#pragma once |
||||
|
||||
#include <cstdint> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include "audio_common.h" |
||||
#include "audio_element.h" |
||||
#include "audio_event_iface.h" |
||||
#include "audio_pipeline.h" |
||||
#include "dac.h" |
||||
#include "esp_err.h" |
||||
#include "fatfs_stream.h" |
||||
#include "i2s_stream.h" |
||||
#include "result.hpp" |
||||
#include "storage.h" |
||||
|
||||
namespace gay_ipod { |
||||
|
||||
class DacAudioPlayback { |
||||
public: |
||||
enum Error { PIPELINE_INIT }; |
||||
static auto create(AudioDac* dac) |
||||
-> cpp::result<std::unique_ptr<DacAudioPlayback>, Error>; |
||||
|
||||
DacAudioPlayback(AudioDac* dac, |
||||
audio_pipeline_handle_t pipeline, |
||||
audio_element_handle_t fatfs_stream_reader, |
||||
audio_element_handle_t i2s_stream_writer, |
||||
audio_event_iface_handle_t event_interface, |
||||
audio_element_handle_t mp3_decoder); |
||||
~DacAudioPlayback(); |
||||
|
||||
void Play(const std::string& filename); |
||||
void Resume(); |
||||
void Pause(); |
||||
|
||||
// For debug :)
|
||||
void WaitForSongEnd(); |
||||
|
||||
/* for gapless */ |
||||
void set_next_file(const std::string& filename); |
||||
|
||||
void set_volume(uint8_t volume); |
||||
auto volume() -> uint8_t; |
||||
|
||||
auto HandleEvent(audio_event_iface_msg_t* event, void* data) -> esp_err_t; |
||||
|
||||
// Not copyable or movable.
|
||||
DacAudioPlayback(const DacAudioPlayback&) = delete; |
||||
DacAudioPlayback& operator=(const DacAudioPlayback&) = delete; |
||||
|
||||
private: |
||||
AudioDac* dac_; |
||||
std::mutex playback_lock_; |
||||
|
||||
std::string next_filename_; |
||||
uint8_t volume_; |
||||
|
||||
audio_pipeline_handle_t pipeline_; |
||||
audio_element_handle_t fatfs_stream_reader_; |
||||
audio_element_handle_t i2s_stream_writer_; |
||||
audio_event_iface_handle_t event_interface_; |
||||
|
||||
audio_element_handle_t mp3_decoder_; |
||||
}; |
||||
|
||||
} // namespace gay_ipod
|
@ -1,73 +1,59 @@ |
||||
#pragma once |
||||
|
||||
#include <memory> |
||||
|
||||
#include "driver/sdmmc_types.h" |
||||
#include "driver/sdspi_host.h" |
||||
#include "esp_err.h" |
||||
#include "esp_vfs_fat.h" |
||||
#include "gpio-expander.h" |
||||
#include "result.hpp" |
||||
|
||||
namespace gay_ipod { |
||||
|
||||
// Static functions for interrop with the ESP IDF API, which requires a function
|
||||
// pointer.
|
||||
namespace sdspi { |
||||
// Holds a lambda created by SdStorage.
|
||||
static std::function<esp_err_t(sdspi_dev_handle_t,sdmmc_command_t*)> do_transaction_wrapper; |
||||
|
||||
// Fits the required function pointer signature, but just delegates to the
|
||||
// wrapper function. Does that make this the wrapper? Who knows.
|
||||
__attribute__ ((unused)) // (gcc incorrectly thinks this is unused)
|
||||
static esp_err_t do_transaction(sdspi_dev_handle_t handle, sdmmc_command_t *cmdinfo) { |
||||
return do_transaction_wrapper(handle, cmdinfo); |
||||
} |
||||
} // namespace sdspi
|
||||
|
||||
static const char *kStoragePath = "/sdcard"; |
||||
static const uint8_t kMaxOpenFiles = 8; |
||||
extern const char* kStoragePath; |
||||
|
||||
class SdStorage { |
||||
public: |
||||
SdStorage(GpioExpander *gpio); |
||||
~SdStorage(); |
||||
|
||||
enum Error { |
||||
OK, |
||||
/** We couldn't interact with the SD card at all. Is it missing? */ |
||||
FAILED_TO_READ, |
||||
/** We couldn't mount the SD card. Is it formatted? */ |
||||
FAILED_TO_MOUNT, |
||||
}; |
||||
|
||||
// FIXME: these methods should also handling powering the SD card up and
|
||||
// down once we have that capability.
|
||||
|
||||
/**
|
||||
* Initialises the SDSPI driver and mounts the SD card for reading and |
||||
* writing. This must be called before any interactions with the underlying |
||||
* storage. |
||||
*/ |
||||
Error Acquire(void); |
||||
|
||||
/**
|
||||
* Unmounts the SD card and frees memory associated with the SDSPI driver. |
||||
*/ |
||||
void Release(void); |
||||
|
||||
// Not copyable or movable.
|
||||
// TODO: maybe this could be movable?
|
||||
SdStorage(const SdStorage&) = delete; |
||||
SdStorage& operator=(const SdStorage&) = delete; |
||||
|
||||
private: |
||||
GpioExpander *gpio_; |
||||
|
||||
// SPI and SD driver info
|
||||
sdspi_dev_handle_t handle_; |
||||
sdmmc_host_t host_; |
||||
sdmmc_card_t card_; |
||||
|
||||
// Filesystem info
|
||||
FATFS *fs_ = nullptr; |
||||
public: |
||||
enum Error { |
||||
FAILED_TO_INIT, |
||||
/** We couldn't interact with the SD card at all. Is it missing? */ |
||||
FAILED_TO_READ, |
||||
/** We couldn't mount the SD card. Is it formatted? */ |
||||
FAILED_TO_MOUNT, |
||||
}; |
||||
|
||||
static auto create(GpioExpander* gpio) |
||||
-> cpp::result<std::unique_ptr<SdStorage>, Error>; |
||||
|
||||
SdStorage(GpioExpander* gpio, |
||||
esp_err_t (*do_transaction)(sdspi_dev_handle_t, sdmmc_command_t*), |
||||
sdspi_dev_handle_t handle_, |
||||
sdmmc_host_t host_, |
||||
sdmmc_card_t card_, |
||||
FATFS* fs_); |
||||
~SdStorage(); |
||||
|
||||
auto HandleTransaction(sdspi_dev_handle_t handle, sdmmc_command_t* cmdinfo) |
||||
-> esp_err_t; |
||||
|
||||
// Not copyable or movable.
|
||||
// TODO: maybe this could be movable?
|
||||
SdStorage(const SdStorage&) = delete; |
||||
SdStorage& operator=(const SdStorage&) = delete; |
||||
|
||||
private: |
||||
GpioExpander* gpio_; |
||||
|
||||
esp_err_t (*do_transaction_)(sdspi_dev_handle_t, sdmmc_command_t*) = nullptr; |
||||
|
||||
// SPI and SD driver info
|
||||
sdspi_dev_handle_t handle_; |
||||
sdmmc_host_t host_; |
||||
sdmmc_card_t card_; |
||||
|
||||
// Filesystem info
|
||||
FATFS* fs_ = nullptr; |
||||
}; |
||||
|
||||
} // namespace gay_ipod
|
||||
} // namespace gay_ipod
|
||||
|
Loading…
Reference in new issue