small tweaks to drivers, starting speaker refactor

This commit is contained in:
Mitchell Marino 2025-03-25 21:42:10 -05:00
parent ab7148be42
commit 42aa5fa315
7 changed files with 141 additions and 117 deletions

View File

@ -1,5 +1,6 @@
#include "all.h"
static const char *TAG = "driver_all";
static void init_i2c();
void init_drivers() {
@ -9,6 +10,8 @@ void init_drivers() {
// init the bottom half so that we can get user input.
init_bottom_half();
init_sd();
init_speaker();
}
/// @brief Initializes I2C_NUM_0.
@ -21,6 +24,8 @@ void init_drivers() {
/// - The PERH port
/// - The Capacitive Touch Panel
static void init_i2c() {
ESP_LOGI(TAG, "Initializing i2c...");
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_5,
@ -37,4 +42,6 @@ static void init_i2c() {
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0));
ESP_LOGI(TAG, "i2c initialized!");
}

View File

@ -36,6 +36,8 @@ static void receive_touch();
// }
void init_bottom_half() {
ESP_LOGI(TAG, "Initializing bottom half...");
ESP_ERROR_CHECK(gpio_set_direction(BOTTOM_PIN_INTERUPT, GPIO_MODE_INPUT));
ESP_ERROR_CHECK(gpio_set_pull_mode(BOTTOM_PIN_INTERUPT, GPIO_PULLUP_ONLY));
@ -52,6 +54,8 @@ void init_bottom_half() {
receive_touch();
xTaskCreate(poll_bottom_task, "poll_bottom", 4096, NULL, 10, NULL);
ESP_LOGI(TAG, "Bottom half initialized!");
}
static uint8_t receive_delta(void) {

View File

@ -8,7 +8,7 @@ i2c_lcd_pcf8574_handle_t lcd;
static const char *TAG = "char_lcd";
void init_lcd() {
ESP_LOGI(TAG, "Initializing LCD");
ESP_LOGI(TAG, "Initializing LCD...");
lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM);
lcd_begin(&lcd, LCD_COLS, LCD_ROWS);

View File

@ -8,7 +8,7 @@ static const char* mount_point = MOUNT_POINT;
static const char* TAG = "sd";
bool init_sd() {
ESP_LOGI(TAG, "Initializing SD card");
ESP_LOGI(TAG, "Initializing SD card...");
esp_err_t ret;
@ -40,6 +40,8 @@ bool init_sd() {
.flags = 0
// .flags = SDMMC_SLOT_FLAG_INTERNAL_PULLUP
};
try_mount:
ESP_LOGI(TAG, "Mounting filesystem");
ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
@ -48,15 +50,27 @@ bool init_sd() {
} else {
ESP_LOGE(TAG, "Failed to mount sd card: %s.", esp_err_to_name(ret));
lcd_print(0, 0, "SD Mount Failed!");
lcd_print(0, 1, esp_err_to_name(ret));
lcd_print(0, 2, "Press Green to retry");
lcd_print(0, 0, "SD: ");
lcd_print(4, 0, esp_err_to_name(ret));
lcd_print(0, 1, "Press Green to retry");
lcd_print(0, 2, "Press Yellow to skip");
lcd_print(0, 3, "Press Red to format");
ButtonKey button;
while (!( ))
while (!( get_button_pressed(&button) && (button == ButtonKey::button_green || button == ButtonKey::button_red || button == ButtonKey::button_yellow) )) vTaskDelay(pdMS_TO_TICKS(10));
return false;
lcd_clear();
if (button == ButtonKey::button_green) {
goto try_mount;
}
if (button == ButtonKey::button_yellow) {
return false;
}
if (button == ButtonKey::button_red) {
mount_config.format_if_mount_failed = true;
goto try_mount;
}
}
// Card has been initialized, print its properties

View File

@ -2,135 +2,137 @@
i2s_chan_handle_t tx_chan;
static const char *TAG = "speaker_driver";
static const char *TAG = "speaker";
esp_err_t play_raw_stop_queue(const char *fp, QueueHandle_t stop_handle) {
FILE *fh = fopen(fp, "rb");
if (fh == NULL)
{
ESP_LOGE(TAG, "Failed to open file");
return ESP_ERR_INVALID_ARG;
}
FILE *fh = fopen(fp, "rb");
if (fh == NULL) {
ESP_LOGE(TAG, "Failed to open file");
return ESP_ERR_INVALID_ARG;
}
// create a writer buffer
uint8_t *read_buf = (uint8_t*) calloc(AUDIO_BUFFER, sizeof(uint8_t));
uint16_t *write_buf = (uint16_t*) calloc(AUDIO_BUFFER, sizeof(uint16_t));
size_t bytes_read = 0;
size_t bytes_written = 0;
// create a writer buffer
uint8_t *read_buf = (uint8_t*) calloc(AUDIO_BUFFER, sizeof(uint8_t));
uint16_t *write_buf = (uint16_t*) calloc(AUDIO_BUFFER, sizeof(uint16_t));
size_t bytes_read = 0;
size_t bytes_written = 0;
esp_err_t channel_enable_result = i2s_channel_enable(tx_chan);
ESP_ERROR_CHECK_WITHOUT_ABORT(channel_enable_result);
if (channel_enable_result != ESP_OK) {
return channel_enable_result;
}
esp_err_t channel_enable_result = i2s_channel_enable(tx_chan);
ESP_ERROR_CHECK_WITHOUT_ABORT(channel_enable_result);
if (channel_enable_result != ESP_OK) {
return channel_enable_result;
}
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
// i2s_channel_enable(tx_handle);
// i2s_channel_enable(tx_handle);
while (bytes_read > 0) {
// write the buffer to the i2s
// ESP_LOGI(TAG, "Writing: ");
// for (int i = 0; i < words_read; i++) {
// int16_t val = buf[i];
// printf("%s0x%04X ", (val < 0 ? "-" : "+"), (val < 0 ? -val : val));
// }>
i2s_channel_write(tx_chan, write_buf, bytes_read * sizeof(int16_t), &bytes_written, portMAX_DELAY);
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
uint8_t recv = 0;
if (xQueueReceive(stop_handle, &recv, 0)) {
break;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
while (bytes_read > 0) {
// write the buffer to the i2s
// ESP_LOGI(TAG, "Writing: ");
// for (int i = 0; i < words_read; i++) {
// int16_t val = buf[i];
// printf("%s0x%04X ", (val < 0 ? "-" : "+"), (val < 0 ? -val : val));
// }>
i2s_channel_write(tx_chan, write_buf, bytes_read * sizeof(int16_t), &bytes_written, portMAX_DELAY);
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
uint8_t recv = 0;
if (xQueueReceive(stop_handle, &recv, 0)) {
break;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
i2s_channel_disable(tx_chan);
free(read_buf);
free(write_buf);
fclose(fh);
i2s_channel_disable(tx_chan);
free(read_buf);
free(write_buf);
fclose(fh);
return ESP_OK;
return ESP_OK;
}
esp_err_t play_raw(const char *fp) {
FILE *fh = fopen(fp, "rb");
if (fh == NULL)
{
ESP_LOGE(TAG, "Failed to open file");
return ESP_ERR_INVALID_ARG;
}
FILE *fh = fopen(fp, "rb");
if (fh == NULL)
ESP_LOGE(TAG, "Failed to open file");
return ESP_ERR_INVALID_ARG;
}
// create a writer buffer
uint8_t *read_buf = (uint8_t*) calloc(AUDIO_BUFFER, sizeof(uint8_t));
uint16_t *write_buf = (uint16_t*) calloc(AUDIO_BUFFER, sizeof(uint16_t));
size_t bytes_read = 0;
size_t bytes_written = 0;
// create a writer buffer
uint8_t *read_buf = (uint8_t*) calloc(AUDIO_BUFFER, sizeof(uint8_t));
uint16_t *write_buf = (uint16_t*) calloc(AUDIO_BUFFER, sizeof(uint16_t));
size_t bytes_read = 0;
size_t bytes_written = 0;
esp_err_t channel_enable_result = i2s_channel_enable(tx_chan);
ESP_ERROR_CHECK_WITHOUT_ABORT(channel_enable_result);
if (channel_enable_result != ESP_OK) {
return channel_enable_result;
}
esp_err_t channel_enable_result = i2s_channel_enable(tx_chan);
ESP_ERROR_CHECK_WITHOUT_ABORT(channel_enable_result);
if (channel_enable_result != ESP_OK) {
return channel_enable_result;
}
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
// i2s_channel_enable(tx_handle);
// i2s_channel_enable(tx_handle);
while (bytes_read > 0) {
// write the buffer to the i2s
// ESP_LOGI(TAG, "Writing: ");
// for (int i = 0; i < words_read; i++) {
// int16_t val = buf[i];
// printf("%s0x%04X ", (val < 0 ? "-" : "+"), (val < 0 ? -val : val));
// }>
i2s_channel_write(tx_chan, write_buf, bytes_read * sizeof(int16_t), &bytes_written, portMAX_DELAY);
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
while (bytes_read > 0) {
// write the buffer to the i2s
// ESP_LOGI(TAG, "Writing: ");
// for (int i = 0; i < words_read; i++) {
// int16_t val = buf[i];
// printf("%s0x%04X ", (val < 0 ? "-" : "+"), (val < 0 ? -val : val));
// }>
i2s_channel_write(tx_chan, write_buf, bytes_read * sizeof(int16_t), &bytes_written, portMAX_DELAY);
bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh);
for (int i = 0; i < bytes_read; i++) {
write_buf[i] = read_buf[i] << 4;
}
vTaskDelay(pdMS_TO_TICKS(10));
}
i2s_channel_disable(tx_chan);
free(read_buf);
free(write_buf);
fclose(fh);
i2s_channel_disable(tx_chan);
free(read_buf);
free(write_buf);
fclose(fh);
return ESP_OK;
return ESP_OK;
}
void init_speaker(void) {
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&tx_chan_cfg, &tx_chan, NULL));
ESP_LOGI(TAG, "Initializing speaker...");
i2s_std_config_t tx_std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(SAMPLE_RATE),
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = SPEAKER_PIN_BCLK,
.ws = SPEAKER_PIN_WS,
.dout = SPEAKER_PIN_DOUT,
.din = GPIO_NUM_NC,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_chan, &tx_std_cfg));
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&tx_chan_cfg, &tx_chan, NULL));
i2s_std_config_t tx_std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(SAMPLE_RATE),
.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = SPEAKER_PIN_BCLK,
.ws = SPEAKER_PIN_WS,
.dout = SPEAKER_PIN_DOUT,
.din = GPIO_NUM_NC,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_chan, &tx_std_cfg));
ESP_LOGI(TAG, "Speaker initialized!");
}
void play_example() {
ESP_ERROR_CHECK_WITHOUT_ABORT(play_raw("/sdcard/o.pcm"));
ESP_ERROR_CHECK_WITHOUT_ABORT(play_raw("/sdcard/o.pcm"));
}

View File

@ -25,13 +25,10 @@ extern "C" void app_main(void) {
init_drivers();
init_tft();
init_speaker();
init_sseg();
init_game_module_timer();
init_leds();
init_wires();
init_bottom_half();
init_power_board();
clean_bomb();

View File

@ -42,7 +42,7 @@ void setup_wires(void) {
ButtonKey button;
while (1) {
while (get_pressed_keypad(&key)) {
while (get_keypad_pressed(&key)) {
if (key == KeypadKey::k1) {
generate_new_wires(wires);
save_wires_to_sd_card(wires);
@ -53,7 +53,7 @@ void setup_wires(void) {
}
}
while (get_pressed_button(&button)) {
while (get_button_pressed(&button)) {
if (button == ButtonKey::b1) {
// left
editing_idx = editing_idx - 1;