/* * project.c * * Created on: Jan 8, 2023 * Author: bruce */ #include "project.h" #include "LED.h" #include "systick.h" #include #include "hw4.h" #include #include "song_info.h" #include "song.h" #define PLAYING_STATE (1) #define PAUSED_STATE (2) #define STOPPED_STATE (3) void handle_input(char buffer[], uint8_t *state); void help(); void next(); void play(); void pause(); void pause_loop(); void stop(); void project_run() { char buffer[8]; char* cur = buffer; uint8_t state = STOPPED_STATE; help(); for (;;) { if (state == PAUSED_STATE) pause_loop(); char ch = read_and_print_char_nonblocking(); if (!ch) continue; if (ch == '\r') { *cur = 0; handle_input(buffer, &state); cur = buffer; continue; } *cur++ = ch; } } void handle_input(char buffer[], uint8_t *state) { if (!strcmp(buffer, "HELP")) { help(); } else if (!strcmp(buffer, "NEXT")) { next(); } else if (!strcmp(buffer, "PLAY")) { play(); *state = PLAYING_STATE; } else if (!strcmp(buffer, "PAUSE")) { pause(); *state = PAUSED_STATE; } else if (!strcmp(buffer, "STOP")) { stop(); *state = STOPPED_STATE; } else { printf("Unknown command \"%s\"\n\r", buffer); } } void 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"); } void 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); 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); } void play() { LED_On(); } void pause() { init_systick(); } void pause_loop() { if (check_systick()) { LED_Toggle(); } } void stop() { LED_Off(); }