mirror of
https://github.com/DashyFox/StackSport.git
synced 2025-05-04 15:20:16 +00:00
59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
/*
|
|
* Sound.h
|
|
*
|
|
* Created on: Oct 1, 2024
|
|
* Author: DashyFox
|
|
*/
|
|
|
|
#ifndef INC_SOUND_H_
|
|
#define INC_SOUND_H_
|
|
|
|
#include <stdint.h>
|
|
|
|
// Define default timer and channels
|
|
#define SOUND_TIMER TIM4
|
|
#define SOUND_TIMER_RCC RCC_APB1ENR_TIM4EN
|
|
#define SOUND_CHANNEL_1 3 // TIM4 Channel 3
|
|
#define SOUND_CHANNEL_2 4 // TIM4 Channel 4
|
|
|
|
// Define the Note structure
|
|
typedef struct {
|
|
uint32_t frequency; // Frequency in Hz
|
|
uint32_t duration; // Duration in milliseconds, 0 means infinite
|
|
uint8_t duty_cycle; // PWM duty cycle in percentage (0-100)
|
|
} Note_t;
|
|
|
|
// Enum for musical notes
|
|
typedef enum {
|
|
NOTE_C4 = 261,
|
|
NOTE_D4 = 293,
|
|
NOTE_E4 = 329,
|
|
NOTE_F4 = 349,
|
|
NOTE_G4 = 392,
|
|
NOTE_A4 = 440,
|
|
NOTE_B4 = 493,
|
|
NOTE_C5 = 523,
|
|
// Add more notes as needed
|
|
NOTE_REST = 0
|
|
} MusicalNote_t;
|
|
|
|
// Enum for note durations (in fractions of a whole note)
|
|
typedef enum {
|
|
DURATION_WHOLE = 1,
|
|
DURATION_HALF = 2,
|
|
DURATION_QUARTER = 4,
|
|
DURATION_EIGHTH = 8,
|
|
DURATION_SIXTEENTH = 16
|
|
// Add more durations as needed
|
|
} NoteDuration_t;
|
|
|
|
// Function declarations
|
|
void sound_init(void);
|
|
void sound_set_bpm(uint32_t bpm);
|
|
void sound_play_note(Note_t note, uint8_t channel);
|
|
void sound_tick(void);
|
|
Note_t sound_create_note(MusicalNote_t note, NoteDuration_t duration, uint8_t duty_cycle);
|
|
|
|
|
|
#endif /* INC_SOUND_H_ */
|