/*
 * Copyright (c) 2023 Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @file   : operation.c
 * @date   : Set 26, 2023
 * @author : Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>
 * @version	v1.0.0
 */

/********************** inclusions *******************************************/
/* Project includes */
#include "main.h"

/* Demo includes */
#include "logger.h"
#include "dwt.h"

/* Application & Tasks includes */
#include "board.h"
#include "app.h"
#include "operation_attribute.h"

/********************** macros and definitions *******************************/
#define BUTTON_UP		0ul
#define BUTTON_DOWN		1ul

/********************** internal data declaration ****************************/
const led_cfg_t led_cfg_list[] = {
	{LED_A,  LED_A_PORT,  LED_A_PIN, LED_A_ON,  LED_A_OFF},
	{LED_B,  LED_B_PORT,  LED_B_PIN, LED_B_ON,  LED_B_OFF},
	{LED_C,  LED_C_PORT,  LED_C_PIN, LED_C_ON,  LED_C_OFF}
};

const button_cfg_t button_cfg_list[] = {
	{BTN_A,  BTN_A_PORT,  BTN_A_PIN,  BTN_A_PRESSED},
	{BTN_B,  BTN_B_PORT,  BTN_B_PIN,  BTN_B_PRESSED},
	{BTN_C,  BTN_C_PORT,  BTN_C_PIN,  BTN_C_PRESSED}
};

/********************** internal functions declaration ***********************/

/********************** internal data definition *****************************/

/********************** external data declaration ****************************/

/********************** external functions definition ************************/
uint32_t op_button_get(uint32_t button_id)
{
	uint32_t index = button_id;
	uint32_t state;
	const button_cfg_t *p_button_cfg;

	p_button_cfg = &button_cfg_list[index];
	if (p_button_cfg->pressed == HAL_GPIO_ReadPin(p_button_cfg->gpio_port, p_button_cfg->pin))
	{
		state = BUTTON_DOWN;
	}
	else
	{
		state = BUTTON_UP;
	}

	return(state);
}

void op_led_on(uint32_t led_id)
{
	uint32_t index = led_id;
	const led_cfg_t *p_led_cfg;

	p_led_cfg = &led_cfg_list[index];
	HAL_GPIO_WritePin(p_led_cfg->gpio_port, p_led_cfg->pin, p_led_cfg->on);
}

void op_led_off(uint32_t led_id)
{
	uint32_t index = led_id;
	const led_cfg_t *p_led_cfg;

	p_led_cfg = &led_cfg_list[index];
	HAL_GPIO_WritePin(p_led_cfg->gpio_port, p_led_cfg->pin, p_led_cfg->off);
}

/********************** end of file ******************************************/
