#ifndef __MEASURE_H
#define	__MEASURE_H

#include <stdio.h>
#include <stdbool.h>
#include "nrf.h"
#include "boards.h"

/**********电流定义规格********/
#define EX9R_1         //0.1-0.5A
//#define EX9R_2	    //0.4-2A
//#define EX9R_3	   //1.8-9A
//#define EX9R_4	  //8-40A

/**********电压定义规格*********/
//#define EX9R_48V         //0.1-0.5A
//#define EX9R_110V	    //0.4-2A
#define EX9R_220V	   //1.8-9A

/**********电流规格参数量********/
#ifdef EX9R_1
#define AD_coefficient 29
#define SetCurrent_Min 543600
#define SetCurrent_Add 117
#define CalibrationCurrent 30
#define GroundProtectValue 13
#define StopCurrent 16

#define Ie_MIN 10
#define Ie_MAX 50
#endif

#ifdef EX9R_2
#define AD_coefficient 77
#define SetCurrent_Min 217400
#define SetCurrent_Add 47
#define CalibrationCurrent 12
#define GroundProtectValue 45
#define StopCurrent 10

#define Ie_MIN 4
#define Ie_MAX 20
#endif

#ifdef EX9R_3
#define AD_coefficient 20
#define SetCurrent_Min 978600
#define SetCurrent_Add 212
#define CalibrationCurrent 54
#define GroundProtectValue  8
#define StopCurrent 10

#define Ie_MIN 18
#define Ie_MAX 90
#endif

#ifdef EX9R_4
#define AD_coefficient 40
#define SetCurrent_Min 434100
#define SetCurrent_Add 94
#define CalibrationCurrent 24
#define GroundProtectValue 20
#define StopCurrent 30	//20201203

#define Ie_MIN 8
#define Ie_MAX 40
#endif

/**********电压规格参数量********/
#ifdef EX9R_48V
#define VotageKeep 2
#endif

#ifdef EX9R_110V
#define VotageKeep 2
#endif

#ifdef EX9R_220V
#define VotageKeep 2
#endif



/***   pin脚组态   ***/

#define  CT3_IN    31    // ADC_IN 		电流采集通道
#define  CT2_IN    30    // ADC_IN 		电流采集通道
#define  CT1_IN    29    // ADC_IN 		电流采集通道
#define  GF_IN     28    // ADC_IN 		电流采集通道

#define  RUN_LED   27		// GPIO_OUT    运行LED
#define  FALUT_LED 26		// GPIO_OUT    错误LED
#define  M_RESET   25		// GPIO_IN     复位按键

#define  B_LEVEL   4			// ADC_IN      额定电流等级输入通道
#define  A_LEVEL   5			// ADC_IN      脱扣等级输入通道

#define  T1_LEVEL  2			// GPIO_IN     不平衡率输入通道
#define  T2_LEVEL  3			// GPIO_IN     接地等级输入通道
#define  STALL     6			// GPIO_IN     缺相开关输入通道
#define  IMBA      7			// GPIO_IN     堵转等级输入通道

#define  Relay     8		  // GPIO_OUT    继电器输出口
#define  STOP      10		  // GPIO_IN     停止/测试按键

#define  CORRECT   13		  // GPIO_IN    系数校正开关-按键
#define  BLE_KEY   17		  // GPIO_IN    蓝牙模式开关

extern uint16_t B_LEVEL_BLE;	//       额定电流等级蓝牙输入
extern uint16_t A_LEVEL_BLE;	//       脱扣等级蓝牙输入

extern uint16_t T1_LEVEL_BLE;    //    不平衡率蓝牙
extern uint16_t T2_LEVEL_BLE;    //    阻塞保护蓝牙
extern uint16_t STALL_BLE; 	//    堵转保护开关蓝牙
extern uint16_t IMBA_BLE ;		//    自动复位蓝牙
extern uint16_t Grand_BLE;     //    接地保护蓝牙
	
extern uint16_t Resetbit_BLE;  //	  蓝牙复位开关，默认为1.按下为0
extern uint16_t Stopbit_BLE;	//	  蓝牙停止按键，默认为1，按下为0

//IMBA是自动复位拨码


#define RunLedOn 		    nrf_gpio_pin_clear(27)     	//running led
#define RunLedOff 			nrf_gpio_pin_set(27)

#define FaultLed1On  	    nrf_gpio_pin_clear(26)			//fault led
#define FaultLed1Off 	    nrf_gpio_pin_set(26)

#define RelayOn             nrf_gpio_pin_clear(8)       	//relay  on or off
#define RelayOff 		    nrf_gpio_pin_set(8)

#define m_reset_key	        nrf_gpio_pin_read(M_RESET)
#define stop_key   	        nrf_gpio_pin_read(STOP)


void measureTask(void);
void countratedcurrent_BLE(uint32_t ratedcurrent1);//整定电流
void counttriplevel_BLE(uint32_t trip1);//脱扣等级(Ctime单位min,Dtime单位20mS)

extern uint32_t result_buffer[5][16];
extern volatile uint32_t ADC_done;

extern uint32_t FaultLEDFlashCount;    
extern uint32_t currFact[3];
extern uint32_t Ie;
extern uint32_t Dtime;
extern uint32_t Stalltime;
extern uint8_t GNDProtectEn;
extern uint8_t LEDRunFlag;

#endif 

#ifndef __MEASURE_H
#define	__MEASURE_H


#include <stdio.h>
#include <stdbool.h>
#include "nrf.h"
#include "boards.h"


#define EX9R_4	

#ifdef EX9R_4
#define AD_coefficient 40
#define SetCurrent_Min 434100
#define SetCurrent_Add 94
#define CalibrationCurrent 24
#define GroundProtectValue 20
#define StopCurrent 30	//20201203

#define Ie_MIN 8
#define Ie_MAX 40
#endif


/***   pin脚组态   ***/

#define  CT3_IN    31    // ADC_IN 		电流采集通道
#define  CT2_IN    30    // ADC_IN 		电流采集通道
#define  CT1_IN    29    // ADC_IN 		电流采集通道
#define  GF_IN     28    // ADC_IN 		电流采集通道

#define  RUN_LED   27		// GPIO_OUT    运行LED
#define  FALUT_LED 26		// GPIO_OUT    错误LED
#define  M_RESET   25		// GPIO_IN     复位按键

#define  T1_LEVEL  2			// GPIO_IN     不平衡率输入通道
#define  T2_LEVEL  3			// GPIO_IN     接地等级输入通道
#define  B_LEVEL   4			// ADC_IN      额定电流等级输入通道
#define  A_LEVEL   5			// ADC_IN      脱扣等级输入通道

#define  STALL     6			// GPIO_IN     缺相开关输入通道
#define  IMBA      7			// GPIO_IN     堵转等级输入通道

#define  Relay     8		  // GPIO_OUT    继电器输出口
#define  STOP      10		// GPIO_IN     停止/测试按键

#define  CORRECT   13		// GPIO_OUT    系数校正开关-按键 

/*****************    测量及保护函数    *******************/
/*
测量

measureImbalance     不平衡率
counttriplevel			 脱扣等级
countratedcurrent    整定电流


保护功能
PhaseLossProtect     缺相保护
GroundProtect				 接地保护
ImbalanceProtect     不平衡保护
JamProtect           阻塞保护
StallProtect         堵转保护
OverloadProtect      过载保护

默认开启缺相保护------原phase保护为不平衡保护



*/
#define RunLedOn  nrf_gpio_pin_clear(27)     	//running led
#define RunLedOff  nrf_gpio_pin_set(27)

#define FaultLedOn  nrf_gpio_pin_clear(26)			//fault led
#define FaultLedOff  nrf_gpio_pin_set(26)

#define RelayOn  nrf_gpio_pin_clear(8)       	//relay  on or off
#define RelayOff  nrf_gpio_pin_set(8)





extern volatile uint32_t ADC_done;
extern uint32_t fault_led_flash_count;//灯闪烁时间计数
extern uint8_t  led_run_flag;//0:主回路接通，1：检测到电流


extern uint32_t imbalance_current;
extern uint32_t current_value[3];
extern uint32_t gf_current_max;
extern uint32_t rated_current_level;
extern uint32_t    Ie;

extern uint32_t    Ctime;	   
extern uint32_t		 Dtime;	



static uint32_t abs(uint32_t r,uint32_t cnt);
void MeasureTask(void);
void MeasureImbalance(uint32_t pcurrFact[],uint32_t *pImbalancecurrent);
void CountTriplevel(uint32_t trip1);
void countratedcurrent(uint32_t ratedcurrent1);

#endif 

