/*************************************************  
  File name     : hal_interface_lightsensor.c
  Module        : 
  Author        : amir 
  Version       : 0.1   
  Created on    : 2022-02-07  
  Description   :
      hal interface for lightsensor

  Modify History:   
  1. Date:        Author:         Modification:
*************************************************/
#include "hal_interface_lightsensor.h"
#include "hal_interface_ircut.h"
#include "hlog.h"

#define TAG "TAG_HAL_LIGHTSESNOR"

#define PERIOD_TO_GET_ADC	 ((uint16_t)(500))//ms
#define IRLED_OPEN 		 	 ((uint8_t)(1))


static void* hal_lightsensor_entry(void *param){
	uint32_t adc;
	if (hal_lightsensor_get_adc(&adc) == 0){
		IRCUT_MODE cur_mode = hal_ircut_mode_get();
		if (adc < NIGHT_ADC && cur_mode != IRCUT_NIGHT){//to night
			kpacket *packet = kpacket_new(EVENT_HAL_DAYNIGHT, sizeof(uint32_t));
			*packet->box->payload = IRCUT_NIGHT;
			hal_pub(packet);
			atomic_dec(packet);
		}else if (adc > DAY_ADC && cur_mode != IRCUT_DAY){//to day
			kpacket *packet = kpacket_new(EVENT_HAL_DAYNIGHT, sizeof(uint32_t));
			*packet->box->payload = IRCUT_DAY;
			hal_pub(packet);
			atomic_dec(packet);
		}
		hlogd("%s adc %u cur_mode:%d",__func__, adc, cur_mode);
	}else{
		hlogd("%s hal_lightsensor_get_adc err",__func__);
	}
	return 0;
}


uint32_t hal_lightsensor_start(){
    hal_timer_start(hal_lightsensor_entry, NULL, PERIOD_TO_GET_ADC);
}

uint32_t hal_lightsensor_stop(){
    hal_timer_stop(hal_lightsensor_entry);
}

