#ifndef _MOTOR_H__
#define _MOTOR_H__
#include "os/os_types.h"
#include "foc/core/PMSM_FOC_Core.h"
#include "foc/motor/hall.h"
#include "foc/motor/encoder.h"
#include "app/nv_storage.h"

#define IDC_USER_LIMIT_NONE ((s16)0x3fff)

typedef struct {
	bool running;
	u32  start_ts;
	u32  det_ts;
	u32  rpm;
	bool error;
}fan_t;

typedef struct {
	s16 idc_lim;
	s16 ebrk_torque;
	u16 ebrk_time;
	u8  n_brkShutPower;
	bool  b_tcs;
}user_rt_set;

typedef struct {
	bool   b_start;
	float  throttle;
	bool   b_ignor_throttle;
	bool   b_ind_start;
	bool   b_calibrate;
	bool   b_force_run;
	bool   b_runStall; //是否堵转
	bool   b_lock_motor;
	bool   b_auto_hold;
	bool   b_break;
	bool   b_epm;
	bool   b_cruise;
	u32    cruise_time;
	float  cruise_torque;
	EPM_Dir_t epm_dir;
	bool   b_epm_cmd_move;
	float  f_epm_vel;
	float  f_epm_trq;
	u32    runStall_time;
	float  runStall_pos;
	u8     u_throttle_ration;
	s8     s_direction;
	u32    n_brake_errors;
	u32    n_CritiCalErrMask;
	u8     mode;
	bool   b_is96Mode;
	u8     n_gear;
	bool   b_limit_pending;
	mc_gear_t *gear_cfg;
	u32    n_autohold_time;
	bool   b_wait_brk_release;
	u8     mos_lim;
	u8     motor_lim;
	s16    s_vbus_hw_min;
	user_rt_set u_set; //用户运行时设置
	fan_t  fan[2];
}motor_t;

motor_t * mc_params(void);
void mc_init(void);
bool mc_start(u8 mode);
bool mc_stop(void);
void mc_encoder_off_calibrate(s16 vd);
bool mc_throttle_released(void);
bool mc_lock_motor(bool lock);
bool mc_auto_hold(bool hold);
void mc_set_throttle_r(bool use, u8 r);
void mc_use_throttle(void);
bool mc_current_sensor_calibrate(float current);
bool mc_encoder_zero_calibrate(s16 vd);
bool mc_set_foc_mode(u8 mode);
bool mc_is_epm(void);
bool mc_start_epm(bool epm);
bool mc_start_epm_move(EPM_Dir_t dir, bool is_command);
void mc_get_running_status(u8 *data);
bool mc_command_epm_move(EPM_Dir_t dir);
bool mc_throttle_epm_move(EPM_Dir_t dir);
bool mc_is_start(void);
bool mc_set_gear(u8 gear);
u8 mc_get_gear(void);
u8 mc_get_internal_gear(void);
bool mc_set_critical_error(u8 err);
void mc_clr_critical_error(u8 err);
u32 mc_get_critical_error(void);
void mc_set_fan_duty(u8 duty);
void mc_force_run_open(s16 vd, s16 vq);
u16 mc_get_running_status2(void);
bool mc_enable_cruise(bool enable);
bool mc_is_cruise_enabled(void);
bool mc_set_cruise_speed(bool rpm_abs, float target_rpm);
void mc_set_idc_limit(s16 limit);
mc_gear_t *mc_get_gear_config(void);
mc_gear_t *mc_get_gear_config_by_gear(u8 n_gear);
void mc_set_motor_lim_level(u8 l);
void mc_set_mos_lim_level(u8 l);
void motor_debug(void);
int mc_get_phase_errinfo_block(u8 *data, int dlen);
void mc_set_ebrk_level(s16 trq, u16 time);
s16 mc_get_ebrk_torque(void);
u16 mc_get_ebrk_time(void);
bool mc_critical_err_is_set(u8 err);
bool mc_hwbrk_can_shutpower(void);
void mc_start_current_rec(bool rec);
bool mc_ind_motor_start(bool start);
void mc_enable_brkshutpower(u8 shut);
void mc_enable_tcs(bool enable);

static __INLINE float motor_encoder_get_angle(void) {
#ifdef USE_ENCODER_HALL
	return hall_sensor_get_theta(true);
#elif defined (USE_ENCODER_ABI)
	return encoder_get_theta(true);
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE u8 motor_encoder_may_error(void) {
#ifdef USE_ENCODER_HALL
	return false;
#elif defined (USE_ENCODER_ABI)
	return encoder_may_error();
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE void motor_encoder_update(bool detect_err) {
#ifdef USE_ENCODER_HALL
	hall_sensor_get_theta(detect_err);
#elif defined (USE_ENCODER_ABI)
	encoder_get_theta(detect_err);
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE float motor_encoder_get_speed(void) {
#ifdef USE_ENCODER_HALL
	return hall_sensor_get_speed();
#elif defined (USE_ENCODER_ABI)
	return encoder_get_speed();
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE float motor_encoder_get_vel_count(void) {
#ifdef USE_ENCODER_HALL
	return 0;
#elif defined (USE_ENCODER_ABI)
	return encoder_get_vel_count();
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE float motor_encoder_get_position(void) {
#ifdef USE_ENCODER_HALL
	return 0;
#elif defined (USE_ENCODER_ABI)
	return encoder_get_position();
#else
	#error "Postion sensor ERROR"
#endif
}


static __INLINE void motor_encoder_init(void) {
#ifdef USE_ENCODER_HALL
	hall_sensor_init();
#elif defined (USE_ENCODER_ABI)
	encoder_init();
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE void motor_encoder_start(bool start) {
#ifdef USE_ENCODER_HALL
	hall_sensor_clear(direction);
#elif defined (USE_ENCODER_ABI)
	encoder_init_clear();
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE float motor_encoder_zero_phase_detect(float *enc_off){
#ifdef USE_ENCODER_HALL
	return 0.0f;
#elif defined (USE_ENCODER_ABI)
	return encoder_zero_phase_detect(enc_off);
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE void motor_encoder_set_direction(s8 dir) {
#ifdef USE_ENCODER_HALL
	hall_set_direction(dir);
#elif defined (USE_ENCODER_ABI)
	encoder_set_direction(dir);
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE void motor_encoder_lock_pos(bool lock) {
#ifdef USE_ENCODER_HALL
		
#elif defined (USE_ENCODER_ABI)
	encoder_lock_position(lock);
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE void motor_encoder_band_epm(bool epm) {
#ifdef USE_ENCODER_HALL
		
#elif defined (USE_ENCODER_ABI)
	encoder_epm_pll_band(epm);
#else
	#error "Postion sensor ERROR"
#endif
}

static __INLINE void motor_encoder_produce_error(bool error) {
#ifdef USE_ENCODER_HALL

#elif defined (USE_ENCODER_ABI)
	encoder_produce_error(error);
#else
	#error "Postion sensor ERROR"
#endif
}

#endif /* _MOTOR_H__ */

