From 39c966fb271ebe3cd589272c0fb3a45eb0c54280 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 14 Nov 2024 01:04:59 -0800 Subject: [PATCH] sound: little cleaner idle handling (#2078) Co-authored-by: Comma Device --- board/stm32h7/sound.h | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/board/stm32h7/sound.h b/board/stm32h7/sound.h index 9228b1613..53f7276bd 100644 --- a/board/stm32h7/sound.h +++ b/board/stm32h7/sound.h @@ -1,15 +1,9 @@ - #define SOUND_RX_BUF_SIZE 2000U #define SOUND_TX_BUF_SIZE (SOUND_RX_BUF_SIZE/2U) __attribute__((section(".sram4"))) static uint16_t sound_rx_buf[2][SOUND_RX_BUF_SIZE]; -typedef enum { - OFF = 0, - IDLE = 1, - PLAYING = 2, -} SoundStatus; -static SoundStatus sound_status = OFF; +static uint8_t sound_idle_count; // Playback processing static void BDMA_Channel0_IRQ_Handler(void) { @@ -24,10 +18,10 @@ static void BDMA_Channel0_IRQ_Handler(void) { tx_buf[i/2U] = ((sound_rx_buf[buf_idx][i] + (1UL << 14)) >> 3); } - if (sound_status == OFF) { + if (sound_idle_count == 0U) { current_board->set_amp_enabled(true); } - sound_status = PLAYING; + sound_idle_count = 2U; DMA1->LIFCR |= 0xF40; DMA1_Stream1->CR &= ~DMA_SxCR_EN; @@ -37,16 +31,11 @@ static void BDMA_Channel0_IRQ_Handler(void) { } void sound_tick(void) { - switch (sound_status) { - case IDLE: + if (sound_idle_count > 0U) { + sound_idle_count--; + if (sound_idle_count == 0U) { current_board->set_amp_enabled(false); - sound_status = OFF; - break; - case PLAYING: - sound_status = IDLE; - break; - default: - break; + } } }