#include "car0104_control.h"
#include "gpio_init.h"
#include "common.h"


void car0104_stop() {
    pwmWrite(PWM_PIN_SPEED,75);
    pwmWrite(PWM_PIN_CHANGE,75);
}

void car0104_back(unsigned char gval) {
	int b=7;
	int c=2;
   if (gval < 50) {
		pwmWrite(PWM_PIN_SPEED, 75);
	}
	else if (gval <= 60) {
		pwmWrite(PWM_PIN_SPEED, 85);
	}else if (gval <= 70) {
		pwmWrite(PWM_PIN_SPEED, 87);
	}else if(gval >70){
		int flont_speed = 80+(gval-70)/10+b;
		if(flont_speed>95) flont_speed = 95;
		pwmWrite(PWM_PIN_SPEED, flont_speed);
	}
}

void car0104_flont(unsigned char gval) {
	int b=7;
	int c=2;
	if (gval < 50) {
		pwmWrite(PWM_PIN_SPEED, 75);
	} else if (gval <= 60) {
		pwmWrite(PWM_PIN_SPEED, 65);
	}else if (gval <= 70) {
		pwmWrite(PWM_PIN_SPEED, 63);
	} else if(gval >70){
		int back_speed = 70 - (gval-70)/10-b;
		if(back_speed<55) back_speed =55;
		pwmWrite(PWM_PIN_SPEED, back_speed);
	}    
}

void car0104_right(unsigned char gval) {
   if (gval < 50) {
		pwmWrite(PWM_PIN_CHANGE, 75);
	} else if (gval <= 55) {
		pwmWrite(PWM_PIN_CHANGE, 78);
	} else if (gval <= 60) {
		pwmWrite(PWM_PIN_CHANGE, 79);
	} else if (gval <= 65) {
		pwmWrite(PWM_PIN_CHANGE, 80);
	} else if (gval <= 70) {
		pwmWrite(PWM_PIN_CHANGE, 81);
	} else if (gval <= 75) {
		pwmWrite(PWM_PIN_CHANGE, 82);
	} else if (gval <= 90) {
		pwmWrite(PWM_PIN_CHANGE, 83);
	} else if (gval <= 100) {
		pwmWrite(PWM_PIN_CHANGE, 84);
	} else if (gval <= 110) {
		pwmWrite(PWM_PIN_CHANGE, 85);
	} else if (gval <= 120) {
		pwmWrite(PWM_PIN_CHANGE, 86);
	} else if (gval <= 130) {
		pwmWrite(PWM_PIN_CHANGE, 87);
	} else if (gval <= 140) {
		pwmWrite(PWM_PIN_CHANGE, 88);
	} else if (gval <= 150) {
		pwmWrite(PWM_PIN_CHANGE, 89);
	} else if (gval <= 160) {
		pwmWrite(PWM_PIN_CHANGE, 90);
	} else if (gval <= 170) {
		pwmWrite(PWM_PIN_CHANGE, 91);
	} else if (gval <= 180) {
		pwmWrite(PWM_PIN_CHANGE, 92);
	} else if (gval <= 190) {
		pwmWrite(PWM_PIN_CHANGE, 93);
	} else if (gval <= 200) {
		pwmWrite(PWM_PIN_CHANGE, 94);
	} 
}

void car0104_lift(unsigned char gval) {
	if (gval < 50) {
		pwmWrite(PWM_PIN_CHANGE, 75);
	} else if (gval <= 55) {
		pwmWrite(PWM_PIN_CHANGE, 71);
	} else if (gval <= 60) {
		pwmWrite(PWM_PIN_CHANGE, 70);
	} else if (gval <= 65) {
		pwmWrite(PWM_PIN_CHANGE, 70);
	} else if (gval <= 70) {
		pwmWrite(PWM_PIN_CHANGE, 70);
	} else if (gval <= 75) {
		pwmWrite(PWM_PIN_CHANGE, 69);
	} else if (gval <= 90) {
		pwmWrite(PWM_PIN_CHANGE, 68);
	} else if (gval <= 100) {
		pwmWrite(PWM_PIN_CHANGE, 68);
	} else if (gval <= 110) {
		pwmWrite(PWM_PIN_CHANGE, 66);
	} else if (gval <= 120) {
		pwmWrite(PWM_PIN_CHANGE, 65);
	} else if (gval <= 130) {
		pwmWrite(PWM_PIN_CHANGE, 64);
	} else if (gval <= 140) {
		pwmWrite(PWM_PIN_CHANGE, 62);
	} else if (gval <= 150) {
		pwmWrite(PWM_PIN_CHANGE, 60);
	} else if (gval <= 160) {
		pwmWrite(PWM_PIN_CHANGE, 59);
	} else if (gval <= 170) {
		pwmWrite(PWM_PIN_CHANGE, 58);
	} else if (gval <= 180) {
		pwmWrite(PWM_PIN_CHANGE, 57);
	} else if (gval <= 190) {
		pwmWrite(PWM_PIN_CHANGE, 56);
	} else if (gval <= 200) {
		pwmWrite(PWM_PIN_CHANGE, 55);
	}    
}

void  car0104_change(unsigned char *buf) {
    unsigned char mode = buf[1];
    unsigned char val = buf[2];
	
    if(mode == 1 ) {
        car0104_flont(val);
    }else if(mode == 2 ) {
        car0104_back(val);
    }else if(mode == 3) {
		if(val !=0) car0104_lift(85);
		else car0104_lift(0);
    }else if(mode == 4) {
		if(val !=0) car0104_right(85);  
		else  car0104_lift(0);
    }
    
}
