/*
 * mazda_can.c
 *
 *  Created on: Mar 17, 2022
 *      Author: v0stap
 */

/*
 * app.c
 *
 *  Created on: Jul 25, 2020
 *      Author: v0stap
 */
#include "main.h"
uint32_t var_time = 0, rpm_time = 0, can_tim = 0;
can_msg_typedef tmpCanMsg;
//https://www.chamberofunderstanding.co.uk/2021/06/11/rx8-project-part-21-canbus-6-working-code/
void MAZDA_Send_Data(void) {
	can_tim++;
	tmpCanMsg.id = 0x201; // Send RPM = Actual RPM * 3.85		Throttle Pedal 0x00 - 0xC8 in 0.5% increments				Send KPH = (Actual KPH * 100) + 10000
	tmpCanMsg.format = STD_FORMAT;
	tmpCanMsg.frame = DATA_FRAME;
	tmpCanMsg.lenght = 7;
	tmpCanMsg.data[0] = (((uint16_t) (var.rpm * 3.85)) >> 8); // RPM high
	tmpCanMsg.data[1] = (uint8_t) ((var.rpm) * 3.85); // RPM low
	tmpCanMsg.data[2] = 0xFF;
	tmpCanMsg.data[3] = 0xFF;
	tmpCanMsg.data[4] = (var.vss * 100 + 10000) >> 8; // VSS KPH high
	tmpCanMsg.data[5] = (var.vss * 100 + 10000); // VSS KPH low
	tmpCanMsg.data[6] = var.pps / 5; // TPS
	tmpCanMsg.data[7] = 0xFF;
	CAN_Add_TX_Buffer(&tmpCanMsg);

	tmpCanMsg.id = 0x250;
	tmpCanMsg.format = STD_FORMAT;
	tmpCanMsg.frame = DATA_FRAME;
	tmpCanMsg.lenght = 8;
	tmpCanMsg.data[0] = 0x00;
	tmpCanMsg.data[1] = 0x00;
	tmpCanMsg.data[2] = 0xCF;
	tmpCanMsg.data[3] = 0x87;
	tmpCanMsg.data[4] = 0x7F;
	tmpCanMsg.data[5] = 0x83;
	tmpCanMsg.data[6] = 0x00;
	tmpCanMsg.data[7] = 0x00;
	CAN_Add_TX_Buffer(&tmpCanMsg);

	tmpCanMsg.id = 0x620; // Type of ABS
	tmpCanMsg.format = STD_FORMAT;
	tmpCanMsg.frame = DATA_FRAME;
	tmpCanMsg.lenght = 1;
	tmpCanMsg.data[0] = 0x00;
	tmpCanMsg.data[1] = 0x00;
	tmpCanMsg.data[2] = 0x00;
	tmpCanMsg.data[3] = 0x00;
	tmpCanMsg.data[4] = 0x00;
	tmpCanMsg.data[5] = 0x00;
	tmpCanMsg.data[6] = 0x02; // ABS type  2,3 or 4
	tmpCanMsg.data[7] = 0x00;
	CAN_Add_TX_Buffer(&tmpCanMsg);
	tmpCanMsg.id = 0x630; // Type of Transmission and Wheel Size
	tmpCanMsg.format = STD_FORMAT;
	tmpCanMsg.frame = DATA_FRAME;
	tmpCanMsg.lenght = 1;
	tmpCanMsg.data[0] = 0x08; //8 = MT, 2 = AT
	tmpCanMsg.data[1] = 0x00;
	tmpCanMsg.data[2] = 0x00;
	tmpCanMsg.data[3] = 0x00;
	tmpCanMsg.data[4] = 0x00;
	tmpCanMsg.data[5] = 0x00;
	tmpCanMsg.data[6] = 0x6A; // Wheel Size
	tmpCanMsg.data[7] = 0x6A; // Wheel Size
	CAN_Add_TX_Buffer(&tmpCanMsg);
	tmpCanMsg.id = 0x650; // CRUISE CONTROL
	tmpCanMsg.format = STD_FORMAT;
	tmpCanMsg.frame = DATA_FRAME;
	tmpCanMsg.lenght = 1;
	tmpCanMsg.data[0] = 0x00; // 0x40 Green 0x80 Yellow 0xC0 Both
	//tmpCanMsg.data[1] = 0;
	//tmpCanMsg.data[2] = 0xcf;
	//tmpCanMsg.data[3] = 0x87;
	//tmpCanMsg.data[4] = 0x7f;
	//tmpCanMsg.data[5] = 0x83;
	//tmpCanMsg.data[6] = 0;
	//tmpCanMsg.data[7] = 0;
	CAN_Add_TX_Buffer(&tmpCanMsg);
	if (can_tim>4){
		can_tim = 0;
		tmpCanMsg.id = 0x203; //This needs sending witin 0.5 seconds of ignition otherwise traction light will come on
			tmpCanMsg.format = STD_FORMAT;
			tmpCanMsg.frame = DATA_FRAME;
			tmpCanMsg.lenght = 7;
			tmpCanMsg.data[0] = 0x16;
			tmpCanMsg.data[1] = 0x16;
			tmpCanMsg.data[2] = 0x16;
			tmpCanMsg.data[3] = 0x16;
			tmpCanMsg.data[4] = 0xAF;
			tmpCanMsg.data[5] = 3;
			tmpCanMsg.data[6] = 0x16;
			//tmpCanMsg.data[7] = ;
			CAN_Add_TX_Buffer(&tmpCanMsg);
			tmpCanMsg.id = 0x215; // This needs sending witin 0.5 seconds of ignition otherwise traction light will come on
			tmpCanMsg.format = STD_FORMAT;
			tmpCanMsg.frame = DATA_FRAME;
			tmpCanMsg.lenght = 8;
			tmpCanMsg.data[0] = 0x02;
			tmpCanMsg.data[1] = 0x2D;
			tmpCanMsg.data[2] = 0x02;
			tmpCanMsg.data[3] = 0x2D;
			tmpCanMsg.data[4] = 0x02;
			tmpCanMsg.data[5] = 0x2A;
			tmpCanMsg.data[6] = 0x06;
			tmpCanMsg.data[7] = 0x81;
			CAN_Add_TX_Buffer(&tmpCanMsg);
			tmpCanMsg.id = 0x231; // This needs sending witin 0.5 seconds of ignition otherwise traction light will come on
			tmpCanMsg.format = STD_FORMAT;
			tmpCanMsg.frame = DATA_FRAME;
			tmpCanMsg.lenght = 5;
			tmpCanMsg.data[0] = 15;
			tmpCanMsg.data[1] = 0;
			tmpCanMsg.data[2] = 0xff;
			tmpCanMsg.data[3] = 0xff;
			tmpCanMsg.data[4] = 0x00;
			//tmpCanMsg.data[5] = 0x37;
			//tmpCanMsg.data[6] = 0x06;
			//tmpCanMsg.data[7] = 0x81;
			CAN_Add_TX_Buffer(&tmpCanMsg);
			tmpCanMsg.id = 0x240; // This needs sending witin 0.5 seconds of ignition otherwise traction light will come on
			tmpCanMsg.format = STD_FORMAT;
			tmpCanMsg.frame = DATA_FRAME;
			tmpCanMsg.lenght = 8;
			tmpCanMsg.data[0] = 0x04;
			tmpCanMsg.data[1] = 0;
			tmpCanMsg.data[2] = 40;
			tmpCanMsg.data[3] = 0x00;
			tmpCanMsg.data[4] = 0x02;
			tmpCanMsg.data[5] = 55;
			tmpCanMsg.data[6] = 0x06;
			tmpCanMsg.data[7] = 0x81;
			CAN_Add_TX_Buffer(&tmpCanMsg);
	}
}
void MAZDA_CAN_Read(void) {
	CAN_Read_RX_Buffer(&tmpCanMsg);
	if (tmpCanMsg.id == 0x4c0) {
		tmpCanMsg.id = 0x420; // Engine Temp	Odometer from 4c0			Oil Pressure Check Engine Low Water Bat Charge Oil Pressure
		tmpCanMsg.format = STD_FORMAT;
		tmpCanMsg.frame = DATA_FRAME;
		tmpCanMsg.lenght = 8;
		tmpCanMsg.data[0] = 0x00; // clt
		tmpCanMsg.data[1] = tmpCanMsg.data[1]; // Odometer from 4c0 (pwm)
		tmpCanMsg.data[2] = 0x00;
		tmpCanMsg.data[3] = 0x00;
		tmpCanMsg.data[4] = 0x00; //Oil Pressure 0x00 - 0x01 (pwm)
		tmpCanMsg.data[5] = 0x00; // Check Engine Bit 7 MIL 		Check Engine Bit 8 BL
		tmpCanMsg.data[6] = 0;
		if (var.vbat_adc < 1200) {
			tmpCanMsg.data[6] |= 1 << 2;
		} else {
			tmpCanMsg.data[6] &= ~(1 << 2);
		}
		//tmpCanMsg.data[6] = var.ic_status2; //Low Water Bit 2 MIL Bat Charge Bit 7 MIL Oil Pressure Bit 8 MIL
		tmpCanMsg.data[7] = 0x00;
		CAN_Add_TX_Buffer(&tmpCanMsg);
	} else if (tmpCanMsg.id == 0x4B1) {
		var.vss = ((tmpCanMsg.data[4] << 8) + tmpCanMsg.data[5] + (tmpCanMsg.data[6] << 8) + tmpCanMsg.data[7]) / 4;
		//todo // add option to make vss output on/off
		VSS_Set((uint8_t)var.vss);
	}
}

