/*
* co_led.c - contains LED services
*
* Copyright (c) 2012-2023 emotas embedded communication GmbH
*-------------------------------------------------------------------
* $Id: co_led.c 49616 2023-09-26 14:22:08Z boe $
*
*
*-------------------------------------------------------------------
*
*
*/


/********************************************************************/
/**
* \brief LED handling according CiA 303-3
*
* \file co_led.c
* contains LED handling according CiA 303-3
*
*/


/* header of standard C - libraries
---------------------------------------------------------------------------*/
#include <stddef.h>

/* header of project specific types
---------------------------------------------------------------------------*/
#include <gen_define.h>

#include <co_datatype.h>
#include <co_timer.h>
#include <co_led.h>
#include "ico_indication.h"
#include "ico_led.h"

#ifdef CO_EVENT_LED

/* constant definitions
---------------------------------------------------------------------------*/

/* local defined data types
---------------------------------------------------------------------------*/
typedef struct {
	UNSIGNED8	blinkEntries;
	UNSIGNED8	blinkState[5];
} CO_NV_STORAGE LED_BLINKTABLE_T;

static const LED_BLINKTABLE_T	ledBlinkTables[] =  {
	{ 1u, { 0u } },			/* CO_LED_STATE_OFF */
	{ 1u, { 1u, 0u, 0u, 0u, 0u } },		/* CO_LED_STATE_FLICKERING */
	{ 3u, { 1u, 0u, 0u, 0u, 0u } },		/* CO_LED_STATE_FLASH_1 */
	{ 4u, { 1u, 1u, 0u, 0u, 0u } },		/* CO_LED_STATE_FLASH_2 */
	{ 5u, { 1u, 1u, 1u, 0u, 0u } },		/* CO_LED_STATE_FLASH_3 */
	{ 1u, { 1u, 0u, 0u, 0u, 0u } },		/* CO_LED_STATE_BLINKING */
	{ 1u, { 1u, 0u, 0u, 0u, 0u } }		/* CO_LED_STATE_ON */
};

typedef struct {
	CO_LED_STATE_T	state;
	BOOL_T			on;
	UNSIGNED8		blinkCnt;
	UNSIGNED8		blinkMaxCnt;
	const LED_BLINKTABLE_T	*blinkTable;
} CO_LED_T;


#define PRIORITY_SIZE	7u
static const CO_LED_STATE_T	priority[PRIORITY_SIZE]  = {
	CO_LED_STATE_OFF,			/**< led is off */
	CO_LED_STATE_FLICKERING,	/**< led is flickering */
	CO_LED_STATE_FLASH_3,		/**< led is flashing mode 3 */
	CO_LED_STATE_FLASH_1,		/**< led is flashing mode 1 */
	CO_LED_STATE_FLASH_2,		/**< led is flashing mode 2 */
	CO_LED_STATE_BLINKING,		/**< led is blinking */
	CO_LED_STATE_ON				/**< led is on */
};

/* list of external used functions, if not in headers
---------------------------------------------------------------------------*/
#ifdef CO_EVENT_STATIC_LED
extern CO_CONST CO_EVENT_LED_T coEventLedRedInd;
extern CO_CONST CO_EVENT_LED_T coEventLedGreenInd;
#endif /* CO_EVENT_STATIC_LED */


/* list of global defined functions
---------------------------------------------------------------------------*/

/* list of local defined functions
---------------------------------------------------------------------------*/
static void ledTimerInd(CO_LINE_DECL, void *pPtr);
static void startLedTimer(CO_LINE_DECL, BOOL_T	normal);
static void stopLedTimer(CO_LINE_DECL);
static void ledRedInd(CO_LINE_DECL, BOOL_T on);
static void ledGreenInd(CO_LINE_DECL, BOOL_T on);

/* external variables
---------------------------------------------------------------------------*/

/* global variables
---------------------------------------------------------------------------*/

/* local defined variables
---------------------------------------------------------------------------*/
static CO_LED_T		ledGreen[CO_LINE_ARRAY_DECL];
static CO_LED_T		ledRed[CO_LINE_ARRAY_DECL];
static BOOL_T		ledTimerOn[CO_LINE_ARRAY_DECL];
static CO_TIMER_T	ledTimer[CO_LINE_ARRAY_DECL];
static UNSIGNED8	ledCnt[CO_LINE_ARRAY_DECL];
static UNSIGNED32	saveErrorStates[CO_LINE_ARRAY_DECL];
#ifdef CO_EVENT_LED_CNT
static CO_EVENT_LED_T	ledGreenTable[CO_EVENT_LED_CNT];
static CO_EVENT_LED_T	ledRedTable[CO_EVENT_LED_CNT];
static UNSIGNED8	ledGreenTableCnt;
static UNSIGNED8	ledRedTableCnt;
#endif /* CO_EVENT_LED_CNT */


/***************************************************************************/
/**
* \brief coLedSetGreen - set green led to new state
*
* Set green LED to one of the following state:
*	- OFF,
*	- FLICKERING,
*	- FLASH_1, FLASH_2, FLASH_3,
*	- BLINKING,
*	- ON
*
*/

void coLedSetGreen(
		CO_LINE_TYPE	CO_LINE,			/**< can line */
		CO_LED_STATE_T	newLedState			/**< new led state */
	)
{
	/* disable flickering */
	if (ledGreen[CO_LINE].state == CO_LED_STATE_FLICKERING)  {
		/* disable timer */
		stopLedTimer(CO_LINE);

		/* reset red led */
		ledRed[CO_LINE].state = CO_LED_STATE_OFF;
	}
	/* enable flickering? */
	if (newLedState == CO_LED_STATE_FLICKERING)  {
		/* disable timer */
		stopLedTimer(CO_LINE);
	}

	/* set state */
	ledGreen[CO_LINE].state = newLedState;

	ledGreen[CO_LINE].blinkCnt = 0u;
	ledGreen[CO_LINE].blinkTable = &ledBlinkTables[newLedState];
	ledGreen[CO_LINE].blinkMaxCnt = ledGreen[CO_LINE].blinkTable->blinkEntries;

	/* is off, return */
	if (newLedState == CO_LED_STATE_OFF)  {
		if (ledRed[CO_LINE].state == CO_LED_STATE_OFF)  {
			stopLedTimer(CO_LINE);
		}

		/* if timer is off, call user indication immediately */
		if (ledTimerOn[CO_LINE] == CO_FALSE)  {
			ledGreenInd(CO_LINE, CO_FALSE);
		}

		return;
	}

	/* start timer if not active */
	if (ledTimerOn[CO_LINE] != CO_TRUE)  {
		if (newLedState == CO_LED_STATE_FLICKERING)  {
			startLedTimer(CO_LINE, CO_FALSE);
		} else {
			startLedTimer(CO_LINE, CO_TRUE);
		}
	}
}


/***************************************************************************/
/**
* \brief coLedSetRed - set red led to new state
*
* Set led LED to one of the following state:
*	OFF,
*	FLICKERING,
*	FLASH_1, FLASH_2, FLASH_3,
*	BLINKING,
*	ON
*
*/

void coLedSetRed(
		CO_LINE_TYPE	CO_LINE,
		CO_LED_STATE_T	newLedState			/**< new led state */
	)
{
	/* disable flickering */
	if (ledRed[CO_LINE].state == CO_LED_STATE_FLICKERING)  {
		/* disable timer */
		stopLedTimer(CO_LINE);

		/* reset red led */
		ledGreen[CO_LINE].state = CO_LED_STATE_OFF;
	}

	/* set state */
	ledRed[CO_LINE].state = newLedState;

	ledRed[CO_LINE].blinkCnt = 0u;
	ledRed[CO_LINE].blinkTable = &ledBlinkTables[newLedState];
	ledRed[CO_LINE].blinkMaxCnt = ledRed[CO_LINE].blinkTable->blinkEntries;

	/* is off, return */
	if (newLedState == CO_LED_STATE_OFF)  {
		if (ledGreen[CO_LINE].state == CO_LED_STATE_OFF)  {
			stopLedTimer(CO_LINE);
		}

		/* if timer is off, call user indication immediately */
		if (ledTimerOn[CO_LINE] == CO_FALSE)  {
			ledRedInd(CO_LINE, CO_FALSE);
		}

		return;
	}

	/* start timer if not active */
	if (ledTimerOn[CO_LINE] != CO_TRUE)  {
		if (newLedState == CO_LED_STATE_FLICKERING)  {
			startLedTimer(CO_LINE, CO_FALSE);
		} else {
			startLedTimer(CO_LINE, CO_TRUE);
		}
	}

}


/***************************************************************************/
/**
* \brief coLedSetState - set led state
*
* Set the error led to special state
*	OFF,
*	FLICKERING,
*	FLASH_1, FLASH_2, FLASH_3,
*	BLINKING,
*	ON
*
* All states are saved. Only the highest prior state is displayed.
* If the highest state is reset, the next state is displayed.
*
*/
void coLedSetState(
		CO_LINE_TYPE	CO_LINE,		/**< can line */
		CO_LED_STATE_T	newState,		/**< new state */
		BOOL_T			on				/**< set state to on/off */
	)
{
UNSIGNED8	i;

	/* set state on/off */
	if (on == CO_TRUE)  {
		saveErrorStates[CO_LINE] |= (1ul << (UNSIGNED32)newState);
	} else {
		saveErrorStates[CO_LINE] &= ~(1ul << (UNSIGNED32)newState);
	}

	if (saveErrorStates[CO_LINE] == 0u)  {
		coLedSetRed(CO_LINE, CO_LED_STATE_OFF);
		return;
	}

	/* set led to the highest priority state */
	i = PRIORITY_SIZE;
	while (i > 0u)  {
		i--;
		if ((saveErrorStates[CO_LINE] & (1ul << (UNSIGNED32)priority[i])) != 0ul)  {
			/* state found, set led */
			coLedSetRed(CO_LINE, priority[i]);
			break;
		}
	}
}


/***************************************************************************/
/**
* \internal
*
* \brief startLedTimer - start led timer
*
* Timer interval depends on the parameter normal:<br>
* If normal == CO_TRUE, use 200 msec<br>
* If normal == CO_FALSE, use 50 msec
*
*
*/
static void startLedTimer(
		CO_LINE_TYPE	CO_LINE,	/* can line */
		BOOL_T	normal				/* normal/fast */
	)
{
UNSIGNED32	takt;

	if (normal == CO_TRUE)  {
		/* 200 msec */
		takt = 200000ul;
	} else  {
		takt = 50000ul;
	}

	(void)coTimerStart(CO_LINE, &ledTimer[CO_LINE], takt, ledTimerInd, NULL,
		CO_TIMER_ATTR_ROUNDUP_CYCLIC); /*lint !e960 */
	/* Derogation MisraC2004 R.16.9 function identifier used without '&'
	 * or parenthesized parameter */

	ledTimerOn[CO_LINE] = CO_TRUE;
	ledCnt[CO_LINE] = 0u;
}


/***************************************************************************/
/**
* \internal
*
* \brief stopLedTimer - stop led timer
*
*
*/
static void stopLedTimer(
		CO_LINE_TYPE	CO_LINE		/* can line */
	)
{
	(void)coTimerStop(CO_LINE, &ledTimer[CO_LINE]);

	ledTimerOn[CO_LINE] = CO_FALSE;
	ledCnt[CO_LINE] = 0u;
}


/***************************************************************************/
/*
* \internal
*
* \brief ledTimerInd - led timer routine
*
*
* \return none
*
*/
static void ledTimerInd(
		CO_LINE_TYPE	CO_LINE,	/* can line */
		void			*pPtr
	)
{
BOOL_T		green = ledGreen[CO_LINE].on;
BOOL_T		red = ledRed[CO_LINE].on;
CO_LED_T	*pGreen = &ledGreen[CO_LINE];
CO_LED_T	*pRed = &ledRed[CO_LINE];
(void)pPtr;

	if ((ledCnt[CO_LINE] & 1u) == 0u)  {
		/* switch off green led, except state is on */
		if (pGreen->state != CO_LED_STATE_ON)  {
			green = CO_FALSE;
		}
		/* work red led */
		if (pRed->blinkTable->blinkState[pRed->blinkCnt] != 0u)  {
			red = CO_TRUE;
		}
		pRed->blinkCnt++;
		if (pRed->blinkCnt == pRed->blinkMaxCnt)  {
			pRed->blinkCnt = 0u;
		}
	} else {
		/* switch off red led */
		if (pRed->state != CO_LED_STATE_ON)  {
			red = CO_FALSE;
		}

		/* work green led */
		if (pGreen->blinkTable->blinkState[pGreen->blinkCnt] != 0u)  {
			green = CO_TRUE;
		}
		pGreen->blinkCnt++;
		if (pGreen->blinkCnt == pGreen->blinkMaxCnt)  {
			pGreen->blinkCnt = 0u;
		}
	}

	if (pGreen->on != green)  {
		/* call user indication */
		ledGreenInd(CO_LINE, green);
		pGreen->on = green;
	}
	if (pRed->on != red)  {
		/* call user indication */
		ledRedInd(CO_LINE, red);
		pRed->on = red;
	}

	ledCnt[CO_LINE]++;
}


/***************************************************************************/
/*
* \internal
*
* \brief ledRedInd - call user indication
*
*
* \return none
*
*/
static void ledRedInd(
		CO_LINE_TYPE	CO_LINE,		/* can line */
		BOOL_T	on
	)
{
#ifdef CO_EVENT_LED_CNT
UNSIGNED8	cnt;

	cnt = ledRedTableCnt;
	while (cnt > 0u)  {
		cnt--;
		/* call user indication */
		ledRedTable[cnt](CO_LINE, on);
	}
#endif /* CO_EVENT_LED_CNT */

#ifdef CO_EVENT_STATIC_LED
	coEventLedRedInd(CO_LINE, on);
#endif /* CO_EVENT_STATIC_LED */
}


/***************************************************************************/
/*
* \internal
*
* \brief ledRedInd - call user indication
*
*
* \return none
*
*/
static void ledGreenInd(
		CO_LINE_TYPE	CO_LINE,		/* can line */
		BOOL_T	on
	)
{
#ifdef CO_EVENT_LED_CNT
UNSIGNED8	cnt;

	cnt = ledGreenTableCnt;
	while (cnt > 0u)  {
		cnt--;
		/* call user indication */
		ledGreenTable[cnt](CO_LINE, on);
	}
#endif /* CO_EVENT_LED_CNT */

#ifdef CO_EVENT_STATIC_LED
	coEventLedGreenInd(CO_LINE, on);
#endif /* CO_EVENT_STATIC_LED */
}


#ifdef CO_EVENT_LED_CNT
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
/**
* \brief coEventRegister_LED_GREEN - register for green LED
*
* \return RET_T
*/

RET_T coEventRegister_LED_GREEN(
		CO_EVENT_LED_T pFunction	/**< pointer to function */
	)
{
CO_LINE_TYPE	CO_LINE;

	/* set new indication function as first at the list */
	if (ledGreenTableCnt >= CO_EVENT_LED)  {
		return(RET_EVENT_NO_RESSOURCE);
	}

	ledGreenTable[ledGreenTableCnt] = pFunction;	/* save function pointer */
	ledGreenTableCnt++;

	/* set actual led state */
	for (CO_LINE = 0u; CO_LINE < CO_LINE_ARRAY_DECL; CO_LINE++)
	{
		ledGreenInd(CO_LINE, ledGreen[CO_LINE].on);
	}

	return(RET_OK);
}


/***************************************************************************/
/**
* \brief coEventRegister_LED_RED - register for red LED

* Register application function for controlling of LED state
*
* \return RET_T
*
*/

RET_T coEventRegister_LED_RED(
		CO_EVENT_LED_T pFunction	/**< pointer to function */
	)
{
CO_LINE_TYPE	CO_LINE;

	/* set new indication function as first at the list */
	if (ledRedTableCnt >= CO_EVENT_LED)  {
		return(RET_EVENT_NO_RESSOURCE);
	}

	ledRedTable[ledRedTableCnt] = pFunction;	/* save function pointer */
	ledRedTableCnt++;

	/* set actual led state */
	for (CO_LINE = 0u; CO_LINE < CO_LINE_ARRAY_DECL; CO_LINE++)
	{
		ledRedInd(CO_LINE, ledRed[CO_LINE].on);
	}

	return(RET_OK);
}
#endif /* CO_EVENT_LED_CNT */


/***************************************************************************/
/*
* \internal
*
* \brief icoLedVarInit - init LED variables
*
* Init LED variables
*
* \return none
*
*/
void icoLedVarInit(
		void
	)
{
CO_LINE_TYPE	CO_LINE;

#ifdef CO_EVENT_LED_CNT
	ledGreenTableCnt = 0u;
	ledRedTableCnt = 0u;
#endif /* CO_EVENT_LED_CNT */
	for (CO_LINE = 0u; CO_LINE < CO_LINE_ARRAY_DECL; CO_LINE++)
	{
		ledTimerOn[CO_LINE] = CO_FALSE;
		ledCnt[CO_LINE] = 0u;
		saveErrorStates[CO_LINE] = 0ul;
	}
}


/***************************************************************************/
/*
* \brief coLedInit - init LED functionality
*
* Init LED functionality
*
* \return none
*
*/
void coLedInit(
		CO_LINE_TYPE	CO_LINE		/* can line */
	)
{
	coLedSetGreen(CO_LINE, CO_LED_STATE_OFF);
	coLedSetRed(CO_LINE, CO_LED_STATE_OFF);
}

#endif /* CO_EVENT_LED */
