/***********************************************************************//**
 * @file		Common.c
 * @brief		Contains functions and variables for global usage
 * @version		1.0
 * @date		21. April. 2014
 * @author		Michal Semrad
 **************************************************************************/

#include "common.h"

 /********************************************************************//*
   * @brief		memcpy secured by Mutex
   * @param[in]	where - pointer to final destination
   * @param[in]	what - pointer to source destination
   * @param[in]	count - number of bytes to copy
   * @return 	None
   **********************************************************************/
 void CopyToAddress(char* where, char* what, int count){

	 if( xSemaphoreTake( xAngXyzMutex, 100 ) != pdTRUE ) {
		 LogMessage("S xAngXyzMutex take failed",26);
	 }

	 memcpy(where,what,count);

	 if( xSemaphoreGive( xAngXyzMutex ) != pdTRUE ){
		 LogMessage("S xAngXyzMutex give failed",26);
	 }
 }


 /********************************************************************//*
   * @brief		Create FreeRTOS Elements like queue, mutexes, semaphores
   * @param[in]	none
   * @return 	None
   **********************************************************************/
 void CreateFreeRTOSElements(void){

	 xSerialReceive = xQueueCreate( 32, ( unsigned portBASE_TYPE ) sizeof( char ) );
	 if( xSerialReceive == NULL ){
		 LogMessage("xSerialReceive was not created",30);
	 }

	 xSemaphoreCOM = xSemaphoreCreateBinary();
	 if( xSemaphoreCOM == NULL ){
		 LogMessage("xSemaphoreCOM was not created",29);
	 }

	 xSemaphoreNewData = xSemaphoreCreateBinary();
	 if( xSemaphoreNewData == NULL ){
		 LogMessage("xSemaphoreNewData was not created",33);
	 }

	 xSemaphoreTime = xSemaphoreCreateBinary();
	 if( xSemaphoreTime == NULL ){
		 LogMessage("xSemaphoreTime was not created",30);
	 }

	 xSemaphoreStartAut = xSemaphoreCreateBinary();
	 if( xSemaphoreStartAut == NULL ){
		 LogMessage("xSemaphoreStartAut was not created",34);
	 }

	 xPWMMutex = xSemaphoreCreateMutex();
	 if( xPWMMutex == NULL ) {
		 LogMessage("PWMmutex was not created",24);
	 }

	 xAngXyzMutex = xSemaphoreCreateMutex();
	 if( xAngXyzMutex == NULL ) {
		 LogMessage("xAngXyzMutex was not created",28);
	 }

	 xMutexSpeed = xSemaphoreCreateMutex();
	 if( xMutexSpeed == NULL ) {
		 LogMessage("xMutexSpeed was not created",27);
	 }

	 xMutexPwmMode = xSemaphoreCreateMutex();
	 if( xMutexPwmMode == NULL ) {
		 LogMessage("xMutexPwmMode was not created",29);
	 }

	 xMutexKinMode = xSemaphoreCreateMutex();
	 if( xMutexKinMode == NULL ) {
		 LogMessage("xMutexKinMode was not created",29);
	 }

	 xMutexDelay = xSemaphoreCreateMutex();
	 if( xMutexDelay == NULL ) {
		 LogMessage("xMutexDelay was not created",27);
	 }

	 xMutexInstCount = xSemaphoreCreateMutex();
	 if( xMutexInstCount == NULL ) {
		 LogMessage("xMutexInstCount was not created",31);
	 }

	 xMutexExecutType = xSemaphoreCreateMutex();
	 if( xMutexExecutType == NULL ) {
		 LogMessage("xMutexExecutType was not created",32);
	 }

	 xMutexLedStatus = xSemaphoreCreateMutex();
	 if( xMutexLedStatus == NULL ) {
		 LogMessage("xMutexLedStatus was not created",31);
	 }
 }


 /************************** SETers and GETers for global variables secured by mutexes *************************/


 void SetSpeed(int value){

	 if( xSemaphoreTake( xMutexSpeed, 100 ) != pdTRUE ) {
		 LogMessage("xMutexSpeed take failed",23);
	 }

	 speed=value*6+50;//convert from percentage to PWM increment

	 if( xSemaphoreGive( xMutexSpeed ) != pdTRUE ){
	 		 LogMessage("xMutexSpeed give failed",23);
	 	 }
 }

 int GetSpeed(){

	 if( xSemaphoreTake( xMutexSpeed, 100 ) != pdTRUE ) {
		 LogMessage("xMutexSpeed take failed",23);
	 }

	 int temp=speed;

	 if( xSemaphoreGive( xMutexSpeed ) != pdTRUE ){
	 		 LogMessage("xMutexSpeed give failed",23);
	 	 }

	 return temp;
 }

 void SetPWMmode(int value){

	 if( xSemaphoreTake( xMutexPwmMode, 100 ) != pdTRUE ) {
		 LogMessage("xMutexPwmMode take failed",25);
	 }

	 PWMmode=value;

	 if( xSemaphoreGive( xMutexPwmMode ) != pdTRUE ){
	 		 LogMessage("xMutexPwmMode give failed",25);
	 	 }
 }

 int GetPWMmode(){

	 if( xSemaphoreTake( xMutexPwmMode, 100 ) != pdTRUE ) {
		 LogMessage("xMutexPwmMode take failed",25);
	 }

	 int temp=PWMmode;

	 if( xSemaphoreGive( xMutexPwmMode ) != pdTRUE ){
	 		 LogMessage("xMutexPwmMode give failed",25);
	 	 }

	 return temp;
 }

 void SetKinMode(int value){

	 if( xSemaphoreTake( xMutexKinMode, 100 ) != pdTRUE ) {
		 LogMessage("xMutexKinMode take failed",25);
	 }

	 kinematicMode=value;

	 if( xSemaphoreGive( xMutexKinMode ) != pdTRUE ){
	 		 LogMessage("xMutexKinMode give failed",25);
	 	 }
 }

 int GetKinMode(){

	 if( xSemaphoreTake( xMutexKinMode, 100 ) != pdTRUE ) {
		 LogMessage("xMutexKinMode take failed",25);
	 }

	 int temp=kinematicMode;

	 if( xSemaphoreGive( xMutexKinMode ) != pdTRUE ){
	 		 LogMessage("xMutexKinMode give failed",25);
	 	 }

	 return temp;
 }

 void SetDelay(int value){

	 if( xSemaphoreTake( xMutexDelay, 100 ) != pdTRUE ) {
		 LogMessage("xMutexDelay take failed",23);
	 }

	 delayTime=value;

	 if( xSemaphoreGive( xMutexDelay ) != pdTRUE ){
	 		 LogMessage("xMutexDelay give failed",23);
	 	 }
 }

 int GetDelay(){

	 if( xSemaphoreTake( xMutexDelay, 100 ) != pdTRUE ) {
		 LogMessage("xMutexDelay take failed",23);
	 }

	 int temp = delayTime;

	 if( xSemaphoreGive( xMutexDelay ) != pdTRUE ){
	 		 LogMessage("xMutexDelay give failed",23);
	 	 }

	 return temp;
 }

 void SetInstCount(int value){

	 if( xSemaphoreTake( xMutexInstCount, 100 ) != pdTRUE ) {
		 LogMessage("xMutexInstCount take failed",27);
	 }

	 instcount=value;

	 if( xSemaphoreGive( xMutexInstCount ) != pdTRUE ){
	 		 LogMessage("xMutexInstCount give failed",27);
	 	 }
 }

 int GetInstCount(){

	 if( xSemaphoreTake( xMutexInstCount, 100 ) != pdTRUE ) {
		 LogMessage("xMutexInstCount take failed",27);
	 }

	 int temp=instcount;

	 if( xSemaphoreGive( xMutexInstCount ) != pdTRUE ){
	 		 LogMessage("xMutexInstCount give failed",27);
	 	 }

	 return temp;
 }

 void SetExecutType(int value){

	 if( xSemaphoreTake( xMutexExecutType, 100 ) != pdTRUE ) {
		 LogMessage("xMutexExecutType take failed",28);
	 }

	 executingType=value;

	 if( xSemaphoreGive( xMutexExecutType ) != pdTRUE ){
	 		 LogMessage("xMutexExecutType give failed",28);
	 	 }
 }

 int GetExecutType(){

	 if( xSemaphoreTake( xMutexExecutType, 100 ) != pdTRUE ) {
		 LogMessage("xMutexExecutType take failed",28);
	 }

	 int temp=executingType;

	 if( xSemaphoreGive( xMutexExecutType ) != pdTRUE ){
	 		 LogMessage("xMutexExecutType give failed",28);
	 	 }

	 return temp;
 }


 void SetLedStatus(int value){

	 if( xSemaphoreTake( xMutexLedStatus, 100 ) != pdTRUE ) {
		 LogMessage("xMutexLedStatus take failed",27);
	 }

	 LedStatus=value;

	 if( xSemaphoreGive( xMutexLedStatus ) != pdTRUE ){
	 		 LogMessage("xMutexLedStatus give failed",27);
	 	 }
 }

 int GetLedStatus(){

	 if( xSemaphoreTake( xMutexLedStatus, 100 ) != pdTRUE ) {
		 LogMessage("xMutexLedStatus take failed",27);
	 }

	 int temp=LedStatus;

	 if( xSemaphoreGive( xMutexLedStatus ) != pdTRUE ){
	 		 LogMessage("xMutexLedStatus give failed",27);
	 	 }

	 return temp;
 }


