diff options
| author | sowgro <tpoke.ferrari@gmail.com> | 2025-12-14 00:21:02 -0500 |
|---|---|---|
| committer | sowgro <tpoke.ferrari@gmail.com> | 2025-12-14 00:21:02 -0500 |
| commit | e68a05b29de507a56b8747557707e8d6cdd68542 (patch) | |
| tree | 1f7c457159af75ae198258a2301a55531d7baff5 /project/Core/Src/Project/player.c | |
| parent | 519f868c445a86b988772307ea1f1e2fc95fe54a (diff) | |
| download | 340-repo-Sowgro-e68a05b29de507a56b8747557707e8d6cdd68542.tar.gz 340-repo-Sowgro-e68a05b29de507a56b8747557707e8d6cdd68542.tar.bz2 340-repo-Sowgro-e68a05b29de507a56b8747557707e8d6cdd68542.zip | |
Refactor
Diffstat (limited to 'project/Core/Src/Project/player.c')
| -rw-r--r-- | project/Core/Src/Project/player.c | 165 |
1 files changed, 84 insertions, 81 deletions
diff --git a/project/Core/Src/Project/player.c b/project/Core/Src/Project/player.c index 53ae0f8..42b652d 100644 --- a/project/Core/Src/Project/player.c +++ b/project/Core/Src/Project/player.c @@ -1,100 +1,103 @@ /* - * player.c + * player_actions.c + * Code related to the player states and actions * - * Created on: Nov 25, 2025 + * Created on: Oct 17, 2025 * Author: sowgro */ -#include "endian_converters.h" -#include <stdint.h> -#include "endian_converters.h" -#include "hw8.h" -#include "activity4.h" -#include "song_info.h" -#include "player.h" +#include <parser.h> +#include <player.h> +#include <stdio.h> +#include <stm32l4xx.h> +#include "song.h" +#include "LED.h" #include "systick.h" -#include "tone.h" -#define NOTE_OFF_EVENT (0) -#define NOTE_ON_EVENT (1) -#define KEY_PRESSURE (2) +static uint8_t state = STOPPED_STATE; -static track_t tracks[16]; -static int nTracks; -static int startTime; - -void play_notes(int count) { - int curAbsTime = count - startTime; - for (int i = 0; i < nTracks; i++) { - note_event_t curEvent = tracks[i].events[tracks[i].curEventIndex]; - if (curEvent.abs_time >= curAbsTime) { - switch (curEvent.ev_type) { - case NOTE_OFF_EVENT: - remove_tone(curEvent.key_number); break; - case NOTE_ON_EVENT: - add_tone(curEvent.key_number, curEvent.value); break; - } - } - tracks[i].curEventIndex++; - } - play_tones(); +/** + * Returns the current player state to other files + */ +uint8_t player_get_state() { + return state; } -void parse_song(uint8_t *p_song) { - header_t header; - header = get_header(p_song); - p_song += sizeof(header_t) - 2; // move pointer past header - - song_info_t song_info; - p_song = get_song_info(p_song, &song_info); - - for(int i = 0; i < header.ntrcks; i++){ - p_song = parse_track(p_song, &tracks[i]); - } - nTracks = header.ntrcks; +/** + * Sets the current player state from other files + */ +void player_set_state(uint8_t newState) { + state = newState; } -uint8_t *parse_track(uint8_t *p_song, track_t *track) { - p_song += 4; // skip MTrk - uint32_t MTrk_len = convert_to_uint32(p_song); - p_song += 4; - - int curEvent = 0; - uint32_t prev_abs_time = 0; - - for (uint8_t *p_end = p_song + MTrk_len; p_song != p_end; p_song++) { - note_event_t note; - - parseDelay_result_t delay_result = parseDelay(p_song); - uint32_t abs_time = prev_abs_time + delay_result.value; - note.abs_time = abs_time; - prev_abs_time = abs_time; - p_song += delay_result.bytes_used; - +/** + * Prints the help message + */ +void player_help() { + printf("\r***REMOTE LED CONTROL MENU***\r\n"); + printf("Available User Commands:\r\n"); + printf("NEXT - Show next song info\r\n"); + printf("PLAY - Play the song (LED on)\r\n"); + printf("PAUSE - Pause the song (LED flash)\r\n"); + printf("STOP - Stop the song (LED off)\r\n"); +} - if (*p_song >> 4 == 0b1000) { - note.ev_type = NOTE_OFF_EVENT; - } else if (*p_song >> 4 == 0b1001) { - note.ev_type = NOTE_ON_EVENT; - } else if (*p_song >> 4 == 0b1010) { - note.ev_type = KEY_PRESSURE; - } else { - continue; - } +/** + * Advances the player to the next song. + */ +void player_next() { + static int current_song = -1; + current_song++; + if (current_song > 4) + current_song = 0; + + void *song = get_song(current_song).p_song; + parse_song(song); + + song_info_t song_info = parser_get_song_info(); + printf("Song #%i\r\n", current_song + 1); + if (song_info.title) + printf("Title: %s\r\n", song_info.title); + if (song_info.copyright) + printf("Copyright: %s\r\n", song_info.copyright); + if (song_info.tempo) + printf("Tempo: %i\r\n", song_info.tempo); +} -// size_t channelNumber = *p_song && 0b00001111; - p_song++; +/** + * Switches to the play state + */ +void player_play() { + state = PLAYING_STATE; + LED_On(); +} - note.key_number = *p_song; - p_song++; +/** + * Switches to the pause state + */ +void player_pause() { + state = PAUSED_STATE; +} - note.value = *p_song; - p_song++; +/** + * Switches to the stop state + */ +void player_stop() { + state = STOPPED_STATE; + LED_Off(); +} - track->events[curEvent++] = note; +/** + * Toggles the LED every second if in pause mode + */ +void player_tick() { + int count = systick_get_count(); + switch (state) { + case PAUSED_STATE: + if(!(count & 1023)) { + LED_Toggle(); + }; break; + case PLAYING_STATE: + parser_play_notes(count); break; } - track->nEvents = curEvent; - track->curEventIndex = 0; - - return p_song; } |
