/*
 * charge_ctrl_test_shell.c
 *
 *  Created on: May 21, 2023
 *      Author: max
 */


#include "charge_ctrl_test_shell.h"
#include "charger_control.h"

UART_HandleTypeDef *suart;

void charge_shell_init(UART_HandleTypeDef *huart)
{
	suart = huart;
}

void charge_shell_loop()
{
	uint8_t command[3];
	HAL_StatusTypeDef status = HAL_UART_Receive(suart, command, 3, 100);
	if(status == HAL_OK)
	{
		switch(command[0])
		{
			case 'r':
				if(command[1] == 'c') //enable remote control
				{
					charger_control_enable_remote();
				}
				else if(command[1] == 'o')	//disable remote control
				{
					charger_control_disable_remote();
				}
				break;

			case 'e':
				if(command[1] == 'c')
				{
					charger_control_enable_charger_relay();
				}
				else if(command[1] == 'o')
				{
					charger_control_disable_charger_relay();
				}
				break;

			case 'v':
				;uint16_t voltage = (command[1]<<8) | command[2];
				charger_control_set_voltage(voltage);
				break;

			case 'c':
				;uint16_t current = (command[1]<<8 | command[2]);
				charger_control_set_current(current);
				break;

			case 's':
				;ChargerStatusHandleTypeDef charg = charger_control_get_state();
				uint8_t txbuffer[9] = {charg.acfail, charg.dcfail, charg.cc_status,
						charg.lim_status, charg.ot_status, (uint8_t)(charg.current>>8),
						(uint8_t)charg.current&0xFF,(uint8_t)(charg.voltage>>8),
						(uint8_t)charg.voltage&0xFF
				};
				HAL_UART_Transmit(suart, txbuffer, 9, 1000);
		}
	}
}
