ece312/final_project/lcd_disp/main.c

135 lines
3.0 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.
struct {
char* names[MAX_SONGS];
uint8_t num_songs;
} 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;
}
/** @brief Handles incoming Serial commands from the main player.
*/
ISR(USART_RX_vect) {
cli();
// Push incoming data to FIFO
fifo_push(UDR);
sei();
}
/** @brief Sets up various GPIO functions
*/
void gpio_init() {
// Initialise button pins as inputs
BUTTONS_DDR &= ~(1 << BUTTONS_LEFT_PIN) &
~(1 << BUTTONS_RIGHT_PIN) &
~(1 << BUTTONS_PLAYPAUSE_PIN);
// Turn on button pullups
BUTTONS_PORT |= (1 << BUTTONS_LEFT_PIN) |
(1 << BUTTONS_RIGHT_PIN) |
(1 << BUTTONS_PLAYPAUSE_PIN);
}
/** @brief Sets up USART for TX and RX with a baud rate of USART_BAUDRATE
*/
void usart_init() {
// initialize USART
UBRRL = UBRR_VALUE & 255;
UBRRH = UBRR_VALUE >> 8;
UCSRB = (1 << TXEN) | (1 << RXEN); // fire-up USART
UCSRC = (1 << UCSZ1) | (1 << UCSZ0); // fire-up USART
// Enable RX complete interrupt
UCSRB |= (1 << RXCIE);
}
void handle_left_press() {
}
void handle_right_press() {
}
// TODO NOT DONE
void handle_playpause_press() {
// If no song selected, select the correct one.
if (selected_song == -1) {
comms_select_file(display_song);
}
}
int main() {
// Scratch variables
uint8_t incoming_cmd = 0;
fifo_init();
gpio_init();
usart_init();
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
}
}
}