/*
 * splitflap_wrapper.c
 *
 *  Created on: 05.12.2022
 *      Author: jonas
 */

#include <stdint.h>
#include <stdbool.h>
#include "splitflap_wrapper.h"
#include "McuUtility.h"
#include "McuShell.h"
#include "Shell.h"

#define RS_CMD_PREFIX "rs sendcmd 0x01 SplitFlap "
#define BUF_SIZE 50


/* moves all split flaps to the zero position
 * After a timeout the initializtion is cancelled and the return value is false
 * If all splitflaps report to be initialized before the timeout, the return value is true */
bool SplitFlap_Wrapper_MoveAllToZeroPosition(void){
	unsigned char cmd[BUF_SIZE] = RS_CMD_PREFIX;
	unsigned char response[128];
	McuUtility_strcat(cmd, sizeof(cmd), (unsigned char*)"initAll");
	SHELL_SendToESPAndGetResponse(cmd, response, sizeof(response));
	McuShell_SendStr(response, McuShell_GetStdio()->stdOut);
	return true;
}

/* display a sentence on the splitflap combination
 * splitflaps must be initialized to be able to display something!
 * returns true when all movements finished */
bool SplitFlap_Wrapper_Display(unsigned char *sentence){
	unsigned char cmd[BUF_SIZE] = RS_CMD_PREFIX;
	unsigned char response[128];
	McuUtility_strcat(cmd, sizeof(cmd), (unsigned char*)"Display ");
	McuUtility_strcat(cmd, sizeof(cmd), sentence);
	SHELL_SendToESPAndGetResponse(cmd, response, sizeof(response));
	McuShell_SendStr(response, McuShell_GetStdio()->stdOut);
	return true;
}

/* sets the hardware identifier <hwId> of a splitflap with <id> in the combination
 * returns true when successful, false when not (e.g. split flap with given id not available) */
bool SplitFlap_Wrapper_SetHardwareIdentifier(uint8_t id, uint8_t hwId){
	unsigned char cmd[BUF_SIZE] = RS_CMD_PREFIX;
	unsigned char response[128];
	unsigned char setupId_str[4] = {0}; unsigned char hardwareId_str[4] = {0};
	McuUtility_Num8uToStr(setupId_str, sizeof(setupId_str), id);
	McuUtility_Num8uToStr(hardwareId_str, sizeof(hardwareId_str), hwId);
	McuUtility_strcat(cmd, sizeof(cmd), (unsigned char*)"setId ");
	McuUtility_strcat(cmd, sizeof(cmd), setupId_str);
	McuUtility_strcat(cmd, sizeof(cmd), (unsigned char*)" ");
	McuUtility_strcat(cmd, sizeof(cmd), hardwareId_str);
	SHELL_SendToESPAndGetResponse(cmd, response, sizeof(response));
	McuShell_SendStr(response, McuShell_GetStdio()->stdOut);
	return true;
}
