From e68a05b29de507a56b8747557707e8d6cdd68542 Mon Sep 17 00:00:00 2001 From: sowgro Date: Sun, 14 Dec 2025 00:21:02 -0500 Subject: Refactor --- project/Core/Inc/Project/parser.h | 46 +++++++++ project/Core/Inc/Project/player.h | 30 +++--- project/Core/Inc/Project/player_actions.h | 26 ----- project/Core/Inc/Project/song_info.h | 19 ---- project/Core/Src/Project/local_control.c | 2 +- project/Core/Src/Project/parser.c | 141 +++++++++++++++++++++++++ project/Core/Src/Project/player.c | 165 +++++++++++++++--------------- project/Core/Src/Project/player_actions.c | 104 ------------------- project/Core/Src/Project/project.c | 3 +- project/Core/Src/Project/remote_control.c | 4 +- project/Core/Src/Project/song_info.c | 51 --------- project/Core/Src/System/systick.c | 2 +- 12 files changed, 291 insertions(+), 302 deletions(-) create mode 100644 project/Core/Inc/Project/parser.h delete mode 100644 project/Core/Inc/Project/player_actions.h delete mode 100644 project/Core/Inc/Project/song_info.h create mode 100644 project/Core/Src/Project/parser.c delete mode 100644 project/Core/Src/Project/player_actions.c delete mode 100644 project/Core/Src/Project/song_info.c (limited to 'project/Core') diff --git a/project/Core/Inc/Project/parser.h b/project/Core/Inc/Project/parser.h new file mode 100644 index 0000000..356a4ad --- /dev/null +++ b/project/Core/Inc/Project/parser.h @@ -0,0 +1,46 @@ +/* + * player.h + * + * Created on: Dec 10, 2025 + * Author: sowgro + */ + +#ifndef INC_PROJECT_PARSER_H_ +#define INC_PROJECT_PARSER_H_ + +#include + +#define NOTE_OFF_EVENT (0) +#define NOTE_ON_EVENT (1) +#define KEY_PRESSURE (2) + +typedef struct { + uint8_t ev_type; + uint32_t abs_time; + uint8_t key_number; + uint8_t value; +} note_event_t; + +typedef struct { + note_event_t events[1024]; + int nEvents; + int curEventIndex; +} track_t; + +typedef struct { + char *title; + char *copyright; + int tempo; +} song_info_t; + +void parser_play_notes(); + +void parse_song(uint8_t *p_song); + +uint8_t *parse_track(uint8_t *p_song, track_t *track); + +uint8_t *parse_song_info(uint8_t *p_song, song_info_t *ret); + +song_info_t parser_get_song_info(); + +#endif /* INC_PROJECT_PARSER_H_ */ diff --git a/project/Core/Inc/Project/player.h b/project/Core/Inc/Project/player.h index 1b1d308..5e3f454 100644 --- a/project/Core/Inc/Project/player.h +++ b/project/Core/Inc/Project/player.h @@ -1,30 +1,28 @@ /* - * player.h + * player_actions.h * - * Created on: Dec 10, 2025 + * Created on: Oct 17, 2025 * Author: sowgro */ #ifndef INC_PROJECT_PLAYER_H_ #define INC_PROJECT_PLAYER_H_ -typedef struct { - uint8_t ev_type; - uint32_t abs_time; - uint8_t key_number; - uint8_t value; -} note_event_t; +#include -typedef struct { - note_event_t events[1024]; - int nEvents; - int curEventIndex; -} track_t; +#define PLAYING_STATE (1) +#define PAUSED_STATE (2) +#define STOPPED_STATE (3) -void play_notes(int count); +uint8_t player_get_state(); +void player_set_state(uint8_t newState); -void parse_song(uint8_t *p_song); +void player_help(); +void player_next(); +void player_play(); +void player_pause(); +void player_stop(); -uint8_t *parse_track(uint8_t *p_song, track_t *track); +void player_tick(); #endif /* INC_PROJECT_PLAYER_H_ */ diff --git a/project/Core/Inc/Project/player_actions.h b/project/Core/Inc/Project/player_actions.h deleted file mode 100644 index 8f8fa1d..0000000 --- a/project/Core/Inc/Project/player_actions.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * player_actions.h - * - * Created on: Oct 17, 2025 - * Author: sowgro - */ - -#ifndef INC_PROJECT_PLAYER_ACTIONS_H_ -#define INC_PROJECT_PLAYER_ACTIONS_H_ - -#define PLAYING_STATE (1) -#define PAUSED_STATE (2) -#define STOPPED_STATE (3) - -uint8_t player_get_state(); -void player_set_state(uint8_t newState); - -void player_help(); -void player_next(); -void player_play(); -void player_pause(); -void player_stop(); - -void player_action_tick(int count); - -#endif /* INC_PROJECT_PLAYER_ACTIONS_H_ */ diff --git a/project/Core/Inc/Project/song_info.h b/project/Core/Inc/Project/song_info.h deleted file mode 100644 index 9d6d5e6..0000000 --- a/project/Core/Inc/Project/song_info.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * song_info.h - * - * Created on: Oct 15, 2025 - * Author: sowgro - */ - -#ifndef INC_PROJECT_SONG_INFO_H_ -#define INC_PROJECT_SONG_INFO_H_ - -typedef struct { - char *title; - char *copyright; - int tempo; -} song_info_t; - -uint8_t *get_song_info(uint8_t *p_song, song_info_t *ret); - -#endif /* INC_PROJECT_SONG_INFO_H_ */ diff --git a/project/Core/Src/Project/local_control.c b/project/Core/Src/Project/local_control.c index df0c1b0..235a5a9 100644 --- a/project/Core/Src/Project/local_control.c +++ b/project/Core/Src/Project/local_control.c @@ -6,13 +6,13 @@ * Author: sowgro */ +#include #include #include #include #include "GPIO.h" #include "project.h" #include "systick.h" -#include "player_actions.h" static int click_count = 0; static uint32_t last_press = 0; diff --git a/project/Core/Src/Project/parser.c b/project/Core/Src/Project/parser.c new file mode 100644 index 0000000..c34b3ba --- /dev/null +++ b/project/Core/Src/Project/parser.c @@ -0,0 +1,141 @@ +/* + * player.c + * + * Created on: Nov 25, 2025 + * Author: sowgro + */ + +#include +#include "endian_converters.h" +#include +#include "endian_converters.h" +#include "hw8.h" +#include "activity4.h" +#include "systick.h" +#include "tone.h" + +static track_t tracks[4]; +static int nTracks; +static int startTime; +static song_info_t song_info; + +void parser_play_notes() { + int count = systick_get_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(); +} + +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 + + parse_song_info(p_song, &song_info); + + nTracks = header.ntrcks; + for(int i = 0; i < nTracks; i++){ + p_song = parse_track(p_song, &tracks[i]); + } +} + +uint8_t *parse_track(uint8_t *p_song, track_t *track) { + printf("Parsing track \r\n"); + 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; + + + 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; + } + +// size_t channelNumber = *p_song && 0b00001111; + p_song++; + + note.key_number = *p_song; + p_song++; + + note.value = *p_song; + p_song++; + + track->events[curEvent++] = note; + } + track->nEvents = curEvent; + track->curEventIndex = 0; + + printf("parsing track done\r\n"); + return p_song; +} + +uint8_t *parse_song_info(uint8_t *p_song, song_info_t *ret) { + ret->copyright = 0; + ret->tempo = 0; + ret->title = 0; + + p_song += 4; // move past MTrk label + uint32_t MTrk_len = convert_to_uint32(p_song); // read in size of MTrk + p_song += sizeof(MTrk_len); + + for (uint8_t *p_end = p_song + MTrk_len; p_song != p_end; p_song++) { + // FF 02 - copyright + if (convert_to_uint16(p_song) == 0xFF02) { + p_song += sizeof(uint16_t); + uint8_t ev_len = *(uint8_t *) p_song; + p_song += sizeof(ev_len); + ret->copyright = (char *) p_song; + ret->copyright[ev_len] = 0; + } + + // FF 03 - title + if (convert_to_uint16(p_song) == 0xFF03) { + p_song += sizeof(uint16_t); + uint8_t ev_len = *(uint8_t *) p_song; + p_song += sizeof(ev_len); + ret->title = (char *) p_song; + ret->title[ev_len] = 0; + } + + // FF 51 - tempo + if (convert_to_uint16(p_song) == 0xFF51) { + p_song += sizeof(uint16_t); + p_song += sizeof(uint8_t); // skip length, always 03 + ret->tempo = convert_to_uint24(p_song); + } + } + + return p_song; +} + +song_info_t parser_get_song_info() { + return song_info; +} 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 -#include "endian_converters.h" -#include "hw8.h" -#include "activity4.h" -#include "song_info.h" -#include "player.h" +#include +#include +#include +#include +#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; } diff --git a/project/Core/Src/Project/player_actions.c b/project/Core/Src/Project/player_actions.c deleted file mode 100644 index b486dd8..0000000 --- a/project/Core/Src/Project/player_actions.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * player_actions.c - * Code related to the player states and actions - * - * Created on: Oct 17, 2025 - * Author: sowgro - */ - -#include -#include -#include "player_actions.h" -#include "song_info.h" -#include "song.h" -#include "LED.h" -#include "player.h" - -static uint8_t state = STOPPED_STATE; - -/** - * Returns the current player state to other files - */ -uint8_t player_get_state() { - return state; -} - -/** - * Sets the current player state from other files - */ -void player_set_state(uint8_t newState) { - state = newState; -} - -/** - * 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"); -} - -/** - * 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; - song_info_t song_info; - get_song_info(song, &song_info); - - parse_song(song); - - 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); -} - -/** - * Switches to the play state - */ -void player_play() { - state = PLAYING_STATE; - LED_On(); -} - -/** - * Switches to the pause state - */ -void player_pause() { - state = PAUSED_STATE; -} - -/** - * Switches to the stop state - */ -void player_stop() { - state = STOPPED_STATE; - LED_Off(); -} - -/** - * Toggles the LED every second if in pause mode - */ -void player_action_tick(int count) { - switch (state) { - case PAUSED_STATE: - if(!(count & 1023)) { - LED_Toggle(); - }; break; - case PLAYING_STATE: - play_notes(count); - } -} diff --git a/project/Core/Src/Project/project.c b/project/Core/Src/Project/project.c index 169a5fb..5ee41a8 100644 --- a/project/Core/Src/Project/project.c +++ b/project/Core/Src/Project/project.c @@ -5,12 +5,12 @@ * Author: bruce */ +#include #include #include #include #include #include "project.h" -#include "player_actions.h" #include "hw4.h" #include "local_control.h" #include "remote_control.h" @@ -45,6 +45,7 @@ void project_run() { case LOCAL_MODE: local_control_loop(); break; } + player_tick(); } } diff --git a/project/Core/Src/Project/remote_control.c b/project/Core/Src/Project/remote_control.c index 57ace89..1073b36 100644 --- a/project/Core/Src/Project/remote_control.c +++ b/project/Core/Src/Project/remote_control.c @@ -6,14 +6,14 @@ * Author: sowgro */ +#include +#include #include #include #include -#include "player_actions.h" #include "UART.h" #include "project.h" #include "systick.h" -#include "player_actions.h" static char buffer[80]; static char* cur = buffer; diff --git a/project/Core/Src/Project/song_info.c b/project/Core/Src/Project/song_info.c deleted file mode 100644 index 5be94e5..0000000 --- a/project/Core/Src/Project/song_info.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * song_info.c - * - * Created on: Oct 9, 2025 - * Author: sowgro - */ - -#include "song.h" -#include "activity4.h" -#include -#include "song_info.h" -#include "endian_converters.h" - -uint8_t *get_song_info(uint8_t *p_song, song_info_t *ret) { - ret->copyright = 0; - ret->tempo = 0; - ret->title = 0; - - p_song += 4; // move past MTrk label - uint32_t MTrk_len = convert_to_uint32(p_song); // read in size of MTrk - p_song += sizeof(MTrk_len); - - for (uint8_t *p_end = p_song + MTrk_len; p_song != p_end; p_song++) { - // FF 02 - copyright - if (convert_to_uint16(p_song) == 0xFF02) { - p_song += sizeof(uint16_t); - uint8_t ev_len = *(uint8_t *) p_song; - p_song += sizeof(ev_len); - ret->copyright = (char *) p_song; - ret->copyright[ev_len] = 0; - } - - // FF 03 - title - if (convert_to_uint16(p_song) == 0xFF03) { - p_song += sizeof(uint16_t); - uint8_t ev_len = *(uint8_t *) p_song; - p_song += sizeof(ev_len); - ret->title = (char *) p_song; - ret->title[ev_len] = 0; - } - - // FF 51 - tempo - if (convert_to_uint16(p_song) == 0xFF51) { - p_song += sizeof(uint16_t); - p_song += sizeof(uint8_t); // skip length, always 03 - ret->tempo = convert_to_uint24(p_song); - } - } - - return p_song; -} diff --git a/project/Core/Src/System/systick.c b/project/Core/Src/System/systick.c index 5d4d259..b58e3cf 100644 --- a/project/Core/Src/System/systick.c +++ b/project/Core/Src/System/systick.c @@ -1,5 +1,5 @@ +#include #include "systick.h" -#include "player_actions.h" systick_t *get_systick() { return (systick_t *) 0xE000E010; -- cgit v1.2.3