/*
 * edcan_handler.c
 *
 * В этом файле расположены обработчики приходящих пакетов для пользовательских регистров
 *
 *  Created on: Jul 5, 2024
 *      Author: colorbass
 */


#include <edcan.h>
#include "stdio.h"
#include "soft_rtc.h"
#include "charger_gbt.h"

// * здесь объявлять внешние устройства и их регистры *
//uint8_t edcan_register_BMS[256];//300...3FF


////meter registers
//#define EDCAN_ID_METER 0x10
//#define EDCAN_REG_METER_VOLTAGE 0x03
//#define EDCAN_REG_METER_CURRENT 0x04


//own registers
#define EDCAN_REG_CHARGER_ENABLE 0x100


/* Charger info registers */
#define EDCAN_REG_CHARGER_INFO 0x200
//UNIX TIME
#define EDCAN_REG_TIME_0   0x210
#define EDCAN_REG_TIME_1   0x211
#define EDCAN_REG_TIME_2   0x212
#define EDCAN_REG_TIME_3   0x213

#define EDCAN_REG_MAX_LOAD  0x220



#define EDCAN_REG_BRM  0x310

#define EDCAN_REG_BCP  0x350

#define EDCAN_REG_BRO  0x35F

#define EDCAN_REG_BCL  0x360

#define EDCAN_REG_BCS  0x370

#define EDCAN_REG_BSM  0x380


/**
  * @brief  Handler for incoming Read packet
  * 		Another device reply value of its registers
  *
  * @param  SourceID: Packet Source ID
  * 		DestinationID: Packet Destination ID
  * 		Addr: First register address in sequence
  * 		*data: pointer for data array
  * 		len: length of data (1..255)
  */
void EDCAN_ReadHandler(uint8_t SourceID, uint8_t DestinationID, uint16_t Addr, uint8_t *data, uint8_t len){
	//Получили пакет Read (запрошенное значение регистров)
//	printf("Received packet: Read\n");
//	printf("Source ID = %d\n", SourceID);
//	printf("Destination ID = %d\n", DestinationID);
//	printf("Address = %d\n", Addr);
//	printf("Len = %d\n", len);
//	printf("\n");

	for (uint16_t AddrOffset = 0; AddrOffset < len; AddrOffset++){ //по очереди перебираем все полученные регистры через Handler

		// * добавить сюда новые устройства *

//		if(SourceID == EDCAN_ID_METER){
//			printf ("register[%d] = %d\n", Addr+AddrOffset, data[AddrOffset]);
//			switch(Addr+AddrOffset){
//
//				// * добавить сюда внешние регистры этого устройства *
//				case EDCAN_REG_METER_VOLTAGE:
//					printf ("Voltage = %d\n", data[AddrOffset]);
//					break;
//
//				case EDCAN_REG_METER_CURRENT:
//					printf ("Current = %d\n", data[AddrOffset]);
//					break;
//				default:
//					printf ("Unknown register\n");
//			}
//		}

	}
//	printf("\n");
}

/**
  * @brief  Handler for incoming Read packet
  * 		Another device reply value of its registers
  *
  * @param  SourceID: Packet Source ID
  * 		DestinationID: Packet Destination ID
  * 		Addr: First register address in sequence
  * 		*data: pointer for data array
  * 		len: length of data (1..255)
  */
void EDCAN_WriteUserRegister(uint16_t addr, uint8_t value){
	switch(addr){
		//edcan_register[addr] = value;
//	case EDCAN_REG_K0:
//		printf ("K0 = %d\n", value);
//		HAL_GPIO_WritePin (K0_GPIO_Port, K0_Pin, (value == 0));
//		break;
//	case EDCAN_REG_CHARGER_ENABLE:
//		if(value)GBT_Charger_Enable = 1;
//		else GBT_Charger_Enable = 0;
//		break;

	case EDCAN_REG_TIME_0:
		writeTimeReg(0, value);
		break;
	case EDCAN_REG_TIME_1:
		writeTimeReg(1, value);
		break;
	case EDCAN_REG_TIME_2:
		writeTimeReg(2, value);
		break;
	case EDCAN_REG_TIME_3:
		writeTimeReg(3, value);
		break;



		//0x220
	case EDCAN_REG_MAX_LOAD ... (EDCAN_REG_MAX_LOAD+sizeof(GBT_CML_t)):
//		((uint8_t*)&GBT_MaxLoad)[addr - EDCAN_REG_MAX_LOAD] = value;
		break;

		//0x200
	case EDCAN_REG_CHARGER_INFO ... (EDCAN_REG_CHARGER_INFO+sizeof(GBT_CRM_t)):
		((uint8_t*)&GBT_ChargerInfo)[addr - EDCAN_REG_CHARGER_INFO] = value;
		break;

	//TODO
		//GBT_EDCAN_Input.measuredCurrent;
		break;



		default:
		//printf ("Unknown register\n");
	}

}


uint8_t EDCAN_GetUserRegisterValue(uint16_t addr){
	switch (addr){

//			/* регистры 256..2047 используются пользовательских нужд */

// 0x400
		case EDCAN_REG_TIME_0:
			return getTimeReg(0);
			break;

		case EDCAN_REG_TIME_1:
			return getTimeReg(1);
			break;

		case EDCAN_REG_TIME_2:
			return getTimeReg(2);
			break;

		case EDCAN_REG_TIME_3:
			return getTimeReg(3);
			break;


			//0x220
		case EDCAN_REG_MAX_LOAD ... (EDCAN_REG_MAX_LOAD+sizeof(GBT_CML_t)):
			return ((uint8_t*)&GBT_MaxLoad)[addr - EDCAN_REG_MAX_LOAD];

			//0x310
		case EDCAN_REG_BRM ... (EDCAN_REG_BRM+sizeof(GBT_BRM_t)-1):
			return ((uint8_t*)&GBT_EVInfo)[addr - EDCAN_REG_BRM];

			//0x340
		case EDCAN_REG_BCP ... (EDCAN_REG_BCP+sizeof(GBT_BCP_t)):
			return ((uint8_t*)&GBT_BATStat)[addr - EDCAN_REG_BCP];

			//0x34F
		case EDCAN_REG_BRO:
			return GBT_BRO;

			//0x350
		case EDCAN_REG_BCL ... (EDCAN_REG_BCL+sizeof(GBT_BCL_t)):
			return ((uint8_t*)&GBT_ReqPower)[addr - EDCAN_REG_BCL];

			//0x360
		case EDCAN_REG_BCS ... (EDCAN_REG_BCS+sizeof(GBT_BCS_t)):
			return ((uint8_t*)&GBT_ChargingStatus)[addr - EDCAN_REG_BCS];

			//0x370
		case EDCAN_REG_BSM ... (EDCAN_REG_BSM+sizeof(GBT_BSM_t)):
				return ((uint8_t*)&GBT_BatteryStatus)[addr - EDCAN_REG_BSM];


		default:
			return 0x00;
		}
}




