159 lines
3.9 KiB
C
159 lines
3.9 KiB
C
#include "main.h"
|
|
|
|
/* ---- Global Flags ---- */
|
|
int16_t display_song = -1; //! ID of the song currently being displayed, -1 if song list is not initialised
|
|
int16_t selected_song = -1; //! ID of the selected song. -1 if no song is selected.
|
|
bool update_display = false; //! flag on whether or not to update display
|
|
|
|
struct {
|
|
char* names[MAX_SONGS]; //! array of all song names, names are allocated on heap
|
|
uint8_t num_songs; //! Number of songs loaded into display memory
|
|
bool playing; //! Whether the currently selected song is playing
|
|
} songs;
|
|
|
|
/** @brief Adds song to list of songs
|
|
*
|
|
* Allocates a new piece of memory for the name.
|
|
*/
|
|
/*
|
|
void add_song(char* name) {
|
|
// Error out on too many songs
|
|
if (songs.num_songs == MAX_SONGS) {
|
|
lcd_gotoxy(0,0);
|
|
lcd_puts("Too many songs!");
|
|
while(1);
|
|
}
|
|
|
|
// Allocate memory to store name, and increase number
|
|
uint8_t name_len = strlen(name);
|
|
songs.names[songs.num_songs] = malloc(name_len);
|
|
songs.num_songs++;
|
|
}
|
|
*/
|
|
|
|
/** @brief Clears list of songs
|
|
*
|
|
* Frees all memory used for name storage
|
|
*/
|
|
/*
|
|
void clear_songs() {
|
|
// Free all memory
|
|
for (uint8_t i = 0; i < songs.num_songs; i++) {
|
|
free(songs.names[i]);
|
|
}
|
|
|
|
songs.num_songs = 0;
|
|
}
|
|
*/
|
|
|
|
|
|
void handle_left_press() {
|
|
if (songs.num_songs == 0) {
|
|
lcd_clrscr();
|
|
lcd_puts("No songs to play!");
|
|
} else {
|
|
// Choose lower ID song. Wrap around.
|
|
display_song--;
|
|
if (display_song == 0) {
|
|
display_song = songs.num_songs - 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void handle_right_press() {
|
|
if (songs.num_songs == 0) {
|
|
lcd_clrscr();
|
|
lcd_puts("No songs to play!");
|
|
} else {
|
|
// Choose higher ID song. Wrap around.
|
|
display_song++;
|
|
if (display_song == songs.num_songs) {
|
|
display_song = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** @brief Handles playpause button press.
|
|
*
|
|
* Based on selected song and displayed song, will select new song
|
|
* or play/pause song.
|
|
*/
|
|
void handle_playpause_press() {
|
|
// If there are no songs displayed, error
|
|
if (display_song == -1) {
|
|
lcd_clrscr();
|
|
lcd_puts("No songs to play!");
|
|
return;
|
|
}
|
|
|
|
// If no song selected, select the correct one.
|
|
if (selected_song == -1) {
|
|
comms_select_file(display_song);
|
|
return;
|
|
}
|
|
|
|
// Song selected, send play/pause
|
|
if (songs.playing) {
|
|
comms_pause();
|
|
songs.playing = false;
|
|
} else {
|
|
comms_play();
|
|
songs.playing = true;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
int main() {
|
|
// Scratch variables
|
|
uint8_t incoming_cmd = 0;
|
|
|
|
fifo_init();
|
|
gpio_init();
|
|
usart_init();
|
|
|
|
lcd_init(LCD_DISP_ON);
|
|
lcd_clrscr();
|
|
lcd_gotoxy(0,0);
|
|
|
|
// Main loop
|
|
while (1) {
|
|
// Handle incoming messages
|
|
if (fifo_pop(&incoming_cmd) == FIFO_SUCCESS) {
|
|
|
|
}
|
|
|
|
// Handle "left" button press
|
|
if (BUTTONS_PORT & (1 << BUTTONS_LEFT_PIN)) {
|
|
handle_left_press();
|
|
_delay_ms(100); // Delay to debounce
|
|
}
|
|
|
|
// Handle "right" button press
|
|
if (BUTTONS_PORT & (1 << BUTTONS_LEFT_PIN)) {
|
|
handle_left_press();
|
|
_delay_ms(100); // Delay to debounce
|
|
}
|
|
|
|
// Handle play/pause button press
|
|
if (BUTTONS_PORT & (1 << BUTTONS_LEFT_PIN)) {
|
|
handle_left_press();
|
|
_delay_ms(100); // Delay to debounce
|
|
}
|
|
|
|
// Update display periodically
|
|
if (update_display) {
|
|
lcd_clrscr();
|
|
lcd_gotoxy(0,0);
|
|
lcd_puts(songs.names[display_song]);
|
|
lcd_gotoxy(0, 1);
|
|
//char tmp_string[17];
|
|
//sprintf(tmp_string, "%1d %2d/%2d",
|
|
// display_song, display_song, songs.num_songs);
|
|
//lcd_puts(tmp_string);
|
|
|
|
update_display = false;
|
|
}
|
|
}
|
|
|
|
} |