speaker updates and tweaks

This commit is contained in:
2024-08-14 18:29:51 -05:00
parent 0cbae7b15a
commit 7b8e38844a
9 changed files with 146 additions and 12 deletions
+22 -6
View File
@@ -1,11 +1,10 @@
#ifndef GAME_TIMER_HPP
#define GAME_TIMER_HPP
#include "game_timer.h"
static bool is_module_playing;
static bool is_game_playing;
static bool game_time_count_up;
// in ms
static uint32_t game_time_left;
static uint32_t module_time_left;
@@ -70,6 +69,18 @@ uint32_t get_module_time() {
return module_time_left;
}
/// Issues a time penalty to the game time
void time_penalty(uint32_t penalty) {
if (game_time_count_up) {
game_time_left += penalty;
} else {
game_time_left = sat_sub(game_time_left, penalty);
if (game_time_left == 0) {
game_time_count_up = true;
}
}
}
static void game_timer_task(void *arg)
{
TickType_t lastWakeTime = xTaskGetTickCount();
@@ -78,7 +89,14 @@ static void game_timer_task(void *arg)
while (1) {
vTaskDelayUntil( &lastWakeTime, pdMS_TO_TICKS(frequency));
if (is_game_playing) {
game_time_left = sat_sub(game_time_left, frequency);
if (game_time_count_up) {
game_time_left += frequency;
} else {
game_time_left = sat_sub(game_time_left, frequency);
if (game_time_left == 0) {
game_time_count_up = true;
}
}
write_game_time(game_time_left);
}
if (is_module_playing) {
@@ -96,5 +114,3 @@ static uint32_t sat_sub(uint32_t x, uint32_t y)
res &= -(res <= x);
return res;
}
#endif /* GAME_TIMER_HPP */
+3
View File
@@ -22,6 +22,9 @@ void set_game_time(uint32_t new_time);
/// Gets the current game time in ms
uint32_t get_game_time();
/// Gets the current game time in ms
void time_penalty(uint32_t penalty);
/// Sets the module time in ms
void set_module_time(uint32_t new_time);
/// Gets the current module time in ms
+54
View File
@@ -4,6 +4,60 @@ i2s_chan_handle_t tx_chan;
static const char *TAG = "speaker_driver";
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;
}
// 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;
}
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);
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);
return ESP_OK;
}
esp_err_t play_raw(const char *fp) {
FILE *fh = fopen(fp, "rb");
if (fh == NULL)
+6
View File
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "driver/i2s_std.h"
#include "driver/gpio.h"
@@ -19,7 +20,12 @@
extern i2s_chan_handle_t tx_chan;
/// Plays a audio file stopping when something is received on `stop_handle`
///
/// `stop_handle` should be of size `uint8_t`.
esp_err_t play_raw_stop_queue(const char *fp, QueueHandle_t stop_handle);
esp_err_t play_raw(const char *fp);
void init_speaker(void);
void play_example();
+4 -1
View File
@@ -1,5 +1,7 @@
#include "wires.h"
uint32_t total_strikes;
static const char *TAG = "wires";
static const uint32_t STRIKE_TIME_PENALTY = 30'000;
@@ -81,7 +83,8 @@ void strike(char* reason) {
ESP_LOGW("strike!", "%s", reason);
lcd_set_cursor(&lcd, 0, 3);
lcd_print(&lcd, reason);
set_game_time(get_game_time() - STRIKE_TIME_PENALTY);
time_penalty(STRIKE_TIME_PENALTY);
total_strikes += 1;
uint8_t reg = 6;
ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_to_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, &reg, 1, (100 / portTICK_PERIOD_MS)));
}
+2
View File
@@ -15,6 +15,8 @@
#define DELTA_BIT_WIRES 0
#define DELTA_BIT_BUTTON 1
extern uint32_t total_strikes;
void init_wires(void);
uint8_t get_wires(void);
uint8_t get_cut_wires(void);