/*
 * debug.c
 *
 *  Created on: Apr 16, 2024
 *      Author: colorbass
 */

#include "main.h"
#include <stdio.h>
#include <string.h>
#include "debug.h"
#include "board.h"
#include "charger_gbt.h"
#include "usart.h"
#include <time.h>
#include <connector.h>


uint8_t  debug_rx_buffer[256];
uint8_t debug_cmd_received;
uint8_t debug_rx_buffer_size = 0;

extern UART_HandleTypeDef huart2;

#if defined(__GNUC__)
int _write(int fd, char * ptr, int len)
{
	HAL_GPIO_WritePin(USART2_DIR_GPIO_Port, USART2_DIR_Pin, 1);
    HAL_UART_Transmit(&huart2, (uint8_t *) ptr, len, HAL_MAX_DELAY);
	HAL_GPIO_WritePin(USART2_DIR_GPIO_Port, USART2_DIR_Pin, 0);
    return len;
}
#endif

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){

//	if(huart->Instance == USART1){
//		mm_rx_interrupt(huart, Size);
//	}
	if(huart->Instance == USART2){
		debug_rx_interrupt(huart, Size);
	}
}

void debug_rx_interrupt(UART_HandleTypeDef *huart, uint16_t Size){
	debug_rx_buffer[Size] = '\0';
	debug_rx_buffer_size = Size;
	debug_cmd_received = 1;
}

void debug_init(){
	HAL_UARTEx_ReceiveToIdle_IT(&huart2,debug_rx_buffer,255);
	//mm_schedule_write(0x01, 0x0000, 0x0800);
	//	mm_schedule_write(0x02, 0x00FF, 0xFFFF);
	//for (int i=0;i<60;i++)
	//	mm_schedule_write(0x02, 0x0000, 0xFF00);
	//	mm_schedule_write(0x01, 0x0000, 0x0100);
	//	mm_schedule_write(0x01, 0x0000, 0x0100);
}

void parse_command(uint8_t* buffer, size_t length) {
	// ignore \r \n symbols
    size_t i = 0;
    for (i = 0; i < length; i++) {
        if (buffer[i] == '\r' || buffer[i] == '\n') {
            buffer[i] = '\0';
            length = i;
            break;
        }
    }
    if (buffer[0] == 0) return;
    if (strncmp((const char*)buffer, "reset", length) == 0) {
        printf("Resetting...\n");
        NVIC_SystemReset();

    } else if (strncmp((const char*)buffer, "relayaux", length) == 0) {
        printf("Relaying...\n");
        RELAY_Write(RELAY_AUX, 1);
        HAL_Delay(200);
        RELAY_Write(RELAY_AUX, 0);
    } else if (strncmp((const char*)buffer, "relaycc", length) == 0) {
        printf("Relaying...\n");
        RELAY_Write(RELAY_CC, 1);
        HAL_Delay(200);
        RELAY_Write(RELAY_CC, 0);


//    } else if (strncmp((const char*)buffer, "voltage", length) == 0) {
//        printf("Voltaging...\n");
//        mm_schedule_read(0x02, 0x0001);

    } else if (strncmp((const char*)buffer, "adc", length) == 0) {
        printf("CC1=%.2f\n", CONN_CC_GetAdc());

    } else if (strncmp((const char*)buffer, "lock_state", length) == 0) {
        printf("Lock state=%d\n", GBT_LockGetState());

    } else if (strncmp((const char*)buffer, "lock_lock", length) == 0) {
        printf("Locked\n");
        GBT_Lock(1);

    } else if (strncmp((const char*)buffer, "lock_unlock", length) == 0) {
        printf("Unlocked\n");
        GBT_Lock(0);

    } else if (strncmp((const char*)buffer, "complete", length) == 0) {
    	CONN_SetState(CONN_Occupied_complete);

    } else if (strncmp((const char*)buffer, "start", length) == 0) {
        printf("Started\n");
        GBT_Start();

    } else if (strncmp((const char*)buffer, "stop", length) == 0) {
        printf("Stopped\n");
        GBT_Stop(GBT_CST_SUSPENDS_ARTIFICIALLY);

    } else if (strncmp((const char*)buffer, "stop1", length) == 0) {
        printf("Stopped\n");
        GBT_ForceStop();

//    } else if (strncmp((const char*)buffer, "force", length) == 0) {
//        printf("Stopped\n");
//		GBT_Lock(1);
//		GBT_SwitchState(GBT_S2_LOCKED);
//		GBT_Delay(500);

    } else if (strncmp((const char*)buffer, "cc_state", length) == 0) {
    	switch(CONN_CC_GetState()){
    	case GBT_CC_UNKNOWN:
    		printf("GBT_CC_UNKNOWN\n");
    		break;
    	case GBT_CC_12V:
    		printf("GBT_CC_12V\n");
    		break;
    	case GBT_CC_6V:
    		printf("GBT_CC_6V\n");
    		break;
    	case GBT_CC_4V:
    		printf("GBT_CC_4V\n");
    		break;
    	case GBT_CC_2V:
    		printf("GBT_CC_2V\n");
    		break;

    	}
    } else if (strncmp((const char*)buffer, "info1", length) == 0) {
        printf("Battery info:\n");
        printf("maxCV %dV\n",GBT_BATStat.maxCellVoltage/100); 		// 0.01v/bit
        printf("maxCC %dA\n",GBT_BATStat.maxChargingCurrent/10); 	// 0.1A/bit
        printf("totE %dkWh\n",GBT_BATStat.totalEnergy/10); 			// 0.1kWh
        printf("maxCV %dV\n",GBT_BATStat.maxChargingVoltage/10); 	// 0.1V/ bit
        printf("maxT %dC\n",(int16_t)GBT_BATStat.maxTemp-50); 		// 1C/bit, -50C offset
        printf("SOC %dp\n",GBT_BATStat.SOC/10); 					// 0.1%/bit , 0..100%
        printf("Volt. %dV\n",GBT_BATStat.measVoltage/10); 			// 0.1V/bit

    } else if (strncmp((const char*)buffer, "info2", length) == 0) {
        printf("EV info:\n");
        printf("GBT_ver V%d.%d%d\n",GBT_EVInfo.version[0],GBT_EVInfo.version[1],GBT_EVInfo.version[2]);
        printf("Battery type: %d\n",GBT_EVInfo.batteryType);
        printf("Battery capacity: %d\n", GBT_EVInfo.batteryCapacity);	// 0.1Ah/bit
    	printf("Battery voltage: %d\n", GBT_EVInfo.batteryVoltage);  	// 0.1V/bit
    	printf("Battery vendor: %.4s\n", GBT_EVInfo.batteryVendor); 	// Battery vendor (ASCII string)
    	printf("Battery SN: %lu\n", GBT_EVInfo.batterySN);			// int
    	printf("Battery manufacture date: %02d.%02d.%04d\n", GBT_EVInfo.batteryManuD, GBT_EVInfo.batteryManuM ,GBT_EVInfo.batteryManuY+1985);		// year (offset 1985)
    	printf("Battery cycles: %d\n", GBT_EVInfo.batteryCycleCount); //uint24_t
    	printf("Own auto: %d\n", GBT_EVInfo.ownAuto);			// 0 = lizing, 1 = own auto
    	printf("EVIN: %.17s\n", GBT_EVInfo.EVIN);			//EVIN
    	printf("EV_SW_VER: %.8s\n", GBT_EVInfo.EV_SW_VER);

    } else if (strncmp((const char*)buffer, "info3", length) == 0) {
        printf("GBT_MaxLoad info:\n");
        printf("Output max current: %d\n",GBT_MaxLoad.maxOutputCurrent);
        printf("Output min current: %d\n",GBT_MaxLoad.minOutputCurrent);
        printf("Output max voltage: %d\n",GBT_MaxLoad.maxOutputVoltage);
        printf("Output min voltage: %d\n",GBT_MaxLoad.minOutputVoltage);
        printf("\nGBT_ChargerInfo info:\n");
        printf("BMS Recognized: %d\n",GBT_ChargerInfo.bmsIdentified);
        printf("Charger location: %.3s\n",GBT_ChargerInfo.chargerLocation);
        printf("Charger number: %lu\n",GBT_ChargerInfo.chargerNumber);


    } else if (strncmp((const char*)buffer, "help", length) == 0) {
        printf("Command list:\n");
        printf("reset\n");
        printf("help\n");
        printf("cc_state\n");
        printf("lock_lock\n");
        printf("lock_unlock\n");
        printf("lock_state\n");
        printf("adc\n");
        printf("relay(cc,aux)\n");
        printf("start\n");
        printf("stop\n");
        printf("stop1\n");
//        printf("force\n");
        printf("info1\n");
        printf("info2\n");
        printf("info3\n");
        printf("time\n");
        printf("cantest\n");

        //TODO: info commands

    } else if (strncmp((const char*)buffer, "time", length) == 0) {

		time_t unix_time = (time_t)get_Current_Time();
		struct tm *parts = localtime(&unix_time);

		printf("Year: %d\n", parts->tm_year + 1900);
		printf("Month: %d\n", parts->tm_mon + 1);
		printf("Day: %d\n", parts->tm_mday);
		printf("Hour: %d\n", parts->tm_hour);
		printf("Minute: %d\n", parts->tm_min);
		printf("Second: %d\n", parts->tm_sec);

    } else if (strncmp((const char*)buffer, "cantest", length) == 0) {
    	//GBT_SendCHM();
    	GBT_Error(0xFDF0C0FC); //BRM Timeout
        printf("can test\n");

    } else {
        printf("Unknown command\n");
    }
}

void debug_task(){
	if(debug_cmd_received){
		parse_command(debug_rx_buffer, debug_rx_buffer_size);
		HAL_UARTEx_ReceiveToIdle_IT(&huart2,debug_rx_buffer,255);
		debug_cmd_received = 0;
	}
}
