/*
 * leds_drv.c
 *
 *  Created on: 10-10-2014
 *      Author: KJ
 */


#include "tdefs.h"
#include "misc.h"

#include "leds_drv.h"
#include "led.h"

u32 led_states = 0;
u32 led_blink_states = 0;

int leds_drv_initlog(void *arguments, void *logic)
{
//	struct events_reg_args *args = (struct events_reg_args *)arguments;
	struct leds_drv_logic *log = (struct leds_drv_logic *)logic;
	int i;

	log->leds_count=0;

	for(i=0;i<MAX_LOG_ELEMENTS;i++)
	{
		if(log_manager.log_element[i].flags & FUNCTION_IS_LED)
		{
			if((u8 *)&log->element_num[log->leds_count]>(u8 *)(log_manager.buf+sizeof(log_manager.buf)-sizeof(u16)))
				return -1;

			log->element_num[log->leds_count]=i;
			log->leds_count++;
		}
	}

//	led_states = 0;
//	led_blink_states = 0;

	return 0;
}

void leds_drv(void *arguments, void *logic)
{
	struct leds_drv_logic *log = (struct leds_drv_logic *)logic;
	struct led_args *l_args;
	struct led_logic *l_log;
	int i;

	for(i=0;i<log->leds_count;i++)
	{
		l_args = (struct led_args *)log_manager.log_element[log->element_num[i]].fun_args_ptr;
		l_log = (struct led_logic *)log_manager.log_element[log->element_num[i]].fun_log_ptr;

		if(*l_log->in_ptr & (1<<l_log->in_bit_no))
		{
			led_states|=(1<<l_args->params.led_num);
		}
		else
		{
			if(!l_args->params.until_clr)
				led_states&=~(1<<l_args->params.led_num);
			else if(*l_log->clr_ptr & (1<<l_log->clr_bit_no))
				led_states&=~(1<<l_args->params.led_num);
		}

		if(*l_log->blink_in_ptr & (1<<l_log->blink_in_bit_no))
		{
			led_blink_states|=(1<<l_args->params.led_num);
		}
		else
		{
			if(!l_args->params.until_clr)
				led_blink_states&=~(1<<l_args->params.led_num);
			else if(*l_log->clr_ptr & (1<<l_log->clr_bit_no))
				led_blink_states&=~(1<<l_args->params.led_num);
		}
	}
}
