Zap'Em Blast'Em Robots
robots that play laser tag!
Loading...
Searching...
No Matches
sound_task.h File Reference

Header file for the sound task system. More...

#include <stdint.h>
#include "stm32f4xx_hal.h"

Go to the source code of this file.

Data Structures

struct  SoundTask
 Structure that represents the sound task state and configuration. More...

Macros

#define SAMPLE_RATE   44100U
#define PWM_FREQ   88200U
#define STEP_RATIO   (PWM_FREQ / SAMPLE_RATE)

Typedefs

typedef void(* sound_fcn_t) (SoundTask *sound_task)
 Function pointer type for sound state functions.

Functions

void sound_task_run (SoundTask *sound_task)
 Executes the current sound task state.
void sound_task_state_0_init (SoundTask *sound_task)
 Initializes the sound task (state 0).
void sound_task_state_1_wait (SoundTask *sound_task)
 Waits for sound flags (state 1).
void sound_task_state_2_laser (SoundTask *sound_task)
 Starts the interrupts and sets the array pointer for the laser sound(state 2).
void sound_task_state_3_hit (SoundTask *sound_task)
 Starts the interrupts and sets the array pointer for the hit sound(state 3).
void sound_task_state_4_win (SoundTask *sound_task)
 Starts the interrupts and sets the array pointer for the win sound(state 4).
void sound_task_state_5_start (SoundTask *sound_task)
 Starts the interrupts and sets the array pointer for the start sound(state 5).
void sound_task_state_6_stop (SoundTask *sound_task)
 Stops the interrupts and PWM for speaker after sound played (state 6)

Variables

const int16_t * audio_buf_ptr
volatile uint32_t audio_buf_len
volatile uint32_t sample_index
volatile uint8_t pwm_div_count
volatile uint32_t playing

Detailed Description

Header file for the sound task system.

Author
Andrew Carr

Created on: May 22, 2025

Macro Definition Documentation

◆ PWM_FREQ

#define PWM_FREQ   88200U

Output Timer PWM frequency

◆ SAMPLE_RATE

#define SAMPLE_RATE   44100U

Audio sample rate

◆ STEP_RATIO

#define STEP_RATIO   (PWM_FREQ / SAMPLE_RATE)

Step ratio for

Typedef Documentation

◆ sound_fcn_t

typedef void(* sound_fcn_t) (SoundTask *sound_task)

Function pointer type for sound state functions.

Each state function must take a pointer to SoundTask as its argument.

Function Documentation

◆ sound_task_run()

void sound_task_run ( SoundTask * sound_task)

Executes the current sound task state.

Parameters
sound_taskPointer to the SoundTask instance.

Executes the current sound task state.

Checks whether the current state index is valid. If so, it calls the corresponding state function from the task's function pointer array.

Parameters
sound_taskPointer to the SoundTask instance.

◆ sound_task_state_0_init()

void sound_task_state_0_init ( SoundTask * sound_task)

Initializes the sound task (state 0).

Goes to state 1, waiting for sound flags.

Parameters
sound_taskPointer to the SoundTask instance.

◆ sound_task_state_1_wait()

void sound_task_state_1_wait ( SoundTask * sound_task)

Waits for sound flags (state 1).

Waits for flags to go high and then sends the state machine into the corresponding state.

Parameters
sound_taskPointer to the SoundTask instance.

◆ sound_task_state_2_laser()

void sound_task_state_2_laser ( SoundTask * sound_task)

Starts the interrupts and sets the array pointer for the laser sound(state 2).

Assigns the corresponding audio buffer and length to the extern variables. Then, restarts interrupts and PWM for the speaker.

Parameters
sound_taskPointer to the SoundTask instance.

◆ sound_task_state_3_hit()

void sound_task_state_3_hit ( SoundTask * sound_task)

Starts the interrupts and sets the array pointer for the hit sound(state 3).

Assigns the corresponding audio buffer and length to the extern variables. Then, restarts interrupts and PWM for the speaker.

Parameters
sound_taskPointer to the SoundTask instance.

◆ sound_task_state_4_win()

void sound_task_state_4_win ( SoundTask * sound_task)

Starts the interrupts and sets the array pointer for the win sound(state 4).

Assigns the corresponding audio buffer and length to the extern variables. Then, restarts interrupts and PWM for the speaker.

Parameters
sound_taskPointer to the SoundTask instance.

◆ sound_task_state_5_start()

void sound_task_state_5_start ( SoundTask * sound_task)

Starts the interrupts and sets the array pointer for the start sound(state 5).

Assigns the corresponding audio buffer and length to the extern variables. Then, restarts interrupts and PWM for the speaker.

Parameters
sound_taskPointer to the SoundTask instance.

◆ sound_task_state_6_stop()

void sound_task_state_6_stop ( SoundTask * sound_task)

Stops the interrupts and PWM for speaker after sound played (state 6)

Stops interrupts and PWM for speaker then goes back to the waiting state.

Parameters
sound_taskPointer to the SoundTask instance.

Variable Documentation

◆ audio_buf_len

volatile uint32_t audio_buf_len
extern

Length current sound buffer

◆ audio_buf_ptr

const int16_t* audio_buf_ptr
extern

Pointer to current sound buffer

◆ playing

volatile uint32_t playing
extern

Flag for when a sound is playing

◆ pwm_div_count

volatile uint8_t pwm_div_count
extern

counter for stepping through audio buf

◆ sample_index

volatile uint32_t sample_index
extern

Index of step in sound buffer