#include "common.h"
#include "gpio_control.h"
#include "tank_angle.h"
#include "gpio_init.h"
#include "device_init.h"
#include "devcontrol_common.h"
#include "http_request.h"
#include "delay.h"

#define GPIO_ID_THREAD_COUNT 3

int gpio_device_id[GPIO_ID_THREAD_COUNT]={DEVICE_TANK0202,DEVICE_TANK0203,DEVICE_TANK0204};

const gpiocontrol_t *gpio_control_config_t = NULL ;

ThreadPool_t *g_pool_device_gpio_control_t;

static bool s_poll_tank_index =0;


void public_pin_value(int pin,int value);
void car0103_pin_value(int pin,int value);
void car0104_pin_value(int pin,int value);

void public_pwm_value(int pin ,int value);
void tank0202_pwm_value(int pin,int value);
void tank0203_pwm_value(int pin,int value);
void tank0204_pwm_value(int pin,int value);
void tank0206_pwm_value(int pin,int value);
void ship0301_pwm_value(int pin,int value); 

void tank_angle_limit_function(void *arg_gpio){

     if (arg_gpio != NULL) {
        free(arg_gpio);
    }
     printf("limit task started.\n");
    while(1){
        int limit_status = angle_limit();
        if(limit_status==1) {
            device_gpio_control(g_device_type,5,0);
            my_zlog_debug("lift limit stop");
        }
        else if(limit_status==2) {
            device_gpio_control(g_device_type,7,0);
            my_zlog_debug("right limit stop");
        }
        else if(limit_status==0) {
            delay_ms(5);
            my_zlog_debug("limit stop");
        }
    }
    free(arg_gpio);
}


void device_gpio_control_threadpoll_init(){
    int *arg_gpio = malloc(sizeof(int));
    my_zlog_info("device_gpio_control_threadpoll_init start\n");
    *arg_gpio = 2;
    g_pool_device_gpio_control_t = thread_pool_init(1,1);
    thread_pool_add_task(g_pool_device_gpio_control_t, tank_angle_limit_function, arg_gpio);
    
}


const gpiocontrol_t gpio_configs[] = {
    {
        .device_id = DEVICE_CAR0101,
        .device_pin_value =public_pin_value,
        .device_pwm_value =public_pwm_value
    },
    {
        .device_id = DEVICE_CAR0102,
        .device_pin_value =public_pin_value,
        .device_pwm_value =public_pwm_value
    },
    {
        .device_id = DEVICE_CAR0103,
        .device_pin_value =car0103_pin_value,
        .device_pwm_value =public_pwm_value
    },
    {
        .device_id = DEVICE_CAR0104,
        .device_pin_value =car0104_pin_value,
        .device_pwm_value =public_pwm_value
    },
    {
        .device_id = DEVICE_TANK0202,
        .device_pin_value =public_pin_value,
        .device_pwm_value =tank0202_pwm_value,
        .device_gpio_pthread_create=device_gpio_control_threadpoll_init
    },
    {
        .device_id = DEVICE_TANK0203,
        .device_pin_value =public_pin_value,
        .device_pwm_value =tank0203_pwm_value,
        .device_gpio_pthread_create=device_gpio_control_threadpoll_init
    },
    {
        .device_id = DEVICE_TANK0204,
        .device_pin_value =public_pin_value,
        .device_pwm_value =tank0204_pwm_value,
        .device_gpio_pthread_create=device_gpio_control_threadpoll_init
    },
    {
        .device_id = DEVICE_TANK0206,
        .device_pin_value =public_pin_value,
        .device_pwm_value =tank0203_pwm_value
    },
    {
        .device_id = DEVICE_SHIP0301,
        .device_pin_value =public_pin_value,
        .device_pwm_value =ship0301_pwm_value
    },
    {
        .device_id = DEVICE_PAO_PTZ0401,
        .device_pin_value =public_pin_value,
        .device_pwm_value =public_pwm_value
    },
};

/*
设备控制代码
*/
void device_gpio_control(int device_id,int pin,int val) {

    if (!gpio_control_config_t || gpio_control_config_t->device_id != device_id) {
        // 查找设备配置
        for(int i = 0; gpio_configs[i].device_id != -1; i++) {
            if(gpio_configs[i].device_id == device_id) {
                gpio_control_config_t = &gpio_configs[i];
                break;
            }
        }
        
        for(int i=0;i<GPIO_ID_THREAD_COUNT;i++ ){
            if(gpio_control_config_t&&device_id == gpio_device_id[i]){
                my_zlog_info("线程函数:%d\n", device_id);
                gpio_control_config_t->device_gpio_pthread_create(); //创建线程，线程关闭在tank.common.h中何tank需要的其他线程关闭
            }
        }
    }
    
    if(!gpio_control_config_t) {
        my_zlog_error("Error: Device gpio stop ID %d not found!", device_id);
        return;
    }

    gpio_control_config_t->device_pin_value(pin,val);
    gpio_control_config_t->device_pwm_value(pin,val);         
}

void public_pin_value(int pin,int value) {	//引脚控制
	for(int i = 0 ; i <= g_gpiocount ; i++) {
        if(pin == g_gpiowpi[i]) {
            break;
        }if(i == g_gpiocount) {
            return ;
        }   
    }
    if(value==1) {
		digitalWrite(pin, HIGH);
		my_zlog_debug("pin:%d,%d",pin,HIGH);
	}else if(value==0) {
		digitalWrite(pin, LOW);
		my_zlog_debug("pin:%d,%d",pin,LOW);
	}
	
}

/*挖机pin控制*/
void car0103_pin_value(int pin,int value){
    if(pin == 26 && value==1) {
        public_pin_value(27,1);
        public_pin_value(pin,value);
    }else if(pin == 26 && value==0){
        public_pin_value(27,0);
        public_pin_value(pin,value);
    }else if(pin == 253 && value==1){
        public_pin_value(27,0);
        public_pin_value(26,1);
        public_pin_value(24,1);
    }else if(pin == 254 && value==1){
        public_pin_value(27,1);
        public_pin_value(26,0);
        public_pin_value(24,0);
    }else if((pin == 254||pin == 253) && value==0){
        public_pin_value(27,0);
        public_pin_value(26,0);
        public_pin_value(24,0);
    }else {
        public_pin_value(pin,value);
    }
}

/*推土机pin控制*/
void car0104_pin_value(int pin,int value){
    if(pin==24){
        public_pin_value(26,value);
    }else if(pin==26){
        public_pin_value(24,value);
    }else public_pin_value(pin,value);
}

void public_pwm_value(int pin ,int value){
        for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
            if(pin == g_gpioPwm[i]) {
                break;
            }
            if(i == g_gpio_softpwmcount) {
                return ;
            }
    }
     
    int shot_speed = (int)(g_shot_speed_value*100);
    if(value==1) {
		
		if(pin == 27){
                softPwmWrite(pin, shot_speed);
                my_zlog_debug("pwm:%d,%d",pin,shot_speed);
        } else {
                softPwmWrite(pin, shot_speed);
                 my_zlog_debug("pwm:%d,%d",pin,shot_speed);
        }
        
	}else if(value==0) {
		softPwmWrite(pin, 0);
		my_zlog_debug("pwm:%d,0",pin);
	}
    my_zlog_debug("public pwm");

}

void tank0202_pwm_value(int pin,int value) {	//软件陪我们控制调速
    for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
        if(pin == g_gpioPwm[i]) {
            break;
        }
        if(i == g_gpio_softpwmcount) {
            return ;
        }
    }
    
    if(value==1) {
		if(pin == 27){
            softPwmWrite(pin, 30);
        } else {
            softPwmWrite(pin, 40);
            my_zlog_debug("pwm:%d",pin);
        }
        
	}else if(value==0) {
		softPwmWrite(pin, 0);
		my_zlog_debug("pwm:%d,0",pin);
	}
     my_zlog_debug("tank0202 pwm");
}

void tank0203_pwm_value(int pin,int value) {	//软件陪我们控制调速
    for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
        if(pin == g_gpioPwm[i]) {
            break;
        }
        if(i == g_gpio_softpwmcount) {
            return ;
        }
    }
    if(value==1) {
		
		if(pin == 27){
            softPwmWrite(pin, 38);
        } else {
            softPwmWrite(pin, 35);
            my_zlog_debug("pwm:%d",pin);
        }
        
	}else if(value==0) {
		softPwmWrite(pin, 0);
		my_zlog_debug("pwm:%d,0",pin);
	}
    my_zlog_debug("tank0203 pwm");
}

void tank0204_pwm_value(int pin,int value){
        for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
        if(pin == g_gpioPwm[i]) {
            break;
        }
        if(i == g_gpio_softpwmcount) {
            return ;
        }
    }
    if(value==1) {
		
		if(pin == 27){
            softPwmWrite(pin, 38);
        } else {
            softPwmWrite(pin, 35);
            my_zlog_debug("pwm:%d",pin);
        }
        
	}else if(value==0) {
		softPwmWrite(pin, 0);
		my_zlog_debug("pwm:%d,0",pin);
	}
    my_zlog_debug("tank0204 pwm");
}

void tank0206_pwm_value(int pin,int value) {	//软件陪我们控制调速
    for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
        if(pin == g_gpioPwm[i]) {
            break;
        }
        if(i == g_gpio_softpwmcount) {
            return ;
        }
    }
    
    if(value==1) {
		if(pin == 27){
            softPwmWrite(pin, 50);
        } else {
            softPwmWrite(pin, 35);
            my_zlog_debug("pwm:%d",pin);
        }
        
	}else if(value==0) {
		softPwmWrite(pin, 0);
		my_zlog_debug("pwm:%d,0",pin);
	}
     my_zlog_debug("tank0206 pwm");
}

void ship0301_pwm_value(int pin,int value) {	//软件陪我们控制调速
    for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
        if(pin == g_gpioPwm[i]) {
            break;
        }
        if(i == g_gpio_softpwmcount) {
            return ;
        }
    }
    
    if(value==1) {
		if(pin == 27){
            softPwmWrite(pin, 50);
        } else {
            softPwmWrite(pin, 35);
            my_zlog_debug("pwm:%d",pin);
        }
        
	}else if(value==0) {
		softPwmWrite(pin, 0);
		my_zlog_debug("pwm:%d,0",pin);
	}
     my_zlog_debug("ship0301 pwm");
}

