/*
 * Core1.c
 *
 * Created: Wed Dec 08 2021 12:57:45
 *  Author Chris
 */
// ============================================================================================
// Includes
#include "Core1.h"
#include "Command_Definition.h"

#include "UART0.h"
#include "Core1_ADC.h"
#include "Core1_LED_Enable.h"
#include "Core1_LED_Control.h"
#include "Core1_MIDI_Receiver.h"
#include "Core1_Light_Controller.h"


// ============================================================================================
// Defines


// ============================================================================================
// Variables


// ============================================================================================
// Function Declarations
void Core1_Parse_Command(uint8_t command, uint8_t parameter, int16_t value);
void Core1_Send_Multicore_Answer(uint8_t command, uint8_t parameter, int16_t value);


/*******************************************************************
	Interrupt Service Routines
*******************************************************************/


/*******************************************************************
	Functions
*******************************************************************/
void Core1_Main(void)
{
	// Analog Converter	=============================================
	Core1_ADC_Init();
	
	// LED PWR Enable	=============================================
	Core1_LED_Enable_Init();

	// PWM / LED Control	=========================================
	Core1_LED_Control_Init();

	// MIDI Receiver	=============================================
	Core1_MIDI_Receiver_Init();

	// Light Controller	=============================================
	Core1_Light_Controller_Init();

	// Main Loop	=================================================
	while(1)
	{
		if(multicore_fifo_rvalid())
		{
			Command_u Command;
			Command.Data = multicore_fifo_pop_blocking();

			Core1_Parse_Command(Command.Fields.Command, Command.Fields.Parameter, Command.Fields.Value);
		}

		Core1_MIDI_Receiver_Process();
		Core1_Light_Controller_Tick();
		Core1_LED_Enable_Tick();
	}
}


/*******************************************************************
	Internal Functions
*******************************************************************/
void Core1_Parse_Command(uint8_t command, uint8_t parameter, int16_t value)
{
	switch (command)
	{
		/////////////
		// General //
		/////////////
		case MULTICORE_COMMAND_SET_MIDI_TO_LIGHT_ENABLED:	Core1_Light_Controller_Set_MIDI_To_Light_Enabled	(value > 0); 		break;


		//////////////////////////
		// Direct Light Control //
		//////////////////////////
		case MULTICORE_COMMAND_SET_DIRECT_RED:				Core1_LED_Control_Set_DC_Target	(LED_Channel_1, R,	(uint8_t)value); 	break;
		case MULTICORE_COMMAND_SET_DIRECT_GREEN:			Core1_LED_Control_Set_DC_Target	(LED_Channel_1, G, 	(uint8_t)value); 	break;
		case MULTICORE_COMMAND_SET_DIRECT_BLUE:				Core1_LED_Control_Set_DC_Target	(LED_Channel_1, B, 	(uint8_t)value); 	break;
		case MULTICORE_COMMAND_SET_DIRECT_FADE_SPEED:		Core1_LED_Control_Set_Fade_Speed(LED_Channel_1, 	(uint8_t)value); 	break;


		/////////////////////////
		// Voltage Supervision //
		/////////////////////////
		case MULTICORE_COMMAND_SET_INA260_BUSVOLTAGE:		Core1_LED_Enable_Update_INA260_BusVoltage((uint16_t)value);				break;


		///////////////////
		// Get Functions //
		///////////////////
		case MULTICORE_COMMAND_GET_ANALOG_VOLTAGE:			Core1_Send_Multicore_Answer(MULTICORE_COMMAND_GET_ANALOG_VOLTAGE	, 0,  (int16_t)Core1_ADC_Get_Result_mV());		break;
		case MULTICORE_COMMAND_GET_LED_POWER_ERROR:			Core1_Send_Multicore_Answer(MULTICORE_COMMAND_GET_LED_POWER_ERROR	, 0,  (int16_t)Core1_LED_Enable_Get_Error()); 	break;

		default:	break;
	}
}

void Core1_Send_Multicore_Answer(uint8_t command, uint8_t parameter, int16_t value)
{
	Command_u Command;
	Command.Fields.Command 		= command;
	Command.Fields.Parameter 	= parameter;
	Command.Fields.Value		= value;

	multicore_fifo_push_blocking(Command.Data);
}
