/*
 * project.c
 *
 *  Created on: Jan 8, 2023
 *      Author: bruce
 */

#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stm32l4xx.h>
#include "project.h"
#include "player_actions.h"
#include "hw4.h"
#include "local_control.h"
#include "remote_control.h"
#include "GPIO.h"
#include "project.h"
#include "systick.h"

static uint8_t mode = REMOTE_MODE;

/**
 * Returns the mode to other files
 */
uint8_t project_get_mode() {
	return mode;
}

/**
 * Runs the project
 */
void project_run() {
	init_systick();
	local_control_init();
	remote_control_init();

	GPIO_Init();
	NVIC_EnableIRQ(EXTI15_10_IRQn);

	for (;;) {
		switch (mode) {
		case REMOTE_MODE:
			remote_control_loop(); break;
		case LOCAL_MODE:
			local_control_loop(); break;
		}
	}
}

/**
 * Listen for blue button presses
 */
void EXTI15_10_IRQHandler() {
    if (~EXTI->PR1 & EXTI_PR1_PIF13)
    	return;

    EXTI->PR1 |= EXTI_PR1_PIF13;

	mode = !mode;
	switch (mode) {
	case REMOTE_MODE:
		puts("\r\n***REMOTE MODE ACTIVE***\r"); break;
	case LOCAL_MODE:
		puts("\r\n***MANUAL OVERRIDE MODE ACTIVE***\r"); break;
	}
}



