/*
 * application.c
 *
 *  Created on: 22.09.2022
 *      Author: jonas
 */

#include "fsl_debug_console.h"
#include "platform.h"
#include "application.h"
#include "McuWait.h"
#include "McuLED.h"
#include "McuLog.h"
#include "debounce.h"

#define APP_DEBUG

/* blue led pins */
#define LED_BLUE_GPIO GPIOC
#define LED_BLUE_PORT PORTC
#define LED_BLUE_PIN 2U

/* vars */
static McuLED_Handle_t LED_blue;
static McuGPIO_Handle_t GPIO_PowerRaspi;
static McuGPIO_Handle_t GPIO_RaspiShutdownIndicator;
static uint32_t taskParameter1 = 5;
static TaskHandle_t appTaskHandle = NULL;
static QueueHandle_t eventQueueHandle = NULL;

/* function declaration */

/* Application initialization */
void App_Init(void){
	/* configure blue LED */
	McuLED_Config_t config;
	McuLED_GetDefaultConfig(&config);
	config.isLowActive = true;
	config.isOnInit = false;
	config.hw.gpio = LED_BLUE_GPIO;
	config.hw.port = LED_BLUE_PORT;
	config.hw.pin = LED_BLUE_PIN;
	LED_blue = McuLED_InitLed(&config);

	/* configure gpio raspi power off GPIO  (GP_0)*/
	McuGPIO_Config_t config_gp0;
	McuGPIO_GetDefaultConfig(&config_gp0);
	config_gp0.hw.gpio = GPIOA;
	config_gp0.hw.port = PORTA;
	config_gp0.hw.pin = 1U;
	config_gp0.isInput = false;      // Output
	config_gp0.isHighOnInit = true;  // active low
	config_gp0.hw.pull = McuGPIO_PULL_DISABLE;  // disable pull
	GPIO_PowerRaspi = McuGPIO_InitGPIO(&config_gp0);

	/* configure gpio raspi shutdown indicator  (GP_1) */
	McuGPIO_Config_t config_gp1;
	McuGPIO_GetDefaultConfig(&config_gp1);
	config_gp1.hw.gpio = GPIOA;
	config_gp1.hw.port = PORTA;
	config_gp1.hw.pin = 2U;
	config_gp1.isInput = true;       // input
	config_gp1.hw.pull = McuGPIO_PULL_DISABLE;  // disable pull
	GPIO_RaspiShutdownIndicator = McuGPIO_InitGPIO(&config_gp1);

	// get the event queue handle from debounce
	eventQueueHandle = Debounce_GetEventQueueHandle();
}

/* App Task */
static void App_Task(void* pv){
	McuLog_info("Application Task starting");


    while(1){

    	Debounce_event_e event = Event_None;

    	// get event data from queue if it can be reached
    	if(eventQueueHandle != NULL)
    	{
    		// ignore errors, since it is very well possible that the queue is empty
    		xQueueReceive(eventQueueHandle, &event, pdMS_TO_TICKS(20));
    	}

    	// turn off raspberry pi if requested by button press
    	if(event == Button_Up_Pressed){
    		McuGPIO_SetLow(GPIO_PowerRaspi);
    		vTaskDelay(pdMS_TO_TICKS(1000));
    		McuGPIO_SetHigh(GPIO_PowerRaspi);
    	}

    	// turn on led if the power off indicator is on (Raspi is shut down)
    	if(McuGPIO_IsHigh(GPIO_RaspiShutdownIndicator)){
    		McuLED_On(LED_blue);
    	}else{
    		McuLED_Off(LED_blue);
    	}


    	vTaskDelay(pdMS_TO_TICKS(100));
    }
}

/* Application run */
void App_Run(void){
	BaseType_t res;

	res = xTaskCreate(		App_Task,
							"appTask",
							900/sizeof(StackType_t),
							&taskParameter1,
							tskIDLE_PRIORITY,
							&appTaskHandle);

	if(res != pdPASS) // task creation not successful?
	{
		PRINTF("Task creation of app task failed");
		for(;;) {} // Endless loop
	}

    vTaskStartScheduler();
}


/* Application de-initialization */
void App_Deinit(void){

}
